Skip to content

Commit b3ac087

Browse files
authored
test(auth): add tests for auto verifying stored session compatibility (#295)
1 parent 60bd77a commit b3ac087

File tree

4 files changed

+162
-91
lines changed

4 files changed

+162
-91
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"supabase.session" : {
3+
"expiration_date" : "2024-04-01T13:25:07Z",
4+
"session" : {
5+
"access_token" : "accesstoken",
6+
"expires_at" : 1711977907,
7+
"expires_in" : 120,
8+
"refresh_token" : "refreshtoken",
9+
"token_type" : "bearer",
10+
"user" : {
11+
"app_metadata" : {
12+
"provider" : "email",
13+
"providers" : [
14+
"email"
15+
]
16+
},
17+
"aud" : "authenticated",
18+
"confirmation_sent_at" : "2022-04-09T11:57:01Z",
19+
"created_at" : "2022-04-09T11:57:01Z",
20+
"email" : "[email protected]",
21+
"id" : "859F402D-B3DE-4105-A1B9-932836D9193B",
22+
"identities" : [
23+
{
24+
"created_at" : "2022-04-09T11:57:01Z",
25+
"id" : "859f402d-b3de-4105-a1b9-932836d9193b",
26+
"identity_data" : {
27+
"sub" : "859f402d-b3de-4105-a1b9-932836d9193b"
28+
},
29+
"identity_id" : "859F402D-B3DE-4105-A1B9-932836D9193B",
30+
"last_sign_in_at" : "2022-04-09T11:57:01Z",
31+
"provider" : "email",
32+
"updated_at" : "2022-04-09T11:57:01Z",
33+
"user_id" : "859F402D-B3DE-4105-A1B9-932836D9193B"
34+
}
35+
],
36+
"phone" : "",
37+
"role" : "authenticated",
38+
"updated_at" : "2022-04-09T11:57:01Z",
39+
"user_metadata" : {
40+
"referrer_id" : null
41+
}
42+
}
43+
}
44+
}
45+
}

Tests/AuthTests/Resources/stored-session_2_4_0.json

Lines changed: 0 additions & 40 deletions
This file was deleted.

Tests/AuthTests/Resources/stored-session_2_5_0.json

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 117 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,126 @@
11
@testable import Auth
2+
import ConcurrencyExtras
23
import SnapshotTesting
34
import XCTest
45

56
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+
)
1115
}
1216

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+
}
18125
}
19126
}

0 commit comments

Comments
 (0)