FIRVerifyAssertionResponseTests.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kProviderIDKey
  28. @brief The name of the "providerId" property in the response.
  29. */
  30. static NSString *const kProviderIDKey = @"providerId";
  31. /** @var kIDTokenKey
  32. @brief The name of the "IDToken" property in the response.
  33. */
  34. static NSString *const kIDTokenKey = @"idToken";
  35. /** @var kExpiresInKey
  36. @brief The name of the "expiresIn" property in the response.
  37. */
  38. static NSString *const kExpiresInKey = @"expiresIn";
  39. /** @var kRefreshTokenKey
  40. @brief The name of the "refreshToken" property in the response.
  41. */
  42. static NSString *const kRefreshTokenKey = @"refreshToken";
  43. /** @var kVerifiedProviderKey
  44. @brief The name of the "VerifiedProvider" property in the response.
  45. */
  46. static NSString *const kVerifiedProviderKey = @"verifiedProvider";
  47. /** @var kRawUserInfoKey
  48. @brief The name of the "rawUserInfo" property in the response.
  49. */
  50. static NSString *const kRawUserInfoKey = @"rawUserInfo";
  51. /** @var kUsernameKey
  52. @brief The name of the "username" property in the response.
  53. */
  54. static NSString *const kUsernameKey = @"username";
  55. /** @var kIsNewUserKey
  56. @brief The name of the "isNewUser" property in the response.
  57. */
  58. static NSString *const kIsNewUserKey = @"isNewUser";
  59. /** @var kTestProviderID
  60. @brief Fake provider ID used for testing.
  61. */
  62. static NSString *const kTestProviderID = @"ProviderID";
  63. /** @var kTestProviderIDToken
  64. @brief Fake provider ID token used for testing.
  65. */
  66. static NSString *const kTestProviderIDToken = @"ProviderIDToken";
  67. /** @var kTestIDToken
  68. @brief Testing ID token for verifying assertion.
  69. */
  70. static NSString *const kTestIDToken = @"ID_TOKEN";
  71. /** @var kTestExpiresIn
  72. @brief Fake token expiration time.
  73. */
  74. static NSString *const kTestExpiresIn = @"12345";
  75. /** @var kTestRefreshToken
  76. @brief Fake refresh token.
  77. */
  78. static NSString *const kTestRefreshToken = @"REFRESH_TOKEN";
  79. /** @var kTestProvider
  80. @brief Fake provider used for testing.
  81. */
  82. static NSString *const kTestProvider = @"Provider";
  83. /** @var kPhotoUrlKey
  84. @brief The name of the "PhotoUrl" property in the response.
  85. */
  86. static NSString *const kPhotoUrlKey = @"photoUrl";
  87. /** @var kTestPhotoUrl
  88. @brief The "PhotoUrl" value for testing the response.
  89. */
  90. static NSString *const kTestPhotoUrl = @"www.example.com";
  91. /** @var kUsername
  92. @brief The "username" value for testing the response.
  93. */
  94. static NSString *const kUsername = @"Joe Doe";
  95. /** @var testInvalidCredentialError
  96. @brief This is the error message the server will respond with if the IDP token or requestUri is
  97. invalid.
  98. */
  99. static NSString *const ktestInvalidCredentialError = @"INVALID_IDP_RESPONSE";
  100. /** @var kUserDisabledErrorMessage
  101. @brief This is the error message the server will respond with if the user's account has been
  102. disabled.
  103. */
  104. static NSString *const kUserDisabledErrorMessage = @"USER_DISABLED";
  105. /** @var kOperationNotAllowedErrorMessage
  106. @brief This is the error message the server will respond with if Admin disables IDP specified by
  107. provider.
  108. */
  109. static NSString *const kOperationNotAllowedErrorMessage = @"OPERATION_NOT_ALLOWED";
  110. /** @var kPasswordLoginDisabledErrorMessage
  111. @brief This is the error message the server responds with if password login is disabled.
  112. */
  113. static NSString *const kPasswordLoginDisabledErrorMessage = @"PASSWORD_LOGIN_DISABLED";
  114. /** @var kFederatedUserIDAlreadyLinkedMessage
  115. @brief This is the error message the server will respond with if the federated user ID has been
  116. already linked with another account.
  117. */
  118. static NSString *const kFederatedUserIDAlreadyLinkedMessage = @"FEDERATED_USER_ID_ALREADY_LINKED:";
  119. /** @var kAllowedTimeDifference
  120. @brief Allowed difference when comparing times because of execution time and floating point
  121. error.
  122. */
  123. static const double kAllowedTimeDifference = 0.1;
  124. /** @class FIRVerifyAssertionResponseTests
  125. @brief Tests for @c FIRVerifyAssertionResponse
  126. */
  127. @interface FIRVerifyAssertionResponseTests : XCTestCase
  128. @end
  129. @implementation FIRVerifyAssertionResponseTests {
  130. /** @var _RPCIssuer
  131. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  132. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  133. */
  134. FIRFakeBackendRPCIssuer *_RPCIssuer;
  135. /** @var _requestConfiguration
  136. @brief This is the request configuration used for testing.
  137. */
  138. FIRAuthRequestConfiguration *_requestConfiguration;
  139. }
  140. /** @fn profile
  141. @brief The "rawUserInfo" value for testing the response.
  142. */
  143. + (NSDictionary *)profile {
  144. static NSDictionary *kGoogleProfile = nil;
  145. static dispatch_once_t onceToken;
  146. dispatch_once(&onceToken, ^{
  147. kGoogleProfile = @{
  148. @"iss": @"https://accounts.google.com\\",
  149. @"email": @"test@email.com",
  150. @"given_name": @"User",
  151. @"family_name": @"Doe"
  152. };
  153. });
  154. return kGoogleProfile;
  155. }
  156. - (void)setUp {
  157. [super setUp];
  158. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  159. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  160. _RPCIssuer = RPCIssuer;
  161. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey];
  162. }
  163. - (void)tearDown {
  164. _RPCIssuer = nil;
  165. _requestConfiguration = nil;
  166. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  167. [super tearDown];
  168. }
  169. /** @fn testInvalidIDPResponseError
  170. @brief This test simulates @c invalidIDPResponseError with @c FIRAuthErrorCodeInvalidIDPResponse
  171. error code.
  172. */
  173. - (void)testInvalidIDPResponseError {
  174. FIRVerifyAssertionRequest *request =
  175. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  176. requestConfiguration:_requestConfiguration];
  177. request.providerIDToken = kTestProviderIDToken;
  178. __block BOOL callbackInvoked;
  179. __block FIRVerifyAssertionResponse *RPCResponse;
  180. __block NSError *RPCError;
  181. [FIRAuthBackend verifyAssertion:request
  182. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  183. NSError *_Nullable error) {
  184. callbackInvoked = YES;
  185. RPCResponse = response;
  186. RPCError = error;
  187. }];
  188. [_RPCIssuer respondWithServerErrorMessage:ktestInvalidCredentialError];
  189. XCTAssert(callbackInvoked);
  190. XCTAssertNotNil(RPCError);
  191. XCTAssertNil(RPCResponse);
  192. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCredential);
  193. }
  194. /** @fn testUserDisabledError
  195. @brief This test simulates @c userDisabledError with @c
  196. FIRAuthErrorCodeUserDisabled error code.
  197. */
  198. - (void)testUserDisabledError {
  199. FIRVerifyAssertionRequest *request =
  200. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  201. requestConfiguration:_requestConfiguration];
  202. request.providerIDToken = kTestProviderIDToken;
  203. __block BOOL callbackInvoked;
  204. __block FIRVerifyAssertionResponse *RPCResponse;
  205. __block NSError *RPCError;
  206. [FIRAuthBackend verifyAssertion:request
  207. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  208. NSError *_Nullable error) {
  209. callbackInvoked = YES;
  210. RPCResponse = response;
  211. RPCError = error;
  212. }];
  213. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  214. XCTAssert(callbackInvoked);
  215. XCTAssertNotNil(RPCError);
  216. XCTAssertNil(RPCResponse);
  217. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  218. }
  219. /** @fn testCredentialAlreadyInUseError
  220. @brief This test simulates a @c FIRAuthErrorCodeCredentialAlreadyInUse error.
  221. */
  222. - (void)testCredentialAlreadyInUseError {
  223. FIRVerifyAssertionRequest *request =
  224. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  225. requestConfiguration:_requestConfiguration];
  226. request.providerIDToken = kTestProviderIDToken;
  227. __block BOOL callbackInvoked;
  228. __block FIRVerifyAssertionResponse *RPCResponse;
  229. __block NSError *RPCError;
  230. [FIRAuthBackend verifyAssertion:request
  231. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  232. NSError *_Nullable error) {
  233. callbackInvoked = YES;
  234. RPCResponse = response;
  235. RPCError = error;
  236. }];
  237. [_RPCIssuer respondWithServerErrorMessage:kFederatedUserIDAlreadyLinkedMessage];
  238. XCTAssert(callbackInvoked);
  239. XCTAssertNotNil(RPCError);
  240. XCTAssertNil(RPCResponse);
  241. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCredentialAlreadyInUse);
  242. }
  243. /** @fn testOperationNotAllowedError
  244. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  245. */
  246. - (void)testOperationNotAllowedError {
  247. FIRVerifyAssertionRequest *request =
  248. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  249. requestConfiguration:_requestConfiguration];
  250. request.providerIDToken = kTestProviderIDToken;
  251. __block BOOL callbackInvoked;
  252. __block FIRVerifyAssertionResponse *RPCResponse;
  253. __block NSError *RPCError;
  254. [FIRAuthBackend verifyAssertion:request
  255. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  256. NSError *_Nullable error) {
  257. callbackInvoked = YES;
  258. RPCResponse = response;
  259. RPCError = error;
  260. }];
  261. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  262. XCTAssert(callbackInvoked);
  263. XCTAssertNotNil(RPCError);
  264. XCTAssertNil(RPCResponse);
  265. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  266. }
  267. /** @fn testPasswordLoginDisabledError
  268. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  269. */
  270. - (void)testPasswordLoginDisabledError {
  271. FIRVerifyAssertionRequest *request =
  272. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  273. requestConfiguration:_requestConfiguration];
  274. request.providerIDToken = kTestProviderIDToken;
  275. __block BOOL callbackInvoked;
  276. __block FIRVerifyAssertionResponse *RPCResponse;
  277. __block NSError *RPCError;
  278. [FIRAuthBackend verifyAssertion:request
  279. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  280. NSError *_Nullable error) {
  281. callbackInvoked = YES;
  282. RPCResponse = response;
  283. RPCError = error;
  284. }];
  285. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  286. XCTAssert(callbackInvoked);
  287. XCTAssertNotNil(RPCError);
  288. XCTAssertNil(RPCResponse);
  289. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  290. }
  291. /** @fn testSuccessfulVerifyAssertionResponse
  292. @brief This test simulates a successful verify assertion flow.
  293. */
  294. - (void)testSuccessfulVerifyAssertionResponse {
  295. FIRVerifyAssertionRequest *request =
  296. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  297. requestConfiguration:_requestConfiguration];
  298. request.providerIDToken = kTestProviderIDToken;
  299. __block BOOL callbackInvoked;
  300. __block FIRVerifyAssertionResponse *RPCResponse;
  301. __block NSError *RPCError;
  302. [FIRAuthBackend verifyAssertion:request
  303. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  304. NSError *_Nullable error) {
  305. callbackInvoked = YES;
  306. RPCResponse = response;
  307. RPCError = error;
  308. }];
  309. [_RPCIssuer respondWithJSON:@{
  310. kProviderIDKey : kTestProviderID,
  311. kIDTokenKey : kTestIDToken,
  312. kExpiresInKey : kTestExpiresIn,
  313. kRefreshTokenKey : kTestRefreshToken,
  314. kVerifiedProviderKey : @[ kTestProvider ],
  315. kPhotoUrlKey : kTestPhotoUrl,
  316. kUsernameKey : kUsername,
  317. kIsNewUserKey : @YES,
  318. kRawUserInfoKey : [[self class] profile]
  319. }];
  320. XCTAssert(callbackInvoked);
  321. XCTAssertNil(RPCError);
  322. XCTAssertNotNil(RPCResponse);
  323. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  324. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  325. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  326. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  327. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  328. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  329. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  330. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  331. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  332. XCTAssertTrue(RPCResponse.isNewUser);
  333. }
  334. /** @fn testSuccessfulVerifyAssertionResponseWithTextData
  335. @brief This test simulates a successful verify assertion flow when response collection
  336. fields are sent as text values.
  337. */
  338. - (void)testSuccessfulVerifyAssertionResponseWithTextData {
  339. FIRVerifyAssertionRequest *request =
  340. [[FIRVerifyAssertionRequest alloc] initWithProviderID:kTestProviderID
  341. requestConfiguration:_requestConfiguration];
  342. request.providerIDToken = kTestProviderIDToken;
  343. __block BOOL callbackInvoked;
  344. __block FIRVerifyAssertionResponse *RPCResponse;
  345. __block NSError *RPCError;
  346. [FIRAuthBackend verifyAssertion:request
  347. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  348. NSError *_Nullable error) {
  349. callbackInvoked = YES;
  350. RPCResponse = response;
  351. RPCError = error;
  352. }];
  353. [_RPCIssuer respondWithJSON:@{
  354. kProviderIDKey : kTestProviderID,
  355. kIDTokenKey : kTestIDToken,
  356. kExpiresInKey : kTestExpiresIn,
  357. kRefreshTokenKey : kTestRefreshToken,
  358. kVerifiedProviderKey : [[self class] convertToJSONString:@[ kTestProvider ]],
  359. kPhotoUrlKey : kTestPhotoUrl,
  360. kUsernameKey : kUsername,
  361. kIsNewUserKey : @NO,
  362. kRawUserInfoKey : [[self class] convertToJSONString:[[self class] profile]]
  363. }];
  364. XCTAssert(callbackInvoked);
  365. XCTAssertNil(RPCError);
  366. XCTAssertNotNil(RPCResponse);
  367. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  368. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  369. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  370. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  371. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  372. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  373. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  374. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  375. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  376. XCTAssertFalse(RPCResponse.isNewUser);
  377. }
  378. #pragma mark - Helpers
  379. + (NSString *)convertToJSONString:(NSObject *)object {
  380. NSData *objectAsData = [NSJSONSerialization dataWithJSONObject:object
  381. options:0
  382. error:nil];
  383. return [[NSString alloc] initWithData:objectAsData encoding:NSUTF8StringEncoding];
  384. }
  385. @end