RevokeTokenTests.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 RevokeTokenTests: RPCBaseTests {
  19. private let kAPPTokenKey = "appToken"
  20. private let kFakeAppToken = "kAPPTokenKey"
  21. private let kFakeTokenKey = "token"
  22. private let kFakeToken = "fakeToken"
  23. private let kFakeIDTokenKey = "idToken"
  24. private let kFakeIDToken = "fakeIDToken"
  25. private let kFakeProviderIDKey = "providerId"
  26. private let kFakeTokenTypeKey = "tokenType"
  27. private let kExpectedAPIURL =
  28. "https://identitytoolkit.googleapis.com/v2/accounts:revokeToken?key=APIKey"
  29. /** @fn testRevokeTokenRequest
  30. @brief Tests the RevokeToken request.
  31. */
  32. func testRevokeTokenRequest() throws {
  33. let request = makeRevokeTokenRequest()
  34. let issuer = try checkRequest(
  35. request: request,
  36. expected: kExpectedAPIURL,
  37. key: kFakeTokenKey,
  38. value: kFakeToken
  39. )
  40. let requestDictionary = try XCTUnwrap(issuer.decodedRequest as? [String: AnyHashable])
  41. XCTAssertEqual(requestDictionary[kFakeProviderIDKey], AuthProviderString.apple.rawValue)
  42. XCTAssertEqual(requestDictionary[kFakeTokenTypeKey], "3")
  43. }
  44. /** @fn testSuccessfulRevokeTokenResponse
  45. @brief Tests a successful attempt of the verify password flow.
  46. */
  47. func testSuccessfulRevokeTokenResponse() throws {
  48. var callbackInvoked = false
  49. var rpcResponse: RevokeTokenResponse?
  50. var rpcError: NSError?
  51. AuthBackend.post(with: makeRevokeTokenRequest()) { response, error in
  52. callbackInvoked = true
  53. rpcResponse = response
  54. rpcError = error as? NSError
  55. }
  56. _ = try rpcIssuer?.respond(withJSON: [:])
  57. XCTAssert(callbackInvoked)
  58. XCTAssertNil(rpcError)
  59. XCTAssertNotNil(rpcResponse)
  60. }
  61. private func makeRevokeTokenRequest() -> RevokeTokenRequest {
  62. return RevokeTokenRequest(withToken: kFakeToken,
  63. idToken: kFakeIDToken,
  64. requestConfiguration: makeRequestConfiguration())
  65. }
  66. }