FIREmailLinkSignInResponseTests.m 6.5 KB

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