api-swift / Tests / IrdAPITests / Core / CredentialTests.swift
125 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Foundation
import Testing
@testable import IrdAPI

@Suite struct CredentialTests {
    @Test func inMemoryStoreSavesLoadsAndDeletes() async throws {
        let store = InMemoryCredentialStore()
        #expect(try await store.token() == nil)
        try store.save("ird_a")
        #expect(try await store.token() == "ird_a")
        try store.delete()
        #expect(try await store.token() == nil)
    }

    @Test func aTokenSavedAfterConstructionIsUsedByTheNextRequest() async throws {
        let store = InMemoryCredentialStore()
        let http = FakeHTTP(script: [json(200, "{}")])
        var c = Configuration()
        c.credential = store
        c.transport = http
        let client = IrdClient(configuration: c)
        _ = try await client.raw.request(method: .get, path: "/domains/example")
        #expect(http.last?.value(forHTTPHeaderField: "Authorization") == nil)
        try store.save("eyJ.jwt")
        _ = try await client.raw.request(method: .get, path: "/domains/example")
        #expect(http.last?.value(forHTTPHeaderField: "Authorization") == "Bearer eyJ.jwt")
    }

    @Test func convenienceInitializersWire() async throws {
        #expect(try await IrdClient(apiKey: "ird_x").configuration.credential.token() == "ird_x")
        #expect(try await IrdClient(credential: StaticCredential("t")).configuration.credential.token() == "t")
        #expect(try await IrdClient().configuration.credential.token() == nil)
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git