FIRVerifyClientResponseTests.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 "FIRAuthErrors.h"
  18. #import "FIRAuthBackend.h"
  19. #import "FIRVerifyClientRequest.h"
  20. #import "FIRVerifyClientResponse.h"
  21. #import "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. }
  78. - (void)setUp {
  79. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  81. _RPCIssuer = RPCIssuer;
  82. }
  83. /** @fn testMissingAppCredentialError
  84. @brief Tests that @c FIRAuthErrorCodeMissingAppCredential error.
  85. */
  86. - (void)testMissingAppCredentialError {
  87. FIRVerifyClientRequest *request =
  88. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  89. isSandbox:YES
  90. APIKey:kFakeAPIKey];
  91. __block BOOL callbackInvoked;
  92. __block FIRVerifyClientResponse *RPCResponse;
  93. __block NSError *RPCError;
  94. [FIRAuthBackend verifyClient:request
  95. callback:^(FIRVerifyClientResponse *_Nullable response,
  96. NSError *_Nullable error) {
  97. RPCResponse = response;
  98. RPCError = error;
  99. callbackInvoked = YES;
  100. }];
  101. [_RPCIssuer respondWithServerErrorMessage:kMissingAppCredentialErrorMessage];
  102. XCTAssert(callbackInvoked);
  103. XCTAssertNil(RPCResponse);
  104. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeMissingAppCredential);
  105. }
  106. /** @fn testInvalidAppCredentialError
  107. @brief Tests that @c FIRAuthErrorCodeInvalidAppCredential error.
  108. */
  109. - (void)testInvalidAppCredentialError {
  110. FIRVerifyClientRequest *request =
  111. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  112. isSandbox:YES
  113. APIKey:kFakeAPIKey];
  114. __block BOOL callbackInvoked;
  115. __block FIRVerifyClientResponse *RPCResponse;
  116. __block NSError *RPCError;
  117. [FIRAuthBackend verifyClient:request
  118. callback:^(FIRVerifyClientResponse *_Nullable response,
  119. NSError *_Nullable error) {
  120. RPCResponse = response;
  121. RPCError = error;
  122. callbackInvoked = YES;
  123. }];
  124. [_RPCIssuer respondWithServerErrorMessage:kInvalidAppCredentialErrorMessage];
  125. XCTAssert(callbackInvoked);
  126. XCTAssertNil(RPCResponse);
  127. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidAppCredential);
  128. }
  129. /** @fn testSuccessfulVerifyClientResponse
  130. @brief Tests a succesful attempt of the verify password flow.
  131. */
  132. - (void)testSuccessfulVerifyPasswordResponse {
  133. FIRVerifyClientRequest *request =
  134. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  135. isSandbox:YES
  136. APIKey:kFakeAPIKey];
  137. __block BOOL callbackInvoked;
  138. __block FIRVerifyClientResponse *RPCResponse;
  139. __block NSError *RPCError;
  140. [FIRAuthBackend verifyClient:request
  141. callback:^(FIRVerifyClientResponse *_Nullable response,
  142. NSError *_Nullable error) {
  143. RPCResponse = response;
  144. RPCError = error;
  145. callbackInvoked = YES;
  146. }];
  147. [_RPCIssuer respondWithJSON:@{
  148. kReceiptKey : kFakeReceipt,
  149. kSuggestedTimeOutKey : kFakeSuggestedTimeout
  150. }];
  151. XCTAssert(callbackInvoked);
  152. XCTAssertNotNil(RPCResponse);
  153. XCTAssertEqualObjects(RPCResponse.receipt, kFakeReceipt);
  154. NSTimeInterval suggestedTimeout = [RPCResponse.suggestedTimeOutDate timeIntervalSinceNow];
  155. XCTAssertEqualWithAccuracy(suggestedTimeout, [kFakeSuggestedTimeout doubleValue],
  156. kAllowedTimeDifference);
  157. }
  158. @end