Skip to content

Commit

Permalink
Merge pull request #68 from torusresearch/fix_tests_master
Browse files Browse the repository at this point in the history
fix: tests
  • Loading branch information
metalurgical authored Mar 12, 2024
2 parents 0f4b421 + e0be73a commit 770d3d1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 92 deletions.
32 changes: 14 additions & 18 deletions Tests/CustomAuthTests/CustomAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import JWTDecode
import TorusUtils
import UIKit
import XCTest
import curveSecp256k1

final class MockSDKTest: XCTestCase {
func test_jwtDecodeTest() {
Expand All @@ -11,20 +12,16 @@ final class MockSDKTest: XCTestCase {
print(decodedData)
}

func testGetTorusKey() async {
func testGetTorusKey() async throws {
let expectation = XCTestExpectation(description: "getTorusKey should correctly proxy input and output to/from TorusUtils")

let expectedPrivateKey = fakeData.generatePrivateKey()
let expectedPublicAddress = fakeData.generatePublicKey()
let expectedPrivateKey = try fakeData.generatePrivateKey()
let expectedPublicAddress = try fakeData.generatePublicKey()
let expectedVerifier = fakeData.generateVerifier()
let expectedVerfierId = fakeData.generateRandomEmail(of: 6)

let subVerifier = [SubVerifierDetails(loginProvider: .jwt, clientId: fakeData.generateVerifier(), verifier: expectedVerifier, redirectURL: fakeData.generateVerifier())]
let factory = MockFactory()

// let CustomAuth = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifiery)
let CustomAuth = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: expectedVerifier, subVerifierDetails: [])
// var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils

var mockTorusUtils = MockTorusUtils()
CustomAuth.torusUtils = mockTorusUtils
// Set Mock data
Expand All @@ -48,11 +45,11 @@ final class MockSDKTest: XCTestCase {
}
}

func testGetAggregateTorusKey() async {
func testGetAggregateTorusKey() async throws {
let expectation = XCTestExpectation(description: "getAggregateTorusKey should correctly proxy input and output to/from TorusUtils")

let expectedPrivateKey = fakeData.generatePrivateKey()
let expectedPublicAddress = fakeData.generatePublicKey()
let expectedPrivateKey = try fakeData.generatePrivateKey()
let expectedPublicAddress = try fakeData.generatePublicKey()
let expectedVerifier = fakeData.generateVerifier()
let expectedVerfierId = fakeData.generateRandomEmail(of: 6)

Expand Down Expand Up @@ -96,15 +93,14 @@ class fakeData {
return String.randomString(length: 10)
}

static func generatePrivateKey() -> String {
let privateKey = Data.randomOfLength(32)
return (privateKey?.toHexString())!
static func generatePrivateKey() throws -> String {
let privateKey = curveSecp256k1.SecretKey();
return try privateKey.serialize()
}

static func generatePublicKey() -> String {
let privateKey = Data.randomOfLength(32)!
let publicKey = SECP256K1.privateToPublic(privateKey: privateKey)?.subdata(in: 1 ..< 65)
return publicKey!.toHexString()
static func generatePublicKey() throws -> String {
let privateKey = curveSecp256k1.SecretKey();
return try privateKey.toPublic().serialize(compressed: false);
}

static func generateRandomEmail(of length: Int) -> String {
Expand Down
58 changes: 18 additions & 40 deletions Tests/CustomAuthTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,62 +26,40 @@ final class IntegrationTests: XCTestCase {
IntegrationTests.sdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: "torus-test-ios-public", subVerifierDetails: [sub], network: .legacy(.CYAN))
}

func test_getTorusKey() async {
func test_getTorusKey() async throws {
let TORUS_TEST_VERIFIER = "torus-test-health"
// let TORUS_TEST_VERIFIER = "torus-google-dhruv-test"
let exp1 = XCTestExpectation(description: "Should be able to get key")
let email = "[email protected]"
let jwt = try! generateIdToken(email: email)
do {
let data = try await IntegrationTests.sdk?.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: email, idToken: jwt)
let finalKeyDataDict = data!.finalKeyData!
let evmAddress = finalKeyDataDict.evmAddress
XCTAssertEqual(evmAddress, "0x8AA6C8ddCD868873120aA265Fc63E3a2180375BA")
exp1.fulfill()
} catch {
print(error)
XCTFail(error.localizedDescription)
}
wait(for: [exp1], timeout: 15)
let data = try await IntegrationTests.sdk?.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: email, idToken: jwt)
let finalKeyDataDict = data!.finalKeyData!
let evmAddress = finalKeyDataDict.evmAddress
XCTAssertEqual(evmAddress, "0xC615aA03Dd8C9b2dc6F7c43cBDfF2c34bBa47Ec9")
}



let TORUS_TEST_EMAIL = "[email protected]";
let TORUS_IMPORT_EMAIL = "[email protected]";

let TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]";
let TORUS_TEST_EMAIL = "[email protected]"
let TORUS_IMPORT_EMAIL = "[email protected]"

let TORUS_TEST_VERIFIER = "torus-test-health";
let TORUS_EXTENDED_VERIFIER_EMAIL = "[email protected]"

let TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate";
let HashEnabledVerifier = "torus-test-verifierid-hash";

func test_Sapphire_getTorusKey() async {
let TORUS_TEST_VERIFIER = "torus-test-health"

let TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate"
let HashEnabledVerifier = "torus-test-verifierid-hash"

func test_Sapphire_getTorusKey() async throws {
let sub = SubVerifierDetails(loginType: .web,
loginProvider: .google,
clientId: "221898609709-obfn3p63741l5333093430j3qeiinaa8.apps.googleusercontent.com",
verifier: "google-lrc",
redirectURL: "com.googleusercontent.apps.238941746713-vfap8uumijal4ump28p9jd3lbe6onqt4:/oauthredirect",
browserRedirectURL: "https://scripts.toruswallet.io/redirect.html")
let sdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: "torus-test-ios-public", subVerifierDetails: [sub], network: .sapphire(.SAPPHIRE_DEVNET))


let exp1 = XCTestExpectation(description: "Should be able to get key")
let email = "[email protected]"

let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL)
do {
let data = try await sdk.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: jwt)
let finalKeyDataDict = data.finalKeyData!
let evmAddress = finalKeyDataDict.evmAddress
XCTAssertEqual(evmAddress, "0x4924F91F5d6701dDd41042D94832bB17B76F316F")
exp1.fulfill()
} catch {
print(error)
XCTFail(error.localizedDescription)
}
wait(for: [exp1], timeout: 15)
let data = try await sdk.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: jwt)
let finalKeyDataDict = data.finalKeyData!
let evmAddress = finalKeyDataDict.evmAddress
XCTAssertEqual(evmAddress, "0x81001206C06AD09b3611b593aEEd3A607d79871E")
}
}

Expand Down
36 changes: 2 additions & 34 deletions Tests/CustomAuthTests/StubURLProtocolTests.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by Michael Lee on 31/10/2021.
//

import CustomAuth
import FetchNodeDetails
import Foundation
Expand All @@ -13,24 +6,7 @@ import TorusUtils
import XCTest
import CommonSources

final class StubURLProtocolTests: XCTestCase {
// func testStubURLProtocol() {
// let expectation = XCTestExpectation(description: "getTorusKey using stubbed URLSession should work")
// let sessionConfiguration =c URLSessionConfiguration.ephemeral
// sessionConfiguration.protocolClasses = [StubURLProtocol.self]
// let urlSession = URLSession(configuration: sessionConfiguration)
// let tdsdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifierName: "torus-direct-mock-ios", subVerifierDetails: [], factory: StubMockCASDKFactory(), network: .ROPSTEN, loglevel: .debug, urlSession: urlSession)
// tdsdk.getTorusKey(verifier: "torus-direct-mock-ios", verifierId: "[email protected]", idToken: "eyJhbGciOiJSUzI1NiIsImtpZCI6ImFkZDhjMGVlNjIzOTU0NGFmNTNmOTM3MTJhNTdiMmUyNmY5NDMzNTIiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiI2MzYxOTk0NjUyNDItZmQ3dWp0b3JwdnZ1ZHRzbDN1M2V2OTBuaWplY3RmcW0uYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiI2MzYxOTk0NjUyNDItZmQ3dWp0b3JwdnZ1ZHRzbDN1M2V2OTBuaWplY3RmcW0uYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDkxMTE5NTM4NTYwMzE3OTk2MzkiLCJoZCI6InRvci51cyIsImVtYWlsIjoibWljaGFlbEB0b3IudXMiLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiYXRfaGFzaCI6InRUNDhSck1vdGFFbi1UN3dzc2U3QnciLCJub25jZSI6InZSU2tPZWwyQTkiLCJuYW1lIjoiTWljaGFlbCBMZWUiLCJwaWN0dXJlIjoiaHR0cHM6Ly9saDMuZ29vZ2xldXNlcmNvbnRlbnQuY29tL2EvQUFUWEFKd3NCYjk4Z1NZalZObEJCQWhYSmp2cU5PdzJHRFNlVGYwSTZTSmg9czk2LWMiLCJnaXZlbl9uYW1lIjoiTWljaGFlbCIsImZhbWlseV9uYW1lIjoiTGVlIiwibG9jYWxlIjoiZW4iLCJpYXQiOjE2MzQ0NjgyNDksImV4cCI6MTYzNDQ3MTg0OX0.XGu1tm_OqlSrc5BMDMzOrlhxLZo1YnpCUT0_j2U1mQt86nJzf_Hp85JfapZj2QeeUz91H6-Ei8FR1i4ICEfjMcoZOW1Azc89qUNfUgWeyjqZ7wCHSsbHAwabE74RFAS9YAja8_ynUvCARfDEtoqcreNgmbw3ZntzAqpuuNBXYfbr87kMvu_wZ7fWjLKM91CvuXytQBwtieTyjAFnTXmEL60Pdu-JSQfHCbS5H39ZHlnYxEO6qztIjvbnQokhjHDGc4PMCx0wfzrEet1ojNOCnbfmaYE5NQudquzQNZtqZfn8f4B-sQhECElnOXagHlafWO5RayS0dCb1mTfr8orcCA", userData: [:]).done { data in
// XCTAssertEqual(data["publicAddress"] as! String, "0x22f2Ce611cE0d0ff4DA661d3a4C4B7A60B2b13F8")
// XCTAssertEqual(data["privateKey"] as! String, "495b9a126c0c703caeaa5c561692d6778952c455789b0a2ba04312cfdc2e1bb9")
// expectation.fulfill()
// }.catch { err in
// XCTFail(err.localizedDescription)
// }
//
// wait(for: [expectation], timeout: 12000)
// }
}
final class StubURLProtocolTests: XCTestCase {}

public class StubMockTorusUtils: TorusUtils {
override open func getTimestamp() -> TimeInterval {
Expand All @@ -39,10 +15,7 @@ public class StubMockTorusUtils: TorusUtils {
return ret
}

override open func generatePrivateKeyData() -> Data? {
// empty bytes
// let ret = Data(count: 32)

open func generatePrivateKeyData() -> Data? {
let ret = Data(base64Encoded: "FBz7bssmbsV6jBWoOJpkVOu14+6/Xgyt1pxTycODG08=")

print("[StubMockTorusUtils] generatePrivateKeyData(): ", ret!.bytes.toBase64())
Expand All @@ -51,11 +24,6 @@ public class StubMockTorusUtils: TorusUtils {
}

public class StubMockCASDKFactory: CASDKFactoryProtocol {
// public func createFetchNodeDetails(network: TorusNetwork, urlSession: URLSession, networkUrl: String? = nil) -> FetchNodeDetails {
// let net = network == .MAINNET ? "0xf20336e16B5182637f09821c27BDe29b0AFcfe80" : "0x6258c9d6c12ed3edda59a1a6527e469517744aa7"
// return FetchNodeDetails(proxyAddress: net, network: network, urlSession: urlSession)
// }

public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: TorusNetwork) -> AbstractTorusUtils {
let allowHost = network.signerMap.appending("/api/allow")
let signerHost = network.signerMap.appending("/api/sign")
Expand Down

0 comments on commit 770d3d1

Please sign in to comment.