FIRDeleteAccountResponseTests.m 6.4 KB

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