FIRSetAccountInfoRequestTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "FIRAuthBackend.h"
  18. #import "FIRAuthErrors.h"
  19. #import "FIRFakeBackendRPCIssuer.h"
  20. #import "FIRSetAccountInfoRequest.h"
  21. #import "FIRSetAccountInfoResponse.h"
  22. /** @var kTestAPIKey
  23. @brief Fake API key used for testing.
  24. */
  25. static NSString *const kTestAPIKey = @"APIKey";
  26. /** @var kIDTokenKey
  27. @brief The key for the "idToken" value in the request. This is actually the STS Access Token,
  28. despite it's confusing (backwards compatiable) parameter name.
  29. */
  30. static NSString *const kIDTokenKey = @"idToken";
  31. /** @var kTestAccessToken
  32. @bried Fake acess token for testing.
  33. */
  34. static NSString *const kTestAccessToken = @"accessToken";
  35. /** @var kDisplayNameKey
  36. @brief The key for the "displayName" value in the request.
  37. */
  38. static NSString *const kDisplayNameKey = @"displayName";
  39. /** @var kTestDisplayName
  40. @brief The fake @c displayName for testing.
  41. */
  42. static NSString *const kTestDisplayName = @"testDisplayName";
  43. /** @var kLocalIDKey
  44. @brief The key for the "localID" value in the request.
  45. */
  46. static NSString *const kLocalIDKey = @"localId";
  47. /** @var kTestLocalID
  48. @brief The fake @c localID for testing in the request.
  49. */
  50. static NSString *const kTestLocalID = @"testLocalId";
  51. /** @var kEmailKey
  52. @brief The key for the "email" value in the request.
  53. */
  54. static NSString *const kEmailKey = @"email";
  55. /** @var kTestEmail
  56. @brief The fake @c email used for testing in the request.
  57. */
  58. static NSString *const ktestEmail = @"testEmail";
  59. /** @var kPasswordKey
  60. @brief The key for the "password" value in the request.
  61. */
  62. static NSString *const kPasswordKey = @"password";
  63. /** @var kTestPassword
  64. @brief The fake @c password used for testing in the request.
  65. */
  66. static NSString *const kTestPassword = @"testPassword";
  67. /** @var kPhotoURLKey
  68. @brief The key for the "photoURL" value in the request.
  69. */
  70. static NSString *const kPhotoURLKey = @"photoUrl";
  71. /** @var kTestPhotoURL
  72. @brief The fake photoUrl for testing in the request.
  73. */
  74. static NSString *const kTestPhotoURL = @"testPhotoUrl";
  75. /** @var kProvidersKey
  76. @brief The key for the "providers" value in the request.
  77. */
  78. static NSString *const kProvidersKey = @"provider";
  79. /** @var kTestProviders
  80. @brief The fake @c providers value used for testing in the request.
  81. */
  82. static NSString *const kTestProviders = @"testProvider";
  83. /** @var kOOBCodeKey
  84. @brief The key for the "OOBCode" value in the request.
  85. */
  86. static NSString *const kOOBCodeKey = @"oobCode";
  87. /** @var kTestOOBCode
  88. @brief The fake @c OOBCode used for testing the request.
  89. */
  90. static NSString *const kTestOOBCode = @"testOobCode";
  91. /** @var kEmailVerifiedKey
  92. @brief The key for the "emailVerified" value in the request.
  93. */
  94. static NSString *const kEmailVerifiedKey = @"emailVerified";
  95. /** @var kTestEmailVerified
  96. @brief The fake @c emailVerified value used for testing the request.
  97. */
  98. static const BOOL kTestEmailVerified = YES;
  99. /** @var kUpgradeToFederatedLoginKey
  100. @brief The key for the "upgradeToFederatedLogin" value in the request.
  101. */
  102. static NSString *const kUpgradeToFederatedLoginKey = @"upgradeToFederatedLogin";
  103. /** @var kTestUpgradeToFederatedLogin
  104. @brief The fake @c upgradeToFederatedLogin value for testing the request.
  105. */
  106. static const BOOL kTestUpgradeToFederatedLogin = YES;
  107. /** @var kCaptchaChallengeKey
  108. @brief The key for the "captchaChallenge" value in the request.
  109. */
  110. static NSString *const kCaptchaChallengeKey = @"captchaChallenge";
  111. /** @var kTestCaptchaChallenge
  112. @brief The fake @c captchaChallenge for testing in the request.
  113. */
  114. static NSString *const kTestCaptchaChallenge = @"TestCaptchaChallenge";
  115. /** @var kCaptchaResponseKey
  116. @brief The key for the "captchaResponse" value the request.
  117. */
  118. static NSString *const kCaptchaResponseKey = @"captchaResponse";
  119. /** @var kTestCaptchaResponse
  120. @brief The fake @c captchaResponse for testing the request.
  121. */
  122. static NSString *const kTestCaptchaResponse = @"TestCaptchaResponse";
  123. /** @var kDeleteAttributesKey
  124. @brief The key for the "deleteAttribute" value in the request.
  125. */
  126. static NSString *const kDeleteAttributesKey = @"deleteAttribute";
  127. /** @var kTestDeleteAttributes
  128. @brief The fake @c deleteAttribute value for testing the request.
  129. */
  130. static NSString *const kTestDeleteAttributes = @"TestDeleteAttributes";
  131. /** @var kDeleteProvidersKey
  132. @brief The key for the "deleteProvider" value in the request.
  133. */
  134. static NSString *const kDeleteProvidersKey = @"deleteProvider";
  135. /** @var kTestDeleteProviders
  136. @brief The fake @c deleteProviders for testing the request.
  137. */
  138. static NSString *const kTestDeleteProviders = @"TestDeleteProviders";
  139. /** @var kReturnSecureTokenKey
  140. @brief The key for the "returnSecureToken" value in the request.
  141. */
  142. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  143. /** @var kExpectedAPIURL
  144. @brief The expected URL for test calls.
  145. */
  146. static NSString *const kExpectedAPIURL =
  147. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=APIKey";
  148. /** @class FIRSetAccountInfoRequestTests
  149. @brief Tests for @c FIRSetAccountInfoRequest.
  150. */
  151. @interface FIRSetAccountInfoRequestTests : XCTestCase
  152. @end
  153. @implementation FIRSetAccountInfoRequestTests {
  154. /** @var _RPCIssuer
  155. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  156. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  157. */
  158. FIRFakeBackendRPCIssuer *_RPCIssuer;
  159. /** @var _requestConfiguration
  160. @brief This is the request configuration used for testing.
  161. */
  162. FIRAuthRequestConfiguration *_requestConfiguration;
  163. }
  164. - (void)setUp {
  165. [super setUp];
  166. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  167. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  168. _RPCIssuer = RPCIssuer;
  169. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  170. }
  171. - (void)tearDown {
  172. _RPCIssuer = nil;
  173. _requestConfiguration = nil;
  174. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  175. [super tearDown];
  176. }
  177. /** @fn testSetAccountInfoRequest
  178. @brief Tests the set account info request.
  179. */
  180. - (void)testSetAccountInfoRequest {
  181. FIRSetAccountInfoRequest *request =
  182. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  183. request.returnSecureToken = NO;
  184. [FIRAuthBackend
  185. setAccountInfo:request
  186. callback:^(FIRSetAccountInfoResponse *_Nullable response, NSError *_Nullable error){
  187. }];
  188. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  189. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  190. XCTAssertNil(_RPCIssuer.decodedRequest[kIDTokenKey]);
  191. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  192. XCTAssertNil(_RPCIssuer.decodedRequest[kLocalIDKey]);
  193. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  194. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  195. XCTAssertNil(_RPCIssuer.decodedRequest[kPhotoURLKey]);
  196. XCTAssertNil(_RPCIssuer.decodedRequest[kProvidersKey]);
  197. XCTAssertNil(_RPCIssuer.decodedRequest[kOOBCodeKey]);
  198. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailVerifiedKey]);
  199. XCTAssertNil(_RPCIssuer.decodedRequest[kUpgradeToFederatedLoginKey]);
  200. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaChallengeKey]);
  201. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaResponseKey]);
  202. XCTAssertNil(_RPCIssuer.decodedRequest[kDeleteAttributesKey]);
  203. XCTAssertNil(_RPCIssuer.decodedRequest[kDeleteProvidersKey]);
  204. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  205. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  206. }
  207. /** @fn testSetAccountInfoRequestOptionalFields
  208. @brief Tests the set account info request with optional fields.
  209. */
  210. - (void)testSetAccountInfoRequestOptionalFields {
  211. FIRSetAccountInfoRequest *request =
  212. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  213. request.accessToken = kTestAccessToken;
  214. request.displayName = kTestDisplayName;
  215. request.localID = kTestLocalID;
  216. request.email = ktestEmail;
  217. request.password = kTestPassword;
  218. request.providers = @[ kTestProviders ];
  219. request.OOBCode = kTestOOBCode;
  220. request.emailVerified = kTestEmailVerified;
  221. request.photoURL = [NSURL URLWithString:kTestPhotoURL];
  222. request.upgradeToFederatedLogin = kTestUpgradeToFederatedLogin;
  223. request.captchaChallenge = kTestCaptchaChallenge;
  224. request.captchaResponse = kTestCaptchaResponse;
  225. request.deleteAttributes = @[ kTestDeleteAttributes ];
  226. request.deleteProviders = @[ kTestDeleteProviders ];
  227. [FIRAuthBackend
  228. setAccountInfo:request
  229. callback:^(FIRSetAccountInfoResponse *_Nullable response, NSError *_Nullable error){
  230. }];
  231. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  232. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  233. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDTokenKey], kTestAccessToken);
  234. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  235. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kLocalIDKey], kTestLocalID);
  236. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], ktestEmail);
  237. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  238. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPhotoURLKey], kTestPhotoURL);
  239. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kProvidersKey], @[ kTestProviders ]);
  240. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kOOBCodeKey], kTestOOBCode);
  241. XCTAssert(_RPCIssuer.decodedRequest[kEmailVerifiedKey]);
  242. XCTAssert(_RPCIssuer.decodedRequest[kUpgradeToFederatedLoginKey]);
  243. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaChallengeKey], kTestCaptchaChallenge);
  244. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  245. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDeleteAttributesKey],
  246. @[ kTestDeleteAttributes ]);
  247. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDeleteProvidersKey], @[ kTestDeleteProviders ]);
  248. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  249. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  250. }
  251. @end