FIRSetAccountInfoRequestTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 kReturnSecureTokenKey
  144. @brief The key for the "returnSecureToken" value in the request.
  145. */
  146. static NSString *const kReturnSecureTokenKey = @"returnSecureToken";
  147. /** @var kExpectedAPIURL
  148. @brief The expected URL for test calls.
  149. */
  150. static NSString *const kExpectedAPIURL =
  151. @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=APIKey";
  152. /** @class FIRSetAccountInfoRequestTests
  153. @brief Tests for @c FIRSetAccountInfoRequest.
  154. */
  155. @interface FIRSetAccountInfoRequestTests : XCTestCase
  156. @end
  157. @implementation FIRSetAccountInfoRequestTests {
  158. /** @var _RPCIssuer
  159. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  160. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  161. */
  162. FIRFakeBackendRPCIssuer *_RPCIssuer;
  163. /** @var _requestConfiguration
  164. @brief This is the request configuration used for testing.
  165. */
  166. FIRAuthRequestConfiguration *_requestConfiguration;
  167. }
  168. - (void)setUp {
  169. [super setUp];
  170. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  171. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  172. _RPCIssuer = RPCIssuer;
  173. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  174. appID:kTestFirebaseAppID];
  175. }
  176. - (void)tearDown {
  177. _RPCIssuer = nil;
  178. _requestConfiguration = nil;
  179. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  180. [super tearDown];
  181. }
  182. /** @fn testSetAccountInfoRequest
  183. @brief Tests the set account info request.
  184. */
  185. - (void)testSetAccountInfoRequest {
  186. FIRSetAccountInfoRequest *request =
  187. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  188. request.returnSecureToken = NO;
  189. [FIRAuthBackend
  190. setAccountInfo:request
  191. callback:^(FIRSetAccountInfoResponse *_Nullable response, NSError *_Nullable error){
  192. }];
  193. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  194. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  195. XCTAssertNil(_RPCIssuer.decodedRequest[kIDTokenKey]);
  196. XCTAssertNil(_RPCIssuer.decodedRequest[kDisplayNameKey]);
  197. XCTAssertNil(_RPCIssuer.decodedRequest[kLocalIDKey]);
  198. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailKey]);
  199. XCTAssertNil(_RPCIssuer.decodedRequest[kPasswordKey]);
  200. XCTAssertNil(_RPCIssuer.decodedRequest[kPhotoURLKey]);
  201. XCTAssertNil(_RPCIssuer.decodedRequest[kProvidersKey]);
  202. XCTAssertNil(_RPCIssuer.decodedRequest[kOOBCodeKey]);
  203. XCTAssertNil(_RPCIssuer.decodedRequest[kEmailVerifiedKey]);
  204. XCTAssertNil(_RPCIssuer.decodedRequest[kUpgradeToFederatedLoginKey]);
  205. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaChallengeKey]);
  206. XCTAssertNil(_RPCIssuer.decodedRequest[kCaptchaResponseKey]);
  207. XCTAssertNil(_RPCIssuer.decodedRequest[kDeleteAttributesKey]);
  208. XCTAssertNil(_RPCIssuer.decodedRequest[kDeleteProvidersKey]);
  209. XCTAssertNil(_RPCIssuer.decodedRequest[kReturnSecureTokenKey]);
  210. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  211. }
  212. /** @fn testSetAccountInfoRequestOptionalFields
  213. @brief Tests the set account info request with optional fields.
  214. */
  215. - (void)testSetAccountInfoRequestOptionalFields {
  216. FIRSetAccountInfoRequest *request =
  217. [[FIRSetAccountInfoRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  218. request.accessToken = kTestAccessToken;
  219. request.displayName = kTestDisplayName;
  220. request.localID = kTestLocalID;
  221. request.email = ktestEmail;
  222. request.password = kTestPassword;
  223. request.providers = @[ kTestProviders ];
  224. request.OOBCode = kTestOOBCode;
  225. request.emailVerified = kTestEmailVerified;
  226. request.photoURL = [NSURL URLWithString:kTestPhotoURL];
  227. request.upgradeToFederatedLogin = kTestUpgradeToFederatedLogin;
  228. request.captchaChallenge = kTestCaptchaChallenge;
  229. request.captchaResponse = kTestCaptchaResponse;
  230. request.deleteAttributes = @[ kTestDeleteAttributes ];
  231. request.deleteProviders = @[ kTestDeleteProviders ];
  232. [FIRAuthBackend
  233. setAccountInfo:request
  234. callback:^(FIRSetAccountInfoResponse *_Nullable response, NSError *_Nullable error){
  235. }];
  236. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  237. XCTAssert([_RPCIssuer.decodedRequest isKindOfClass:[NSDictionary class]]);
  238. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDTokenKey], kTestAccessToken);
  239. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDisplayNameKey], kTestDisplayName);
  240. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kLocalIDKey], kTestLocalID);
  241. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kEmailKey], ktestEmail);
  242. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPasswordKey], kTestPassword);
  243. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kPhotoURLKey], kTestPhotoURL);
  244. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kProvidersKey], @[ kTestProviders ]);
  245. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kOOBCodeKey], kTestOOBCode);
  246. XCTAssert(_RPCIssuer.decodedRequest[kEmailVerifiedKey]);
  247. XCTAssert(_RPCIssuer.decodedRequest[kUpgradeToFederatedLoginKey]);
  248. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaChallengeKey], kTestCaptchaChallenge);
  249. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kCaptchaResponseKey], kTestCaptchaResponse);
  250. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDeleteAttributesKey],
  251. @[ kTestDeleteAttributes ]);
  252. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kDeleteProvidersKey], @[ kTestDeleteProviders ]);
  253. XCTAssertTrue([_RPCIssuer.decodedRequest[kReturnSecureTokenKey] boolValue]);
  254. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  255. }
  256. @end