Skip to content

Commit 9c344d1

Browse files
committed
fix integration tests
1 parent 9ed1245 commit 9c344d1

File tree

5 files changed

+55
-13
lines changed

5 files changed

+55
-13
lines changed

Sources/Auth/AuthAdmin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public struct AuthAdmin: Sendable {
1414
var configuration: AuthClient.Configuration { Dependencies[clientID].configuration }
1515
var api: APIClient { Dependencies[clientID].api }
1616
var encoder: JSONEncoder { Dependencies[clientID].encoder }
17+
var sessionManager: SessionManager { Dependencies[clientID].sessionManager }
1718

1819
/// Get user by id.
1920
/// - Parameter uid: The user's unique identifier.

Sources/Auth/AuthClient.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ public actor AuthClient {
9999

100100
Dependencies[clientID] = Dependencies(
101101
configuration: configuration,
102-
session: configuration.session.newSession(adapter: SupabaseApiVersionAdapter()),
102+
session: configuration.session.newSession(
103+
adapters: [
104+
configuration.headers["apikey"].map(SupabaseApiKeyAdapter.init(apiKey:)),
105+
SupabaseApiVersionAdapter(),
106+
].compactMap { $0 }
107+
),
103108
api: APIClient(clientID: clientID),
104109
codeVerifierStorage: .live(clientID: clientID),
105110
sessionStorage: .live(clientID: clientID),
@@ -782,7 +787,8 @@ public actor AuthClient {
782787
case .implicit:
783788
guard self.isImplicitGrantFlow(params: params) else {
784789
throw AuthError.implicitGrantRedirect(
785-
message: "Not a valid implicit grant flow URL: \(url)")
790+
message: "Not a valid implicit grant flow URL: \(url)"
791+
)
786792
}
787793
return try await self.handleImplicitGrantFlow(params: params)
788794

Sources/Auth/Internal/APIClient.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ struct _RawAPIErrorResponse: Decodable {
143143
extension Alamofire.Session {
144144
/// Create a new session with the same configuration but with some overridden properties.
145145
func newSession(
146-
adapter: (any RequestAdapter)? = nil
146+
adapters: [any RequestAdapter] = []
147147
) -> Alamofire.Session {
148148
return Alamofire.Session(
149149
session: session,
@@ -152,7 +152,9 @@ extension Alamofire.Session {
152152
startRequestsImmediately: startRequestsImmediately,
153153
requestQueue: requestQueue,
154154
serializationQueue: serializationQueue,
155-
interceptor: Interceptor(adapters: [self.interceptor, adapter].compactMap { $0 }),
155+
interceptor: Interceptor(
156+
adapters: self.interceptor != nil ? [self.interceptor!] + adapters : adapters
157+
),
156158
serverTrustManager: serverTrustManager,
157159
redirectHandler: redirectHandler,
158160
cachedResponseHandler: cachedResponseHandler,
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// SessionAdapters.swift
3+
// Supabase
4+
//
5+
// Created by Guilherme Souza on 26/08/25.
6+
//
7+
8+
import Alamofire
9+
import Foundation
10+
11+
package struct SupabaseApiKeyAdapter: RequestAdapter {
12+
13+
let apiKey: String
14+
15+
package init(apiKey: String) {
16+
self.apiKey = apiKey
17+
}
18+
19+
package func adapt(
20+
_ urlRequest: URLRequest,
21+
for session: Session,
22+
completion: @escaping (Result<URLRequest, any Error>) -> Void
23+
) {
24+
var urlRequest = urlRequest
25+
26+
if urlRequest.value(forHTTPHeaderField: "apikey") == nil {
27+
urlRequest.setValue(apiKey, forHTTPHeaderField: "apikey")
28+
}
29+
30+
if urlRequest.headers["Authorization"] == nil {
31+
urlRequest.headers.add(.authorization(bearerToken: apiKey))
32+
}
33+
34+
completion(.success(urlRequest))
35+
}
36+
}

Tests/IntegrationTests/AuthClientIntegrationTests.swift

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class AuthClientIntegrationTests: XCTestCase {
3030
"Authorization": "Bearer \(key)",
3131
],
3232
localStorage: InMemoryLocalStorage(),
33-
logger: TestLogger()
33+
logger: OSLogSupabaseLogger()
3434
)
3535
)
3636
}
@@ -102,11 +102,7 @@ final class AuthClientIntegrationTests: XCTestCase {
102102
try await authClient.signIn(email: email, password: password)
103103
XCTFail("Expect failure")
104104
} catch {
105-
if let error = error as? AuthError {
106-
XCTAssertEqual(error.localizedDescription, "Invalid login credentials")
107-
} else {
108-
XCTFail("Unexpected error: \(error)")
109-
}
105+
XCTAssertEqual(error.localizedDescription, "Invalid login credentials")
110106
}
111107
}
112108

@@ -186,7 +182,7 @@ final class AuthClientIntegrationTests: XCTestCase {
186182
do {
187183
try await authClient.unlinkIdentity(identity)
188184
XCTFail("Expect failure")
189-
} catch let error as AuthError {
185+
} catch {
190186
XCTAssertEqual(error.errorCode, .singleIdentityNotDeletable)
191187
}
192188
}
@@ -269,8 +265,9 @@ final class AuthClientIntegrationTests: XCTestCase {
269265
do {
270266
_ = try await authClient.session
271267
XCTFail("Expected to throw AuthError.sessionMissing")
272-
} catch let error as AuthError {
273-
XCTAssertEqual(error, .sessionMissing)
268+
} catch AuthError.sessionMissing {
269+
} catch {
270+
XCTFail("Expected \(AuthError.sessionMissing) error")
274271
}
275272
XCTAssertNil(authClient.currentSession)
276273
}

0 commit comments

Comments
 (0)