Skip to content

Split auth api tests into multiple cases #2399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Example/Auth/ApiTests/AccountInfoTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

#import "FIRAuthApiTestsBase.h"

/** The testing email address for testCreateAccountWithEmailAndPassword. */
static NSString *const kOldUserEmail = @"[email protected]";

/** The testing email address for testUpdatingUsersEmail. */
static NSString *const kNewUserEmail = @"[email protected]";

@interface AccountInfoTests : FIRAuthApiTestsBase

@end

@implementation AccountInfoTests

- (void)testUpdatingUsersEmail {
SKIP_IF_ON_MOBILE_HARNESS
FIRAuth *auth = [FIRAuth auth];
if (!auth) {
XCTFail(@"Could not obtain auth object.");
}

__block NSError *apiError;
XCTestExpectation *expectation =
[self expectationWithDescription:@"Created account with email and password."];
[auth createUserWithEmail:kOldUserEmail
password:@"password"
completion:^(FIRAuthDataResult *user, NSError *error) {
apiError = error;
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
expectation = [self expectationWithDescription:@"Created account with email and password."];
XCTAssertEqualObjects(auth.currentUser.email, kOldUserEmail);
XCTAssertNil(apiError);

[auth.currentUser updateEmail:kNewUserEmail
completion:^(NSError *_Nullable error) {
apiError = error;
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
XCTAssertNil(apiError);
XCTAssertEqualObjects(auth.currentUser.email, kNewUserEmail);

// Clean up the created Firebase user for future runs.
[self deleteCurrentUser];
}

@end
34 changes: 34 additions & 0 deletions Example/Auth/ApiTests/AnonymousAuthTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

#import "FIRAuthApiTestsBase.h"

@interface AnonymousAuthTests : FIRAuthApiTestsBase

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the new line here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean line 22 right? I prefer keep it since it's makes the class structure clearer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the line for a simple class with 0 method is more common among Google3 code. see http://google.github.io/styleguide/objcguide.html#prefixes

It's ok if you dont remove, because I don't care much about it.

@end

@implementation AnonymousAuthTests

- (void)testSignInAnonymously {
[self signInAnonymously];
XCTAssertTrue([FIRAuth auth].currentUser.anonymous);

[self deleteCurrentUser];
}

@end
5 changes: 5 additions & 0 deletions Example/Auth/ApiTests/Auth_ApiTests-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "FIRAuthApiTestsBase.h"
191 changes: 191 additions & 0 deletions Example/Auth/ApiTests/CustomAuthTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

#import "FIRAuthApiTestsBase.h"

/** The user name string for Custom Auth testing account. */
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;

/** The invalid custom token string for testing Custom Auth. */
static NSString *const kInvalidCustomToken = @"invalid token.";

/** Error message for invalid custom token sign in. */
NSString *kInvalidTokenErrorMessage =
@"Invalid assertion format. 3 dot separated segments required.";

@interface CustomAuthTests : FIRAuthApiTestsBase

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove new line. same for other files

@end

@implementation CustomAuthTests

- (void)testSignInWithValidCustomAuthToken {
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);
}
NSLog(@"The valid token is: %@", customToken);

XCTestExpectation *expectation =
[self expectationWithDescription:@"CustomAuthToken sign-in finished."];

[auth signInWithCustomToken:customToken
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
if (error) {
NSLog(@"Valid token sign in error: %@", 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);
}

- (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:^(FIRAuthDataResult *_Nullable result, 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);
}

- (void)testSignInWithInvalidCustomAuthToken {
FIRAuth *auth = [FIRAuth auth];
if (!auth) {
XCTFail(@"Could not obtain auth object.");
}
XCTestExpectation *expectation =
[self expectationWithDescription:@"Invalid CustomAuthToken sign-in finished."];

[auth signInWithCustomToken:kInvalidCustomToken
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
XCTAssertEqualObjects(error.localizedDescription, kInvalidTokenErrorMessage);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationsTimeout
handler:^(NSError *error) {
if (error != nil) {
XCTFail(@"Failed to wait for expectations "
@"in CustomAuthToken sign in. Error: %@",
error.localizedDescription);
}
}];
}

- (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:^(FIRAuthDataResult *_Nullable result, 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);
}

@end
93 changes: 93 additions & 0 deletions Example/Auth/ApiTests/EmailPasswordAuthTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <XCTest/XCTest.h>

#import "FIRAuthApiTestsBase.h"

/** The testing email address for testCreateAccountWithEmailAndPassword. */
static NSString *const kNewEmailToCreateUser = @"[email protected]";

/** The testing email address for testSignInExistingUserWithEmailAndPassword. */
static NSString *const kExistingEmailToSignIn = @"[email protected]";

/** The testing password for testSignInExistingUserWithEmailAndPassword. */
static NSString *const kExistingPasswordToSignIn = @"password";

@interface EmailPasswordAuthTests : FIRAuthApiTestsBase

@end

@implementation EmailPasswordAuthTests

- (void)testCreateAccountWithEmailAndPassword {
SKIP_IF_ON_MOBILE_HARNESS
FIRAuth *auth = [FIRAuth auth];
if (!auth) {
XCTFail(@"Could not obtain auth object.");
}
XCTestExpectation *expectation =
[self expectationWithDescription:@"Created account with email and password."];
[auth createUserWithEmail:kNewEmailToCreateUser
password:@"password"
completion:^(FIRAuthDataResult *result, NSError *error) {
if (error) {
NSLog(@"createUserWithEmail has error: %@", error);
}
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationsTimeout
handler:^(NSError *error) {
if (error != nil) {
XCTFail(@"Failed to wait for expectations "
@"in creating account. Error: %@",
error.localizedDescription);
}
}];

XCTAssertEqualObjects(auth.currentUser.email, kNewEmailToCreateUser);

[self deleteCurrentUser];
}

- (void)testSignInExistingUserWithEmailAndPassword {
FIRAuth *auth = [FIRAuth auth];
if (!auth) {
XCTFail(@"Could not obtain auth object.");
}
XCTestExpectation *expectation =
[self expectationWithDescription:@"Signed in existing account with email and password."];
[auth signInWithEmail:kExistingEmailToSignIn
password:kExistingPasswordToSignIn
completion:^(FIRAuthDataResult *user, NSError *error) {
if (error) {
NSLog(@"Signing in existing account has error: %@", error);
}
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kExpectationsTimeout
handler:^(NSError *error) {
if (error != nil) {
XCTFail(@"Failed to wait for expectations "
@"in signing in existing account. Error: %@",
error.localizedDescription);
}
}];

XCTAssertEqualObjects(auth.currentUser.email, kExistingEmailToSignIn);
}

@end
Loading