Skip to content

Commit

Permalink
Merge pull request #500 from WalletConnect/#250-Sign-restore-integrat…
Browse files Browse the repository at this point in the history
…ion-tests

#250 sign restore integration tests
  • Loading branch information
flypaper0 authored Sep 12, 2022
2 parents bc769e6 + 1a731e5 commit b10c626
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 158 deletions.
4 changes: 4 additions & 0 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
8448F1D427E4726F0000B866 /* WalletConnect in Frameworks */ = {isa = PBXBuildFile; productRef = 8448F1D327E4726F0000B866 /* WalletConnect */; };
84494388278D9C1B00CC26BB /* UIAlertController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84494387278D9C1B00CC26BB /* UIAlertController.swift */; };
8460DCFC274F98A10081F94C /* RequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8460DCFB274F98A10081F94C /* RequestViewController.swift */; };
84AA01DB28CF0CD7005D48D8 /* XCTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84AA01DA28CF0CD7005D48D8 /* XCTest.swift */; };
84CE641F27981DED00142511 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE641E27981DED00142511 /* AppDelegate.swift */; };
84CE642127981DED00142511 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE642027981DED00142511 /* SceneDelegate.swift */; };
84CE642827981DF000142511 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 84CE642727981DF000142511 /* Assets.xcassets */; };
Expand Down Expand Up @@ -219,6 +220,7 @@
76B6E39E2807A3B6004DF775 /* WalletViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletViewController.swift; sourceTree = "<group>"; };
84494387278D9C1B00CC26BB /* UIAlertController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIAlertController.swift; sourceTree = "<group>"; };
8460DCFB274F98A10081F94C /* RequestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RequestViewController.swift; sourceTree = "<group>"; };
84AA01DA28CF0CD7005D48D8 /* XCTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTest.swift; sourceTree = "<group>"; };
84CE641C27981DED00142511 /* DApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
84CE641E27981DED00142511 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
84CE642027981DED00142511 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -503,6 +505,7 @@
children = (
A50C036428AAD32200FE72D3 /* ClientDelegate.swift */,
767DC83428997F8E00080FA9 /* EthSendTransaction.swift */,
84AA01DA28CF0CD7005D48D8 /* XCTest.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1462,6 +1465,7 @@
A501AC2728C8E59800CEAA42 /* URLConfig.swift in Sources */,
A5E03DFA286465C700888481 /* SignClientTests.swift in Sources */,
A5E03E0F28646D8A00888481 /* WebSocketFactory.swift in Sources */,
84AA01DB28CF0CD7005D48D8 /* XCTest.swift in Sources */,
A5E03E1128646F8000888481 /* KeychainStorageMock.swift in Sources */,
A5E03DFD286465D100888481 /* Stubs.swift in Sources */,
);
Expand Down
19 changes: 19 additions & 0 deletions Example/IntegrationTests/Sign/Helpers/XCTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation
import XCTest

extension XCTest {
public func XCTAssertThrowsErrorAsync<T: Sendable>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath,
line: UInt = #line,
_ errorHandler: (_ error: Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
XCTFail(message(), file: file, line: line)
} catch {
errorHandler(error)
}
}
}
306 changes: 169 additions & 137 deletions Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import WalletConnectUtils

final class SignClientTests: XCTestCase {

let defaultTimeout: TimeInterval = 5
let defaultTimeout: TimeInterval = 8

var dapp: ClientDelegate!
var wallet: ClientDelegate!
Expand Down Expand Up @@ -242,140 +242,172 @@ final class SignClientTests: XCTestCase {
wait(for: [expectation], timeout: defaultTimeout)
}

//
// func testNewSessionOnExistingPairing() async {
// await waitClientsConnected()
// let proposerSettlesSessionExpectation = expectation(description: "Proposer settles session")
// proposerSettlesSessionExpectation.expectedFulfillmentCount = 2
// let responderSettlesSessionExpectation = expectation(description: "Responder settles session")
// responderSettlesSessionExpectation.expectedFulfillmentCount = 2
// var initiatedSecondSession = false
// let uri = try! await proposer.client.connect(namespaces: [Namespace.stub()])!
//
// try! await responder.client.pair(uri: uri)
//
// responder.onSessionProposal = { [unowned self] proposal in
// try? responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [])
// }
// responder.onSessionSettled = { sessionSettled in
// responderSettlesSessionExpectation.fulfill()
// }
// proposer.onSessionSettled = { [unowned self] sessionSettled in
// proposerSettlesSessionExpectation.fulfill()
// let pairingTopic = proposer.client.getSettledPairings().first!.topic
// if !initiatedSecondSession {
// Task {
// let _ = try! await proposer.client.connect(namespaces: [Namespace.stub()], topic: pairingTopic)
// }
// initiatedSecondSession = true
// }
// }
// wait(for: [proposerSettlesSessionExpectation, responderSettlesSessionExpectation], timeout: defaultTimeout)
// }
//
// func testSessionPing() async {
// await waitClientsConnected()
// let proposerReceivesPingResponseExpectation = expectation(description: "Proposer receives ping response")
// let uri = try! await proposer.client.connect(namespaces: [Namespace.stub()])!
//
// try! await responder.client.pair(uri: uri)
// responder.onSessionProposal = { [unowned self] proposal in
// try? self.responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [])
// }
// proposer.onSessionSettled = { [unowned self] sessionSettled in
// self.proposer.client.ping(topic: sessionSettled.topic) { response in
// XCTAssertTrue(response.isSuccess)
// proposerReceivesPingResponseExpectation.fulfill()
// }
// }
// wait(for: [proposerReceivesPingResponseExpectation], timeout: defaultTimeout)
// }
//
// func testSuccessfulSessionUpdateNamespaces() async {
// await waitClientsConnected()
// let proposerSessionUpdateExpectation = expectation(description: "Proposer updates session methods on responder request")
// let responderSessionUpdateExpectation = expectation(description: "Responder updates session methods on proposer response")
// let uri = try! await proposer.client.connect(namespaces: [Namespace.stub()])!
// let namespacesToUpdateWith: Set<Namespace> = [Namespace(chains: [Blockchain("eip155:1")!, Blockchain("eip155:137")!], methods: ["xyz"], events: ["abc"])]
// try! await responder.client.pair(uri: uri)
// responder.onSessionProposal = { [unowned self] proposal in
// try? self.responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [])
// }
// responder.onSessionSettled = { [unowned self] session in
// try? responder.client.updateNamespaces(topic: session.topic, namespaces: namespacesToUpdateWith)
// }
// proposer.onSessionUpdateNamespaces = { topic, namespaces in
// XCTAssertEqual(namespaces, namespacesToUpdateWith)
// proposerSessionUpdateExpectation.fulfill()
// }
// responder.onSessionUpdateNamespaces = { topic, namespaces in
// XCTAssertEqual(namespaces, namespacesToUpdateWith)
// responderSessionUpdateExpectation.fulfill()
// }
// wait(for: [proposerSessionUpdateExpectation, responderSessionUpdateExpectation], timeout: defaultTimeout)
// }
//
// func testSuccessfulSessionUpdateExpiry() async {
// await waitClientsConnected()
// let proposerSessionUpdateExpectation = expectation(description: "Proposer updates session expiry on responder request")
// let responderSessionUpdateExpectation = expectation(description: "Responder updates session expiry on proposer response")
// let uri = try! await proposer.client.connect(namespaces: [Namespace.stub()])!
// try! await responder.client.pair(uri: uri)
// responder.onSessionProposal = { [unowned self] proposal in
// try? self.responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [])
// }
// responder.onSessionSettled = { [unowned self] session in
// Thread.sleep(forTimeInterval: 1) //sleep because new expiry must be greater than current
// try? responder.client.updateExpiry(topic: session.topic)
// }
// proposer.onSessionUpdateExpiry = { _, _ in
// proposerSessionUpdateExpectation.fulfill()
// }
// responder.onSessionUpdateExpiry = { _, _ in
// responderSessionUpdateExpectation.fulfill()
// }
// wait(for: [proposerSessionUpdateExpectation, responderSessionUpdateExpectation], timeout: defaultTimeout)
// }
//
// func testSessionEventSucceeds() async {
// await waitClientsConnected()
// let proposerReceivesEventExpectation = expectation(description: "Proposer receives event")
// let namespace = Namespace(chains: [Blockchain("eip155:1")!], methods: [], events: ["type1"]) // TODO: Fix namespace with empty chain array / protocol change
// let uri = try! await proposer.client.connect(namespaces: [namespace])!
//
// try! await responder.client.pair(uri: uri)
// let event = Session.Event(name: "type1", data: AnyCodable("event_data"))
// responder.onSessionProposal = { [unowned self] proposal in
// try? self.responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [namespace])
// }
// responder.onSessionSettled = { [unowned self] session in
// Task{try? await responder.client.emit(topic: session.topic, event: event, chainId: Blockchain("eip155:1")!)}
// }
// proposer.onEventReceived = { event, _ in
// XCTAssertEqual(event, event)
// proposerReceivesEventExpectation.fulfill()
// }
// wait(for: [proposerReceivesEventExpectation], timeout: defaultTimeout)
// }
//
// func testSessionEventFails() async {
// await waitClientsConnected()
// let proposerReceivesEventExpectation = expectation(description: "Proposer receives event")
// proposerReceivesEventExpectation.isInverted = true
// let uri = try! await proposer.client.connect(namespaces: [Namespace.stub()])!
//
// try! await responder.client.pair(uri: uri)
// let event = Session.Event(name: "type2", data: AnyCodable("event_data"))
// responder.onSessionProposal = { [unowned self] proposal in
// try? self.responder.client.approve(proposalId: proposal.id, accounts: [], namespaces: [])
// }
// proposer.onSessionSettled = { [unowned self] session in
// Task {await XCTAssertThrowsErrorAsync(try await proposer.client.emit(topic: session.topic, event: event, chainId: Blockchain("eip155:1")!))}
// }
// responder.onEventReceived = { _, _ in
// XCTFail()
// proposerReceivesEventExpectation.fulfill()
// }
// wait(for: [proposerReceivesEventExpectation], timeout: defaultTimeout)
// }

func testNewSessionOnExistingPairing() async {
let dappSettlementExpectation = expectation(description: "Dapp settles session")
dappSettlementExpectation.expectedFulfillmentCount = 2
let walletSettlementExpectation = expectation(description: "Wallet settles session")
walletSettlementExpectation.expectedFulfillmentCount = 2
let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)
var initiatedSecondSession = false

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
do {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
} catch {
XCTFail("\(error)")
}
}
}
dapp.onSessionSettled = { [unowned self] _ in
dappSettlementExpectation.fulfill()
let pairingTopic = dapp.client.getPairings().first!.topic
if !initiatedSecondSession {
Task(priority: .high) {
let _ = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces, topic: pairingTopic)
}
initiatedSecondSession = true
}
}
wallet.onSessionSettled = { _ in
walletSettlementExpectation.fulfill()
}

let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)
wait(for: [dappSettlementExpectation, walletSettlementExpectation], timeout: defaultTimeout)

}

func testSessionPing() async {
let expectation = expectation(description: "Dapp receives ping response")
let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
}
}
dapp.onSessionSettled = { [unowned self] settledSession in
dapp.client.ping(topic: settledSession.topic) {_ in
expectation.fulfill()
}
}
let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)
wait(for: [expectation], timeout: defaultTimeout)
}


func testSuccessfulSessionUpdateNamespaces() async {
let expectation = expectation(description: "Dapp updates namespaces")
let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
}
}
dapp.onSessionSettled = { [unowned self] settledSession in
Task(priority: .high) {
try! await wallet.client.update(topic: settledSession.topic, namespaces: sessionNamespaces)
}
}
dapp.onSessionUpdateNamespaces = { _, _ in
expectation.fulfill()
}
let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)
wait(for: [expectation], timeout: defaultTimeout)
}


func testSuccessfulSessionExtend() async {
let expectation = expectation(description: "Dapp extends session")

let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
}
}

dapp.onSessionExtend = { _, _ in
expectation.fulfill()
}

dapp.onSessionSettled = { [unowned self] settledSession in
Task(priority: .high) {
try! await wallet.client.extend(topic: settledSession.topic)
}
}

let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)

wait(for: [expectation], timeout: defaultTimeout)
}

func testSessionEventSucceeds() async {
let expectation = expectation(description: "Dapp receives session event")

let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)
let event = Session.Event(name: "any", data: AnyCodable("event_data"))
let chain = Blockchain("eip155:1")!

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
}
}

dapp.onEventReceived = { _, _ in
expectation.fulfill()
}

dapp.onSessionSettled = { [unowned self] settledSession in
Task(priority: .high) {
try! await wallet.client.emit(topic: settledSession.topic, event: event, chainId: chain)
}
}

let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)

wait(for: [expectation], timeout: defaultTimeout)
}

func testSessionEventFails() async {
let expectation = expectation(description: "Dapp receives session event")

let requiredNamespaces = ProposalNamespace.stubRequired()
let sessionNamespaces = SessionNamespace.make(toRespond: requiredNamespaces)
let event = Session.Event(name: "unknown", data: AnyCodable("event_data"))
let chain = Blockchain("eip155:1")!

wallet.onSessionProposal = { [unowned self] proposal in
Task(priority: .high) {
try await wallet.client.approve(proposalId: proposal.id, namespaces: sessionNamespaces)
}
}

dapp.onSessionSettled = { [unowned self] settledSession in
Task(priority: .high) {
await XCTAssertThrowsErrorAsync(try await wallet.client.emit(topic: settledSession.topic, event: event, chainId: chain))
expectation.fulfill()
}
}

let uri = try! await dapp.client.connect(requiredNamespaces: requiredNamespaces)
try! await wallet.client.pair(uri: uri!)

wait(for: [expectation], timeout: defaultTimeout)
}
}
2 changes: 1 addition & 1 deletion Example/IntegrationTests/Stubs/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension ProposalNamespace {
"eip155": ProposalNamespace(
chains: [Blockchain("eip155:1")!],
methods: ["personal_sign", "eth_sendTransaction"],
events: [])
events: ["any"])
]
}
}
Expand Down
Loading

0 comments on commit b10c626

Please sign in to comment.