FIRDeleteAccountResponseTests.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright 2017 Google
  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 <XCTest/XCTest.h>
  17. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRDeleteAccountResponse.h"
  21. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kTestFirebaseAppID
  27. @brief Fake Firebase app ID used for testing.
  28. */
  29. static NSString *const kTestFirebaseAppID = @"appID";
  30. /** @var kLocalID
  31. @brief Fake LocalID used for testing.
  32. */
  33. static NSString *const kLocalID = @"LocalID";
  34. /** @var kAccessToken
  35. @brief Fake AccessToken used for testing.
  36. */
  37. static NSString *const kAccessToken = @"AccessToken";
  38. /** @var kUserDisabledErrorMessage
  39. @brief The error returned by the server if the user account is diabled.
  40. */
  41. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  42. /** @var kinvalidUserTokenErrorMessage
  43. @brief This is the error message the server responds with if user's saved auth credential is
  44. invalid, and the user needs to sign in again.
  45. */
  46. static NSString *const kinvalidUserTokenErrorMessage = @"INVALID_ID_TOKEN:";
  47. /** @var kCredentialTooOldErrorMessage
  48. @brief This is the error message the server responds with if account change is attempted 5
  49. minutes after signing in.
  50. */
  51. static NSString *const kCredentialTooOldErrorMessage = @"CREDENTIAL_TOO_OLD_LOGIN_AGAIN:";
  52. /** @class FIRDeleteUserResponseTests
  53. @brief Tests for @c FIRDeleteAccountResponse.
  54. */
  55. @interface FIRDeleteAccountResponseTests : XCTestCase
  56. @end
  57. @implementation FIRDeleteAccountResponseTests {
  58. /** @var _RPCIssuer
  59. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  60. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  61. */
  62. FIRFakeBackendRPCIssuer *_RPCIssuer;
  63. /** @var _requestConfiguration
  64. @brief This is the request configuration used for testing.
  65. */
  66. FIRAuthRequestConfiguration *_requestConfiguration;
  67. }
  68. - (void)setUp {
  69. [super setUp];
  70. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  71. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  72. _RPCIssuer = RPCIssuer;
  73. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  74. appID:kTestFirebaseAppID];
  75. }
  76. - (void)tearDown {
  77. _requestConfiguration = nil;
  78. _RPCIssuer = nil;
  79. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  80. [super tearDown];
  81. }
  82. /** @fn testUserDisabledError
  83. @brief This test simulates the occurrence of a @c userDisabled error.
  84. */
  85. - (void)testUserDisabledError {
  86. FIRDeleteAccountRequest *request =
  87. [[FIRDeleteAccountRequest alloc] initWitLocalID:kLocalID
  88. accessToken:kAccessToken
  89. requestConfiguration:_requestConfiguration];
  90. __block BOOL callbackInvoked;
  91. __block NSError *RPCError;
  92. [FIRAuthBackend deleteAccount:request
  93. callback:^(NSError *_Nullable error) {
  94. callbackInvoked = YES;
  95. RPCError = error;
  96. }];
  97. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  98. XCTAssert(callbackInvoked);
  99. XCTAssertNotNil(RPCError);
  100. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  101. }
  102. /** @fn testinvalidUserTokenError
  103. @brief This test simulates the occurrence of a @c invalidUserToken error.
  104. */
  105. - (void)testinvalidUserTokenError {
  106. FIRDeleteAccountRequest *request =
  107. [[FIRDeleteAccountRequest alloc] initWitLocalID:kLocalID
  108. accessToken:kAccessToken
  109. requestConfiguration:_requestConfiguration];
  110. __block BOOL callbackInvoked;
  111. __block NSError *RPCError;
  112. [FIRAuthBackend deleteAccount:request
  113. callback:^(NSError *_Nullable error) {
  114. callbackInvoked = YES;
  115. RPCError = error;
  116. }];
  117. [_RPCIssuer respondWithServerErrorMessage:kinvalidUserTokenErrorMessage];
  118. XCTAssert(callbackInvoked);
  119. XCTAssertNotNil(RPCError);
  120. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidUserToken);
  121. }
  122. /** @fn testrequiredRecentLoginError
  123. @brief This test simulates the occurrence of a @c credentialTooOld error.
  124. */
  125. - (void)testrequiredRecentLoginError {
  126. FIRDeleteAccountRequest *request =
  127. [[FIRDeleteAccountRequest alloc] initWitLocalID:kLocalID
  128. accessToken:kAccessToken
  129. requestConfiguration:_requestConfiguration];
  130. __block BOOL callbackInvoked;
  131. __block NSError *RPCError;
  132. [FIRAuthBackend deleteAccount:request
  133. callback:^(NSError *_Nullable error) {
  134. callbackInvoked = YES;
  135. RPCError = error;
  136. }];
  137. [_RPCIssuer respondWithServerErrorMessage:kCredentialTooOldErrorMessage];
  138. XCTAssert(callbackInvoked);
  139. XCTAssertNotNil(RPCError);
  140. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeRequiresRecentLogin);
  141. }
  142. /** @fn testSuccessfulDeleteAccount
  143. @brief This test simulates a completed succesful deleteAccount operation.
  144. */
  145. - (void)testSuccessfulDeleteAccountResponse {
  146. FIRDeleteAccountRequest *request =
  147. [[FIRDeleteAccountRequest alloc] initWitLocalID:kLocalID
  148. accessToken:kAccessToken
  149. requestConfiguration:_requestConfiguration];
  150. __block BOOL callbackInvoked;
  151. __block NSError *RPCError;
  152. [FIRAuthBackend deleteAccount:request
  153. callback:^(NSError *_Nullable error) {
  154. callbackInvoked = YES;
  155. RPCError = error;
  156. }];
  157. [_RPCIssuer respondWithJSON:@{}];
  158. XCTAssert(callbackInvoked);
  159. XCTAssertNil(RPCError);
  160. }
  161. @end