Skip to content

Commit 77fe59e

Browse files
committed
test: add test for the session revoke
1 parent 4ae4433 commit 77fe59e

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

Sources/Auth/Internal/APIClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ struct APIClient: Sendable {
107107
reasons: error.weakPassword?.reasons ?? []
108108
)
109109
} else if errorCode == .sessionNotFound {
110+
// The `session_id` inside the JWT does not correspond to a row in the
111+
// `sessions` table. This usually means the user has signed out, has been
112+
// deleted, or their session has somehow been terminated.
110113
await sessionManager.remove()
111114
eventEmitter.emit(.signedOut, session: nil)
112115
return .sessionMissing

Tests/AuthTests/AuthClientTests.swift

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,43 @@ final class AuthClientTests: XCTestCase {
21512151
)
21522152
}
21532153

2154+
func testRemoveSessionAndSignoutIfSessionNotFoundErrorReturned() async throws {
2155+
let sut = makeSUT()
2156+
2157+
Mock(
2158+
url: clientURL.appendingPathComponent("user"),
2159+
statusCode: 403,
2160+
data: [
2161+
.get: Data(
2162+
"""
2163+
{
2164+
"error_code": "session_not_found",
2165+
"message": "Session not found"
2166+
}
2167+
""".utf8
2168+
)
2169+
]
2170+
)
2171+
.register()
2172+
2173+
Dependencies[sut.clientID].sessionStorage.store(.validSession)
2174+
2175+
try await assertAuthStateChanges(
2176+
sut: sut,
2177+
action: {
2178+
do {
2179+
_ = try await sut.user()
2180+
XCTFail("Expected failure")
2181+
} catch {
2182+
XCTAssertEqual(error as? AuthError, .sessionMissing)
2183+
}
2184+
},
2185+
expectedEvents: [.initialSession, .signedOut]
2186+
)
2187+
2188+
XCTAssertNil(Dependencies[sut.clientID].sessionStorage.get())
2189+
}
2190+
21542191
private func makeSUT(flowType: AuthFlowType = .pkce) -> AuthClient {
21552192
let sessionConfiguration = URLSessionConfiguration.default
21562193
sessionConfiguration.protocolClasses = [MockingURLProtocol.self]
@@ -2198,6 +2235,7 @@ final class AuthClientTests: XCTestCase {
21982235
action: () async throws -> T,
21992236
expectedEvents: [AuthChangeEvent],
22002237
expectedSessions: [Session?]? = nil,
2238+
timeout: TimeInterval = 2,
22012239
fileID: StaticString = #fileID,
22022240
filePath: StaticString = #filePath,
22032241
line: UInt = #line,
@@ -2211,14 +2249,30 @@ final class AuthClientTests: XCTestCase {
22112249

22122250
let result = try await action()
22132251

2214-
let authStateChanges = await eventsTask.value
2252+
let authStateChanges = try await withTimeout(interval: timeout) {
2253+
await eventsTask.value
2254+
}
22152255
let events = authStateChanges.map(\.event)
22162256
let sessions = authStateChanges.map(\.session)
22172257

2218-
expectNoDifference(events, expectedEvents, fileID: fileID, filePath: filePath, line: line, column: column)
2258+
expectNoDifference(
2259+
events,
2260+
expectedEvents,
2261+
fileID: fileID,
2262+
filePath: filePath,
2263+
line: line,
2264+
column: column
2265+
)
22192266

22202267
if let expectedSessions = expectedSessions {
2221-
expectNoDifference(sessions, expectedSessions, fileID: fileID, filePath: filePath, line: line, column: column)
2268+
expectNoDifference(
2269+
sessions,
2270+
expectedSessions,
2271+
fileID: fileID,
2272+
filePath: filePath,
2273+
line: line,
2274+
column: column
2275+
)
22222276
}
22232277

22242278
return result

0 commit comments

Comments
 (0)