-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
284 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,32 @@ | ||
import Foundation | ||
@_spi(Internal) import _Helpers | ||
|
||
protocol CodeVerifierStorage { | ||
func getCodeVerifier() throws -> String? | ||
func storeCodeVerifier(_ code: String) throws | ||
func deleteCodeVerifier() throws | ||
struct CodeVerifierStorage: Sendable { | ||
var getCodeVerifier: @Sendable () throws -> String? | ||
var storeCodeVerifier: @Sendable (_ code: String) throws -> Void | ||
var deleteCodeVerifier: @Sendable () throws -> Void | ||
} | ||
|
||
struct DefaultCodeVerifierStorage: CodeVerifierStorage { | ||
private var localStorage: GoTrueLocalStorage { | ||
Dependencies.current.value!.configuration.localStorage | ||
} | ||
|
||
private let key = "supabase.code-verifier" | ||
|
||
func getCodeVerifier() throws -> String? { | ||
try localStorage.retrieve(key: key).flatMap { | ||
String(data: $0, encoding: .utf8) | ||
extension CodeVerifierStorage { | ||
static var live: Self = { | ||
var localStorage: GoTrueLocalStorage { | ||
Dependencies.current.value!.configuration.localStorage | ||
} | ||
} | ||
|
||
func storeCodeVerifier(_ code: String) throws { | ||
try localStorage.store(key: key, value: Data(code.utf8)) | ||
} | ||
let key = "supabase.code-verifier" | ||
|
||
func deleteCodeVerifier() throws { | ||
try localStorage.remove(key: key) | ||
} | ||
return Self( | ||
getCodeVerifier: { | ||
try localStorage.retrieve(key: key).flatMap { | ||
String(data: $0, encoding: .utf8) | ||
} | ||
}, | ||
storeCodeVerifier: { code in | ||
try localStorage.store(key: key, value: Data(code.utf8)) | ||
}, | ||
deleteCodeVerifier: { | ||
try localStorage.remove(key: key) | ||
} | ||
) | ||
}() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Guilherme Souza on 27/10/23. | ||
// | ||
|
||
import Foundation | ||
import XCTestDynamicOverlay | ||
@_spi(Internal) import _Helpers | ||
|
||
@testable import GoTrue | ||
|
||
let clientURL = URL(string: "http://localhost:54321/auth/v1")! | ||
|
||
extension CodeVerifierStorage { | ||
static let mock = Self( | ||
getCodeVerifier: unimplemented("getCodeVerifier"), | ||
storeCodeVerifier: unimplemented("storeCodeVerifier"), | ||
deleteCodeVerifier: unimplemented("deleteCodeVerifier") | ||
) | ||
} | ||
|
||
extension SessionManager { | ||
static let mock = Self( | ||
session: unimplemented("session"), | ||
update: unimplemented("update"), | ||
remove: unimplemented("remove") | ||
) | ||
} | ||
|
||
extension EventEmitter { | ||
static let mock = Self( | ||
attachListener: unimplemented("attachListener"), | ||
emit: unimplemented("emit") | ||
) | ||
|
||
static let noop = Self( | ||
attachListener: { (UUID(), AsyncStream.makeStream().stream) }, | ||
emit: { _, _ in } | ||
) | ||
} | ||
|
||
extension SessionStorage { | ||
static let mock = Self( | ||
getSession: unimplemented("getSession"), | ||
storeSession: unimplemented("storeSession"), | ||
deleteSession: unimplemented("deleteSession") | ||
) | ||
} | ||
|
||
extension SessionRefresher { | ||
static let mock = Self(refreshSession: unimplemented("refreshSession")) | ||
} | ||
|
||
extension Dependencies { | ||
static let mock = Dependencies( | ||
configuration: GoTrueClient.Configuration(url: clientURL), | ||
sessionManager: .mock, | ||
api: APIClient(), | ||
eventEmitter: .mock, | ||
sessionStorage: .mock, | ||
sessionRefresher: .mock, | ||
codeVerifierStorage: .mock | ||
) | ||
} | ||
|
||
func withDependencies( | ||
_ mutation: (inout Dependencies) -> Void, | ||
operation: () async throws -> Void | ||
) async rethrows { | ||
let current = Dependencies.current.value ?? .mock | ||
var copy = current | ||
mutation(©) | ||
Dependencies.current.withValue { $0 = copy } | ||
defer { Dependencies.current.setValue(current) } | ||
try await operation() | ||
} | ||
|
||
extension Session { | ||
static let validSession = Session( | ||
accessToken: "accesstoken", | ||
tokenType: "bearer", | ||
expiresIn: 120, | ||
refreshToken: "refreshtoken", | ||
user: User(fromMockNamed: "user") | ||
) | ||
|
||
static let expiredSession = Session( | ||
accessToken: "accesstoken", | ||
tokenType: "bearer", | ||
expiresIn: 60, | ||
refreshToken: "refreshtoken", | ||
user: User(fromMockNamed: "user") | ||
) | ||
} |
Oops, something went wrong.