diff options
author | Zsika Phillip <protocol86@users.noreply.github.com> | 2018-01-03 00:05:48 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-03 00:05:48 -0800 |
commit | 727edcead2b7620dcd0c586352486370e15ffa45 (patch) | |
tree | d8fc2cc94fe746cd6db996908810ebe76bd4cdce /Example/Auth | |
parent | 7ab9c3c498a3cadb7a1d1a706642e10d228d4690 (diff) |
Adds new API tests for Firebase Auth (#610)
Diffstat (limited to 'Example/Auth')
-rw-r--r-- | Example/Auth/ApiTests/AuthCredentialsTemplate.h | 4 | ||||
-rw-r--r-- | Example/Auth/ApiTests/FirebaseAuthApiTests.m | 102 |
2 files changed, 106 insertions, 0 deletions
diff --git a/Example/Auth/ApiTests/AuthCredentialsTemplate.h b/Example/Auth/ApiTests/AuthCredentialsTemplate.h index a8bf379..09eb62a 100644 --- a/Example/Auth/ApiTests/AuthCredentialsTemplate.h +++ b/Example/Auth/ApiTests/AuthCredentialsTemplate.h @@ -50,6 +50,9 @@ The name of the test user for Facebook Login. $KCUSTOM_AUTH_TOKEN_URL A URL to return a Custom Auth token. +$KCUSTOM_AUTH_TOKEN_EXPIRED_URL +A URL to return an expired Custom Auth token. + $KCUSTOM_AUTH_USER_ID The ID of the test user in the Custom Auth token. */ @@ -61,4 +64,5 @@ The ID of the test user in the Custom Auth token. #define KFACEBOOK_APP_ACCESS_TOKEN $KFACEBOOK_APP_ACCESS_TOKEN #define KFACEBOOK_USER_NAME $KFACEBOOK_USER_NAME #define KCUSTOM_AUTH_TOKEN_URL $KCUSTOM_AUTH_TOKEN_URL +#define KCUSTOM_AUTH_TOKEN_EXPIRED_URL $KCUSTOM_AUTH_TOKEN_EXPIRED_URL #define KCUSTOM_AUTH_USER_ID $KCUSTOM_AUTH_USER_ID diff --git a/Example/Auth/ApiTests/FirebaseAuthApiTests.m b/Example/Auth/ApiTests/FirebaseAuthApiTests.m index 454d9dd..ee96bd0 100644 --- a/Example/Auth/ApiTests/FirebaseAuthApiTests.m +++ b/Example/Auth/ApiTests/FirebaseAuthApiTests.m @@ -36,6 +36,10 @@ static NSString *const kCustomAuthTestingAccountUserID = KCUSTOM_AUTH_USER_ID; /** The url for obtaining a valid custom token string used to test Custom Auth. */ static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL; +/** The url for obtaining an expired but valid custom token string used to test Custom Auth failure. + */ +static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL; + /** Facebook app access token that will be used for Facebook Graph API, which is different from * account access token. */ @@ -253,6 +257,93 @@ static NSTimeInterval const kExpectationsTimeout = 30; XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID); } +- (void)testSignInWithValidCustomAuthExpiredToken { + FIRAuth *auth = [FIRAuth auth]; + if (!auth) { + XCTFail(@"Could not obtain auth object."); + } + + NSError *error; + NSString *customToken = + [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl] + encoding:NSUTF8StringEncoding + error:&error]; + if (!customToken) { + XCTFail(@"There was an error retrieving the custom token: %@", error); + } + XCTestExpectation *expectation = + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; + + __block NSError *apiError; + [auth signInWithCustomToken:customToken + completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { + if (error) { + apiError = error; + } + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationsTimeout + handler:^(NSError *error) { + if (error != nil) { + XCTFail(@"Failed to wait for expectations " + @"in CustomAuthToken sign in. Error: %@", + error.localizedDescription); + } + }]; + + XCTAssertNil(auth.currentUser); + XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken); + [NSThread sleepForTimeInterval:3.0]; +} + +- (void)testInMemoryUserAfterSignOut { + FIRAuth *auth = [FIRAuth auth]; + if (!auth) { + XCTFail(@"Could not obtain auth object."); + } + NSError *error; + NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl] + encoding:NSUTF8StringEncoding + error:&error]; + if (!customToken) { + XCTFail(@"There was an error retrieving the custom token: %@", error); + } + XCTestExpectation *expectation = + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; + __block NSError *rpcError; + [auth signInWithCustomToken:customToken + completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { + if (error) { + rpcError = error; + } + [expectation fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationsTimeout + handler:^(NSError *error) { + if (error != nil) { + XCTFail(@"Failed to wait for expectations " + @"in CustomAuthToken sign in. Error: %@", + error.localizedDescription); + } + }]; + XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID); + XCTAssertNil(rpcError); + FIRUser *inMemoryUser = auth.currentUser; + XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."]; + [auth signOut:NULL]; + rpcError = nil; + NSString *newEmailAddress = [self fakeRandomEmail]; + XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email); + [inMemoryUser updateEmail:newEmailAddress completion:^(NSError *_Nullable error) { + rpcError = error; + [expectation1 fulfill]; + }]; + [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil]; + XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress); + XCTAssertNil(rpcError); + XCTAssertNil(auth.currentUser); +} + - (void)testSignInWithInvalidCustomAuthToken { FIRAuth *auth = [FIRAuth auth]; if (!auth) { @@ -354,6 +445,17 @@ static NSTimeInterval const kExpectationsTimeout = 30; #pragma mark - Helpers +/** Generate fake random email address */ +- (NSString *)fakeRandomEmail { + NSMutableString *fakeEmail = [[NSMutableString alloc] init]; + for (int i=0; i<10; i++) { + [fakeEmail appendString: + [NSString stringWithFormat:@"%c", 'a' + arc4random_uniform('z' - 'a' + 1)]]; + } + [fakeEmail appendString:@"@gmail.com"]; + return fakeEmail; +} + /** Sign out current account. */ - (void)signOut { NSError *signOutError; |