FIRCreateAuthURIResponseTests.m 7.4 KB

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