FIRVerifyAssertionRequestTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "FIRGetOOBConfirmationCodeResponse.h"
  20. #import "FIRVerifyAssertionRequest.h"
  21. #import "FIRVerifyAssertionResponse.h"
  22. #import "FIRFakeBackendRPCIssuer.h"
  23. #import <GoogleToolboxForMac/GTMNSDictionary+URLArguments.h>
  24. /** @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /** @var kTestPostBodyKey
  29. @brief The name of the "postBody" property in the response.
  30. */
  31. static NSString *const kPostBodyKey = @"postBody";
  32. /** @var kExpectedAPIURL
  33. @brief The expected URL for test calls.
  34. */
  35. static NSString *const kExpectedAPIURL =
  36. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=APIKey";
  37. /** @var kIDTokenKey
  38. @brief The name of the "idToken" property in the response.
  39. */
  40. static NSString *const kIDTokenKey = @"idToken";
  41. /** @var kTestAccessToken
  42. @brief Fake access token used for testing.
  43. */
  44. static NSString *const kTestAccessToken = @"ACCESS_TOKEN";
  45. /** @var kProviderIDKey
  46. @brief The key for the "providerId" value in the request.
  47. */
  48. static NSString *const kProviderIDKey = @"providerId";
  49. /** @var kTestProviderID
  50. @brief Fake provider ID used for testing.
  51. */
  52. static NSString *const kTestProviderID = @"ProviderID";
  53. /** @var kProviderIDTokenKey
  54. @brief The key for the "id_token" value in the request.
  55. */
  56. static NSString *const kProviderIDTokenKey = @"id_token";
  57. /** @var kTestProviderIDToken
  58. @brief Fake provider ID token used for testing.
  59. */
  60. static NSString *const kTestProviderIDToken = @"ProviderIDToken";
  61. /** @var kInputEmailKey
  62. @brief The key for the "inputEmail" value in the request.
  63. */
  64. static NSString *const kInputEmailKey = @"identifier";
  65. /** @var kTestInputEmail
  66. @brief Fake input email used for testing.
  67. */
  68. static NSString *const kTestInputEmail = @"testInputEmail";
  69. /** @var kPendingIDTokenKey
  70. @brief The key for the "pendingIdToken" value in the request.
  71. */
  72. static NSString *const kPendingIDTokenKey = @"pendingIdToken";
  73. /** @var kTestPendingToken
  74. @brief Fake pending token used for testing.
  75. */
  76. static NSString *const kTestPendingToken = @"testPendingToken";
  77. /** @var kProviderAccessTokenKey
  78. @brief The key for the "access_token" value in the request.
  79. */
  80. static NSString *const kProviderAccessTokenKey = @"access_token";
  81. /** @var kTestProviderAccessToken
  82. @brief Fake @c providerAccessToken used for testing the request.
  83. */
  84. static NSString *const kTestProviderAccessToken = @"testProviderAccessToken";
  85. /** @var kProviderOAuthTokenSecretKey
  86. @brief The key for the "oauth_token_secret" value in the request.
  87. */
  88. static NSString *const kProviderOAuthTokenSecretKey = @"oauth_token_secret";
  89. /** @var kTestProviderOAuthTokenSecret
  90. @brief Fake @c providerOAuthTokenSecret used for testing the request.
  91. */
  92. static NSString *const kTestProviderOAuthTokenSecret = @"testProviderOAuthTokenSecret";
  93. /** @var kReturnSecureTokenKey
  94. @brief The key for the "returnSecureToken" value in the request.
  95. */
  96. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  97. /** @var kAutoCreateKey
  98. @brief The key for the "auto-create" value in the request.
  99. */
  100. static NSString *const kAutoCreateKey = @"autoCreate";
  101. /** @class FIRVerifyAssertionRequestTests
  102. @brief Tests for @c FIRVerifyAssertionReuqest
  103. */
  104. @interface FIRVerifyAssertionRequestTests : XCTestCase
  105. @end
  106. @implementation FIRVerifyAssertionRequestTests{
  107. /** @var _RPCIssuer
  108. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  109. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  110. */
  111. FIRFakeBackendRPCIssuer *_RPCIssuer;
  112. /** @var _requestConfiguration
  113. @brief This is the request configuration used for testing.
  114. */
  115. FIRAuthRequestConfiguration *_requestConfiguration;
  116. }
  117. - (void)setUp {
  118. [super setUp];
  119. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  120. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  121. _RPCIssuer = RPCIssuer;
  122. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  123. }
  124. - (void)tearDown {
  125. _RPCIssuer = nil;
  126. _requestConfiguration = nil;
  127. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  128. [super tearDown];
  129. }
  130. /** @fn testVerifyAssertionRequestMissingTokens
  131. @brief Tests the request with missing @c providerAccessToken and @c provideIDToken.
  132. @remarks The request creation will raise an @c NSInvalidArgumentException exception when both
  133. these tokens are missing.
  134. */
  135. - (void)testVerifyAssertionRequestMissingTokens {
  136. FIRVerifyAssertionRequest *request =
  137. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  138. requestConfiguration:_requestConfiguration];
  139. FIRVerifyAssertionResponseCallback callback =
  140. ^(FIRVerifyAssertionResponse *_Nullable response, NSError *_Nullable error) {};
  141. void (^verifyAssertionBlock)(void) = ^{
  142. [FIRAuthBackend verifyAssertion:request callback:callback];
  143. };
  144. XCTAssertThrowsSpecificNamed(verifyAssertionBlock(), NSException, NSInvalidArgumentException,
  145. @"Either IDToken or accessToken must be supplied.");
  146. XCTAssertNil(_RPCIssuer.decodedRequest[kPostBodyKey]);
  147. }
  148. /** @fn testVerifyAssertionRequestProviderAccessToken
  149. @brief Tests the verify assertion request with the @c providerAccessToken field set.
  150. @remarks The presence of the @c providerAccessToken will prevent an @c
  151. NSInvalidArgumentException exception from being raised.
  152. */
  153. - (void)testVerifyAssertionRequestProviderAccessToken {
  154. FIRVerifyAssertionRequest *request =
  155. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  156. requestConfiguration:_requestConfiguration];
  157. request.providerAccessToken = kTestProviderAccessToken;
  158. request.returnSecureToken = NO;
  159. [FIRAuthBackend verifyAssertion:request
  160. callback:^(FIRVerifyAssertionResponse *_Nullable response,
  161. NSError *_Nullable error) {
  162. }];
  163. NSDictionary *postBody = @{
  164. kProviderIDKey : kTestProviderID,
  165. kProviderAccessTokenKey : kTestProviderAccessToken
  166. };
  167. NSString *postBodyArgs = [postBody gtm_httpArgumentsString];
  168. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  169. XCTAssertNotNil(_RPCIssuer.decodedRequest[kPostBodyKey]);
  170. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPostBodyKey], postBodyArgs);
  171. XCTAssertNil(_RPCIssuer.decodedRequest[kIDTokenKey]);
  172. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  173. // Auto-create flag Should be true by default.
  174. XCTAssertTrue([_RPCIssuer.decodedRequest[kAutoCreateKey] boolValue]);
  175. }
  176. /** @fn testVerifyAssertionRequestOptionalFields
  177. @brief Tests the verify assertion request with all optinal fields set.
  178. */
  179. - (void)testVerifyAssertionRequestOptionalFields {
  180. FIRVerifyAssertionRequest *request =
  181. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  182. requestConfiguration:_requestConfiguration];
  183. request.providerIDToken = kTestProviderIDToken;
  184. request.providerAccessToken = kTestProviderAccessToken;
  185. request.accessToken = kTestAccessToken;
  186. request.inputEmail = kTestInputEmail;
  187. request.pendingIDToken = kTestPendingToken;
  188. request.providerOAuthTokenSecret = kTestProviderOAuthTokenSecret;
  189. request.autoCreate = NO;
  190. [FIRAuthBackend verifyAssertion:request
  191. callback:^(FIRVerifyAssertionResponse *_Nullable response,
  192. NSError *_Nullable error) {
  193. }];
  194. NSDictionary *postBody = @{
  195. kProviderIDKey : kTestProviderID,
  196. kProviderIDTokenKey : kTestProviderIDToken,
  197. kProviderAccessTokenKey : kTestProviderAccessToken,
  198. kProviderOAuthTokenSecretKey : kTestProviderOAuthTokenSecret,
  199. kInputEmailKey : kTestInputEmail
  200. };
  201. NSString *postBodyArgs = [postBody gtm_httpArgumentsString];
  202. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  203. XCTAssertNotNil(_RPCIssuer.decodedRequest[kPostBodyKey]);
  204. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPostBodyKey], postBodyArgs);
  205. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDTokenKey], kTestAccessToken);
  206. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  207. XCTAssertFalse([_RPCIssuer.decodedRequest[kAutoCreateKey] boolValue]);
  208. }
  209. @end