FIRCreateAuthURIResponseTests.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRCreateAuthURIResponse.h"
  21. #import "FirebaseAuth/Tests/Unit/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. /** @var _requestConfiguration
  66. @brief This is the request configuration used for testing.
  67. */
  68. FIRAuthRequestConfiguration *_requestConfiguration;
  69. }
  70. - (void)setUp {
  71. [super setUp];
  72. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  73. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  74. _RPCIssuer = RPCIssuer;
  75. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  76. }
  77. - (void)tearDown {
  78. _requestConfiguration = nil;
  79. _RPCIssuer = nil;
  80. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  81. [super tearDown];
  82. }
  83. /** @fn testMissingContinueURIError
  84. @brief This test checks for invalid continue URI in the response.
  85. */
  86. - (void)testMissingContinueURIError {
  87. FIRCreateAuthURIRequest *request =
  88. [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
  89. continueURI:kTestContinueURI
  90. requestConfiguration:_requestConfiguration];
  91. __block BOOL callbackInvoked;
  92. __block FIRCreateAuthURIResponse *RPCResponse;
  93. __block NSError *RPCError;
  94. [FIRAuthBackend
  95. createAuthURI:request
  96. callback:^(FIRCreateAuthURIResponse *_Nullable response, NSError *_Nullable error) {
  97. callbackInvoked = YES;
  98. RPCResponse = response;
  99. RPCError = error;
  100. }];
  101. [_RPCIssuer respondWithServerErrorMessage:kMissingContinueURIErrorMessage];
  102. XCTAssert(callbackInvoked);
  103. XCTAssertNotNil(RPCError);
  104. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeMissingContinueURI);
  105. XCTAssertNil(RPCResponse);
  106. }
  107. /** @fn testInvalidIdentifierError
  108. @brief This test checks for the INVALID_IDENTIFIER error message from the backend.
  109. */
  110. - (void)testInvalidIdentifierError {
  111. FIRCreateAuthURIRequest *request =
  112. [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
  113. continueURI:kTestContinueURI
  114. requestConfiguration:_requestConfiguration];
  115. __block BOOL callbackInvoked;
  116. __block FIRCreateAuthURIResponse *RPCResponse;
  117. __block NSError *RPCError;
  118. [FIRAuthBackend
  119. createAuthURI:request
  120. callback:^(FIRCreateAuthURIResponse *_Nullable response, NSError *_Nullable error) {
  121. callbackInvoked = YES;
  122. RPCResponse = response;
  123. RPCError = error;
  124. }];
  125. [_RPCIssuer respondWithServerErrorMessage:kInvalidIdentifierErrorMessage];
  126. XCTAssert(callbackInvoked);
  127. XCTAssertNotNil(RPCError);
  128. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  129. XCTAssertNil(RPCResponse);
  130. }
  131. /** @fn testInvalidEmailError
  132. @brief This test checks for INVALID_EMAIL error message from the backend.
  133. */
  134. - (void)testInvalidEmailError {
  135. FIRCreateAuthURIRequest *request =
  136. [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
  137. continueURI:kTestContinueURI
  138. requestConfiguration:_requestConfiguration];
  139. __block BOOL callbackInvoked;
  140. __block FIRCreateAuthURIResponse *RPCResponse;
  141. __block NSError *RPCError;
  142. [FIRAuthBackend
  143. createAuthURI:request
  144. callback:^(FIRCreateAuthURIResponse *_Nullable response, NSError *_Nullable error) {
  145. callbackInvoked = YES;
  146. RPCResponse = response;
  147. RPCError = error;
  148. }];
  149. [_RPCIssuer respondWithServerErrorMessage:kInvalidEmailErrorMessage];
  150. XCTAssert(callbackInvoked);
  151. XCTAssertNotNil(RPCError);
  152. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidEmail);
  153. XCTAssertNil(RPCResponse);
  154. }
  155. /** @fn testSuccessfulCreateAuthURI
  156. @brief This test checks for invalid email identifier error.
  157. */
  158. - (void)testSuccessfulCreateAuthURIResponse {
  159. FIRCreateAuthURIRequest *request =
  160. [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
  161. continueURI:kTestContinueURI
  162. requestConfiguration:_requestConfiguration];
  163. __block BOOL callbackInvoked;
  164. __block FIRCreateAuthURIResponse *RPCResponse;
  165. __block NSError *RPCError;
  166. [FIRAuthBackend
  167. createAuthURI:request
  168. callback:^(FIRCreateAuthURIResponse *_Nullable response, NSError *_Nullable error) {
  169. callbackInvoked = YES;
  170. RPCResponse = response;
  171. RPCError = error;
  172. }];
  173. [_RPCIssuer respondWithJSON:@{kAuthUriKey : kTestAuthUri}];
  174. XCTAssert(callbackInvoked);
  175. XCTAssertNil(RPCError);
  176. XCTAssertNotNil(RPCResponse);
  177. XCTAssertEqualObjects(RPCResponse.authURI, kTestAuthUri);
  178. }
  179. @end