GetAccountInfoTests.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2023 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. import Foundation
  15. import XCTest
  16. @testable import FirebaseAuth
  17. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  18. class GetAccountInfoTests: RPCBaseTests {
  19. /** var kTestAccessToken
  20. brief testing token.
  21. */
  22. let kTestAccessToken = "testAccessToken"
  23. /** var kIDTokenKey
  24. brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  25. despite it's confusing (backwards compatible) parameter name.
  26. */
  27. let kIDTokenKey = "idToken"
  28. func testGetAccountInfoRequest() async {
  29. let kExpectedAPIURL =
  30. "https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=APIKey"
  31. do {
  32. try await checkRequest(
  33. request: makeGetAccountInfoRequest(),
  34. expected: kExpectedAPIURL,
  35. key: kIDTokenKey,
  36. value: kTestAccessToken
  37. )
  38. } catch {
  39. // Ignore error from missing users array in fake JSON return.
  40. return
  41. }
  42. }
  43. /** fn testGetAccountInfoUnexpectedResponseError
  44. brief This test simulates an unexpected response returned from server in c GetAccountInfo
  45. flow.
  46. */
  47. func testGetAccountInfoUnexpectedResponseError() async throws {
  48. let kUsersKey = "users"
  49. try await checkBackendError(
  50. request: makeGetAccountInfoRequest(),
  51. json: [kUsersKey: ["user1Data", "user2Data"]],
  52. errorCode: AuthErrorCode.internalError,
  53. underlyingErrorKey: AuthErrorUtils.userInfoDeserializedResponseKey
  54. )
  55. }
  56. /** @fn testSuccessfulGetAccountInfoResponse
  57. @brief This test simulates a successful @c GetAccountInfo flow.
  58. */
  59. func testSuccessfulGetAccountInfoResponse() async throws {
  60. let kProviderUserInfoKey = "providerUserInfo"
  61. let kPhotoUrlKey = "photoUrl"
  62. let kTestPhotoURL = "testPhotoURL"
  63. let kProviderIDkey = "providerId"
  64. let kDisplayNameKey = "displayName"
  65. let kTestDisplayName = "DisplayName"
  66. let kFederatedIDKey = "federatedId"
  67. let kTestFederatedID = "testFederatedId"
  68. let kEmailKey = "email"
  69. let kTestEmail = "testEmail"
  70. let kPasswordHashKey = "passwordHash"
  71. let kTestPasswordHash = "testPasswordHash"
  72. let kTestProviderID = "testProviderID"
  73. let kEmailVerifiedKey = "emailVerified"
  74. let kLocalIDKey = "localId"
  75. let kTestLocalID = "testLocalId"
  76. let usersIn = [[
  77. kProviderUserInfoKey: [[
  78. kProviderIDkey: kTestProviderID,
  79. kDisplayNameKey: kTestDisplayName,
  80. kPhotoUrlKey: kTestPhotoURL,
  81. kFederatedIDKey: kTestFederatedID,
  82. kEmailKey: kTestEmail,
  83. ]],
  84. kLocalIDKey: kTestLocalID,
  85. kDisplayNameKey: kTestDisplayName,
  86. kEmailKey: kTestEmail,
  87. kPhotoUrlKey: kTestPhotoURL,
  88. kEmailVerifiedKey: true,
  89. kPasswordHashKey: kTestPasswordHash,
  90. ] as [String: Any]]
  91. rpcIssuer?.respondBlock = {
  92. try self.rpcIssuer?.respond(withJSON: ["users": usersIn])
  93. }
  94. let rpcResponse = try await AuthBackend.call(with: makeGetAccountInfoRequest())
  95. let users = try XCTUnwrap(rpcResponse.users)
  96. XCTAssertGreaterThan(users.count, 0)
  97. let firstUser = try XCTUnwrap(users.first)
  98. XCTAssertEqual(firstUser.photoURL?.absoluteString, kTestPhotoURL)
  99. XCTAssertEqual(firstUser.displayName, kTestDisplayName)
  100. XCTAssertEqual(firstUser.email, kTestEmail)
  101. XCTAssertEqual(firstUser.localID, kTestLocalID)
  102. XCTAssertTrue(firstUser.emailVerified)
  103. let providerUserInfo = try XCTUnwrap(firstUser.providerUserInfo)
  104. XCTAssertGreaterThan(providerUserInfo.count, 0)
  105. let firstProviderUser = try XCTUnwrap(providerUserInfo.first)
  106. XCTAssertEqual(firstProviderUser.photoURL?.absoluteString, kTestPhotoURL)
  107. XCTAssertEqual(firstProviderUser.displayName, kTestDisplayName)
  108. XCTAssertEqual(firstProviderUser.email, kTestEmail)
  109. XCTAssertEqual(firstProviderUser.providerID, kTestProviderID)
  110. XCTAssertEqual(firstProviderUser.federatedID, kTestFederatedID)
  111. }
  112. private func makeGetAccountInfoRequest() -> GetAccountInfoRequest {
  113. return GetAccountInfoRequest(accessToken: kTestAccessToken,
  114. requestConfiguration: makeRequestConfiguration())
  115. }
  116. }