Skip to content

Commit

Permalink
test(auth): add tests for auto verifying stored session compatibility (
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Apr 1, 2024
1 parent 60bd77a commit b3ac087
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 91 deletions.
45 changes: 45 additions & 0 deletions Tests/AuthTests/Resources/local-storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"supabase.session" : {
"expiration_date" : "2024-04-01T13:25:07Z",
"session" : {
"access_token" : "accesstoken",
"expires_at" : 1711977907,
"expires_in" : 120,
"refresh_token" : "refreshtoken",
"token_type" : "bearer",
"user" : {
"app_metadata" : {
"provider" : "email",
"providers" : [
"email"
]
},
"aud" : "authenticated",
"confirmation_sent_at" : "2022-04-09T11:57:01Z",
"created_at" : "2022-04-09T11:57:01Z",
"email" : "[email protected]",
"id" : "859F402D-B3DE-4105-A1B9-932836D9193B",
"identities" : [
{
"created_at" : "2022-04-09T11:57:01Z",
"id" : "859f402d-b3de-4105-a1b9-932836d9193b",
"identity_data" : {
"sub" : "859f402d-b3de-4105-a1b9-932836d9193b"
},
"identity_id" : "859F402D-B3DE-4105-A1B9-932836D9193B",
"last_sign_in_at" : "2022-04-09T11:57:01Z",
"provider" : "email",
"updated_at" : "2022-04-09T11:57:01Z",
"user_id" : "859F402D-B3DE-4105-A1B9-932836D9193B"
}
],
"phone" : "",
"role" : "authenticated",
"updated_at" : "2022-04-09T11:57:01Z",
"user_metadata" : {
"referrer_id" : null
}
}
}
}
}
40 changes: 0 additions & 40 deletions Tests/AuthTests/Resources/stored-session_2_4_0.json

This file was deleted.

41 changes: 0 additions & 41 deletions Tests/AuthTests/Resources/stored-session_2_5_0.json

This file was deleted.

127 changes: 117 additions & 10 deletions Tests/AuthTests/StoredSessionTests.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,126 @@
@testable import Auth
import ConcurrencyExtras
import SnapshotTesting
import XCTest

final class StoredSessionTests: XCTestCase {
func testDecode2_4_0() throws {
XCTAssertNoThrow(try AuthClient.Configuration.jsonDecoder.decode(
StoredSession.self,
from: json(named: "stored-session_2_4_0")
))
override func setUpWithError() throws {
try super.setUpWithError()
Current = .mock
Current.configuration = .init(
url: clientURL,
localStorage: try! DiskTestStorage(),
logger: nil
)
}

func testDecode2_5_0() throws {
XCTAssertNoThrow(try AuthClient.Configuration.jsonDecoder.decode(
StoredSession.self,
from: json(named: "stored-session_2_5_0")
))
func testStoredSession() throws {
let sut = SessionStorage.live

let _ = try sut.getSession()

let session = Session(
accessToken: "accesstoken",
tokenType: "bearer",
expiresIn: 120,
expiresAt: ISO8601DateFormatter().date(from: "2024-04-01T13:25:07Z")!.timeIntervalSince1970,
refreshToken: "refreshtoken",
user: User(
id: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!,
appMetadata: [
"provider": "email",
"providers": [
"email",
],
],
userMetadata: [
"referrer_id": nil,
],
aud: "authenticated",
confirmationSentAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!,
recoverySentAt: nil,
emailChangeSentAt: nil,
newEmail: nil,
invitedAt: nil,
actionLink: nil,
email: "[email protected]",
phone: "",
createdAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!,
confirmedAt: nil,
emailConfirmedAt: nil,
phoneConfirmedAt: nil,
lastSignInAt: nil,
role: "authenticated",
updatedAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!,
identities: [
UserIdentity(
id: "859f402d-b3de-4105-a1b9-932836d9193b",
identityId: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!,
userId: UUID(uuidString: "859F402D-B3DE-4105-A1B9-932836D9193B")!,
identityData: [
"sub": "859f402d-b3de-4105-a1b9-932836d9193b",
],
provider: "email",
createdAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!,
lastSignInAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!,
updatedAt: ISO8601DateFormatter().date(from: "2022-04-09T11:57:01Z")!
),
],
factors: nil
)
)

try sut.storeSession(.init(session: session))
}

private final class DiskTestStorage: AuthLocalStorage {
let url: URL
let storage: LockIsolated<[String: AnyJSON]>

let encoder = JSONEncoder()
let decoder = JSONDecoder()

init() throws {
url = URL(fileURLWithPath: #filePath)
.deletingLastPathComponent()
.appendingPathComponent("Resources")
.appendingPathComponent("local-storage.json")

encoder.outputFormatting = [.prettyPrinted, .sortedKeys]

if !FileManager.default.fileExists(atPath: url.path) {
let contents = "{}".data(using: .utf8)
FileManager.default.createFile(atPath: url.path, contents: contents)
}

let contents = try Data(contentsOf: url)
storage = try LockIsolated(decoder.decode([String: AnyJSON].self, from: contents))
}

func store(key: String, value: Data) throws {
let json = try decoder.decode(AnyJSON.self, from: value)
storage.withValue {
$0[key] = json
}

try saveToDisk()
}

func retrieve(key: String) throws -> Data? {
guard let json = storage[key] else { return nil }
return try encoder.encode(json)
}

func remove(key: String) throws {
storage.withValue {
$0[key] = nil
}
try saveToDisk()
}

private func saveToDisk() throws {
let data = try encoder.encode(storage.value)
try data.write(to: url)
}
}
}

0 comments on commit b3ac087

Please sign in to comment.