Skip to content

Commit c09966e

Browse files
author
Chuan Ren
authored
Split Auth unit tests (#2396)
1 parent 3d3509a commit c09966e

File tree

4 files changed

+186
-104
lines changed

4 files changed

+186
-104
lines changed

Example/Auth/ApiTests/FirebaseAuthApiTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
/** Error message for invalid custom token sign in. */
7171
NSString *kInvalidTokenErrorMessage =
72-
@"The custom token format is incorrect. Please check the documentation.";
72+
@"Invalid assertion format. 3 dot separated segments required.";
7373

7474
NSString *kGoogleCliendId = KGOOGLE_CLIENT_ID;
7575

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 <FirebaseAuthInterop/FIRAuthInterop.h>
20+
#import <FirebaseCore/FIRAppInternal.h>
21+
#import <FirebaseCore/FIRComponent.h>
22+
#import <FirebaseCore/FIRLibrary.h>
23+
24+
#import "FIRApp+FIRAuthUnitTests.h"
25+
#import "FIRAuthRequestConfiguration.h"
26+
#import "FIRAuth_Internal.h"
27+
28+
/** @var kFirebaseAppName1
29+
@brief A fake Firebase app name.
30+
*/
31+
static NSString *const kFirebaseAppName1 = @"FIREBASE_APP_NAME_1";
32+
33+
/** @var kFirebaseAppName2
34+
@brief Another fake Firebase app name.
35+
*/
36+
static NSString *const kFirebaseAppName2 = @"FIREBASE_APP_NAME_2";
37+
38+
/** @var kAPIKey
39+
@brief The fake API key.
40+
*/
41+
static NSString *const kAPIKey = @"FAKE_API_KEY";
42+
43+
/** @var kExpectationTimeout
44+
@brief The maximum time waiting for expectations to fulfill.
45+
*/
46+
static const NSTimeInterval kExpectationTimeout = 2;
47+
48+
/** @var kWaitInterval
49+
@brief The time waiting for background tasks to finish before continue when necessary.
50+
*/
51+
static const NSTimeInterval kWaitInterval = .5;
52+
53+
@interface FIRAuthLifeCycleTests : XCTestCase
54+
55+
@end
56+
57+
@implementation FIRAuthLifeCycleTests
58+
59+
- (void)setUp {
60+
[super setUp];
61+
62+
[FIRApp resetAppForAuthUnitTests];
63+
}
64+
65+
- (void)tearDown {
66+
[super tearDown];
67+
}
68+
69+
/** @fn testSingleton
70+
@brief Verifies the @c auth method behaves like a singleton.
71+
*/
72+
- (void)testSingleton {
73+
FIRAuth *auth1 = [FIRAuth auth];
74+
XCTAssertNotNil(auth1);
75+
FIRAuth *auth2 = [FIRAuth auth];
76+
XCTAssertEqual(auth1, auth2);
77+
}
78+
79+
/** @fn testDefaultAuth
80+
@brief Verifies the @c auth method associates with the default Firebase app.
81+
*/
82+
- (void)testDefaultAuth {
83+
FIRAuth *auth1 = [FIRAuth auth];
84+
FIRAuth *auth2 = [FIRAuth authWithApp:[FIRApp defaultApp]];
85+
XCTAssertEqual(auth1, auth2);
86+
XCTAssertEqual(auth1.app, [FIRApp defaultApp]);
87+
}
88+
89+
/** @fn testNilAppException
90+
@brief Verifies the @c auth method raises an exception if the default FIRApp is not configured.
91+
*/
92+
- (void)testNilAppException {
93+
[FIRApp resetApps];
94+
XCTAssertThrows([FIRAuth auth]);
95+
}
96+
97+
/** @fn testAppAPIkey
98+
@brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth .
99+
*/
100+
- (void)testAppAPIkey {
101+
FIRAuth *auth = [FIRAuth auth];
102+
XCTAssertEqualObjects(auth.requestConfiguration.APIKey, kAPIKey);
103+
}
104+
105+
/** @fn testAppAssociation
106+
@brief Verifies each @c FIRApp instance associates with a @c FIRAuth .
107+
*/
108+
- (void)testAppAssociation {
109+
FIRApp *app1 = [self app1];
110+
FIRAuth *auth1 = [FIRAuth authWithApp:app1];
111+
XCTAssertNotNil(auth1);
112+
XCTAssertEqual(auth1.app, app1);
113+
114+
FIRApp *app2 = [self app2];
115+
FIRAuth *auth2 = [FIRAuth authWithApp:app2];
116+
XCTAssertNotNil(auth2);
117+
XCTAssertEqual(auth2.app, app2);
118+
119+
XCTAssertNotEqual(auth1, auth2);
120+
}
121+
122+
/** @fn testLifeCycle
123+
@brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp .
124+
*/
125+
- (void)testLifeCycle {
126+
__weak FIRApp *app;
127+
__weak FIRAuth *auth;
128+
@autoreleasepool {
129+
FIRApp *app1 = [self app1];
130+
app = app1;
131+
auth = [FIRAuth authWithApp:app1];
132+
// Verify that neither the app nor the auth is released yet, i.e., the app owns the auth
133+
// because nothing else retains the auth.
134+
XCTAssertNotNil(app);
135+
XCTAssertNotNil(auth);
136+
}
137+
[self waitForTimeIntervel:kWaitInterval];
138+
// Verify that both the app and the auth are released upon exit of the autorelease pool,
139+
// i.e., the app is the sole owner of the auth.
140+
XCTAssertNil(app);
141+
XCTAssertNil(auth);
142+
}
143+
144+
/** @fn app1
145+
@brief Creates a Firebase app.
146+
@return A @c FIRApp with some name.
147+
*/
148+
- (FIRApp *)app1 {
149+
return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName1];
150+
}
151+
152+
/** @fn app2
153+
@brief Creates another Firebase app.
154+
@return A @c FIRApp with some other name.
155+
*/
156+
- (FIRApp *)app2 {
157+
return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName2];
158+
}
159+
160+
/** @fn waitForTimeInterval:
161+
@brief Wait for a particular time interval.
162+
@remarks This method also waits for all other pending @c XCTestExpectation instances.
163+
*/
164+
- (void)waitForTimeIntervel:(NSTimeInterval)timeInterval {
165+
static dispatch_queue_t queue;
166+
static dispatch_once_t onceToken;
167+
XCTestExpectation *expectation = [self expectationWithDescription:@"waitForTimeIntervel:"];
168+
dispatch_once(&onceToken, ^{
169+
queue = dispatch_queue_create("com.google.FIRAuthUnitTests.waitForTimeIntervel", NULL);
170+
});
171+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeInterval * NSEC_PER_SEC), queue, ^() {
172+
[expectation fulfill];
173+
});
174+
[self waitForExpectationsWithTimeout:timeInterval + kExpectationTimeout handler:nil];
175+
}
176+
177+
@end

Example/Auth/Tests/FIRAuthTests.m

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@
6868
#import "FIRPhoneAuthProvider.h"
6969
#endif
7070

71-
/** @var kFirebaseAppName1
72-
@brief A fake Firebase app name.
73-
*/
74-
static NSString *const kFirebaseAppName1 = @"FIREBASE_APP_NAME_1";
75-
76-
/** @var kFirebaseAppName2
77-
@brief Another fake Firebase app name.
78-
*/
79-
static NSString *const kFirebaseAppName2 = @"FIREBASE_APP_NAME_2";
80-
8171
/** @var kAPIKey
8272
@brief The fake API key.
8373
*/
@@ -288,83 +278,6 @@ - (void)tearDown {
288278
[super tearDown];
289279
}
290280

291-
#pragma mark - Life Cycle Tests
292-
293-
/** @fn testSingleton
294-
@brief Verifies the @c auth method behaves like a singleton.
295-
*/
296-
- (void)testSingleton {
297-
FIRAuth *auth1 = [FIRAuth auth];
298-
XCTAssertNotNil(auth1);
299-
FIRAuth *auth2 = [FIRAuth auth];
300-
XCTAssertEqual(auth1, auth2);
301-
}
302-
303-
/** @fn testDefaultAuth
304-
@brief Verifies the @c auth method associates with the default Firebase app.
305-
*/
306-
- (void)testDefaultAuth {
307-
FIRAuth *auth1 = [FIRAuth auth];
308-
FIRAuth *auth2 = [FIRAuth authWithApp:[FIRApp defaultApp]];
309-
XCTAssertEqual(auth1, auth2);
310-
XCTAssertEqual(auth1.app, [FIRApp defaultApp]);
311-
}
312-
313-
/** @fn testNilAppException
314-
@brief Verifies the @c auth method raises an exception if the default FIRApp is not configured.
315-
*/
316-
- (void)testNilAppException {
317-
[FIRApp resetApps];
318-
XCTAssertThrows([FIRAuth auth]);
319-
}
320-
321-
/** @fn testAppAPIkey
322-
@brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth .
323-
*/
324-
- (void)testAppAPIkey {
325-
FIRAuth *auth = [FIRAuth auth];
326-
XCTAssertEqualObjects(auth.requestConfiguration.APIKey, kAPIKey);
327-
}
328-
329-
/** @fn testAppAssociation
330-
@brief Verifies each @c FIRApp instance associates with a @c FIRAuth .
331-
*/
332-
- (void)testAppAssociation {
333-
FIRApp *app1 = [self app1];
334-
FIRAuth *auth1 = [FIRAuth authWithApp:app1];
335-
XCTAssertNotNil(auth1);
336-
XCTAssertEqual(auth1.app, app1);
337-
338-
FIRApp *app2 = [self app2];
339-
FIRAuth *auth2 = [FIRAuth authWithApp:app2];
340-
XCTAssertNotNil(auth2);
341-
XCTAssertEqual(auth2.app, app2);
342-
343-
XCTAssertNotEqual(auth1, auth2);
344-
}
345-
346-
/** @fn testLifeCycle
347-
@brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp .
348-
*/
349-
- (void)testLifeCycle {
350-
__weak FIRApp *app;
351-
__weak FIRAuth *auth;
352-
@autoreleasepool {
353-
FIRApp *app1 = [self app1];
354-
app = app1;
355-
auth = [FIRAuth authWithApp:app1];
356-
// Verify that neither the app nor the auth is released yet, i.e., the app owns the auth
357-
// because nothing else retains the auth.
358-
XCTAssertNotNil(app);
359-
XCTAssertNotNil(auth);
360-
}
361-
[self waitForTimeIntervel:kWaitInterval];
362-
// Verify that both the app and the auth are released upon exit of the autorelease pool,
363-
// i.e., the app is the sole owner of the auth.
364-
XCTAssertNil(app);
365-
XCTAssertNil(auth);
366-
}
367-
368281
#pragma mark - Server API Tests
369282

370283
/** @fn testFetchProvidersForEmailSuccess
@@ -2276,22 +2189,6 @@ - (void)enableAutoTokenRefresh {
22762189
[self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
22772190
}
22782191

2279-
/** @fn app1
2280-
@brief Creates a Firebase app.
2281-
@return A @c FIRApp with some name.
2282-
*/
2283-
- (FIRApp *)app1 {
2284-
return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName1];
2285-
}
2286-
2287-
/** @fn app2
2288-
@brief Creates another Firebase app.
2289-
@return A @c FIRApp with some other name.
2290-
*/
2291-
- (FIRApp *)app2 {
2292-
return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName2];
2293-
}
2294-
22952192
/** @fn stubSecureTokensWithMockResponse
22962193
@brief Creates stubs on the mock response object with access and refresh tokens
22972194
@param mockResponse The mock response object.

Example/Firebase.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
0672F2F31EBBA7D900818E87 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0672F2F11EBBA7D900818E87 /* GoogleService-Info.plist */; };
104104
069428831EC3B38C00F7BC69 /* 1mb.dat in Resources */ = {isa = PBXBuildFile; fileRef = 069428801EC3B35A00F7BC69 /* 1mb.dat */; };
105105
06C24A061EC39BCB005208CA /* FIRStorageIntegrationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 06121ECA1EC39A0B0008D70E /* FIRStorageIntegrationTests.m */; };
106+
405EEF4C2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 405EEF4B2216518A00B08FF4 /* FIRAuthLifeCycleTests.m */; };
107+
405EEF4D2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 405EEF4B2216518A00B08FF4 /* FIRAuthLifeCycleTests.m */; };
108+
405EEF4E2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 405EEF4B2216518A00B08FF4 /* FIRAuthLifeCycleTests.m */; };
106109
408870AB21AE0218008AAE73 /* FIRSignInWithGameCenterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 408870AA21AE0218008AAE73 /* FIRSignInWithGameCenterTests.m */; };
107110
409E1130219FA260000E6CFC /* FIRVerifyIOSClientTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 409E112F219FA260000E6CFC /* FIRVerifyIOSClientTests.m */; };
108111
7E9485421F578AC4005A3939 /* FIRAuthURLPresenterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7E94853F1F578A9D005A3939 /* FIRAuthURLPresenterTests.m */; };
@@ -914,6 +917,7 @@
914917
069428801EC3B35A00F7BC69 /* 1mb.dat */ = {isa = PBXFileReference; lastKnownFileType = file; path = 1mb.dat; sourceTree = "<group>"; };
915918
0697B1201EC13D8A00542174 /* Base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Base64.h; sourceTree = "<group>"; };
916919
0697B1211EC13D8A00542174 /* Base64.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Base64.m; sourceTree = "<group>"; };
920+
405EEF4B2216518A00B08FF4 /* FIRAuthLifeCycleTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRAuthLifeCycleTests.m; sourceTree = "<group>"; };
917921
408870AA21AE0218008AAE73 /* FIRSignInWithGameCenterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRSignInWithGameCenterTests.m; sourceTree = "<group>"; };
918922
409E112F219FA260000E6CFC /* FIRVerifyIOSClientTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRVerifyIOSClientTests.m; sourceTree = "<group>"; };
919923
6003F58D195388D20070C39A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -2174,6 +2178,7 @@
21742178
DE9314FF1E86C6FF0083EDBF /* FIRAuthDispatcherTests.m */,
21752179
DE9315001E86C6FF0083EDBF /* FIRAuthGlobalWorkQueueTests.m */,
21762180
DE9315011E86C6FF0083EDBF /* FIRAuthKeychainTests.m */,
2181+
405EEF4B2216518A00B08FF4 /* FIRAuthLifeCycleTests.m */,
21772182
DE750DB81EB3DD4000A75E47 /* FIRAuthNotificationManagerTests.m */,
21782183
DE9315021E86C6FF0083EDBF /* FIRAuthSerialTaskQueueTests.m */,
21792184
DE9315031E86C6FF0083EDBF /* FIRAuthTests.m */,
@@ -3763,6 +3768,7 @@
37633768
buildActionMask = 2147483647;
37643769
files = (
37653770
D018538D1EDAD364003A645C /* FIRGetOOBConfirmationCodeResponseTests.m in Sources */,
3771+
405EEF4D2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */,
37663772
D018538E1EDAD364003A645C /* FIRGetAccountInfoRequestTests.m in Sources */,
37673773
D018538F1EDAD364003A645C /* FIRSignUpNewUserResponseTests.m in Sources */,
37683774
D01853901EDAD364003A645C /* FIRGetOOBConfirmationCodeRequestTests.m in Sources */,
@@ -4126,6 +4132,7 @@
41264132
DEF6C3191FBCE775005D0740 /* FIRAuthKeychainTests.m in Sources */,
41274133
DEF6C32A1FBCE775005D0740 /* FIRGitHubAuthProviderTests.m in Sources */,
41284134
DEF6C3321FBCE775005D0740 /* FIRSignUpNewUserRequestTests.m in Sources */,
4135+
405EEF4E2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */,
41294136
DEF6C30F1FBCE775005D0740 /* FIRAdditionalUserInfoTests.m in Sources */,
41304137
DEF6C31B1FBCE775005D0740 /* FIRAuthSerialTaskQueueTests.m in Sources */,
41314138
DEF6C3251FBCE775005D0740 /* FIRGetAccountInfoResponseTests.m in Sources */,
@@ -4235,6 +4242,7 @@
42354242
DE9315741E86C71C0083EDBF /* FIRTwitterAuthProviderTests.m in Sources */,
42364243
DE750DC01EB3DD6F00A75E47 /* FIRAuthNotificationManagerTests.m in Sources */,
42374244
DE93156A1E86C71C0083EDBF /* FIRGitHubAuthProviderTests.m in Sources */,
4245+
405EEF4C2216518B00B08FF4 /* FIRAuthLifeCycleTests.m in Sources */,
42384246
DE9315761E86C71C0083EDBF /* FIRVerifyAssertionRequestTests.m in Sources */,
42394247
DE9315781E86C71C0083EDBF /* FIRVerifyCustomTokenRequestTests.m in Sources */,
42404248
DE93157C1E86C71C0083EDBF /* FIRVerifyPhoneNumberRequestTests.m in Sources */,

0 commit comments

Comments
 (0)