FIRCreateAuthURIResponseTests.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "FIRCreateAuthURIRequest.h"
  20. #import "FIRCreateAuthURIResponse.h"
  21. #import "FIRFakeBackendRPCIssuer.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kAuthUriKey
  27. @brief The name of the "authURI" property in the json response.
  28. */
  29. static NSString *const kAuthUriKey = @"authUri";
  30. /** @var kTestAuthUri
  31. @brief The test value of the "authURI" property in the json response.
  32. */
  33. static NSString *const kTestAuthUri = @"AuthURI";
  34. /** @var kTestIdentifier
  35. @brief Fake identifier key used for testing.
  36. */
  37. static NSString *const kTestIdentifier = @"Identifier";
  38. /** @var kTestContinueURI
  39. @brief Fake Continue URI key used for testing.
  40. */
  41. static NSString *const kTestContinueURI = @"ContinueUri";
  42. /** @var kMissingContinueURIErrorMessage
  43. @brief The error returned by the server if continue Uri is invalid.
  44. */
  45. static NSString *const kMissingContinueURIErrorMessage = @"MISSING_CONTINUE_URI:";
  46. /** @var kInvalidEmailErrorMessage
  47. @brief The error returned by the server if the email is invalid.
  48. */
  49. static NSString *const kInvalidIdentifierErrorMessage = @"INVALID_IDENTIFIER :";
  50. /** @var kInvalidEmailErrorMessage
  51. @brief The error returned by the server if the email is invalid.
  52. */
  53. static NSString *const kInvalidEmailErrorMessage = @"INVALID_EMAIL:";
  54. /** @class CreateAuthURIResponseTests
  55. @brief Tests for @c FIRCreateAuthURIResponse.
  56. */
  57. @interface FIRCreateAuthURIResponseTests : XCTestCase
  58. @end
  59. @implementation FIRCreateAuthURIResponseTests{
  60. /** @var _RPCIssuer
  61. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  62. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  63. */
  64. FIRFakeBackendRPCIssuer *_RPCIssuer;
  65. }
  66. - (void)setUp {
  67. [super setUp];
  68. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  69. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  70. _RPCIssuer = RPCIssuer;
  71. }
  72. - (void)tearDown {
  73. _RPCIssuer = nil;
  74. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  75. [super tearDown];
  76. }
  77. /** @fn testMissingContinueURIError
  78. @brief This test checks for invalid continue URI in the response.
  79. */
  80. - (void)testMissingContinueURIError {
  81. FIRCreateAuthURIRequest *request =
  82. [[FIRCreateAuthURIRequest alloc]initWithIdentifier:kTestIdentifier
  83. continueURI:kTestContinueURI
  84. APIKey:kTestAPIKey];
  85. __block BOOL callbackInvoked;
  86. __block FIRCreateAuthURIResponse *RPCResponse;
  87. __block NSError *RPCError;
  88. [FIRAuthBackend createAuthURI:request
  89. callback:^(FIRCreateAuthURIResponse *_Nullable response,
  90. NSError *_Nullable error) {
  91. callbackInvoked = YES;
  92. RPCResponse = response;
  93. RPCError = error;
  94. }];
  95. [_RPCIssuer respondWithServerErrorMessage:kMissingContinueURIErrorMessage];
  96. XCTAssert(callbackInvoked);
  97. XCTAssertNotNil(RPCError);
  98. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  99. XCTAssertNil(RPCResponse);
  100. }
  101. /** @fn testInvalidIdentifierError
  102. @brief This test checks for the INVALID_IDENTIFIER error message from the backend.
  103. */
  104. - (void)testInvalidIdentifierError {
  105. FIRCreateAuthURIRequest *request =
  106. [[FIRCreateAuthURIRequest alloc]initWithIdentifier:kTestIdentifier
  107. continueURI:kTestContinueURI
  108. APIKey:kTestAPIKey];
  109. __block BOOL callbackInvoked;
  110. __block FIRCreateAuthURIResponse *RPCResponse;
  111. __block NSError *RPCError;
  112. [FIRAuthBackend createAuthURI:request
  113. callback:^(FIRCreateAuthURIResponse *_Nullable response,
  114. NSError *_Nullable error) {
  115. callbackInvoked = YES;
  116. RPCResponse = response;
  117. RPCError = error;
  118. }];
  119. [_RPCIssuer respondWithServerErrorMessage:kInvalidIdentifierErrorMessage];
  120. XCTAssert(callbackInvoked);
  121. XCTAssertNotNil(RPCError);
  122. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  123. XCTAssertNil(RPCResponse);
  124. }
  125. /** @fn testInvalidEmailError
  126. @brief This test checks for INVALID_EMAIL error message from the backend.
  127. */
  128. - (void)testInvalidEmailError {
  129. FIRCreateAuthURIRequest *request =
  130. [[FIRCreateAuthURIRequest alloc]initWithIdentifier:kTestIdentifier
  131. continueURI:kTestContinueURI
  132. APIKey:kTestAPIKey];
  133. __block BOOL callbackInvoked;
  134. __block FIRCreateAuthURIResponse *RPCResponse;
  135. __block NSError *RPCError;
  136. [FIRAuthBackend createAuthURI:request
  137. callback:^(FIRCreateAuthURIResponse *_Nullable response,
  138. NSError *_Nullable error) {
  139. callbackInvoked = YES;
  140. RPCResponse = response;
  141. RPCError = error;
  142. }];
  143. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  144. XCTAssert(callbackInvoked);
  145. XCTAssertNotNil(RPCError);
  146. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  147. XCTAssertNil(RPCResponse);
  148. }
  149. /** @fn testSuccessfulCreateAuthURI
  150. @brief This test checks for invalid email identifier error.
  151. */
  152. - (void)testSuccessfulCreateAuthURIResponse {
  153. FIRCreateAuthURIRequest *request =
  154. [[FIRCreateAuthURIRequest alloc]initWithIdentifier:kTestIdentifier
  155. continueURI:kTestContinueURI
  156. APIKey:kTestAPIKey];
  157. __block BOOL callbackInvoked;
  158. __block FIRCreateAuthURIResponse *RPCResponse;
  159. __block NSError *RPCError;
  160. [FIRAuthBackend createAuthURI:request
  161. callback:^(FIRCreateAuthURIResponse *_Nullable response,
  162. NSError *_Nullable error) {
  163. callbackInvoked = YES;
  164. RPCResponse = response;
  165. RPCError = error;
  166. }];
  167. [_RPCIssuer respondWithJSON:@{
  168. kAuthUriKey : kTestAuthUri
  169. }];
  170. XCTAssert(callbackInvoked);
  171. XCTAssertNil(RPCError);
  172. XCTAssertNotNil(RPCResponse);
  173. XCTAssertEqualObjects(RPCResponse.authURI, kTestAuthUri);
  174. }
  175. @end