|
36 | 36 | /** The url for obtaining a valid custom token string used to test Custom Auth. */
|
37 | 37 | static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL;
|
38 | 38 |
|
| 39 | +/** The url for obtaining an expired but valid custom token string used to test Custom Auth failure. |
| 40 | + */ |
| 41 | +static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL; |
| 42 | + |
39 | 43 | /** Facebook app access token that will be used for Facebook Graph API, which is different from
|
40 | 44 | * account access token.
|
41 | 45 | */
|
|
59 | 63 | /** The testing email address for testSignInExistingUserWithEmailAndPassword. */
|
60 | 64 | static NSString * const kExistingTestingEmailToSignIn = @"[email protected]";
|
61 | 65 |
|
| 66 | +/** The testing email address for testUpdatingUsersEmail. */ |
| 67 | +static NSString * const kNewTestingEmail = @"[email protected]"; |
| 68 | + |
| 69 | +/** The testing password for testSignInExistingUserWithModifiedEmailAndPassword. */ |
| 70 | +static NSString *const kNewTestingPasswordToSignIn = @"password_new"; |
| 71 | + |
62 | 72 | /** Error message for invalid custom token sign in. */
|
63 | 73 | NSString *kInvalidTokenErrorMessage =
|
64 | 74 | @"The custom token format is incorrect. Please check the documentation.";
|
|
71 | 81 | */
|
72 | 82 | NSString *kGoogleTestAccountRefreshToken = KGOOGLE_TEST_ACCOUNT_REFRESH_TOKEN;
|
73 | 83 |
|
74 |
| -static NSTimeInterval const kExpectationsTimeout = 30; |
| 84 | +static NSTimeInterval const kExpectationsTimeout = 10; |
75 | 85 |
|
76 | 86 | #ifdef NO_NETWORK
|
77 | 87 | #define SKIP_IF_ON_MOBILE_HARNESS \
|
@@ -141,6 +151,38 @@ - (void)testCreateAccountWithEmailAndPassword {
|
141 | 151 | [self deleteCurrentFirebaseUser];
|
142 | 152 | }
|
143 | 153 |
|
| 154 | +- (void)testUpdatingUsersEmail { |
| 155 | + SKIP_IF_ON_MOBILE_HARNESS |
| 156 | + FIRAuth *auth = [FIRAuth auth]; |
| 157 | + if (!auth) { |
| 158 | + XCTFail(@"Could not obtain auth object."); |
| 159 | + } |
| 160 | + |
| 161 | + __block NSError *apiError; |
| 162 | + XCTestExpectation *expectation = |
| 163 | + [self expectationWithDescription:@"Created account with email and password."]; |
| 164 | + [auth createUserWithEmail:kTestingEmailToCreateUser |
| 165 | + password:@"password" |
| 166 | + completion:^(FIRUser *user, NSError *error) { |
| 167 | + apiError = error; |
| 168 | + [expectation fulfill]; |
| 169 | + }]; |
| 170 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil]; |
| 171 | + expectation = [self expectationWithDescription:@"Created account with email and password."]; |
| 172 | + XCTAssertEqualObjects(auth.currentUser.email, kTestingEmailToCreateUser); |
| 173 | + XCTAssertNil(apiError); |
| 174 | + [auth.currentUser updateEmail:kNewTestingEmail |
| 175 | + completion:^(NSError *_Nullable error) { |
| 176 | + apiError = error; |
| 177 | + [expectation fulfill]; |
| 178 | + }]; |
| 179 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil]; |
| 180 | + XCTAssertNil(apiError); |
| 181 | + XCTAssertEqualObjects(auth.currentUser.email, kNewTestingEmail); |
| 182 | + // Clean up the created Firebase user for future runs. |
| 183 | + [self deleteCurrentFirebaseUser]; |
| 184 | +} |
| 185 | + |
144 | 186 | - (void)testLinkAnonymousAccountToFacebookAccount {
|
145 | 187 | FIRAuth *auth = [FIRAuth auth];
|
146 | 188 | if (!auth) {
|
@@ -253,6 +295,92 @@ - (void)testSignInWithValidCustomAuthToken {
|
253 | 295 | XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
|
254 | 296 | }
|
255 | 297 |
|
| 298 | +- (void)testSignInWithValidCustomAuthExpiredToken { |
| 299 | + FIRAuth *auth = [FIRAuth auth]; |
| 300 | + if (!auth) { |
| 301 | + XCTFail(@"Could not obtain auth object."); |
| 302 | + } |
| 303 | + |
| 304 | + NSError *error; |
| 305 | + NSString *customToken = |
| 306 | + [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl] |
| 307 | + encoding:NSUTF8StringEncoding |
| 308 | + error:&error]; |
| 309 | + if (!customToken) { |
| 310 | + XCTFail(@"There was an error retrieving the custom token: %@", error); |
| 311 | + } |
| 312 | + XCTestExpectation *expectation = |
| 313 | + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; |
| 314 | + |
| 315 | + __block NSError *apiError; |
| 316 | + [auth signInWithCustomToken:customToken |
| 317 | + completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { |
| 318 | + if (error) { |
| 319 | + apiError = error; |
| 320 | + } |
| 321 | + [expectation fulfill]; |
| 322 | + }]; |
| 323 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 324 | + handler:^(NSError *error) { |
| 325 | + if (error != nil) { |
| 326 | + XCTFail(@"Failed to wait for expectations " |
| 327 | + @"in CustomAuthToken sign in. Error: %@", |
| 328 | + error.localizedDescription); |
| 329 | + } |
| 330 | + }]; |
| 331 | + |
| 332 | + XCTAssertNil(auth.currentUser); |
| 333 | + XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken); |
| 334 | +} |
| 335 | + |
| 336 | +- (void)testInMemoryUserAfterSignOut { |
| 337 | + FIRAuth *auth = [FIRAuth auth]; |
| 338 | + if (!auth) { |
| 339 | + XCTFail(@"Could not obtain auth object."); |
| 340 | + } |
| 341 | + NSError *error; |
| 342 | + NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl] |
| 343 | + encoding:NSUTF8StringEncoding |
| 344 | + error:&error]; |
| 345 | + if (!customToken) { |
| 346 | + XCTFail(@"There was an error retrieving the custom token: %@", error); |
| 347 | + } |
| 348 | + XCTestExpectation *expectation = |
| 349 | + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; |
| 350 | + __block NSError *rpcError; |
| 351 | + [auth signInWithCustomToken:customToken |
| 352 | + completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { |
| 353 | + if (error) { |
| 354 | + rpcError = error; |
| 355 | + } |
| 356 | + [expectation fulfill]; |
| 357 | + }]; |
| 358 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 359 | + handler:^(NSError *error) { |
| 360 | + if (error != nil) { |
| 361 | + XCTFail(@"Failed to wait for expectations " |
| 362 | + @"in CustomAuthToken sign in. Error: %@", |
| 363 | + error.localizedDescription); |
| 364 | + } |
| 365 | + }]; |
| 366 | + XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID); |
| 367 | + XCTAssertNil(rpcError); |
| 368 | + FIRUser *inMemoryUser = auth.currentUser; |
| 369 | + XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."]; |
| 370 | + [auth signOut:NULL]; |
| 371 | + rpcError = nil; |
| 372 | + NSString *newEmailAddress = [self fakeRandomEmail]; |
| 373 | + XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email); |
| 374 | + [inMemoryUser updateEmail:newEmailAddress completion:^(NSError *_Nullable error) { |
| 375 | + rpcError = error; |
| 376 | + [expectation1 fulfill]; |
| 377 | + }]; |
| 378 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil]; |
| 379 | + XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress); |
| 380 | + XCTAssertNil(rpcError); |
| 381 | + XCTAssertNil(auth.currentUser); |
| 382 | +} |
| 383 | + |
256 | 384 | - (void)testSignInWithInvalidCustomAuthToken {
|
257 | 385 | FIRAuth *auth = [FIRAuth auth];
|
258 | 386 | if (!auth) {
|
@@ -354,6 +482,17 @@ - (void)testSignInWithGoogle {
|
354 | 482 |
|
355 | 483 | #pragma mark - Helpers
|
356 | 484 |
|
| 485 | +/** Generate fake random email address */ |
| 486 | +- (NSString *)fakeRandomEmail { |
| 487 | + NSMutableString *fakeEmail = [[NSMutableString alloc] init]; |
| 488 | + for (int i=0; i<10; i++) { |
| 489 | + [fakeEmail appendString: |
| 490 | + [NSString stringWithFormat:@"%c", 'a' + arc4random_uniform('z' - 'a' + 1)]]; |
| 491 | + } |
| 492 | + [fakeEmail appendString:@"@gmail.com"]; |
| 493 | + return fakeEmail; |
| 494 | +} |
| 495 | + |
357 | 496 | /** Sign out current account. */
|
358 | 497 | - (void)signOut {
|
359 | 498 | NSError *signOutError;
|
|
0 commit comments