-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Exchange token tests #14984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
srushtisv
wants to merge
10
commits into
exchangeToken-auth
Choose a base branch
from
exchange-token-tests
base: exchangeToken-auth
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Exchange token tests #14984
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2458702
Add OIDC token exchange methods for BYO-CIAM
srushtisv 9536ca4
lint fixes
srushtisv 4b068b0
Removing function using completion handler for the new API
srushtisv bc40336
Add OIDC token exchange methods for BYO-CIAM
srushtisv 296526d
Add tests for ExchangeTokenRequest
srushtisv 591e590
update comments in exchange token tests
srushtisv 0d97ade
Update ExchangeTokenRequestTests.swift from review
srushtisv 0b78065
updating ExchangeTokenRequestTests
srushtisv 4a8edc3
Merge branch 'exchangeToken-auth' into exchange-token-tests
srushtisv 4303bda
lint fix
srushtisv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,154 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import FirebaseAuth | ||
import FirebaseCore | ||
|
||
/// Tests for `ExchangeTokenRequest` | ||
@available(iOS 13, *) | ||
class ExchangeTokenRequestTests: XCTestCase { | ||
// MARK: - Constants for Testing | ||
|
||
let kAPIKey = "test-api-key" | ||
let kProjectID = "test-project-id" | ||
let kLocation = "us-east1" | ||
let kTenantID = "test-tenant-id-123" | ||
let kIdToken = "a-very-long-and-secure-oidc-token-string" | ||
let kIdpConfigId = "oidc.my-test-provider" | ||
|
||
let kProductionHost = "identityplatform.googleapis.com" | ||
let kStagingHost = "staging-identityplatform.sandbox.googleapis.com" | ||
|
||
// MARK: - Test Cases | ||
|
||
/// Tests that the production URL is correctly formed for a specific region. | ||
func testProductionURLIsCorrectlyConstructed() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: false | ||
) | ||
|
||
let expectedHost = "\(kLocation)-\(kProductionHost)" | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/\(kLocation)/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the production URL is correctly formed for the "prod-global" location. | ||
func testProductionURLIsCorrectlyConstructedForGlobalLocation() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: "prod-global", | ||
tenantId: kTenantID | ||
) | ||
_ = app | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: false | ||
) | ||
|
||
let expectedHost = kProductionHost | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/global/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the staging URL is correctly formed. | ||
func testStagingURLIsCorrectlyConstructed() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
_ = app | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: true | ||
) | ||
|
||
let expectedHost = "\(kLocation)-\(kStagingHost)" | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/\(kLocation)/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the unencoded HTTP body contains the correct id_token. | ||
func testUnencodedHTTPBodyIsCorrect() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
_ = app | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration | ||
) | ||
|
||
let body = request.unencodedHTTPRequestBody | ||
XCTAssertNotNil(body) | ||
XCTAssertEqual(body?.count, 1) | ||
XCTAssertEqual(body?["id_token"] as? String, kIdToken) | ||
} | ||
|
||
// MARK: - Helper Function | ||
|
||
/// Creates a test FirebaseApp and Auth instance with specified configurations. | ||
private func createTestAuthInstance(projectID: String?, location: String?, | ||
tenantId: String?) -> (auth: Auth, app: FirebaseApp) { | ||
let appName = "TestApp-\(UUID().uuidString)" | ||
let options = FirebaseOptions( | ||
googleAppID: "1:1234567890:ios:abcdef123456", | ||
gcmSenderID: "1234567890" | ||
) | ||
options.apiKey = kAPIKey | ||
if let projectID = projectID { | ||
options.projectID = projectID | ||
} | ||
|
||
if FirebaseApp.app(name: appName) != nil { | ||
FirebaseApp.app(name: appName)?.delete { _ in } | ||
} | ||
let app = FirebaseApp(instanceWithName: appName, options: options) | ||
|
||
let auth = Auth(app: app) | ||
auth.app = app | ||
auth.requestConfiguration.location = location | ||
auth.requestConfiguration.tenantId = tenantId | ||
|
||
return (auth, app) | ||
} | ||
} |
213 changes: 213 additions & 0 deletions
213
FirebaseAuth/Tests/Unit/ExchangeTokenRequestTests.swift
This file contains hidden or 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,213 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import FirebaseAuth | ||
import FirebaseCore | ||
|
||
/// Tests for `ExchangeTokenRequest` | ||
@available(iOS 13, *) | ||
class ExchangeTokenRequestTests: XCTestCase { | ||
// MARK: - Constants for Testing | ||
|
||
let kAPIKey = "test-api-key" | ||
let kProjectID = "test-project-id" | ||
let kLocation = "us-east1" | ||
let kTenantID = "test-tenant-id-123" | ||
let kIdToken = "a-very-long-and-secure-oidc-token-string" | ||
let kIdpConfigId = "oidc.my-test-provider" | ||
|
||
// These should match the constants in ExchangeTokenRequest.swift | ||
let kProductionHost = "identityplatform.googleapis.com" | ||
let kStagingHost = "staging-identityplatform.sandbox.googleapis.com" | ||
|
||
// MARK: - Helper Function | ||
|
||
/// Creates a test FirebaseApp and Auth instance with specified configurations. | ||
private func createTestAuthInstance(projectID: String?, location: String?, | ||
tenantId: String?) -> (auth: Auth, app: FirebaseApp) { | ||
let appName = "TestApp-\(UUID().uuidString)" | ||
let options = FirebaseOptions( | ||
googleAppID: "1:1234567890:ios:abcdef123456", | ||
gcmSenderID: "1234567890" | ||
) | ||
options.apiKey = kAPIKey | ||
if let projectID = projectID { | ||
options.projectID = projectID | ||
} | ||
|
||
if let existingApp = FirebaseApp.app(name: appName) { | ||
existingApp.delete { _ in } | ||
} | ||
let app = FirebaseApp(instanceWithName: appName, options: options) | ||
|
||
let auth: Auth | ||
if let loc = location, let tid = tenantId { | ||
let tenantConfig = Auth.TenantConfig(tenantId: tid, location: loc) | ||
auth = Auth(app: app, tenantConfig: tenantConfig) | ||
} else { | ||
// This case should not be hit in these tests as all tests provide location and tenantId | ||
auth = Auth(app: app) | ||
} | ||
|
||
return (auth, app) | ||
} | ||
|
||
/// Helper to add debugging assertions. | ||
private func checkPreconditions(auth: Auth, app: FirebaseApp, expectedLocation: String, | ||
expectedTenantId: String, expectedProjectId: String) { | ||
XCTAssertNotNil(auth.requestConfiguration.tenantConfig, "tenantConfig should not be nil") | ||
XCTAssertEqual( | ||
auth.requestConfiguration.tenantConfig?.location, | ||
expectedLocation, | ||
"Location should match" | ||
) | ||
XCTAssertEqual( | ||
auth.requestConfiguration.tenantConfig?.tenantId, | ||
expectedTenantId, | ||
"Tenant ID should match" | ||
) | ||
|
||
XCTAssertNotNil(auth.requestConfiguration.auth, "config.auth should not be nil") | ||
XCTAssertTrue( | ||
auth.requestConfiguration.auth === auth, | ||
"config.auth should be the same instance" | ||
) | ||
|
||
XCTAssertNotNil(auth.app, "Auth.app should not be nil") | ||
XCTAssertTrue(auth.app === app, "Auth.app should be the same instance") | ||
|
||
XCTAssertNotNil(auth.app?.options, "App options should not be nil") | ||
XCTAssertEqual(auth.app?.options.projectID, expectedProjectId, "Project ID should match") | ||
} | ||
|
||
// MARK: - Test Cases | ||
|
||
/// Tests that the production URL is correctly formed for a specific region. | ||
func testProductionURLIsCorrectlyConstructed() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
checkPreconditions( | ||
auth: auth, | ||
app: app, | ||
expectedLocation: kLocation, | ||
expectedTenantId: kTenantID, | ||
expectedProjectId: kProjectID | ||
) | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: false | ||
) | ||
|
||
let expectedHost = "\(kLocation)-\(kProductionHost)" | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/\(kLocation)/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the production URL is correctly formed for the "global" location. | ||
func testProductionURLIsCorrectlyConstructedForGlobalLocation() { | ||
let globalLocation = "global" | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: globalLocation, | ||
tenantId: kTenantID | ||
) | ||
checkPreconditions( | ||
auth: auth, | ||
app: app, | ||
expectedLocation: globalLocation, | ||
expectedTenantId: kTenantID, | ||
expectedProjectId: kProjectID | ||
) | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: false | ||
) | ||
|
||
let expectedHost = kProductionHost | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/global/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the staging URL is correctly formed. | ||
func testStagingURLIsCorrectlyConstructed() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
checkPreconditions( | ||
auth: auth, | ||
app: app, | ||
expectedLocation: kLocation, | ||
expectedTenantId: kTenantID, | ||
expectedProjectId: kProjectID | ||
) | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration, | ||
useStaging: true | ||
) | ||
|
||
let expectedHost = "\(kLocation)-\(kStagingHost)" | ||
let expectedURL = "https://\(expectedHost)/v2beta/projects/\(kProjectID)" + | ||
"/locations/\(kLocation)/tenants/\(kTenantID)/idpConfigs/\(kIdpConfigId):exchangeOidcToken?key=\(kAPIKey)" | ||
|
||
XCTAssertEqual(request.requestURL().absoluteString, expectedURL) | ||
} | ||
|
||
/// Tests that the unencoded HTTP body contains the correct id_token. | ||
func testUnencodedHTTPBodyIsCorrect() { | ||
let (auth, app) = createTestAuthInstance( | ||
projectID: kProjectID, | ||
location: kLocation, | ||
tenantId: kTenantID | ||
) | ||
checkPreconditions( | ||
auth: auth, | ||
app: app, | ||
expectedLocation: kLocation, | ||
expectedTenantId: kTenantID, | ||
expectedProjectId: kProjectID | ||
) | ||
|
||
let request = ExchangeTokenRequest( | ||
idToken: kIdToken, | ||
idpConfigID: kIdpConfigId, | ||
config: auth.requestConfiguration | ||
) | ||
|
||
let body = request.unencodedHTTPRequestBody | ||
XCTAssertNotNil(body) | ||
XCTAssertEqual(body?.count, 1) | ||
XCTAssertEqual(body?["id_token"] as? String, kIdToken) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we not do
let auth = Auth(app: app, tenantConfig: tenantConfig)
? Why are we setting these 2 separately? This is different from how we implemented in other client SDKs.