-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathSupabaseClientTests.swift
141 lines (122 loc) · 4.38 KB
/
SupabaseClientTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import CustomDump
import InlineSnapshotTesting
import IssueReporting
import SnapshotTestingCustomDump
import XCTest
@testable import Auth
@testable import Functions
@testable import Realtime
@testable import Supabase
final class AuthLocalStorageMock: AuthLocalStorage {
func store(key _: String, value _: Data) throws {}
func retrieve(key _: String) throws -> Data? {
nil
}
func remove(key _: String) throws {}
}
final class SupabaseClientTests: XCTestCase {
func testClientInitialization() async {
final class Logger: SupabaseLogger {
func log(message _: SupabaseLogMessage) {
// no-op
}
}
let logger = Logger()
let customSchema = "custom_schema"
let localStorage = AuthLocalStorageMock()
let customHeaders = ["header_field": "header_value"]
let client = SupabaseClient(
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
supabaseKey: "ANON_KEY",
options: SupabaseClientOptions(
db: SupabaseClientOptions.DatabaseOptions(schema: customSchema),
auth: SupabaseClientOptions.AuthOptions(
storage: localStorage,
autoRefreshToken: false
),
global: SupabaseClientOptions.GlobalOptions(
headers: customHeaders,
session: .shared,
logger: logger
),
functions: SupabaseClientOptions.FunctionsOptions(
region: .apNortheast1
),
realtime: RealtimeClientOptions(
headers: ["custom_realtime_header_key": "custom_realtime_header_value"]
)
)
)
XCTAssertEqual(client.supabaseURL.absoluteString, "https://project-ref.supabase.co")
XCTAssertEqual(client.supabaseKey, "ANON_KEY")
XCTAssertEqual(client.storageURL.absoluteString, "https://project-ref.supabase.co/storage/v1")
XCTAssertEqual(client.databaseURL.absoluteString, "https://project-ref.supabase.co/rest/v1")
XCTAssertEqual(
client.functionsURL.absoluteString,
"https://project-ref.supabase.co/functions/v1"
)
assertInlineSnapshot(of: client.headers, as: .customDump) {
"""
[
"Apikey": "ANON_KEY",
"Authorization": "Bearer ANON_KEY",
"X-Client-Info": "supabase-swift/0.0.0",
"X-Supabase-Client-Platform": "macOS",
"X-Supabase-Client-Platform-Version": "0.0.0",
"header_field": "header_value"
]
"""
}
expectNoDifference(client.headers, client.auth.configuration.headers)
expectNoDifference(client.headers, client.functions.headers.dictionary)
expectNoDifference(client.headers, client.storage.configuration.headers)
expectNoDifference(client.headers, client.rest.configuration.headers)
XCTAssertEqual(client.functions.region, "ap-northeast-1")
let realtimeURL = client.realtimeV2.url
XCTAssertEqual(realtimeURL.absoluteString, "https://project-ref.supabase.co/realtime/v1")
let realtimeOptions = client.realtimeV2.options
let expectedRealtimeHeader = client._headers.merging(with: [
.init("custom_realtime_header_key")!: "custom_realtime_header_value"
]
)
expectNoDifference(realtimeOptions.headers, expectedRealtimeHeader)
XCTAssertIdentical(realtimeOptions.logger as? Logger, logger)
XCTAssertFalse(client.auth.configuration.autoRefreshToken)
XCTAssertEqual(client.auth.configuration.storageKey, "sb-project-ref-auth-token")
XCTAssertNotNil(
client.mutableState.listenForAuthEventsTask,
"should listen for internal auth events"
)
}
#if !os(Linux) && !os(Android)
func testClientInitWithDefaultOptionsShouldBeAvailableInNonLinux() {
_ = SupabaseClient(
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
supabaseKey: "ANON_KEY"
)
}
#endif
func testClientInitWithCustomAccessToken() async {
let localStorage = AuthLocalStorageMock()
let client = SupabaseClient(
supabaseURL: URL(string: "https://project-ref.supabase.co")!,
supabaseKey: "ANON_KEY",
options: .init(
auth: .init(
storage: localStorage,
accessToken: { "jwt" }
)
)
)
XCTAssertNil(
client.mutableState.listenForAuthEventsTask,
"should not listen for internal auth events when using 3p authentication"
)
#if canImport(Darwin)
// withExpectedIssue is unavailable on non-Darwin platform.
withExpectedIssue {
_ = client.auth
}
#endif
}
}