89 lines
2.9 KiB
Swift
89 lines
2.9 KiB
Swift
|
|
import XCTest
|
||
|
|
@testable import Closer
|
||
|
|
|
||
|
|
/// Mock implementation of the release-key callable for testing the iOS request shape.
|
||
|
|
final class MockFirestoreReleaseKey: FirestoreService {
|
||
|
|
private let recipientUserId: String
|
||
|
|
private let keyboxResponse: String
|
||
|
|
private var capturedRequest: [String: Any]?
|
||
|
|
|
||
|
|
init(recipientUserId: String, keyboxResponse: String) {
|
||
|
|
self.recipientUserId = recipientUserId
|
||
|
|
self.keyboxResponse = keyboxResponse
|
||
|
|
super.init()
|
||
|
|
}
|
||
|
|
|
||
|
|
override func wrapReleaseKeyForPartner(
|
||
|
|
oneTimeKey: Data,
|
||
|
|
recipientUserId: String,
|
||
|
|
aad: String = "closer_release_key"
|
||
|
|
) async throws -> Keybox {
|
||
|
|
// Capture the request shape exactly as the real implementation would send it.
|
||
|
|
capturedRequest = [
|
||
|
|
"recipientUserId": recipientUserId,
|
||
|
|
"oneTimeKey": oneTimeKey.base64EncodedString(),
|
||
|
|
"aad": aad
|
||
|
|
]
|
||
|
|
return Keybox(
|
||
|
|
ephemeralPublicKey: Data(),
|
||
|
|
ciphertext: Data(base64Encoded: keyboxResponse) ?? Data(),
|
||
|
|
mac: Data()
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
func capturedRequestDictionary() -> [String: Any]? {
|
||
|
|
capturedRequest
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
final class KeyboxCallableTests: XCTestCase {
|
||
|
|
private let oneTimeKey = Data((0..<32).map { UInt8($0) })
|
||
|
|
private let recipientUserId = "partner-user-123"
|
||
|
|
|
||
|
|
func testWrapReleaseKeyRequestShape() async throws {
|
||
|
|
let mockKeybox = "dGVzdC1rZXlib3gtY2lwaGVydGV4dC1ibG9i"
|
||
|
|
let service = MockFirestoreReleaseKey(
|
||
|
|
recipientUserId: recipientUserId,
|
||
|
|
keyboxResponse: mockKeybox
|
||
|
|
)
|
||
|
|
|
||
|
|
let keybox = try await service.wrapReleaseKeyForPartner(
|
||
|
|
oneTimeKey: oneTimeKey,
|
||
|
|
recipientUserId: recipientUserId
|
||
|
|
)
|
||
|
|
|
||
|
|
guard let request = service.capturedRequestDictionary() else {
|
||
|
|
XCTFail("Expected a captured request")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
XCTAssertEqual(request["recipientUserId"] as? String, recipientUserId)
|
||
|
|
XCTAssertEqual(request["oneTimeKey"] as? String, oneTimeKey.base64EncodedString())
|
||
|
|
XCTAssertEqual(request["aad"] as? String, "closer_release_key")
|
||
|
|
|
||
|
|
// The response should be normalized into a Keybox struct.
|
||
|
|
XCTAssertTrue(keybox.ciphertext.count > 0)
|
||
|
|
XCTAssertEqual(keybox.ephemeralPublicKey.count, 0)
|
||
|
|
XCTAssertEqual(keybox.mac.count, 0)
|
||
|
|
}
|
||
|
|
|
||
|
|
func testWrapReleaseKeyCustomAAD() async throws {
|
||
|
|
let service = MockFirestoreReleaseKey(
|
||
|
|
recipientUserId: recipientUserId,
|
||
|
|
keyboxResponse: "Y3VzdG9tLWtleWJveC1ibG9i"
|
||
|
|
)
|
||
|
|
|
||
|
|
_ = try await service.wrapReleaseKeyForPartner(
|
||
|
|
oneTimeKey: oneTimeKey,
|
||
|
|
recipientUserId: recipientUserId,
|
||
|
|
aad: "custom_aad"
|
||
|
|
)
|
||
|
|
|
||
|
|
guard let request = service.capturedRequestDictionary() else {
|
||
|
|
XCTFail("Expected a captured request")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
XCTAssertEqual(request["aad"] as? String, "custom_aad")
|
||
|
|
}
|
||
|
|
}
|