Skip to content

Commit

Permalink
Fix tests in linux
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Apr 19, 2024
1 parent a497df0 commit 871930a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
11 changes: 6 additions & 5 deletions Tests/AuthTests/AuthClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,18 @@ final class AuthClientTests: XCTestCase {
XCTAssertEqual(emitReceivedEvents.value.map(\.0), [.signedIn])
}

@available(watchOS 6.2, tvOS 16.0, *)
func testSignInWithOAuthWithInvalidRedirecTo() async {
let sut = makeSUT()

do {
try await sut.signInWithOAuth(
provider: .google,
redirectTo: nil
) { _ in
XCTFail("Should not call launchFlow.")
}
redirectTo: nil,
launchFlow: { _ in
XCTFail("Should not call launchFlow.")
return URL(string: "https://supabase.com")!
}
)
} catch let error as AuthError {
XCTAssertEqual(error, .invalidRedirectScheme)
} catch {
Expand Down
79 changes: 50 additions & 29 deletions Tests/AuthTests/RequestsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,38 @@ final class RequestsTests: XCTestCase {
}
}

func testSessionFromURL() async throws {
let sut = makeSUT(fetch: { request in
let authorizationHeader = request.allHTTPHeaderFields?["Authorization"]
XCTAssertEqual(authorizationHeader, "bearer accesstoken")
return (json(named: "user"), HTTPURLResponse())
})

let currentDate = Date()

Current.sessionManager = .live
Current.sessionStorage.storeSession = { _ in }
Current.codeVerifierStorage.get = { nil }
Current.currentDate = { currentDate }

let url = URL(
string:
"https://dummy-url.com/callback#access_token=accesstoken&expires_in=60&refresh_token=refreshtoken&token_type=bearer"
)!
#if !os(Linux) && !os(Windows)
func testSessionFromURL() async throws {
let sut = makeSUT(fetch: { request in
let authorizationHeader = request.allHTTPHeaderFields?["Authorization"]
XCTAssertEqual(authorizationHeader, "bearer accesstoken")
return (json(named: "user"), HTTPURLResponse.empty())
})

let currentDate = Date()

Current.sessionManager = .live
Current.sessionStorage.storeSession = { _ in }
Current.codeVerifierStorage.get = { nil }
Current.currentDate = { currentDate }

let url = URL(
string:
"https://dummy-url.com/callback#access_token=accesstoken&expires_in=60&refresh_token=refreshtoken&token_type=bearer"
)!

let session = try await sut.session(from: url)
let expectedSession = Session(
accessToken: "accesstoken",
tokenType: "bearer",
expiresIn: 60,
expiresAt: currentDate.addingTimeInterval(60).timeIntervalSince1970,
refreshToken: "refreshtoken",
user: User(fromMockNamed: "user")
)
XCTAssertEqual(session, expectedSession)
}
let session = try await sut.session(from: url)
let expectedSession = Session(
accessToken: "accesstoken",
tokenType: "bearer",
expiresIn: 60,
expiresAt: currentDate.addingTimeInterval(60).timeIntervalSince1970,
refreshToken: "refreshtoken",
user: User(fromMockNamed: "user")
)
XCTAssertEqual(session, expectedSession)
}
#endif

func testSessionFromURLWithMissingComponent() async {
let sut = makeSUT()
Expand Down Expand Up @@ -479,3 +481,22 @@ final class RequestsTests: XCTestCase {
)
}
}

extension URLResponse {
// Windows and Linux don't have the ability to empty initialize a URLResponse like `URLResponse()`
// so
// We provide a function that can give us the right value on an platform.
// See https://github.com/apple/swift-corelibs-foundation/pull/4778
fileprivate static func empty() -> URLResponse {
#if os(Windows) || os(Linux)
URLResponse(
url: .init(string: "https://supabase.com")!,
mimeType: nil,
expectedContentLength: 0,
textEncodingName: nil
)
#else
URLResponse()
#endif
}
}

0 comments on commit 871930a

Please sign in to comment.