FIRVerifyAssertionRequestTests.m 11 KB

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