Skip to content

Commit e63a770

Browse files
committed
adding unit tests
1 parent 714df23 commit e63a770

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if os(iOS) || os(tvOS) || os(macOS)
16+
17+
@testable import FirebaseAuth
18+
import FirebaseCore
19+
import Foundation
20+
import XCTest
21+
22+
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *)
23+
class StartPasskeyEnrollmentRequestTests: XCTestCase {
24+
private var request: StartPasskeyEnrollmentRequest!
25+
private var fakeConfig: AuthRequestConfiguration!
26+
27+
override func setUp() {
28+
super.setUp()
29+
fakeConfig = AuthRequestConfiguration(
30+
apiKey: "FAKE_API_KEY",
31+
appID: "FAKE_APP_ID"
32+
)
33+
}
34+
35+
override func tearDown() {
36+
request = nil
37+
fakeConfig = nil
38+
super.tearDown()
39+
}
40+
41+
func testInitWithValidIdTokenAndConfiguration() {
42+
request = StartPasskeyEnrollmentRequest(
43+
idToken: "FAKE_ID_TOKEN",
44+
requestConfiguration: fakeConfig
45+
)
46+
XCTAssertEqual(request.idToken, "FAKE_ID_TOKEN")
47+
XCTAssertEqual(request.endpoint, "accounts/passkeyEnrollment:start")
48+
XCTAssertTrue(request.useIdentityPlatform)
49+
}
50+
51+
func testUnencodedHTTPRequestBodyWithoutTenantId() {
52+
request = StartPasskeyEnrollmentRequest(
53+
idToken: "FAKE_ID_TOKEN",
54+
requestConfiguration: fakeConfig
55+
)
56+
let body = request.unencodedHTTPRequestBody
57+
XCTAssertNotNil(body)
58+
XCTAssertEqual(body?["idToken"] as? String, "FAKE_ID_TOKEN")
59+
XCTAssertNil(body?["tenantId"])
60+
}
61+
62+
func testUnencodedHTTPRequestBodyWithTenantId() {
63+
// setting up fake auth to set tenantId
64+
let options = FirebaseOptions(googleAppID: "0:0000000000000:ios:0000000000000000",
65+
gcmSenderID: "00000000000000000-00000000000-000000000")
66+
options.apiKey = AuthTests.kFakeAPIKey
67+
options.projectID = "myProjectID"
68+
let name = "test-AuthTests\(AuthTests.testNum)"
69+
AuthTests.testNum = AuthTests.testNum + 1
70+
let fakeAuth = Auth(app: FirebaseApp(instanceWithName: name, options: options))
71+
fakeAuth.tenantID = "TEST_TENANT"
72+
let configWithTenant = AuthRequestConfiguration(
73+
apiKey: "FAKE_API_KEY",
74+
appID: "FAKE_APP_ID",
75+
auth: fakeAuth
76+
)
77+
request = StartPasskeyEnrollmentRequest(
78+
idToken: "FAKE_ID_TOKEN",
79+
requestConfiguration: configWithTenant
80+
)
81+
let body = request.unencodedHTTPRequestBody
82+
XCTAssertNotNil(body)
83+
XCTAssertEqual(body?["idToken"] as? String, "FAKE_ID_TOKEN")
84+
XCTAssertEqual(body?["tenantId"] as? String, "TEST_TENANT")
85+
}
86+
}
87+
88+
#endif
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#if os(iOS) || os(tvOS) || os(macOS)
16+
17+
@testable import FirebaseAuth
18+
import XCTest
19+
20+
@available(iOS 15.0, macOS 12.0, tvOS 16.0, *)
21+
class StartPasskeyEnrollmentResponseTests: XCTestCase {
22+
private func makeValidDictionary() -> [String: AnyHashable] {
23+
return [
24+
"credentialCreationOptions": [
25+
"rp": ["id": "example.com"] as [String: AnyHashable],
26+
"user": ["id": "USER_123"] as [String: AnyHashable],
27+
"challenge": "FAKE_CHALLENGE" as String,
28+
] as [String: AnyHashable],
29+
]
30+
}
31+
32+
func testInitWithValidDictionary() throws {
33+
let response = try StartPasskeyEnrollmentResponse(dictionary: makeValidDictionary())
34+
XCTAssertEqual(response.rpID, "example.com")
35+
XCTAssertEqual(response.userID, "USER_123")
36+
XCTAssertEqual(response.challenge, "FAKE_CHALLENGE")
37+
}
38+
39+
func testInitWithMissingCredentialCreationOptionsThrowsError() {
40+
let invalidDict: [String: AnyHashable] = [:]
41+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: invalidDict))
42+
}
43+
44+
func testInitWithMissingRpThrowsError() {
45+
var dict = makeValidDictionary()
46+
if var options = dict["credentialCreationOptions"] as? [String: Any] {
47+
options.removeValue(forKey: "rp")
48+
dict["credentialCreationOptions"] = options as? AnyHashable
49+
}
50+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict))
51+
}
52+
53+
func testInitWithMissingRpIdThrowsError() {
54+
var dict = makeValidDictionary()
55+
if var options = dict["credentialCreationOptions"] as? [String: Any],
56+
var rp = options["rp"] as? [String: Any] {
57+
rp.removeValue(forKey: "id")
58+
options["rp"] = rp
59+
dict["credentialCreationOptions"] = options as? AnyHashable
60+
}
61+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict))
62+
}
63+
64+
func testInitWithMissingUserThrowsError() {
65+
var dict = makeValidDictionary()
66+
if var options = dict["credentialCreationOptions"] as? [String: Any] {
67+
options.removeValue(forKey: "user")
68+
dict["credentialCreationOptions"] = options as? AnyHashable
69+
}
70+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict))
71+
}
72+
73+
func testInitWithMissingUserIdThrowsError() {
74+
var dict = makeValidDictionary()
75+
if var options = dict["credentialCreationOptions"] as? [String: Any],
76+
var user = options["user"] as? [String: Any] {
77+
user.removeValue(forKey: "id")
78+
options["user"] = user
79+
dict["credentialCreationOptions"] = options as? AnyHashable
80+
}
81+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict))
82+
}
83+
84+
func testInitWithMissingChallengeThrowsError() {
85+
var dict = makeValidDictionary()
86+
if var options = dict["credentialCreationOptions"] as? [String: Any] {
87+
options.removeValue(forKey: "challenge")
88+
dict["credentialCreationOptions"] = options as? AnyHashable
89+
}
90+
XCTAssertThrowsError(try StartPasskeyEnrollmentResponse(dictionary: dict))
91+
}
92+
}
93+
94+
#endif

0 commit comments

Comments
 (0)