From 130526dcaad1c29206841d5e014f4cc43483fcc5 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 2 Jan 2024 10:53:36 +0300 Subject: [PATCH] add tests for verify context --- .../xcschemes/WalletConnectVerify.xcscheme | 10 +++++ .../WalletConnectVerify/VerifyClient.swift | 23 +++-------- .../VerifyContextFactory.swift | 24 ++++++++++++ .../VerifyContextFactoryTests.swift | 38 +++++++++++++++++++ 4 files changed, 77 insertions(+), 18 deletions(-) create mode 100644 Sources/WalletConnectVerify/VerifyContextFactory.swift create mode 100644 Tests/VerifyTests/VerifyContextFactoryTests.swift diff --git a/Example/ExampleApp.xcodeproj/xcshareddata/xcschemes/WalletConnectVerify.xcscheme b/Example/ExampleApp.xcodeproj/xcshareddata/xcschemes/WalletConnectVerify.xcscheme index 5b4aa4030..0f35eb712 100644 --- a/Example/ExampleApp.xcodeproj/xcshareddata/xcschemes/WalletConnectVerify.xcscheme +++ b/Example/ExampleApp.xcodeproj/xcshareddata/xcschemes/WalletConnectVerify.xcscheme @@ -28,6 +28,16 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> + + + + VerifyContext { - guard isScam != true else { - return VerifyContext( - origin: origin, - validation: .scam - ) - } - if let origin, let originUrl = URL(string: origin), let domainUrl = URL(string: domain) { - return VerifyContext( - origin: origin, - validation: (originUrl.host == domainUrl.host) ? .valid : .invalid - ) - } else { - return VerifyContext( - origin: origin, - validation: .unknown - ) - } + verifyContextFactory.createVerifyContext(origin: origin, domain: domain, isScam: isScam) } public func registerAssertion() async throws { diff --git a/Sources/WalletConnectVerify/VerifyContextFactory.swift b/Sources/WalletConnectVerify/VerifyContextFactory.swift new file mode 100644 index 000000000..b687932e4 --- /dev/null +++ b/Sources/WalletConnectVerify/VerifyContextFactory.swift @@ -0,0 +1,24 @@ + +import Foundation + +class VerifyContextFactory { + public func createVerifyContext(origin: String?, domain: String, isScam: Bool?) -> VerifyContext { + guard isScam != true else { + return VerifyContext( + origin: origin, + validation: .scam + ) + } + if let origin, let originUrl = URL(string: origin), let domainUrl = URL(string: domain) { + return VerifyContext( + origin: origin, + validation: (originUrl.host == domainUrl.host) ? .valid : .invalid + ) + } else { + return VerifyContext( + origin: origin, + validation: .unknown + ) + } + } +} diff --git a/Tests/VerifyTests/VerifyContextFactoryTests.swift b/Tests/VerifyTests/VerifyContextFactoryTests.swift new file mode 100644 index 000000000..ac540cc0e --- /dev/null +++ b/Tests/VerifyTests/VerifyContextFactoryTests.swift @@ -0,0 +1,38 @@ +import Foundation +import XCTest +@testable import WalletConnectVerify + + +class VerifyContextFactoryTests: XCTestCase { + var factory: VerifyContextFactory! + + override func setUp() { + super.setUp() + factory = VerifyContextFactory() + } + + override func tearDown() { + factory = nil + super.tearDown() + } + + func testScamValidation() { + let context = factory.createVerifyContext(origin: "http://example.com", domain: "http://example.com", isScam: true) + XCTAssertEqual(context.validation, .scam) + } + + func testValidOriginAndDomain() { + let context = factory.createVerifyContext(origin: "http://example.com", domain: "http://example.com", isScam: false) + XCTAssertEqual(context.validation, .valid) + } + + func testInvalidOriginAndDomain() { + let context = factory.createVerifyContext(origin: "http://example.com", domain: "http://different.com", isScam: false) + XCTAssertEqual(context.validation, .invalid) + } + + func testUnknownValidation() { + let context = factory.createVerifyContext(origin: nil, domain: "http://example.com", isScam: false) + XCTAssertEqual(context.validation, .unknown) + } + }