FIRDeleteAccountResponseTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "FIRAuthErrors.h"
  18. #import "FIRAuthBackend.h"
  19. #import "FIRDeleteAccountRequest.h"
  20. #import "FIRDeleteAccountResponse.h"
  21. #import "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. }
  60. - (void)setUp {
  61. [super setUp];
  62. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  63. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  64. _RPCIssuer = RPCIssuer;
  65. }
  66. - (void)tearDown {
  67. _RPCIssuer = nil;
  68. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  69. [super tearDown];
  70. }
  71. /** @fn testUserDisabledError
  72. @brief This test simulates the occurrence of a @c userDisabled error.
  73. */
  74. - (void)testUserDisabledError {
  75. FIRDeleteAccountRequest *request = [[FIRDeleteAccountRequest alloc] initWithAPIKey:kTestAPIKey
  76. localID:kLocalID
  77. accessToken:kAccessToken];
  78. __block BOOL callbackInvoked;
  79. __block NSError *RPCError;
  80. [FIRAuthBackend deleteAccount:request
  81. callback:^(NSError *_Nullable error) {
  82. callbackInvoked = YES;
  83. RPCError = error;
  84. }];
  85. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  86. XCTAssert(callbackInvoked);
  87. XCTAssertNotNil(RPCError);
  88. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  89. }
  90. /** @fn testinvalidUserTokenError
  91. @brief This test simulates the occurrence of a @c invalidUserToken error.
  92. */
  93. - (void)testinvalidUserTokenError {
  94. FIRDeleteAccountRequest *request = [[FIRDeleteAccountRequest alloc] initWithAPIKey:kTestAPIKey
  95. localID:kLocalID
  96. accessToken:kAccessToken];
  97. __block BOOL callbackInvoked;
  98. __block NSError *RPCError;
  99. [FIRAuthBackend deleteAccount:request
  100. callback:^(NSError *_Nullable error) {
  101. callbackInvoked = YES;
  102. RPCError = error;
  103. }];
  104. [_RPCIssuer respondWithServerErrorMessage:kinvalidUserTokenErrorMessage];
  105. XCTAssert(callbackInvoked);
  106. XCTAssertNotNil(RPCError);
  107. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidUserToken);
  108. }
  109. /** @fn testrequiredRecentLoginError
  110. @brief This test simulates the occurrence of a @c credentialTooOld error.
  111. */
  112. - (void)testrequiredRecentLoginError {
  113. FIRDeleteAccountRequest *request = [[FIRDeleteAccountRequest alloc] initWithAPIKey:kTestAPIKey
  114. localID:kLocalID
  115. accessToken:kAccessToken];
  116. __block BOOL callbackInvoked;
  117. __block NSError *RPCError;
  118. [FIRAuthBackend deleteAccount:request
  119. callback:^(NSError *_Nullable error) {
  120. callbackInvoked = YES;
  121. RPCError = error;
  122. }];
  123. [_RPCIssuer respondWithServerErrorMessage:kCredentialTooOldErrorMessage];
  124. XCTAssert(callbackInvoked);
  125. XCTAssertNotNil(RPCError);
  126. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeRequiresRecentLogin);
  127. }
  128. /** @fn testSuccessfulDeleteAccount
  129. @brief This test simulates a completed succesful deleteAccount operation.
  130. */
  131. - (void)testSuccessfulDeleteAccountResponse {
  132. FIRDeleteAccountRequest *request = [[FIRDeleteAccountRequest alloc] initWithAPIKey:kTestAPIKey
  133. localID:kLocalID
  134. accessToken:kAccessToken];
  135. __block BOOL callbackInvoked;
  136. __block NSError *RPCError;
  137. [FIRAuthBackend deleteAccount:request
  138. callback:^(NSError *_Nullable error) {
  139. callbackInvoked = YES;
  140. RPCError = error;
  141. }];
  142. [_RPCIssuer respondWithJSON:@{}];
  143. XCTAssert(callbackInvoked);
  144. XCTAssertNil(RPCError);
  145. }
  146. @end