FIRGetOOBConfirmationCodeRequestTests.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "FIRActionCodeSettings.h"
  18. #import "FIRAuthErrors.h"
  19. #import "FIRAuthBackend.h"
  20. #import "FIRGetOOBConfirmationCodeRequest.h"
  21. #import "FIRGetOOBConfirmationCodeResponse.h"
  22. #import "FIRFakeBackendRPCIssuer.h"
  23. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kExpectedAPIURL
  28. @brief The expected URL for the test calls.
  29. */
  30. static NSString *const kExpectedAPIURL =
  31. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=APIKey";
  32. /** @var kRequestTypeKey
  33. @brief The name of the required "requestType" property in the request.
  34. */
  35. static NSString *const kRequestTypeKey = @"requestType";
  36. /** @var kPasswordResetRequestTypeValue
  37. @brief The value for the "PASSWORD_RESET" request type.
  38. */
  39. static NSString *const kPasswordResetRequestTypeValue = @"PASSWORD_RESET";
  40. /** @var kVerifyEmailRequestTypeValue
  41. @brief The value for the "VERIFY_EMAIL" request type.
  42. */
  43. static NSString *const kVerifyEmailRequestTypeValue = @"VERIFY_EMAIL";
  44. /** @var kEmailKey
  45. @brief The name of the "email" property in the request.
  46. */
  47. static NSString *const kEmailKey = @"email";
  48. /** @var kTestEmail
  49. @brief Testing user email adadress.
  50. */
  51. static NSString *const kTestEmail = @"test@gmail.com";
  52. /** @var kAccessTokenKey
  53. @brief The name of the "accessToken" property in the request.
  54. */
  55. static NSString *const kAccessTokenKey = @"idToken";
  56. /** @var kTestAccessToken
  57. @brief Testing access token.
  58. */
  59. static NSString *const kTestAccessToken = @"ACCESS_TOKEN";
  60. /** @var kIosBundleID
  61. @brief Fake iOS bundle ID for testing.
  62. */
  63. static NSString *const kIosBundleID = @"testBundleID";
  64. /** @var kAndroidPackageName
  65. @brief Fake android package name for testing.
  66. */
  67. static NSString *const kAndroidPackageName = @"adroidpackagename";
  68. /** @var kContinueURL
  69. @brief Fake string value of continue url.
  70. */
  71. static NSString *const kContinueURL = @"continueURL";
  72. /** @var kAndroidMinimumVersion
  73. @brief Fake android minimum version for testing.
  74. */
  75. static NSString *const kAndroidMinimumVersion = @"3.0";
  76. /** @var kContinueURLKey
  77. @brief The key for the "continue URL" value in the request.
  78. */
  79. static NSString *const kContinueURLKey = @"continueUrl";
  80. /** @var kIosBundeIDKey
  81. @brief The key for the "iOS Bundle Identifier" value in the request.
  82. */
  83. static NSString *const kIosBundleIDKey = @"iOSBundleId";
  84. /** @var kAndroidPackageNameKey
  85. @brief The key for the "Android Package Name" value in the request.
  86. */
  87. static NSString *const kAndroidPackageNameKey = @"androidPackageName";
  88. /** @var kAndroidInstallAppKey
  89. @brief The key for the request parameter indicating whether the android app should be installed
  90. or not.
  91. */
  92. static NSString *const kAndroidInstallAppKey = @"androidInstallApp";
  93. /** @var kAndroidMinimumVersionKey
  94. @brief The key for the "minimum Android version supported" value in the request.
  95. */
  96. static NSString *const kAndroidMinimumVersionKey = @"androidMinimumVersion";
  97. /** @var kCanHandleCodeInAppKey
  98. @brief The key for the request parameter indicating whether the action code can be handled in
  99. the app or not.
  100. */
  101. static NSString *const kCanHandleCodeInAppKey = @"canHandleCodeInApp";
  102. /** @class FIRGetOOBConfirmationCodeRequestTests
  103. @brief Tests for @c FIRGetOOBConfirmationCodeRequest.
  104. */
  105. @interface FIRGetOOBConfirmationCodeRequestTests : XCTestCase
  106. @end
  107. @implementation FIRGetOOBConfirmationCodeRequestTests {
  108. /** @var _RPCIssuer
  109. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  110. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  111. */
  112. FIRFakeBackendRPCIssuer *_RPCIssuer;
  113. /** @var _requestConfiguration
  114. @brief This is the request configuration used for testing.
  115. */
  116. FIRAuthRequestConfiguration *_requestConfiguration;
  117. }
  118. - (void)setUp {
  119. [super setUp];
  120. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  121. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  122. _RPCIssuer = RPCIssuer;
  123. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  124. }
  125. - (void)tearDown {
  126. _requestConfiguration = nil;
  127. _RPCIssuer = nil;
  128. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  129. [super tearDown];
  130. }
  131. /** @fn testPasswordResetRequest
  132. @brief Tests the encoding of a password reset request.
  133. */
  134. - (void)testPasswordResetRequest {
  135. FIRGetOOBConfirmationCodeRequest *request =
  136. [FIRGetOOBConfirmationCodeRequest passwordResetRequestWithEmail:kTestEmail
  137. actionCodeSettings:[self fakeActionCodeSettings]
  138. requestConfiguration:_requestConfiguration];
  139. __block BOOL callbackInvoked;
  140. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  141. __block NSError *RPCError;
  142. [FIRAuthBackend getOOBConfirmationCode:request
  143. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  144. NSError *_Nullable error) {
  145. callbackInvoked = YES;
  146. RPCResponse = response;
  147. RPCError = error;
  148. }];
  149. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  150. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  151. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  152. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], kTestEmail);
  153. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRequestTypeKey], kPasswordResetRequestTypeValue);
  154. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kContinueURLKey], kContinueURL);
  155. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIosBundleIDKey], kIosBundleID);
  156. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidPackageNameKey], kAndroidPackageName);
  157. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidMinimumVersionKey],
  158. kAndroidMinimumVersion);
  159. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidInstallAppKey],
  160. [NSNumber numberWithBool:YES]);
  161. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCanHandleCodeInAppKey],
  162. [NSNumber numberWithBool:YES]);
  163. }
  164. /** @fn testEmailVerificationRequest
  165. @brief Tests the encoding of an email verification request.
  166. */
  167. - (void)testEmailVerificationRequest {
  168. FIRActionCodeSettings *testSettings = [self fakeActionCodeSettings];
  169. FIRGetOOBConfirmationCodeRequest *request =
  170. [FIRGetOOBConfirmationCodeRequest verifyEmailRequestWithAccessToken:kTestAccessToken
  171. actionCodeSettings:testSettings
  172. requestConfiguration:_requestConfiguration];
  173. __block BOOL callbackInvoked;
  174. __block FIRGetOOBConfirmationCodeResponse *RPCResponse;
  175. __block NSError *RPCError;
  176. [FIRAuthBackend getOOBConfirmationCode:request
  177. callback:^(FIRGetOOBConfirmationCodeResponse *_Nullable response,
  178. NSError *_Nullable error) {
  179. callbackInvoked = YES;
  180. RPCResponse = response;
  181. RPCError = error;
  182. }];
  183. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  184. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  185. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  186. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAccessTokenKey], kTestAccessToken);
  187. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kRequestTypeKey], kVerifyEmailRequestTypeValue);
  188. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kContinueURLKey], kContinueURL);
  189. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIosBundleIDKey], kIosBundleID);
  190. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidPackageNameKey], kAndroidPackageName);
  191. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidMinimumVersionKey],
  192. kAndroidMinimumVersion);
  193. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAndroidInstallAppKey],
  194. [NSNumber numberWithBool:YES]);
  195. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCanHandleCodeInAppKey],
  196. [NSNumber numberWithBool:YES]);
  197. }
  198. #pragma mark - Helpers
  199. /** @fn fakeActionCodeSettings
  200. @brief Constructs and returns a fake instance of @c FIRActionCodeSettings for testing.
  201. @return An instance of @c FIRActionCodeSettings for testing.
  202. */
  203. - (FIRActionCodeSettings *)fakeActionCodeSettings {
  204. FIRActionCodeSettings *actionCodeSettings = [[FIRActionCodeSettings alloc]init];
  205. [actionCodeSettings setIOSBundleID:kIosBundleID];
  206. [actionCodeSettings setAndroidPackageName:kAndroidPackageName
  207. installIfNotAvailable:YES
  208. minimumVersion:kAndroidMinimumVersion];
  209. actionCodeSettings.handleCodeInApp = YES;
  210. actionCodeSettings.URL = [NSURL URLWithString:kContinueURL];
  211. return actionCodeSettings;
  212. }
  213. @end