Skip to content

Commit 2f85297

Browse files
committed
add test
1 parent 2942d69 commit 2f85297

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

Tests/SupabaseTests/SupabaseClientTests.swift

+38-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
@testable import Auth
21
import CustomDump
3-
@testable import Functions
42
import IssueReporting
3+
import XCTest
4+
5+
@testable import Auth
6+
@testable import Functions
57
@testable import Realtime
68
@testable import Supabase
7-
import XCTest
89

910
final class AuthLocalStorageMock: AuthLocalStorage {
1011
func store(key _: String, value _: Data) throws {}
@@ -17,6 +18,9 @@ final class AuthLocalStorageMock: AuthLocalStorage {
1718
}
1819

1920
final class SupabaseClientTests: XCTestCase {
21+
let jwt =
22+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
23+
2024
func testClientInitialization() async {
2125
final class Logger: SupabaseLogger {
2226
func log(message _: SupabaseLogMessage) {
@@ -31,7 +35,7 @@ final class SupabaseClientTests: XCTestCase {
3135

3236
let client = SupabaseClient(
3337
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
34-
supabaseKey: "ANON_KEY",
38+
supabaseKey: jwt,
3539
options: SupabaseClientOptions(
3640
db: SupabaseClientOptions.DatabaseOptions(schema: customSchema),
3741
auth: SupabaseClientOptions.AuthOptions(
@@ -53,7 +57,7 @@ final class SupabaseClientTests: XCTestCase {
5357
)
5458

5559
XCTAssertEqual(client.supabaseURL.absoluteString, "https://project-ref.supabase.co")
56-
XCTAssertEqual(client.supabaseKey, "ANON_KEY")
60+
XCTAssertEqual(client.supabaseKey, jwt)
5761
XCTAssertEqual(client.storageURL.absoluteString, "https://project-ref.supabase.co/storage/v1")
5862
XCTAssertEqual(client.databaseURL.absoluteString, "https://project-ref.supabase.co/rest/v1")
5963
XCTAssertEqual(
@@ -65,9 +69,9 @@ final class SupabaseClientTests: XCTestCase {
6569
client.headers,
6670
[
6771
"X-Client-Info": "supabase-swift/\(Supabase.version)",
68-
"Apikey": "ANON_KEY",
72+
"Apikey": jwt,
6973
"header_field": "header_value",
70-
"Authorization": "Bearer ANON_KEY",
74+
"Authorization": "Bearer \(jwt)",
7175
]
7276
)
7377
expectNoDifference(client._headers.dictionary, client.headers)
@@ -79,7 +83,8 @@ final class SupabaseClientTests: XCTestCase {
7983

8084
let realtimeOptions = client.realtimeV2.options
8185
let expectedRealtimeHeader = client._headers.merging(with: [
82-
.init("custom_realtime_header_key")!: "custom_realtime_header_value"]
86+
.init("custom_realtime_header_key")!: "custom_realtime_header_value"
87+
]
8388
)
8489
expectNoDifference(realtimeOptions.headers, expectedRealtimeHeader)
8590
XCTAssertIdentical(realtimeOptions.logger as? Logger, logger)
@@ -97,7 +102,7 @@ final class SupabaseClientTests: XCTestCase {
97102
func testClientInitWithDefaultOptionsShouldBeAvailableInNonLinux() {
98103
_ = SupabaseClient(
99104
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
100-
supabaseKey: "ANON_KEY"
105+
supabaseKey: jwt
101106
)
102107
}
103108
#endif
@@ -107,7 +112,7 @@ final class SupabaseClientTests: XCTestCase {
107112

108113
let client = SupabaseClient(
109114
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
110-
supabaseKey: "ANON_KEY",
115+
supabaseKey: jwt,
111116
options: .init(
112117
auth: .init(
113118
storage: localStorage,
@@ -123,9 +128,31 @@ final class SupabaseClientTests: XCTestCase {
123128

124129
#if canImport(Darwin)
125130
// withExpectedIssue is unavailable on non-Darwin platform.
126-
withExpectedIssue {
131+
withExpectedIssue(
132+
"""
133+
Supabase Client is configured with the auth.accessToken option,
134+
accessing supabase.auth is not possible.
135+
"""
136+
) {
127137
_ = client.auth
128138
}
129139
#endif
130140
}
141+
142+
#if canImport(Darwin)
143+
// withExpectedIssue is unavailable on non-Darwin platform.
144+
func testClientInitWithNonJWTAPIKey() {
145+
withExpectedIssue("Authorization header does not contain a JWT") {
146+
_ = SupabaseClient(
147+
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
148+
supabaseKey: "invalid.token.format",
149+
options: SupabaseClientOptions(
150+
auth: .init(
151+
storage: AuthLocalStorageMock()
152+
)
153+
)
154+
)
155+
}
156+
}
157+
#endif
131158
}

0 commit comments

Comments
 (0)