This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add `Cancel` * add `createCancel` in `DevTools` * update `CloseTests` to always make pfi the sender * add `CancelTests`
- Loading branch information
Showing
6 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
|
||
public typealias Cancel = Message<CancelData> | ||
|
||
/// Data that makes up a Cancel Message. | ||
/// | ||
/// [Specification Reference](https://github.com/TBD54566975/tbdex/blob/main/specs/protocol/README.md#cancel) | ||
public struct CancelData: MessageData { | ||
|
||
/// An explanation of why the exchange is being cancelled | ||
public let reason: String? | ||
|
||
/// Returns the MessageKind of cancel | ||
public func kind() -> MessageKind { | ||
return .cancel | ||
} | ||
|
||
/// Default Initializer | ||
public init( | ||
reason: String? = nil | ||
) { | ||
self.reason = reason | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Tests/tbDEXTests/Protocol/Models/Messages/CancelTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Web5 | ||
import XCTest | ||
|
||
@testable import tbDEX | ||
|
||
final class CancelTests: XCTestCase { | ||
|
||
let did = try! DIDJWK.create(keyManager: InMemoryKeyManager()) | ||
let pfi = try! DIDJWK.create(keyManager: InMemoryKeyManager()) | ||
|
||
func test_init() { | ||
let cancel = DevTools.createCancel(from: did.uri, to: pfi.uri) | ||
|
||
XCTAssertEqual(cancel.metadata.id.prefix, "cancel") | ||
XCTAssertEqual(cancel.metadata.from, did.uri) | ||
XCTAssertEqual(cancel.metadata.to, pfi.uri) | ||
XCTAssertEqual(cancel.metadata.exchangeID, "exchange_123") | ||
XCTAssertEqual(cancel.data.reason, "test reason") | ||
XCTAssertEqual(cancel.metadata.protocol, "1.0") | ||
} | ||
|
||
func test_overrideProtocolVersion() { | ||
let cancel = DevTools.createCancel( | ||
from: did.uri, | ||
to: pfi.uri, | ||
protocol: "2.0" | ||
) | ||
|
||
XCTAssertEqual(cancel.metadata.protocol, "2.0") | ||
} | ||
|
||
func test_signSuccess() async throws { | ||
var cancel = DevTools.createCancel(from: did.uri, to: pfi.uri) | ||
|
||
XCTAssertNil(cancel.signature) | ||
try cancel.sign(did: did) | ||
XCTAssertNotNil(cancel.signature) | ||
} | ||
|
||
func test_verifySuccess() async throws { | ||
var cancel = DevTools.createCancel(from: did.uri, to: pfi.uri) | ||
try cancel.sign(did: did) | ||
|
||
let isValid = try await cancel.verify() | ||
XCTAssertTrue(isValid) | ||
} | ||
|
||
func test_verifyWithoutSigningFailure() async throws { | ||
let cancel = DevTools.createCancel(from: did.uri, to: pfi.uri) | ||
|
||
await XCTAssertThrowsErrorAsync(try await cancel.verify()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters