oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import Testing
3@testable import EquipoShareCore
4
5@Test(arguments: ["missing-selection", "missing-account", "person", "origin", "expired", "token", "identity", "generation", "http", "path", "query"])
6func shareAuthorityRefusesChangedOrUnavailableBinding(_ fault: String) throws {
7 let store = MemorySecretStore(), now = Date()
8 let good = ShareSelection(origin: URL(string: "https://team.example")!, person: "a@example.net", team: "Team", generation: "one")
9 var selection = good
10 if fault == "generation" { selection.generation = "two" }
11 if fault == "http" { selection.origin = URL(string: "http://team.example")! }
12 if fault == "path" { selection.origin = URL(string: "https://team.example/other")! }
13 if fault == "query" { selection.origin = URL(string: "https://team.example?q=x")! }
14 let account = ShareAccount(origin: fault == "origin" ? URL(string: "https://other.example")! : selection.origin,
15 credential: .init(token: fault == "token" ? "" : "bearer", expiresAt: now.addingTimeInterval(fault == "expired" ? -1 : 100)),
16 address: fault == "person" ? "other@example.net" : good.person, oficinaIdentity: "id", oficinaSessionHash: "hash")
17 if fault != "missing-selection" { try store.write(JSONEncoder().encode(selection), account: ShareSelection.key) }
18 if fault != "missing-account" { try store.write(JSONEncoder().encode(account), account: selection.origin.absoluteString) }
19 let auth = ShareAuthentication(store: store, identityIsLive: { _, _ in fault != "identity" })
20 #expect(throws: ShareFailure.signedOut) {
21 if fault == "generation" { _ = try auth.require(good, now: now) } else { _ = try auth.read(now: now) }
22 }
23}
24
25@Test(arguments: ["not-screenshot", "hidden", "denied", "future", "old", "dismissed"])
26func screenshotRefusesIneligibleAssets(_ fault: String) {
27 let now = Date(), id = "s"
28 var policy = ScreenshotPolicy()
29 if fault == "dismissed" { policy.dismiss(id, created: now) }
30 let created = now.addingTimeInterval(fault == "future" ? 1 : fault == "old" ? -301 : -1)
31 #expect(!policy.eligible(id: id, created: created, isScreenshot: fault != "not-screenshot", hidden: fault == "hidden", permitted: fault != "denied", now: now))
32}
33
34@MainActor @Test func accountSwitchAfterReadCannotCommitSendSuccess() async throws {
35 let store = MemorySecretStore(), directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
36 defer { try? FileManager.default.removeItem(at: directory) }
37 let selection = ShareSelection(origin: URL(string: "https://team.example")!, person: "a@example.net", team: "Team")
38 try store.write(JSONEncoder().encode(selection), account: ShareSelection.key)
39 try store.write(JSONEncoder().encode(ShareAccount(origin: selection.origin, credential: .init(token: "secret", expiresAt: Date().addingTimeInterval(60)), address: selection.person, oficinaIdentity: nil, oficinaSessionHash: nil)), account: selection.origin.absoluteString)
40 let sender = ShareSender(auth: ShareAuthentication(store: store), transport: SwitchOnReply(store: store), journal: ShareJournal(directory: directory))
41 sender.text = "Bug"; await sender.send()
42 #expect(!sender.finished)
43 #expect(sender.error == .signedOut)
44 #expect(sender.selection == nil)
45}
46private struct SwitchOnReply: ShareTransport {
47 let store: MemorySecretStore
48 func send(_ request: URLRequest, file: URL?, progress: @escaping @Sendable (Double) -> Void) async throws -> ShareResponse {
49 store.remove(ShareSelection.key)
50 return .init(status: 200, data: Data(#"{"message":{"id":"sent"}}"#.utf8))
51 }
52}
53
54@MainActor @Test(arguments: ["cancelled", "401", "403", "oversize"])
55func refusedOrCancelledResponsesNeverFinish(_ fault: String) async throws {
56 let store = MemorySecretStore(), directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
57 defer { try? FileManager.default.removeItem(at: directory) }
58 let selection = ShareSelection(origin: URL(string: "https://team.example")!, person: "a@example.net", team: "Team")
59 try store.write(JSONEncoder().encode(selection), account: ShareSelection.key)
60 try store.write(JSONEncoder().encode(ShareAccount(origin: selection.origin, credential: .init(token: "secret", expiresAt: Date().addingTimeInterval(60)), address: selection.person, oficinaIdentity: nil, oficinaSessionHash: nil)), account: selection.origin.absoluteString)
61 let sender = ShareSender(auth: ShareAuthentication(store: store), transport: RefusedResponse(fault: fault), journal: ShareJournal(directory: directory))
62 sender.text = "Bug"
63 let task = Task { await sender.send() }; await task.value
64 #expect(!sender.finished)
65 if ["401", "403"].contains(fault) { #expect(sender.selection == nil); #expect(sender.error == .signedOut) }
66}
67private struct RefusedResponse: ShareTransport {
68 let fault: String
69 func send(_ request: URLRequest, file: URL?, progress: @escaping @Sendable (Double) -> Void) async throws -> ShareResponse {
70 if fault == "cancelled" { withUnsafeCurrentTask { $0?.cancel() } }
71 let body = #"{"message":{"id":"sent"}}"# + (fault == "oversize" ? String(repeating: " ", count: 128 * 1024) : "")
72 return .init(status: Int(fault) ?? 200, data: Data(body.utf8))
73 }
74}
75
76@Test(arguments: ["valid", "removed", "wrong-hash", "expired", "old", "new-version", "other-identity", "missing-session", "old-grant", "malformed-grant"])
77func pinnedOficinaRecordsRequireLiveIdentityAndBoundSession(_ fault: String) {
78 let grant = fault == "malformed-grant" ? "AQ" : "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
79 let identity = Data("{\"v\":1,\"identity_id\":\"person\",\"grant\":\"\(grant)\"}".utf8)
80 let session = Data("{\"grant_id\":\"\(fault == "old-grant" ? "old" : fault == "malformed-grant" ? "jtbgCLytFO7uf7AmMnW8kA" : "L4vtVQrojtsak8-oO3BFRA")\",\"v\":\(fault == "new-version" ? 2 : 1),\"identity_id\":\"\(fault == "other-identity" ? "other" : "person")\",\"session\":{\"name\":\"session\",\"value\":\"session-secret\"},\"minted_at\":\(fault == "old" ? -604800 : 900),\"expires_at\":\(fault == "expired" ? 1000 : 1100)}".utf8)
81 let live = SharedIdentity.validate(identityData: fault == "removed" ? [] : [identity], sessions: fault == "missing-session" ? [] : [session], identity: "person", hash: fault == "wrong-hash" ? "other" : "E1-n1nFGylQPF-UeEB1F3lse3ejqihP5x7k7caoh-KQ", now: Date(timeIntervalSince1970: 1000))
82 #expect(live == (fault == "valid"))
83}
84