FIREmailLinkSignInResponseTests.m 6.8 KB

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