FIRRevokeTokenRequestTests.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2023 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRRevokeTokenResponse.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kFakeToken
  24. @brief The fake token to use in the test request.
  25. */
  26. static NSString *const kFakeTokenKey = @"token";
  27. /** @var kFakeToken
  28. @brief The fake token to use in the test request.
  29. */
  30. static NSString *const kFakeToken = @"fakeToken";
  31. /** @var kFakeIDToken
  32. @brief The fake ID token to use in the test request.
  33. */
  34. static NSString *const kFakeIDTokenKey = @"idToken";
  35. /** @var kFakeIDToken
  36. @brief The fake ID token to use in the test request.
  37. */
  38. static NSString *const kFakeIDToken = @"fakeIDToken";
  39. /** @var kFakeProviderIDKey
  40. @brief The fake provider id key to use in the test request.
  41. */
  42. static NSString *const kFakeProviderIDKey = @"providerId";
  43. /** @var kFakeTokenTypeKey
  44. @brief The fake ID token to use in the test request.
  45. */
  46. static NSString *const kFakeTokenTypeKey = @"tokenType";
  47. /** @var kFakeAPIKey
  48. @brief The fake API key to use in the test request.
  49. */
  50. static NSString *const kFakeAPIKey = @"APIKey";
  51. /** @var kFakeFirebaseAppID
  52. @brief The fake Firebase app ID to use in the test request.
  53. */
  54. static NSString *const kFakeFirebaseAppID = @"appID";
  55. /** @var kExpectedAPIURL
  56. @brief The expected URL for the test calls.
  57. */
  58. static NSString *const kExpectedAPIURL =
  59. @"https://identitytoolkit.googleapis.com/v2/accounts:revokeToken?key=APIKey";
  60. /** @class FIRRevokeTokenRequestTest
  61. @brief Tests for @c FIRRevokeTokenRequests.
  62. */
  63. @interface FIRRevokeTokenRequestTest : XCTestCase
  64. @end
  65. @implementation FIRRevokeTokenRequestTest {
  66. /** @var _RPCIssuer
  67. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  68. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  69. */
  70. FIRFakeBackendRPCIssuer *_RPCIssuer;
  71. }
  72. - (void)setUp {
  73. [super setUp];
  74. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  75. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  76. _RPCIssuer = RPCIssuer;
  77. }
  78. - (void)tearDown {
  79. _RPCIssuer = nil;
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  81. [super tearDown];
  82. }
  83. /** @fn testRevokeTokenRequest
  84. @brief Tests the token revocation request.
  85. */
  86. - (void)testRevokeTokenRequest {
  87. FIRAuthRequestConfiguration *requestConfiguration =
  88. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIKey appID:kFakeFirebaseAppID];
  89. FIRRevokeTokenRequest *request =
  90. [[FIRRevokeTokenRequest alloc] initWithToken:kFakeToken
  91. idToken:kFakeIDToken
  92. requestConfiguration:requestConfiguration];
  93. [FIRAuthBackend
  94. revokeToken:request
  95. callback:^(FIRRevokeTokenResponse *_Nullable response, NSError *_Nullable error){
  96. }];
  97. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  98. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  99. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kFakeIDTokenKey], kFakeIDToken);
  100. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kFakeTokenKey], kFakeToken);
  101. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kFakeProviderIDKey], @"apple.com");
  102. XCTAssertEqual([_RPCIssuer.decodedRequest[kFakeTokenTypeKey] intValue], 3);
  103. }
  104. @end
  105. #endif