FIRVerifyClientResponseTests.m 6.6 KB

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