|
1 | 1 | @testable import Auth
|
| 2 | +import ConcurrencyExtras |
2 | 3 | import SnapshotTesting
|
3 | 4 | import XCTest
|
4 | 5 |
|
5 | 6 | final class StoredSessionTests: XCTestCase {
|
6 |
| - func testDecode2_4_0() throws { |
7 |
| - XCTAssertNoThrow(try AuthClient.Configuration.jsonDecoder.decode( |
8 |
| - StoredSession.self, |
9 |
| - from: json(named: "stored-session_2_4_0") |
10 |
| - )) |
| 7 | + override func setUpWithError() throws { |
| 8 | + try super.setUpWithError() |
| 9 | + Current = .mock |
| 10 | + Current.configuration = .init( |
| 11 | + url: clientURL, |
| 12 | + localStorage: try! DiskTestStorage(), |
| 13 | + logger: nil |
| 14 | + ) |
11 | 15 | }
|
12 | 16 |
|
13 |
| - func testDecode2_5_0() throws { |
14 |
| - XCTAssertNoThrow(try AuthClient.Configuration.jsonDecoder.decode( |
15 |
| - StoredSession.self, |
16 |
| - from: json(named: "stored-session_2_5_0") |
17 |
| - )) |
| 17 | + func testStoredSession() throws { |
| 18 | + let sut = SessionStorage.live |
| 19 | + |
| 20 | + let _ = try sut.getSession() |
| 21 | + |
| 22 | + let session = Session( |
| 23 | + accessToken: "accesstoken", |
| 24 | + tokenType: "bearer", |
| 25 | + expiresIn: 120, |
| 26 | + expiresAt: ISO8601DateFormatter().date(from: "2024-04-01T13:25:07Z")!.timeIntervalSince1970, |
| 27 | + refreshToken: "refreshtoken", |
| 28 | + user: User( |
| 29 | + id: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!, |
| 30 | + appMetadata: [ |
| 31 | + "provider": "email", |
| 32 | + "providers": [ |
| 33 | + "email", |
| 34 | + ], |
| 35 | + ], |
| 36 | + userMetadata: [ |
| 37 | + "referrer_id": nil, |
| 38 | + ], |
| 39 | + aud: "authenticated", |
| 40 | + confirmationSentAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!, |
| 41 | + recoverySentAt: nil, |
| 42 | + emailChangeSentAt: nil, |
| 43 | + newEmail: nil, |
| 44 | + invitedAt: nil, |
| 45 | + actionLink: nil, |
| 46 | + |
| 47 | + phone: "", |
| 48 | + createdAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!, |
| 49 | + confirmedAt: nil, |
| 50 | + emailConfirmedAt: nil, |
| 51 | + phoneConfirmedAt: nil, |
| 52 | + lastSignInAt: nil, |
| 53 | + role: "authenticated", |
| 54 | + updatedAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!, |
| 55 | + identities: [ |
| 56 | + UserIdentity( |
| 57 | + id: "859f402d-b3de-4105-a1b9-932836d9193b", |
| 58 | + identityId: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!, |
| 59 | + userId: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!, |
| 60 | + identityData: [ |
| 61 | + "sub": "859f402d-b3de-4105-a1b9-932836d9193b", |
| 62 | + ], |
| 63 | + provider: "email", |
| 64 | + createdAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!, |
| 65 | + lastSignInAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!, |
| 66 | + updatedAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")! |
| 67 | + ), |
| 68 | + ], |
| 69 | + factors: nil |
| 70 | + ) |
| 71 | + ) |
| 72 | + |
| 73 | + try sut.storeSession(.init(session: session)) |
| 74 | + } |
| 75 | + |
| 76 | + private final class DiskTestStorage: AuthLocalStorage { |
| 77 | + let url: URL |
| 78 | + let storage: LockIsolated<[String: AnyJSON]> |
| 79 | + |
| 80 | + let encoder = JSONEncoder() |
| 81 | + let decoder = JSONDecoder() |
| 82 | + |
| 83 | + init() throws { |
| 84 | + url = URL(fileURLWithPath: #filePath) |
| 85 | + .deletingLastPathComponent() |
| 86 | + .appendingPathComponent("Resources") |
| 87 | + .appendingPathComponent("local-storage.json") |
| 88 | + |
| 89 | + encoder.outputFormatting = [.prettyPrinted, .sortedKeys] |
| 90 | + |
| 91 | + if !FileManager.default.fileExists(atPath: url.path) { |
| 92 | + let contents = "{}".data(using: .utf8) |
| 93 | + FileManager.default.createFile(atPath: url.path, contents: contents) |
| 94 | + } |
| 95 | + |
| 96 | + let contents = try Data(contentsOf: url) |
| 97 | + storage = try LockIsolated(decoder.decode([String: AnyJSON].self, from: contents)) |
| 98 | + } |
| 99 | + |
| 100 | + func store(key: String, value: Data) throws { |
| 101 | + let json = try decoder.decode(AnyJSON.self, from: value) |
| 102 | + storage.withValue { |
| 103 | + $0[key] = json |
| 104 | + } |
| 105 | + |
| 106 | + try saveToDisk() |
| 107 | + } |
| 108 | + |
| 109 | + func retrieve(key: String) throws -> Data? { |
| 110 | + guard let json = storage[key] else { return nil } |
| 111 | + return try encoder.encode(json) |
| 112 | + } |
| 113 | + |
| 114 | + func remove(key: String) throws { |
| 115 | + storage.withValue { |
| 116 | + $0[key] = nil |
| 117 | + } |
| 118 | + try saveToDisk() |
| 119 | + } |
| 120 | + |
| 121 | + private func saveToDisk() throws { |
| 122 | + let data = try encoder.encode(storage.value) |
| 123 | + try data.write(to: url) |
| 124 | + } |
18 | 125 | }
|
19 | 126 | }
|
0 commit comments