Skip to content

Commit

Permalink
Unit tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Feb 27, 2023
1 parent 379fb3b commit d7b2409
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
21 changes: 18 additions & 3 deletions Tests/ChatTests/RegistryServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import WalletConnectKMS
@testable import WalletConnectIdentity

final class RegistryServiceTests: XCTestCase {
var resubscriptionService: ResubscriptionService!
var identityClient: IdentityClient!
var identityStorage: IdentityStorage!
var networkService: IdentityNetwotkServiceMock!
Expand Down Expand Up @@ -37,7 +38,18 @@ final class RegistryServiceTests: XCTestCase {
iatProvader: DefaultIATProvider(),
messageFormatter: SIWECacaoFormatter()
)
identityClient = IdentityClient(identityService: identitySevice, identityStorage: identityStorage, networkingInteractor: networkingInteractor, kms: kms, logger: ConsoleLoggerMock())
identityClient = IdentityClient(identityService: identitySevice, identityStorage: identityStorage, logger: ConsoleLoggerMock())

let storage = RuntimeKeyValueStorage()
let accountService = AccountService(currentAccount: account)
let chatStorage = ChatStorage(
accountService: accountService,
messageStore: .init(storage: storage, identifier: ""),
receivedInviteStore: .init(storage: storage, identifier: ""),
sentInviteStore: .init(storage: storage, identifier: ""),
threadStore: .init(storage: storage, identifier: "")
)
resubscriptionService = ResubscriptionService(networkingInteractor: networkingInteractor, kms: kms, accountService: accountService, chatStorage: chatStorage, logger: ConsoleLoggerMock())
}

func testRegister() async throws {
Expand All @@ -53,7 +65,8 @@ final class RegistryServiceTests: XCTestCase {
XCTAssertTrue(networkingInteractor.subscriptions.isEmpty)

_ = try await identityClient.register(account: account, onSign: onSign)
try await identityClient.goPublic(account: account)
let inviteKey = try await identityClient.goPublic(account: account)
try await resubscriptionService.subscribeForInvites(inviteKey: inviteKey)

XCTAssertNoThrow(try identityStorage.getInviteKey(for: account))
XCTAssertTrue(networkService.callRegisterInvite)
Expand Down Expand Up @@ -83,7 +96,9 @@ final class RegistryServiceTests: XCTestCase {
let topic = invitePubKey.rawRepresentation.sha256().toHexString()
try await networkingInteractor.subscribe(topic: topic)

try await identityClient.goPrivate(account: account)
let inviteKey = try await identityClient.goPrivate(account: account)
resubscriptionService.unsubscribeFromInvites(inviteKey: inviteKey)

XCTAssertThrowsError(try identityStorage.getInviteKey(for: account))
XCTAssertTrue(networkingInteractor.unsubscriptions.contains(topic))
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/RelayerTests/AuthTests/JWTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ extension AuthPayload.Claims {
let iss = "did:key:z6MkodHZwneVRShtaLf8JKYkxpDGp1vGZnpGmdBpX8M2exxH"
let sub = "c479fe5dc464e771e78b193d239a65b58d278cad1c34bfb0b5716e5bb514928e"
let iatDate = Date(timeIntervalSince1970: 1656910097)
let iat = Int64(iatDate.timeIntervalSince1970)
let iat = UInt64(iatDate.timeIntervalSince1970)
var components = DateComponents()
components.setValue(1, for: .day)
let aud = "wss://relay.walletconnect.com"
let expDate = Calendar.current.date(byAdding: components, to: iatDate)!
let exp = Int64(expDate.timeIntervalSince1970)
let exp = UInt64(expDate.timeIntervalSince1970)
return AuthPayload.Claims(iss: iss, sub: sub, aud: aud, iat: iat, exp: exp)
}
}
10 changes: 5 additions & 5 deletions Tests/TestingUtils/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public class NetworkingInteractorMock: NetworkInteracting {
}

public let requestPublisherSubject = PassthroughSubject<(topic: String, request: RPCRequest, publishedAt: Date), Never>()
public let responsePublisherSubject = PassthroughSubject<(topic: String, request: RPCRequest, response: RPCResponse), Never>()
public let responsePublisherSubject = PassthroughSubject<(topic: String, request: RPCRequest, response: RPCResponse, publishedAt: Date), Never>()

public var requestPublisher: AnyPublisher<(topic: String, request: RPCRequest, publishedAt: Date), Never> {
requestPublisherSubject.eraseToAnyPublisher()
}

private var responsePublisher: AnyPublisher<(topic: String, request: RPCRequest, response: RPCResponse), Never> {
private var responsePublisher: AnyPublisher<(topic: String, request: RPCRequest, response: RPCResponse, publishedAt: Date), Never> {
responsePublisherSubject.eraseToAnyPublisher()
}

Expand All @@ -60,12 +60,12 @@ public class NetworkingInteractorMock: NetworkInteracting {
.filter { rpcRequest in
return rpcRequest.request.method == request.method
}
.compactMap { topic, rpcRequest, rpcResponse in
.compactMap { topic, rpcRequest, rpcResponse, publishedAt in
guard
let id = rpcRequest.id,
let request = try? rpcRequest.params?.get(Request.self),
let response = try? rpcResponse.result?.get(Response.self) else { return nil }
return ResponseSubscriptionPayload(id: id, topic: topic, request: request, response: response)
return ResponseSubscriptionPayload(id: id, topic: topic, request: request, response: response, publishedAt: publishedAt)
}
.eraseToAnyPublisher()
}
Expand All @@ -74,7 +74,7 @@ public class NetworkingInteractorMock: NetworkInteracting {
public func responseErrorSubscription<Request: Codable>(on request: ProtocolMethod) -> AnyPublisher<ResponseSubscriptionErrorPayload<Request>, Never> {
return responsePublisher
.filter { $0.request.method == request.method }
.compactMap { (topic, rpcRequest, rpcResponse) in
.compactMap { (topic, rpcRequest, rpcResponse, publishedAt) in
guard let id = rpcResponse.id, let request = try? rpcRequest.params?.get(Request.self), let error = rpcResponse.error else { return nil }
return ResponseSubscriptionErrorPayload(id: id, topic: topic, request: request, error: error)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/WalletConnectSignTests/AppProposalServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ final class AppProposalServiceTests: XCTestCase {
exp.fulfill()
}

networkingInteractor.responsePublisherSubject.send((topicA, request, response))
networkingInteractor.responsePublisherSubject.send((topicA, request, response, Date()))
let privateKey = try! cryptoMock.getPrivateKey(for: proposal.proposer.publicKey)!
let topicB = deriveTopic(publicKey: responder.publicKey, privateKey: privateKey)
_ = storageMock.getPairing(forTopic: topicA)!
Expand Down Expand Up @@ -143,7 +143,7 @@ final class AppProposalServiceTests: XCTestCase {
}

let response = RPCResponse.stubError(forRequest: request)
networkingInteractor.responsePublisherSubject.send((topicA, request, response))
networkingInteractor.responsePublisherSubject.send((topicA, request, response, Date()))

XCTAssert(networkingInteractor.didUnsubscribe(to: pairing.topic), "Proposer must unsubscribe if pairing is inactive.")
XCTAssertFalse(storageMock.hasPairing(forTopic: pairing.topic), "Proposer must delete an inactive pairing.")
Expand Down Expand Up @@ -171,7 +171,7 @@ final class AppProposalServiceTests: XCTestCase {
storageMock.setPairing(storedPairing)

let response = RPCResponse.stubError(forRequest: request)
networkingInteractor.responsePublisherSubject.send((topicA, request, response))
networkingInteractor.responsePublisherSubject.send((topicA, request, response, Date()))

XCTAssertFalse(networkingInteractor.didUnsubscribe(to: pairing.topic), "Proposer must not unsubscribe if pairing is active.")
XCTAssert(storageMock.hasPairing(forTopic: pairing.topic), "Proposer must not delete an active pairing.")
Expand Down
4 changes: 2 additions & 2 deletions Tests/WalletConnectSignTests/ApproveEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ final class ApproveEngineTests: XCTestCase {
let request = RPCRequest(method: SessionSettleProtocolMethod().method, params: SessionType.SettleParams.stub())
let response = RPCResponse(matchingRequest: request, result: RPCResult.response(AnyCodable(true)))

networkingInteractor.responsePublisherSubject.send((session.topic, request, response))
networkingInteractor.responsePublisherSubject.send((session.topic, request, response, Date()))

XCTAssertTrue(sessionStorageMock.getSession(forTopic: session.topic)!.acknowledged, "Responder must acknowledged session")
}
Expand All @@ -139,7 +139,7 @@ final class ApproveEngineTests: XCTestCase {
let request = RPCRequest(method: SessionSettleProtocolMethod().method, params: SessionType.SettleParams.stub())
let response = RPCResponse.stubError(forRequest: request)

networkingInteractor.responsePublisherSubject.send((session.topic, request, response))
networkingInteractor.responsePublisherSubject.send((session.topic, request, response, Date()))

XCTAssertNil(sessionStorageMock.getSession(forTopic: session.topic), "Responder must remove session")
XCTAssertTrue(networkingInteractor.didUnsubscribe(to: session.topic), "Responder must unsubscribe topic B")
Expand Down

0 comments on commit d7b2409

Please sign in to comment.