FIRVerifyAssertionResponseTests.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. }
  136. /** @fn profile
  137. @brief The "rawUserInfo" value for testing the response.
  138. */
  139. + (NSDictionary *)profile {
  140. static NSDictionary *kGoogleProfile = nil;
  141. static dispatch_once_t onceToken;
  142. dispatch_once(&onceToken, ^{
  143. kGoogleProfile = @{
  144. @"iss": @"https://accounts.google.com\\",
  145. @"email": @"test@email.com",
  146. @"given_name": @"User",
  147. @"family_name": @"Doe"
  148. };
  149. });
  150. return kGoogleProfile;
  151. }
  152. - (void)setUp {
  153. [super setUp];
  154. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  155. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  156. _RPCIssuer = RPCIssuer;
  157. }
  158. - (void)tearDown {
  159. _RPCIssuer = nil;
  160. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  161. [super tearDown];
  162. }
  163. /** @fn testInvalidIDPResponseError
  164. @brief This test simulates @c invalidIDPResponseError with @c FIRAuthErrorCodeInvalidIDPResponse
  165. error code.
  166. */
  167. - (void)testInvalidIDPResponseError {
  168. FIRVerifyAssertionRequest *request =
  169. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  170. request.providerIDToken = kTestProviderIDToken;
  171. __block BOOL callbackInvoked;
  172. __block FIRVerifyAssertionResponse *RPCResponse;
  173. __block NSError *RPCError;
  174. [FIRAuthBackend verifyAssertion:request
  175. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  176. NSError *_Nullable error) {
  177. callbackInvoked = YES;
  178. RPCResponse = response;
  179. RPCError = error;
  180. }];
  181. [_RPCIssuer respondWithServerErrorMessage:ktestInvalidCredentialError];
  182. XCTAssert(callbackInvoked);
  183. XCTAssertNotNil(RPCError);
  184. XCTAssertNil(RPCResponse);
  185. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInvalidCredential);
  186. }
  187. /** @fn testUserDisabledError
  188. @brief This test simulates @c userDisabledError with @c
  189. FIRAuthErrorCodeUserDisabled error code.
  190. */
  191. - (void)testUserDisabledError {
  192. FIRVerifyAssertionRequest *request =
  193. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  194. request.providerIDToken = kTestProviderIDToken;
  195. __block BOOL callbackInvoked;
  196. __block FIRVerifyAssertionResponse *RPCResponse;
  197. __block NSError *RPCError;
  198. [FIRAuthBackend verifyAssertion:request
  199. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  200. NSError *_Nullable error) {
  201. callbackInvoked = YES;
  202. RPCResponse = response;
  203. RPCError = error;
  204. }];
  205. [_RPCIssuer respondWithServerErrorMessage:kUserDisabledErrorMessage];
  206. XCTAssert(callbackInvoked);
  207. XCTAssertNotNil(RPCError);
  208. XCTAssertNil(RPCResponse);
  209. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeUserDisabled);
  210. }
  211. /** @fn testCredentialAlreadyInUseError
  212. @brief This test simulates a @c FIRAuthErrorCodeCredentialAlreadyInUse error.
  213. */
  214. - (void)testCredentialAlreadyInUseError {
  215. FIRVerifyAssertionRequest *request =
  216. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  217. request.providerIDToken = kTestProviderIDToken;
  218. __block BOOL callbackInvoked;
  219. __block FIRVerifyAssertionResponse *RPCResponse;
  220. __block NSError *RPCError;
  221. [FIRAuthBackend verifyAssertion:request
  222. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  223. NSError *_Nullable error) {
  224. callbackInvoked = YES;
  225. RPCResponse = response;
  226. RPCError = error;
  227. }];
  228. [_RPCIssuer respondWithServerErrorMessage:kFederatedUserIDAlreadyLinkedMessage];
  229. XCTAssert(callbackInvoked);
  230. XCTAssertNotNil(RPCError);
  231. XCTAssertNil(RPCResponse);
  232. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeCredentialAlreadyInUse);
  233. }
  234. /** @fn testOperationNotAllowedError
  235. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  236. */
  237. - (void)testOperationNotAllowedError {
  238. FIRVerifyAssertionRequest *request =
  239. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  240. request.providerIDToken = kTestProviderIDToken;
  241. __block BOOL callbackInvoked;
  242. __block FIRVerifyAssertionResponse *RPCResponse;
  243. __block NSError *RPCError;
  244. [FIRAuthBackend verifyAssertion:request
  245. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  246. NSError *_Nullable error) {
  247. callbackInvoked = YES;
  248. RPCResponse = response;
  249. RPCError = error;
  250. }];
  251. [_RPCIssuer respondWithServerErrorMessage:kOperationNotAllowedErrorMessage];
  252. XCTAssert(callbackInvoked);
  253. XCTAssertNotNil(RPCError);
  254. XCTAssertNil(RPCResponse);
  255. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  256. }
  257. /** @fn testPasswordLoginDisabledError
  258. @brief This test simulates a @c FIRAuthErrorCodeOperationNotAllowed error.
  259. */
  260. - (void)testPasswordLoginDisabledError {
  261. FIRVerifyAssertionRequest *request =
  262. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  263. request.providerIDToken = kTestProviderIDToken;
  264. __block BOOL callbackInvoked;
  265. __block FIRVerifyAssertionResponse *RPCResponse;
  266. __block NSError *RPCError;
  267. [FIRAuthBackend verifyAssertion:request
  268. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  269. NSError *_Nullable error) {
  270. callbackInvoked = YES;
  271. RPCResponse = response;
  272. RPCError = error;
  273. }];
  274. [_RPCIssuer respondWithServerErrorMessage:kPasswordLoginDisabledErrorMessage];
  275. XCTAssert(callbackInvoked);
  276. XCTAssertNotNil(RPCError);
  277. XCTAssertNil(RPCResponse);
  278. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeOperationNotAllowed);
  279. }
  280. /** @fn testSuccessfulVerifyAssertionResponse
  281. @brief This test simulates a successful verify assertion flow.
  282. */
  283. - (void)testSuccessfulVerifyAssertionResponse {
  284. FIRVerifyAssertionRequest *request =
  285. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  286. request.providerIDToken = kTestProviderIDToken;
  287. __block BOOL callbackInvoked;
  288. __block FIRVerifyAssertionResponse *RPCResponse;
  289. __block NSError *RPCError;
  290. [FIRAuthBackend verifyAssertion:request
  291. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  292. NSError *_Nullable error) {
  293. callbackInvoked = YES;
  294. RPCResponse = response;
  295. RPCError = error;
  296. }];
  297. [_RPCIssuer respondWithJSON:@{
  298. kProviderIDKey : kTestProviderID,
  299. kIDTokenKey : kTestIDToken,
  300. kExpiresInKey : kTestExpiresIn,
  301. kRefreshTokenKey : kTestRefreshToken,
  302. kVerifiedProviderKey : @[ kTestProvider ],
  303. kPhotoUrlKey : kTestPhotoUrl,
  304. kUsernameKey : kUsername,
  305. kIsNewUserKey : @YES,
  306. kRawUserInfoKey : [[self class] profile]
  307. }];
  308. XCTAssert(callbackInvoked);
  309. XCTAssertNil(RPCError);
  310. XCTAssertNotNil(RPCResponse);
  311. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  312. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  313. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  314. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  315. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  316. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  317. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  318. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  319. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  320. XCTAssertTrue(RPCResponse.isNewUser);
  321. }
  322. /** @fn testSuccessfulVerifyAssertionResponseWithTextData
  323. @brief This test simulates a successful verify assertion flow when response collection
  324. fields are sent as text values.
  325. */
  326. - (void)testSuccessfulVerifyAssertionResponseWithTextData {
  327. FIRVerifyAssertionRequest *request =
  328. [[FIRVerifyAssertionRequest alloc] initWithAPIKey:kTestAPIKey providerID:kTestProviderID];
  329. request.providerIDToken = kTestProviderIDToken;
  330. __block BOOL callbackInvoked;
  331. __block FIRVerifyAssertionResponse *RPCResponse;
  332. __block NSError *RPCError;
  333. [FIRAuthBackend verifyAssertion:request
  334. callback:^(FIRVerifyAssertionResponse*_Nullable response,
  335. NSError *_Nullable error) {
  336. callbackInvoked = YES;
  337. RPCResponse = response;
  338. RPCError = error;
  339. }];
  340. [_RPCIssuer respondWithJSON:@{
  341. kProviderIDKey : kTestProviderID,
  342. kIDTokenKey : kTestIDToken,
  343. kExpiresInKey : kTestExpiresIn,
  344. kRefreshTokenKey : kTestRefreshToken,
  345. kVerifiedProviderKey : [[self class] convertToJSONString:@[ kTestProvider ]],
  346. kPhotoUrlKey : kTestPhotoUrl,
  347. kUsernameKey : kUsername,
  348. kIsNewUserKey : @NO,
  349. kRawUserInfoKey : [[self class] convertToJSONString:[[self class] profile]]
  350. }];
  351. XCTAssert(callbackInvoked);
  352. XCTAssertNil(RPCError);
  353. XCTAssertNotNil(RPCResponse);
  354. XCTAssertEqualObjects(RPCResponse.IDToken, kTestIDToken);
  355. NSTimeInterval expiresIn = [RPCResponse.approximateExpirationDate timeIntervalSinceNow];
  356. XCTAssertEqualWithAccuracy(expiresIn, [kTestExpiresIn doubleValue], kAllowedTimeDifference);
  357. XCTAssertEqualObjects(RPCResponse.refreshToken, kTestRefreshToken);
  358. XCTAssertEqualObjects(RPCResponse.verifiedProvider, @[ kTestProvider ]);
  359. XCTAssertEqualObjects(RPCResponse.photoURL, [NSURL URLWithString:kTestPhotoUrl]);
  360. XCTAssertEqualObjects(RPCResponse.username, kUsername);
  361. XCTAssertEqualObjects(RPCResponse.profile, [[self class] profile]);
  362. XCTAssertEqualObjects(RPCResponse.providerID, kTestProviderID);
  363. XCTAssertFalse(RPCResponse.isNewUser);
  364. }
  365. #pragma mark - Helpers
  366. + (NSString *)convertToJSONString:(NSObject *)object {
  367. NSData *objectAsData = [NSJSONSerialization dataWithJSONObject:object
  368. options:0
  369. error:nil];
  370. return [[NSString alloc] initWithData:objectAsData encoding:NSUTF8StringEncoding];
  371. }
  372. @end