FIREmailLinkSignInResponseTests.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInRequest.h"
  22. #import "FirebaseAuth/Sources/Backend/RPC/FIREmailLinkSignInResponse.h"
  23. #import "FirebaseAuth/Sources/Utilities/FIRAuthErrorUtils.h"
  24. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  25. /** @var kTestAPIKey
  26. @brief Fake API key used for testing.
  27. */
  28. static NSString *const kTestAPIKey = @"APIKey";
  29. /** @var kTestFirebaseAppID
  30. @brief Fake Firebase app ID used for testing.
  31. */
  32. static NSString *const kTestFirebaseAppID = @"appID";
  33. /** @var kTestEmail
  34. @brief The key for the "email" value in the request.
  35. */
  36. static NSString *const kTestEmail = @"TestEmail@email.com";
  37. /** @var kTestOOBCode
  38. @brief The test value for the "oobCode" in the request.
  39. */
  40. static NSString *const kTestOOBCode = @"TestOOBCode";
  41. /** @var kTestIDToken
  42. @brief The test value for "idToken" in the request.
  43. */
  44. static NSString *const kTestIDToken = @"testIDToken";
  45. /** @var kEmailKey
  46. @brief The key for the "identifier" value in the request.
  47. */
  48. static NSString *const kEmailKey = @"email";
  49. /** @var kEmailLinkKey
  50. @brief The key for the "emailLink" value in the request.
  51. */
  52. static NSString *const kOOBCodeKey = @"oobCode";
  53. /** @var kIDTokenKey
  54. @brief The key for the "IDToken" value in the request.
  55. */
  56. static NSString *const kIDTokenKey = @"idToken";
  57. /** @var kTestIDTokenResponse
  58. @brief A fake ID Token in the server response.
  59. */
  60. static NSString *const kTestIDTokenResponse = @"fakeToken";
  61. /** @var kTestEmailResponse
  62. @brief A fake email in the server response.
  63. */
  64. static NSString *const kTestEmailResponse = @"fake email";
  65. /** @var kTestRefreshToken
  66. @brief A fake refresh token in the server response.
  67. */
  68. static NSString *const kTestRefreshToken = @"testRefreshToken";
  69. /** @var kInvalidEmailErrorMessage
  70. @brief The error returned by the server if the email is invalid.
  71. */
  72. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL";
  73. /** @var kTestTokenExpirationTimeInterval
  74. @brief The fake time interval that it takes a token to expire.
  75. */
  76. static const NSTimeInterval kTestTokenExpirationTimeInterval = 55 * 60;
  77. /** @var kMaxDifferenceBetweenDates
  78. @brief The maximum difference between time two dates (in seconds), after which they will be
  79. considered different.
  80. */
  81. static const NSTimeInterval kMaxDifferenceBetweenDates = 0.001;
  82. /** @var kFakeIsNewUSerFlag
  83. @brief The fake fake isNewUser flag in the response.
  84. */
  85. static const BOOL kFakeIsNewUSerFlag = YES;
  86. /** @class FIREmailLinkRequestTests
  87. @brief Tests for @c FIREmailLinkRequests.
  88. */
  89. @interface FIREmailLinkSignInResponseTests : XCTestCase
  90. @end
  91. @implementation FIREmailLinkSignInResponseTests {
  92. /** @var _RPCIssuer
  93. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  94. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  95. */
  96. FIRFakeBackendRPCIssuer *_RPCIssuer;
  97. /** @var _requestConfiguration
  98. @brief This is the request configuration used for testing.
  99. */
  100. FIRAuthRequestConfiguration *_requestConfiguration;
  101. }
  102. - (void)setUp {
  103. [super setUp];
  104. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  105. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  106. _RPCIssuer = RPCIssuer;
  107. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  108. appID:kTestFirebaseAppID];
  109. }
  110. /** @fn testFailedEmailLinkSignInResponse
  111. @brief Tests a failed email link sign-in response.
  112. */
  113. - (void)testFailedEmailLinkSignInResponse {
  114. FIREmailLinkSignInRequest *request =
  115. [[FIREmailLinkSignInRequest alloc] initWithEmail:kTestEmail
  116. oobCode:kTestOOBCode
  117. requestConfiguration:_requestConfiguration];
  118. __block BOOL callbackInvoked = NO;
  119. __block FIREmailLinkSignInResponse *RPCResponse;
  120. __block NSError *RPCError;
  121. [FIRAuthBackend
  122. emailLinkSignin:request
  123. callback:^(FIREmailLinkSignInResponse *_Nullable response, NSError *_Nullable error) {
  124. callbackInvoked = YES;
  125. RPCResponse = response;
  126. RPCError = error;
  127. }];
  128. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  129. XCTAssert(callbackInvoked);
  130. XCTAssertNil(RPCResponse);
  131. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  132. }
  133. /** @fn testSuccessfulEmailLinkSignInResponse
  134. @brief Tests a successful email link sign-in response.
  135. */
  136. - (void)testSuccessfulEmailLinkSignInResponse {
  137. FIREmailLinkSignInRequest *request =
  138. [[FIREmailLinkSignInRequest alloc] initWithEmail:kTestEmail
  139. oobCode:kTestOOBCode
  140. requestConfiguration:_requestConfiguration];
  141. __block BOOL callbackInvoked = NO;
  142. __block FIREmailLinkSignInResponse *RPCResponse;
  143. __block NSError *RPCError;
  144. [FIRAuthBackend
  145. emailLinkSignin:request
  146. callback:^(FIREmailLinkSignInResponse *_Nullable response, NSError *_Nullable error) {
  147. callbackInvoked = YES;
  148. RPCResponse = response;
  149. RPCError = error;
  150. }];
  151. [_RPCIssuer respondWithJSON:@{
  152. @"idToken" : kTestIDTokenResponse,
  153. @"email" : kTestEmailResponse,
  154. @"isNewUser" : kFakeIsNewUSerFlag ? @YES : @NO,
  155. @"expiresIn" : [NSString stringWithFormat:@"%f", kTestTokenExpirationTimeInterval],
  156. @"refreshToken" : kTestRefreshToken,
  157. }];
  158. XCTAssert(callbackInvoked);
  159. XCTAssertNil(RPCError);
  160. XCTAssertNotNil(RPCResponse);
  161. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDTokenResponse);
  162. XCTAssertEqualObjects(RPCResponse.email, kTestEmailResponse);
  163. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  164. XCTAssertTrue(RPCResponse.isNewUser);
  165. NSTimeInterval expirationTimeInterval =
  166. [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  167. NSTimeInterval testTimeInterval =
  168. [[NSDate dateWithTimeIntervalSinceNow:kTestTokenExpirationTimeInterval] timeIntervalSinceNow];
  169. NSTimeInterval timeIntervalDifference = fabs(expirationTimeInterval - testTimeInterval);
  170. XCTAssert(timeIntervalDifference < kMaxDifferenceBetweenDates);
  171. }
  172. @end
  173. #endif