SPB Git

Repository Public

SessionStoreTests.swift 4.2 KB 87 lines swift

1import Foundation2import Testing3@testable import MCCCore45@Suite("Session store")6struct SessionStoreTests {7    @Test("tokens and device id round-trip; sign-out keeps the device, revocation wipes everything")8    func roundTrip() throws {9        let secure = InMemorySecureStore()10        let store = SessionStore(store: secure, baseURL: URL(string: "http://localhost:8760")!)11        #expect(store.loadTokens() == nil)12        let t = Fixture.tokens()13        try store.save(t)14        try store.saveDeviceID(Fixture.deviceID)15        let loaded = try #require(store.loadTokens())16        #expect(loaded.accessToken == t.accessToken && loaded.refreshToken == t.refreshToken)17        #expect(abs(loaded.accessExpiresAt.timeIntervalSince(t.accessExpiresAt)) < 0.01)18        #expect(store.loadDeviceID() == Fixture.deviceID)19        #expect(secure.accounts == ["device@localhost:8760", "tokens@localhost:8760"])2021        // Another server (production) is a separate namespace.22        let prod = SessionStore(store: secure, baseURL: CoreConfiguration.productionOrigin)23        #expect(prod.loadTokens() == nil)2425        store.wipeTokens()26        #expect(store.loadTokens() == nil)27        #expect(store.loadDeviceID() == Fixture.deviceID)28        try store.save(t)29        store.wipeAll()30        #expect(secure.accounts.isEmpty)31    }3233    @Test("tokens never appear in descriptions (logs, string interpolation)")34    func redaction() {35        let t = Fixture.tokens(access: "SECRET-ACCESS", refresh: "SECRET-REFRESH")36        for text in ["\(t)", String(describing: t), String(reflecting: t)] {37            #expect(!text.contains("SECRET"))38        }39    }4041    @Test("a failing secure store never crashes; the error is reported")42    func failingStore() async throws {43        let secure = InMemorySecureStore()44        secure.failWrites = true45        let store = SessionStore(store: secure, baseURL: URL(string: "http://localhost:8760")!)46        #expect(throws: SecureStoreError.self) { try store.save(Fixture.tokens()) }47        let manager = TokenManager(store: store) { _ in Fixture.tokens() }48        await #expect(throws: SecureStoreError.self) { try await manager.install(Fixture.tokens(), deviceID: Fixture.deviceID) }49    }5051    @Test("Keychain store: generic password, not synchronisable (login keychain in unsigned test runs)")52    func keychain() throws {53        // The data-protection keychain needs an application identifier, which a command-line test binary lacks:54        // exercise the same code path against the login keychain and tolerate environments without one.55        let service = "ai.spboucher.clustr.tests.\(UUID().uuidString)"56        let keychain = KeychainSecureStore(service: service, dataProtection: false)57        do {58            try keychain.write(Data("v1".utf8), account: "tokens@test")59        } catch let e as SecureStoreError {60            print("Keychain unavailable in this environment (\(e)); skipped")61            return62        }63        defer { try? keychain.delete("tokens@test") }64        #expect(try keychain.read("tokens@test") == Data("v1".utf8))65        try keychain.write(Data("v2".utf8), account: "tokens@test")   // update path66        #expect(try keychain.read("tokens@test") == Data("v2".utf8))67        #expect(try keychain.read("absent") == nil)68        try keychain.delete("tokens@test")69        #expect(try keychain.read("tokens@test") == nil)70        try keychain.delete("tokens@test")                            // deleting twice is not an error71        // With the data-protection keychain an unsigned binary gets a clean error, not a crash.72        let dp = KeychainSecureStore(service: service, dataProtection: true)73        do { try dp.write(Data("x".utf8), account: "a"); try dp.delete("a") } catch is SecureStoreError {}74    }7576    @Test("device info describes this client")77    func deviceInfo() {78        let defaults = UserDefaults(suiteName: "mcc-tests-\(UUID().uuidString)")!79        let d = DeviceInfo.current(defaults: defaults)80        #expect(d.kind == .macos)81        #expect(!d.label.isEmpty && !d.model.isEmpty)82        #expect(d.osVersion.hasPrefix("macOS "))83        DeviceInfo.setStoredLabel("  Bureau de SPB  ", defaults: defaults)84        #expect(DeviceInfo.current(defaults: defaults).label == "Bureau de SPB")85    }86}87
Files main
Menu

Theme