FIRVerifyClientResponseTests.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/FIRVerifyClientRequest.h"
  22. #import "FirebaseAuth/Sources/Backend/RPC/FIRVerifyClientResponse.h"
  23. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  24. /** @var kFakeAppToken
  25. @brief The fake app token to use in the test request.
  26. */
  27. static NSString *const kFakeAppToken = @"appToken";
  28. /** @var kFakeAPIKey
  29. @brief The fake API key to use in the test request.
  30. */
  31. static NSString *const kFakeAPIKey = @"APIKey";
  32. /** @var kAppTokenKey
  33. @brief The key for the appToken request paramenter.
  34. */
  35. static NSString *const kAPPTokenKey = @"appToken";
  36. /** @var kIsSandboxKey
  37. @brief The key for the isSandbox request parameter
  38. */
  39. static NSString *const kIsSandboxKey = @"isSandbox";
  40. /** @var kReceiptKey
  41. @brief The key for the receipt response paramenter.
  42. */
  43. static NSString *const kReceiptKey = @"receipt";
  44. /** @var kFakeReceipt
  45. @brief The fake receipt returned in the response.
  46. */
  47. static NSString *const kFakeReceipt = @"receipt";
  48. /** @var kSuggestedTimeOutKey
  49. @brief The key for the suggested timeout response parameter
  50. */
  51. static NSString *const kSuggestedTimeOutKey = @"suggestedTimeout";
  52. /** @var kFakeSuggestedTimeout
  53. @brief The fake suggested timeout returned in the response.
  54. */
  55. static NSString *const kFakeSuggestedTimeout = @"1234";
  56. /** @var kAllowedTimeDifference
  57. @brief Allowed difference when comparing times because of execution time and floating point
  58. error.
  59. */
  60. static const double kAllowedTimeDifference = 0.1;
  61. /** @var kMissingAppCredentialErrorMessage
  62. @brief This is the error message the server will respond with if the APNS token is missing in a
  63. verifyClient request is missing.
  64. */
  65. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  66. /** @var kMissingAppCredentialErrorMessage
  67. @brief This is the error message the server will respond with if the APNS token is missing in a
  68. verifyClient request is invalid.
  69. */
  70. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  71. @interface FIRVerifyClientResponseTests : XCTestCase
  72. @end
  73. @implementation FIRVerifyClientResponseTests {
  74. /** @var _RPCIssuer
  75. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  76. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  77. */
  78. FIRFakeBackendRPCIssuer *_RPCIssuer;
  79. /** @var _requestConfiguration
  80. @brief This is the request configuration used for testing.
  81. */
  82. FIRAuthRequestConfiguration *_requestConfiguration;
  83. }
  84. - (void)setUp {
  85. [super setUp];
  86. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  87. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  88. _RPCIssuer = RPCIssuer;
  89. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIKey];
  90. }
  91. /** @fn testMissingAppCredentialError
  92. @brief Tests that @c FIRAuthErrorCodeMissingAppCredential error.
  93. */
  94. - (void)testMissingAppCredentialError {
  95. FIRVerifyClientRequest *request =
  96. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  97. isSandbox:YES
  98. requestConfiguration:_requestConfiguration];
  99. __block BOOL callbackInvoked;
  100. __block FIRVerifyClientResponse *RPCResponse;
  101. __block NSError *RPCError;
  102. [FIRAuthBackend
  103. verifyClient:request
  104. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  105. RPCResponse = response;
  106. RPCError = error;
  107. callbackInvoked = YES;
  108. }];
  109. [_RPCIssuer respondWithServerErrorMessage:kMissingAppCredentialErrorMessage];
  110. XCTAssert(callbackInvoked);
  111. XCTAssertNil(RPCResponse);
  112. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeMissingAppCredential);
  113. }
  114. /** @fn testInvalidAppCredentialError
  115. @brief Tests that @c FIRAuthErrorCodeInvalidAppCredential error.
  116. */
  117. - (void)testInvalidAppCredentialError {
  118. FIRVerifyClientRequest *request =
  119. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  120. isSandbox:YES
  121. requestConfiguration:_requestConfiguration];
  122. __block BOOL callbackInvoked;
  123. __block FIRVerifyClientResponse *RPCResponse;
  124. __block NSError *RPCError;
  125. [FIRAuthBackend
  126. verifyClient:request
  127. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  128. RPCResponse = response;
  129. RPCError = error;
  130. callbackInvoked = YES;
  131. }];
  132. [_RPCIssuer respondWithServerErrorMessage:kInvalidAppCredentialErrorMessage];
  133. XCTAssert(callbackInvoked);
  134. XCTAssertNil(RPCResponse);
  135. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidAppCredential);
  136. }
  137. /** @fn testSuccessfulVerifyClientResponse
  138. @brief Tests a succesful attempt of the verify password flow.
  139. */
  140. - (void)testSuccessfulVerifyPasswordResponse {
  141. FIRVerifyClientRequest *request =
  142. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  143. isSandbox:YES
  144. requestConfiguration:_requestConfiguration];
  145. __block BOOL callbackInvoked;
  146. __block FIRVerifyClientResponse *RPCResponse;
  147. __block NSError *RPCError;
  148. [FIRAuthBackend
  149. verifyClient:request
  150. callback:^(FIRVerifyClientResponse *_Nullable response, NSError *_Nullable error) {
  151. RPCResponse = response;
  152. RPCError = error;
  153. callbackInvoked = YES;
  154. }];
  155. [_RPCIssuer
  156. respondWithJSON:@{kReceiptKey : kFakeReceipt, kSuggestedTimeOutKey : kFakeSuggestedTimeout}];
  157. XCTAssert(callbackInvoked);
  158. XCTAssertNotNil(RPCResponse);
  159. XCTAssertEqualObjects(RPCResponse.receipt, kFakeReceipt);
  160. NSTimeInterval suggestedTimeout = [RPCResponse.suggestedTimeOutDate timeIntervalSinceNow];
  161. XCTAssertEqualWithAccuracy(suggestedTimeout, [kFakeSuggestedTimeout doubleValue],
  162. kAllowedTimeDifference);
  163. }
  164. @end
  165. #endif