GoogleAuthProviderTests.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. /** @class FIRGoogleAuthProviderTests
  18. @brief Tests for @c FIRGoogleAuthProvider
  19. */
  20. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  21. class GoogleAuthProviderTests: XCTestCase {
  22. let kAccessToken = "Token"
  23. let kIDToken = "idToken"
  24. /** @fn testCredentialWithToken
  25. @brief Tests the @c credentialWithToken method to make sure the credential it produces populates
  26. the appropriate fields in a verify assertion request.
  27. */
  28. func testCredentialWithToken() {
  29. let requestConfiguration = AuthRequestConfiguration(apiKey: "APIKey", appID: "appID")
  30. let credential = GoogleAuthProvider.credential(withIDToken: kIDToken, accessToken: kAccessToken)
  31. let request = VerifyAssertionRequest(providerID: GoogleAuthProvider.id,
  32. requestConfiguration: requestConfiguration)
  33. credential.prepare(request)
  34. XCTAssertEqual(kAccessToken, request.providerAccessToken)
  35. }
  36. /** @fn testGoogleAuthCredentialCoding
  37. @brief Tests successful archiving and unarchiving of @c GoogleAuthCredential.
  38. */
  39. func testGoogleAuthCredentialCoding() throws {
  40. let credential = GoogleAuthProvider.credential(withIDToken: kIDToken, accessToken: kAccessToken)
  41. XCTAssertTrue(GoogleAuthCredential.supportsSecureCoding)
  42. let data = try NSKeyedArchiver.archivedData(
  43. withRootObject: credential,
  44. requiringSecureCoding: true
  45. )
  46. let unarchivedCredential = try XCTUnwrap(NSKeyedUnarchiver.unarchivedObject(
  47. ofClass: GoogleAuthCredential.self, from: data
  48. ))
  49. XCTAssertEqual(unarchivedCredential.accessToken, kAccessToken)
  50. XCTAssertEqual(unarchivedCredential.idToken, kIDToken)
  51. }
  52. }