FIRSetAccountInfoRequestTests.m 12 KB

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