import CottonKit import Foundation import XCTest @testable import CottonUI /// The approving card on a signed-in iPhone (after the sign-in hardening; /// Android's `SignInAskCard.kt` is the reference). Synthetic /// asks, codes and keys only. @MainActor final class SignInAsksTests: XCTestCase { private static let scalar = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8" private static func card(_ phase: String, show: Bool = false, poll: Int = 10) -> String { let shown = show ? #", "show": "This is me, show the code""# : "" return #""" {"waiting": [{"askId": "ask-1", "name": "Walk Mac", "kind": "mac", "phase": "\#(phase)", "line": "Walk Mac wants to sign in", "title": "Your Mac wants to sign in to your Cotton", "sub": "Walk Mac asked just now. Approve only if you're holding it yourself.", "notMe": "Not me", "notMeConfirm": "Say no to this Mac?", "notMeDone": "Done.", "scan": {"heading": "Scan the code on your Mac", "typeInstead": "Type the code instead", "none": "No camera here. Type the code instead."}, "typeHeading": "Type the six digits shown on your Mac", "typeWaiting": "The digits show on your Mac in a moment.", "type": "Type the code it shows", "never": "Never type a code someone sends you", "digits": 6, "vault": true, "when": "Just now", "approve": "Approve", "refuse": "Refuse", "confirm": "Refuse Walk Mac?"\#(shown)}], "pollSeconds": \#(poll)} """# } final class Said { var words: [String] = [] } private func asks(_ core: ScriptedCore) -> (SignInAsks, Said) { let said = Said() let keyring = VaultKeyring(keys: InMemoryVaultKeyStore(seeded: Self.scalar), cache: InMemorySecretStore()) let model = SignInAsks(core: core, vault: VaultAccess(keyring: keyring, ask: { .allowed }), say: { words, _ in said.words.append(words) }) return (model, said) } /// RULE (GETTING-IN.md): the one card says who asks in the core's words, with Approve and Not me; looking at it /// calls signin.asks and nothing else. func testTheOneCardSaysWhoAsksWithApproveAndNotMe() async throws { let core = ScriptedCore([CottonMethod.signinAsks: .ok(Self.card("asked", show: true))]) let (model, _) = asks(core) await model.read() let ask = try XCTUnwrap(model.ask) XCTAssertEqual(ask.title, "Your Mac wants to sign in to your Cotton") XCTAssertEqual(ask.notMe, "Not me") XCTAssertEqual(ask.scanHeading, "Scan the code on your Mac") XCTAssertFalse(model.typing) model.typed = "123456" XCTAssertFalse(model.ready, "no typed approval before Type the code instead and the codes compared") let called = await core.methodsCalled() XCTAssertEqual(called, [CottonMethod.signinAsks], "looking sets nothing") } /// RULE: Type the code instead opens that ask (signin.show) and stands the digits field; Approve sends the typed /// code with the key lent behind the lock. func testTypeTheCodeInsteadOpensTheAskAndApprovesTheTypedCode() async throws { let core = ScriptedCore([ CottonMethod.signinAsks: .ok(Self.card("asked", show: true)), CottonMethod.signinShow: .ok(Self.card("compare", poll: 2)), CottonMethod.signinApprove: .ok(#"{"ok": true, "vault": true, "said": "Your Mac can open your things now."}"#), CottonMethod.syncNow: .ok("{}"), ]) let (model, said) = asks(core) await model.read() await model.typeInstead() let sent = await core.calls.last { $0.method == CottonMethod.signinShow } let body = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(try XCTUnwrap(sent).requestJSON.utf8)) as? [String: Any]) XCTAssertEqual(body["askId"] as? String, "ask-1") XCTAssertTrue(model.typing) model.typed = "123456" XCTAssertTrue(model.ready) await model.approveTyped() let approve = await core.calls.last { $0.method == CottonMethod.signinApprove } let approved = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(try XCTUnwrap(approve).requestJSON.utf8)) as? [String: Any]) XCTAssertEqual(approved["code"] as? String, "123456") XCTAssertNotNil(approved["privateKey"] as? String, "the key is lent, so the core can hand it over") XCTAssertEqual(said.words.first, "Your Mac can open your things now.") } /// RULE: a scan goes to signin.approve untouched; a scan of another screen is said over the camera, which stays. func testAScanIsSentAsReadAndAWrongOneKeepsTheCamera() async throws { let core = ScriptedCore([ CottonMethod.signinAsks: .ok(Self.card("asked", show: true)), CottonMethod.signinApprove: .refuse(code: "scan_wrong", message: "That's not the code for this request. Scan the one showing on your Mac."), ]) let (model, said) = asks(core) await model.read() model.scanning = true await model.scanned("COTTON-IN:1:ask-1:bm9uY2U") let approve = await core.calls.last { $0.method == CottonMethod.signinApprove } let body = try XCTUnwrap(try JSONSerialization.jsonObject(with: Data(try XCTUnwrap(approve).requestJSON.utf8)) as? [String: Any]) XCTAssertEqual(body["scanned"] as? String, "COTTON-IN:1:ask-1:bm9uY2U") XCTAssertTrue(model.scanning, "the camera stays open") XCTAssertEqual(model.wrong, "That's not the code for this request. Scan the one showing on your Mac.") XCTAssertEqual(said.words, []) } /// RULE (OWNER.md): a device that cannot hand over the key draws no card with Approve, only the core's line. func testADeviceThatCannotHandOverTheKeyDrawsOnlyTheLine() async throws { let core = ScriptedCore([CottonMethod.signinAsks: .ok(#""" {"waiting": [], "cannot": {"askId": "ask-1", "title": "Your Mac wants to sign in to your Cotton", "line": "This device can't open your things yet, so it can't say yes."}, "pollSeconds": 10} """#)]) let (model, _) = asks(core) await model.read() XCTAssertNil(model.ask, "no Approve anywhere") XCTAssertEqual(model.cannot?.line, "This device can't open your things yet, so it can't say yes.") } }