Skip to content

Commit b3f2abc

Browse files
author
Chuan Ren
authored
Split auth api tests into multiple cases (#2399)
1 parent 5efbb08 commit b3f2abc

13 files changed

+1018
-683
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 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+
17+
#import <XCTest/XCTest.h>
18+
19+
#import "FIRAuthApiTestsBase.h"
20+
21+
/** The testing email address for testCreateAccountWithEmailAndPassword. */
22+
static NSString *const kOldUserEmail = @"[email protected]";
23+
24+
/** The testing email address for testUpdatingUsersEmail. */
25+
static NSString *const kNewUserEmail = @"[email protected]";
26+
27+
@interface AccountInfoTests : FIRAuthApiTestsBase
28+
29+
@end
30+
31+
@implementation AccountInfoTests
32+
33+
- (void)testUpdatingUsersEmail {
34+
SKIP_IF_ON_MOBILE_HARNESS
35+
FIRAuth *auth = [FIRAuth auth];
36+
if (!auth) {
37+
XCTFail(@"Could not obtain auth object.");
38+
}
39+
40+
__block NSError *apiError;
41+
XCTestExpectation *expectation =
42+
[self expectationWithDescription:@"Created account with email and password."];
43+
[auth createUserWithEmail:kOldUserEmail
44+
password:@"password"
45+
completion:^(FIRAuthDataResult *user, NSError *error) {
46+
apiError = error;
47+
[expectation fulfill];
48+
}];
49+
[self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
50+
expectation = [self expectationWithDescription:@"Created account with email and password."];
51+
XCTAssertEqualObjects(auth.currentUser.email, kOldUserEmail);
52+
XCTAssertNil(apiError);
53+
54+
[auth.currentUser updateEmail:kNewUserEmail
55+
completion:^(NSError *_Nullable error) {
56+
apiError = error;
57+
[expectation fulfill];
58+
}];
59+
[self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
60+
XCTAssertNil(apiError);
61+
XCTAssertEqualObjects(auth.currentUser.email, kNewUserEmail);
62+
63+
// Clean up the created Firebase user for future runs.
64+
[self deleteCurrentUser];
65+
}
66+
67+
@end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 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+
17+
#import <XCTest/XCTest.h>
18+
19+
#import "FIRAuthApiTestsBase.h"
20+
21+
@interface AnonymousAuthTests : FIRAuthApiTestsBase
22+
23+
@end
24+
25+
@implementation AnonymousAuthTests
26+
27+
- (void)testSignInAnonymously {
28+
[self signInAnonymously];
29+
XCTAssertTrue([FIRAuth auth].currentUser.anonymous);
30+
31+
[self deleteCurrentUser];
32+
}
33+
34+
@end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//
2+
// Use this file to import your target's public headers that you would like to expose to Swift.
3+
//
4+
5+
#import "FIRAuthApiTestsBase.h"
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* Copyright 2019 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+
17+
#import <XCTest/XCTest.h>
18+
19+
#import "FIRAuthApiTestsBase.h"
20+
21+
/** The user name string for Custom Auth testing account. */
22+
static NSString *const kCustomAuthTestingAccountUserID = KCUSTOM_AUTH_USER_ID;
23+
24+
/** The url for obtaining a valid custom token string used to test Custom Auth. */
25+
static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL;
26+
27+
/** The url for obtaining an expired but valid custom token string used to test Custom Auth failure.
28+
*/
29+
static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL;
30+
31+
/** The invalid custom token string for testing Custom Auth. */
32+
static NSString *const kInvalidCustomToken = @"invalid token.";
33+
34+
/** Error message for invalid custom token sign in. */
35+
NSString *kInvalidTokenErrorMessage =
36+
@"Invalid assertion format. 3 dot separated segments required.";
37+
38+
@interface CustomAuthTests : FIRAuthApiTestsBase
39+
40+
@end
41+
42+
@implementation CustomAuthTests
43+
44+
- (void)testSignInWithValidCustomAuthToken {
45+
FIRAuth *auth = [FIRAuth auth];
46+
if (!auth) {
47+
XCTFail(@"Could not obtain auth object.");
48+
}
49+
50+
NSError *error;
51+
NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
52+
encoding:NSUTF8StringEncoding
53+
error:&error];
54+
if (!customToken) {
55+
XCTFail(@"There was an error retrieving the custom token: %@", error);
56+
}
57+
NSLog(@"The valid token is: %@", customToken);
58+
59+
XCTestExpectation *expectation =
60+
[self expectationWithDescription:@"CustomAuthToken sign-in finished."];
61+
62+
[auth signInWithCustomToken:customToken
63+
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
64+
if (error) {
65+
NSLog(@"Valid token sign in error: %@", error);
66+
}
67+
[expectation fulfill];
68+
}];
69+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
70+
handler:^(NSError *error) {
71+
if (error != nil) {
72+
XCTFail(@"Failed to wait for expectations "
73+
@"in CustomAuthToken sign in. Error: %@",
74+
error.localizedDescription);
75+
}
76+
}];
77+
78+
XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
79+
}
80+
81+
- (void)testSignInWithValidCustomAuthExpiredToken {
82+
FIRAuth *auth = [FIRAuth auth];
83+
if (!auth) {
84+
XCTFail(@"Could not obtain auth object.");
85+
}
86+
87+
NSError *error;
88+
NSString *customToken =
89+
[NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl]
90+
encoding:NSUTF8StringEncoding
91+
error:&error];
92+
if (!customToken) {
93+
XCTFail(@"There was an error retrieving the custom token: %@", error);
94+
}
95+
XCTestExpectation *expectation =
96+
[self expectationWithDescription:@"CustomAuthToken sign-in finished."];
97+
98+
__block NSError *apiError;
99+
[auth signInWithCustomToken:customToken
100+
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
101+
if (error) {
102+
apiError = error;
103+
}
104+
[expectation fulfill];
105+
}];
106+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
107+
handler:^(NSError *error) {
108+
if (error != nil) {
109+
XCTFail(@"Failed to wait for expectations "
110+
@"in CustomAuthToken sign in. Error: %@",
111+
error.localizedDescription);
112+
}
113+
}];
114+
115+
XCTAssertNil(auth.currentUser);
116+
XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken);
117+
}
118+
119+
- (void)testSignInWithInvalidCustomAuthToken {
120+
FIRAuth *auth = [FIRAuth auth];
121+
if (!auth) {
122+
XCTFail(@"Could not obtain auth object.");
123+
}
124+
XCTestExpectation *expectation =
125+
[self expectationWithDescription:@"Invalid CustomAuthToken sign-in finished."];
126+
127+
[auth signInWithCustomToken:kInvalidCustomToken
128+
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
129+
XCTAssertEqualObjects(error.localizedDescription, kInvalidTokenErrorMessage);
130+
[expectation fulfill];
131+
}];
132+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
133+
handler:^(NSError *error) {
134+
if (error != nil) {
135+
XCTFail(@"Failed to wait for expectations "
136+
@"in CustomAuthToken sign in. Error: %@",
137+
error.localizedDescription);
138+
}
139+
}];
140+
}
141+
142+
- (void)testInMemoryUserAfterSignOut {
143+
FIRAuth *auth = [FIRAuth auth];
144+
if (!auth) {
145+
XCTFail(@"Could not obtain auth object.");
146+
}
147+
NSError *error;
148+
NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl]
149+
encoding:NSUTF8StringEncoding
150+
error:&error];
151+
if (!customToken) {
152+
XCTFail(@"There was an error retrieving the custom token: %@", error);
153+
}
154+
XCTestExpectation *expectation =
155+
[self expectationWithDescription:@"CustomAuthToken sign-in finished."];
156+
__block NSError *rpcError;
157+
[auth signInWithCustomToken:customToken
158+
completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) {
159+
if (error) {
160+
rpcError = error;
161+
}
162+
[expectation fulfill];
163+
}];
164+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
165+
handler:^(NSError *error) {
166+
if (error != nil) {
167+
XCTFail(@"Failed to wait for expectations "
168+
@"in CustomAuthToken sign in. Error: %@",
169+
error.localizedDescription);
170+
}
171+
}];
172+
XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID);
173+
XCTAssertNil(rpcError);
174+
FIRUser *inMemoryUser = auth.currentUser;
175+
XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."];
176+
[auth signOut:NULL];
177+
rpcError = nil;
178+
NSString *newEmailAddress = [self fakeRandomEmail];
179+
XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email);
180+
[inMemoryUser updateEmail:newEmailAddress
181+
completion:^(NSError *_Nullable error) {
182+
rpcError = error;
183+
[expectation1 fulfill];
184+
}];
185+
[self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil];
186+
XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress);
187+
XCTAssertNil(rpcError);
188+
XCTAssertNil(auth.currentUser);
189+
}
190+
191+
@end
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
17+
#import <XCTest/XCTest.h>
18+
19+
#import "FIRAuthApiTestsBase.h"
20+
21+
/** The testing email address for testCreateAccountWithEmailAndPassword. */
22+
static NSString *const kNewEmailToCreateUser = @"[email protected]";
23+
24+
/** The testing email address for testSignInExistingUserWithEmailAndPassword. */
25+
static NSString *const kExistingEmailToSignIn = @"[email protected]";
26+
27+
/** The testing password for testSignInExistingUserWithEmailAndPassword. */
28+
static NSString *const kExistingPasswordToSignIn = @"password";
29+
30+
@interface EmailPasswordAuthTests : FIRAuthApiTestsBase
31+
32+
@end
33+
34+
@implementation EmailPasswordAuthTests
35+
36+
- (void)testCreateAccountWithEmailAndPassword {
37+
SKIP_IF_ON_MOBILE_HARNESS
38+
FIRAuth *auth = [FIRAuth auth];
39+
if (!auth) {
40+
XCTFail(@"Could not obtain auth object.");
41+
}
42+
XCTestExpectation *expectation =
43+
[self expectationWithDescription:@"Created account with email and password."];
44+
[auth createUserWithEmail:kNewEmailToCreateUser
45+
password:@"password"
46+
completion:^(FIRAuthDataResult *result, NSError *error) {
47+
if (error) {
48+
NSLog(@"createUserWithEmail has error: %@", error);
49+
}
50+
[expectation fulfill];
51+
}];
52+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
53+
handler:^(NSError *error) {
54+
if (error != nil) {
55+
XCTFail(@"Failed to wait for expectations "
56+
@"in creating account. Error: %@",
57+
error.localizedDescription);
58+
}
59+
}];
60+
61+
XCTAssertEqualObjects(auth.currentUser.email, kNewEmailToCreateUser);
62+
63+
[self deleteCurrentUser];
64+
}
65+
66+
- (void)testSignInExistingUserWithEmailAndPassword {
67+
FIRAuth *auth = [FIRAuth auth];
68+
if (!auth) {
69+
XCTFail(@"Could not obtain auth object.");
70+
}
71+
XCTestExpectation *expectation =
72+
[self expectationWithDescription:@"Signed in existing account with email and password."];
73+
[auth signInWithEmail:kExistingEmailToSignIn
74+
password:kExistingPasswordToSignIn
75+
completion:^(FIRAuthDataResult *user, NSError *error) {
76+
if (error) {
77+
NSLog(@"Signing in existing account has error: %@", error);
78+
}
79+
[expectation fulfill];
80+
}];
81+
[self waitForExpectationsWithTimeout:kExpectationsTimeout
82+
handler:^(NSError *error) {
83+
if (error != nil) {
84+
XCTFail(@"Failed to wait for expectations "
85+
@"in signing in existing account. Error: %@",
86+
error.localizedDescription);
87+
}
88+
}];
89+
90+
XCTAssertEqualObjects(auth.currentUser.email, kExistingEmailToSignIn);
91+
}
92+
93+
@end

0 commit comments

Comments
 (0)