Skip to content

Commit

Permalink
tests: fetch requests test
Browse files Browse the repository at this point in the history
  • Loading branch information
MdTeach committed Jul 17, 2023
1 parent 69d47a1 commit c2b23f5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Sources/Chat/Send.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ extension PushChat {
try await ConversationHash(conversationId: receiverAddress, account: senderAddress) == nil

if isConversationFirst {
print("sending intenting")
return try await PushChat.sendIntent(chatOptions)
} else {
// send regular message
Expand Down Expand Up @@ -237,9 +238,11 @@ extension PushChat {
var shouldEncrypt = true

// else create the user frist and send unencrypted intent message
if anotherUser == nil || anotherUser?.publicKey == nil {
let _ = try await PushUser.createUserEmpty(
userAddress: sendOptions.receiverAddress, env: sendOptions.env)
if anotherUser?.publicKey == nil || anotherUser?.publicKey == "" {
if anotherUser == nil {
let _ = try await PushUser.createUserEmpty(
userAddress: sendOptions.receiverAddress, env: sendOptions.env)
}

shouldEncrypt = false
}
Expand Down
56 changes: 55 additions & 1 deletion Tests/Chat/P2P/GetChatsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class GetChatsTests: XCTestCase {
}
}

func testGetChatRequests() async throws {
func testGetChatRequestsForEmptyUser() async throws {

// send intent
let reqAddress = generateRandomEthereumAddress()
Expand All @@ -71,4 +71,58 @@ class GetChatsTests: XCTestCase {
XCTAssertEqual(userReqs[0].msg!.messageContent, messageToSen1)
}

func testGetChatRequestsForNonExistingUser() async throws {

// send intent
let reqAddress = generateRandomEthereumAddress()
let messageToSen1 = "Hello user --- Intent \(reqAddress)"

let _ = try await Push.PushChat.send(
PushChat.SendOptions(
messageContent: messageToSen1,
messageType: "Text",
receiverAddress: reqAddress,
account: UserAddress,
pgpPrivateKey: UserPrivateKey
))

let userReqs = try await PushChat.requests(
options: PushChat.RequestOptionsType(account: reqAddress, pgpPrivateKey: "", toDecrypt: false)
)

XCTAssertEqual(userReqs[0].msg!.messageContent, messageToSen1)
}

func testGetChatRequestsForExistingUser() async throws {

let userPk1 = getRandomAccount()
let _ = try await PushUser.create(
options: PushUser.CreateUserOptions(
env: ENV.STAGING,
signer: SignerPrivateKey(
privateKey: userPk1
),
progressHook: nil
))

// send intent
let reqAddress = generateRandomEthereumAddress()
let messageToSen1 = "Hello user --- Intent \(reqAddress)"

let _ = try await Push.PushChat.send(
PushChat.SendOptions(
messageContent: messageToSen1,
messageType: "Text",
receiverAddress: reqAddress,
account: UserAddress,
pgpPrivateKey: UserPrivateKey
))

let userReqs = try await PushChat.requests(
options: PushChat.RequestOptionsType(account: reqAddress, pgpPrivateKey: UserPrivateKey, toDecrypt: true)
)

XCTAssertEqual(userReqs[0].msg!.messageContent, messageToSen1)
}

}

0 comments on commit c2b23f5

Please sign in to comment.