FIRVerifyClientResponseTests.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 kEpsilon
  55. @brief Allowed difference when comparing floating point numbers.
  56. */
  57. static const double kEpsilon = 1e-3;
  58. /** @var kMissingAppCredentialErrorMessage
  59. @brief This is the error message the server will respond with if the APNS token is missing in a
  60. verifyClient request is missing.
  61. */
  62. static NSString *const kMissingAppCredentialErrorMessage = @"MISSING_APP_CREDENTIAL";
  63. /** @var kMissingAppCredentialErrorMessage
  64. @brief This is the error message the server will respond with if the APNS token is missing in a
  65. verifyClient request is invalid.
  66. */
  67. static NSString *const kInvalidAppCredentialErrorMessage = @"INVALID_APP_CREDENTIAL";
  68. @interface FIRVerifyClientResponseTests : XCTestCase
  69. @end
  70. @implementation FIRVerifyClientResponseTests{
  71. /** @var _RPCIssuer
  72. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  73. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  74. */
  75. FIRFakeBackendRPCIssuer *_RPCIssuer;
  76. }
  77. - (void)setUp {
  78. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  79. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  80. _RPCIssuer = RPCIssuer;
  81. }
  82. /** @fn testMissingAppCredentialError
  83. @brief Tests that @c FIRAuthErrorCodeMissingAppCredential error.
  84. */
  85. - (void)testMissingAppCredentialError {
  86. FIRVerifyClientRequest *request =
  87. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  88. isSandbox:YES
  89. APIKey:kFakeAPIKey];
  90. __block BOOL callbackInvoked;
  91. __block FIRVerifyClientResponse *RPCResponse;
  92. __block NSError *RPCError;
  93. [FIRAuthBackend verifyClient:request
  94. callback:^(FIRVerifyClientResponse *_Nullable response,
  95. NSError *_Nullable error) {
  96. RPCResponse = response;
  97. RPCError = error;
  98. callbackInvoked = YES;
  99. }];
  100. [_RPCIssuer respondWithServerErrorMessage:kMissingAppCredentialErrorMessage];
  101. XCTAssert(callbackInvoked);
  102. XCTAssertNil(RPCResponse);
  103. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeMissingAppCredential);
  104. }
  105. /** @fn testInvalidAppCredentialError
  106. @brief Tests that @c FIRAuthErrorCodeInvalidAppCredential error.
  107. */
  108. - (void)testInvalidAppCredentialError {
  109. FIRVerifyClientRequest *request =
  110. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  111. isSandbox:YES
  112. APIKey:kFakeAPIKey];
  113. __block BOOL callbackInvoked;
  114. __block FIRVerifyClientResponse *RPCResponse;
  115. __block NSError *RPCError;
  116. [FIRAuthBackend verifyClient:request
  117. callback:^(FIRVerifyClientResponse *_Nullable response,
  118. NSError *_Nullable error) {
  119. RPCResponse = response;
  120. RPCError = error;
  121. callbackInvoked = YES;
  122. }];
  123. [_RPCIssuer respondWithServerErrorMessage:kInvalidAppCredentialErrorMessage];
  124. XCTAssert(callbackInvoked);
  125. XCTAssertNil(RPCResponse);
  126. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidAppCredential);
  127. }
  128. /** @fn testSuccessfulVerifyClientResponse
  129. @brief Tests a succesful attempt of the verify password flow.
  130. */
  131. - (void)testSuccessfulVerifyPasswordResponse {
  132. FIRVerifyClientRequest *request =
  133. [[FIRVerifyClientRequest alloc] initWithAppToken:kFakeAppToken
  134. isSandbox:YES
  135. APIKey:kFakeAPIKey];
  136. __block BOOL callbackInvoked;
  137. __block FIRVerifyClientResponse *RPCResponse;
  138. __block NSError *RPCError;
  139. [FIRAuthBackend verifyClient:request
  140. callback:^(FIRVerifyClientResponse *_Nullable response,
  141. NSError *_Nullable error) {
  142. RPCResponse = response;
  143. RPCError = error;
  144. callbackInvoked = YES;
  145. }];
  146. [_RPCIssuer respondWithJSON:@{
  147. kReceiptKey : kFakeReceipt,
  148. kSuggestedTimeOutKey : kFakeSuggestedTimeout
  149. }];
  150. XCTAssert(callbackInvoked);
  151. XCTAssertNotNil(RPCResponse);
  152. XCTAssertEqualObjects(RPCResponse.receipt, kFakeReceipt);
  153. NSTimeInterval suggestedTimeout = [RPCResponse.suggestedTimeOutDate timeIntervalSinceNow];
  154. XCTAssertLessThanOrEqual(fabs(suggestedTimeout - [kFakeSuggestedTimeout doubleValue]), kEpsilon);
  155. }
  156. @end