Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for verify context #1269

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "VerifyTests"
BuildableName = "VerifyTests"
BlueprintName = "VerifyTests"
ReferencedContainer = "container:..">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
23 changes: 5 additions & 18 deletions Sources/WalletConnectVerify/VerifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public actor VerifyClient: VerifyClientProtocol {
let originVerifier: OriginVerifier
let assertionRegistrer: AssertionRegistrer
let appAttestationRegistrer: AppAttestationRegistrer
let verifyContextFactory: VerifyContextFactory

init(
originVerifier: OriginVerifier,
assertionRegistrer: AssertionRegistrer,
appAttestationRegistrer: AppAttestationRegistrer
appAttestationRegistrer: AppAttestationRegistrer,
verifyContextFactory: VerifyContextFactory = VerifyContextFactory()
) {
self.originVerifier = originVerifier
self.assertionRegistrer = assertionRegistrer
self.appAttestationRegistrer = appAttestationRegistrer
self.verifyContextFactory = verifyContextFactory
}

public func registerAttestationIfNeeded() async throws {
Expand All @@ -34,23 +37,7 @@ public actor VerifyClient: VerifyClientProtocol {
}

nonisolated 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
)
}
verifyContextFactory.createVerifyContext(origin: origin, domain: domain, isScam: isScam)
}

public func registerAssertion() async throws {
Expand Down
24 changes: 24 additions & 0 deletions Sources/WalletConnectVerify/VerifyContextFactory.swift
Original file line number Diff line number Diff line change
@@ -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
)
}
}
}
38 changes: 38 additions & 0 deletions Tests/VerifyTests/VerifyContextFactoryTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}