Skip to content

Commit 942a23d

Browse files
FirebaseInstanceID integration tests (#4560)
* InstanceID integration tests * ./scripts/style.sh * Travis: run install_prereqs.sh for IID * install_prereqs.sh: run `install_secrets` for IID * Flag to disable IID integration tests * Travis: IID disable integration tests for forks. * ./scripts/style.sh * Temporary disable IID test due to b/147102327 * Travis: IID integration tests enabled for all IID builds.
1 parent 89bd10f commit 942a23d

File tree

5 files changed

+209
-1
lines changed

5 files changed

+209
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Example/Storage/App/GoogleService-Info.plist
1212
# FirebaseInstallations integration tests GoogleService-Info.plist
1313
FirebaseInstallations/Source/Tests/Resources/GoogleService-Info.plist
1414

15+
# FirebaseInstanceID integration tests GoogleService-Info.plist
16+
Example/InstanceID/Resources/GoogleService-Info.plist
17+
1518
Secrets.tar
1619

1720
# OS X

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ jobs:
140140
- stage: test
141141
env:
142142
- PROJECT=InstanceID METHOD=pod-lib-lint
143+
- FIR_IID_INTEGRATION_TESTS_REQUIRED=$(if [[ "$TRAVIS_PULL_REQUEST" == "false" || "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then echo "1"; else echo "0"; fi)
144+
before_install:
145+
- ./scripts/if_changed.sh ./scripts/install_prereqs.sh
143146
script:
144147
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseInstanceID.podspec --platforms=ios
145148
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseInstanceID.podspec --platforms=tvos
@@ -149,13 +152,19 @@ jobs:
149152
osx_image: xcode10.3
150153
env:
151154
- PROJECT=InstanceID METHOD=pod-lib-lint
155+
- FIR_IID_INTEGRATION_TESTS_REQUIRED=$(if [[ "$TRAVIS_PULL_REQUEST" == "false" || "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then echo "1"; else echo "0"; fi)
156+
before_install:
157+
- ./scripts/if_changed.sh ./scripts/install_prereqs.sh
152158
script:
153159
- travis_retry ./scripts/if_changed.sh ./scripts/pod_lib_lint.rb FirebaseInstanceID.podspec --platforms=ios
154160

155161
- stage: test
156162
if: type = cron
157163
env:
158164
- PROJECT=InstanceIDCron METHOD=pod-lib-lint
165+
- FIR_IID_INTEGRATION_TESTS_REQUIRED=$(if [[ "$TRAVIS_PULL_REQUEST" == "false" || "$TRAVIS_PULL_REQUEST_SLUG" == "$TRAVIS_REPO_SLUG" ]]; then echo "1"; else echo "0"; fi)
166+
before_install:
167+
- ./scripts/if_changed.sh ./scripts/install_prereqs.sh
159168
script:
160169
- travis_retry ./scripts/pod_lib_lint.rb FirebaseInstanceID.podspec --platforms=ios --use-libraries
161170
- travis_retry ./scripts/pod_lib_lint.rb FirebaseInstanceID.podspec --platforms=tvos --use-libraries
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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 <FirebaseCore/FirebaseCore.h>
20+
#import <FirebaseInstanceID/FirebaseInstanceID.h>
21+
22+
static BOOL sFIRInstanceIDFirebaseDefaultAppConfigured = NO;
23+
24+
@interface FIRInstanceIDIntegrationTests : XCTestCase
25+
@property(nonatomic, strong) FIRInstanceID *instanceID;
26+
@end
27+
28+
@implementation FIRInstanceIDIntegrationTests
29+
30+
- (void)setUp {
31+
[self configureFirebaseDefaultAppIfCan];
32+
33+
if (![self isDefaultAppConfigured]) {
34+
return;
35+
}
36+
37+
self.instanceID = [FIRInstanceID instanceID];
38+
}
39+
40+
- (void)tearDown {
41+
self.instanceID = nil;
42+
}
43+
44+
- (void)testGetID {
45+
if (![self isDefaultAppConfigured]) {
46+
return;
47+
}
48+
49+
XCTestExpectation *expectation = [self expectationWithDescription:@"getID"];
50+
[self.instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
51+
XCTAssertNil(error);
52+
XCTAssertEqual(identity.length, 22);
53+
[expectation fulfill];
54+
}];
55+
[self waitForExpectations:@[ expectation ] timeout:5];
56+
}
57+
58+
- (void)testInstanceIDWithHandler {
59+
if (![self isDefaultAppConfigured]) {
60+
return;
61+
}
62+
63+
XCTestExpectation *expectation = [self expectationWithDescription:@"instanceIDWithHandler"];
64+
[self.instanceID
65+
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
66+
XCTAssertNil(error);
67+
XCTAssertNotNil(result);
68+
XCTAssert(result.instanceID.length > 0);
69+
XCTAssert(result.token.length > 0);
70+
[expectation fulfill];
71+
}];
72+
73+
[self waitForExpectations:@[ expectation ] timeout:5];
74+
}
75+
76+
- (void)testTokenWithAuthorizedEntity {
77+
if (![self isDefaultAppConfigured]) {
78+
return;
79+
}
80+
81+
[self assertTokenWithAuthorizedEntity];
82+
}
83+
84+
- (void)testDeleteToken {
85+
if (![self isDefaultAppConfigured]) {
86+
return;
87+
}
88+
89+
[self assertTokenWithAuthorizedEntity];
90+
91+
XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteToken"];
92+
[self.instanceID deleteTokenWithAuthorizedEntity:[self tokenAuthorizedEntity]
93+
scope:@"*"
94+
handler:^(NSError *_Nonnull error) {
95+
XCTAssertNil(error);
96+
[expectation fulfill];
97+
}];
98+
99+
[self waitForExpectations:@[ expectation ] timeout:5];
100+
}
101+
102+
// TODO: b/147102327 - re-enable the test once the bug fixed.
103+
- (void)disabled_testDeleteID {
104+
if (![self isDefaultAppConfigured]) {
105+
return;
106+
}
107+
108+
XCTestExpectation *expectation = [self expectationWithDescription:@"deleteID"];
109+
[self.instanceID deleteIDWithHandler:^(NSError *_Nullable error) {
110+
XCTAssertNil(error);
111+
[expectation fulfill];
112+
}];
113+
114+
[self waitForExpectations:@[ expectation ] timeout:5];
115+
}
116+
117+
#pragma mark - Helpers
118+
119+
- (void)assertTokenWithAuthorizedEntity {
120+
XCTestExpectation *expectation = [self expectationWithDescription:@"tokenWithAuthorizedEntity"];
121+
[self.instanceID
122+
tokenWithAuthorizedEntity:[self tokenAuthorizedEntity]
123+
scope:@"*"
124+
options:nil
125+
handler:^(NSString *_Nullable token, NSError *_Nullable error) {
126+
XCTAssertNil(error);
127+
XCTAssert(token > 0);
128+
[expectation fulfill];
129+
}];
130+
131+
[self waitForExpectations:@[ expectation ] timeout:5];
132+
}
133+
134+
- (NSString *)tokenAuthorizedEntity {
135+
if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
136+
return @"";
137+
}
138+
139+
return [FIRApp defaultApp].options.GCMSenderID;
140+
}
141+
142+
- (void)configureFirebaseDefaultAppIfCan {
143+
if (sFIRInstanceIDFirebaseDefaultAppConfigured) {
144+
return;
145+
}
146+
147+
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
148+
NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
149+
if (plistPath == nil) {
150+
return;
151+
}
152+
153+
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
154+
[FIRApp configureWithOptions:options];
155+
sFIRInstanceIDFirebaseDefaultAppConfigured = YES;
156+
}
157+
158+
- (BOOL)isDefaultAppConfigured {
159+
if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
160+
// Fail tests requiring GoogleService-Info.plist only if it is required.
161+
#if FIR_IID_INTEGRATION_TESTS_REQUIRED
162+
XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
163+
@"your project.");
164+
#else
165+
NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
166+
self.name);
167+
#endif // FIR_IID_INTEGRATION_TESTS_REQUIRED
168+
}
169+
170+
return sFIRInstanceIDFirebaseDefaultAppConfigured;
171+
}
172+
173+
@end

FirebaseInstanceID.podspec

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ services.
5555
# store:didDeleteFCMScopedTokensForCheckin:
5656
'OTHER_LDFLAGS' => '-Xlinker -no_objc_category_merging',
5757
'CLANG_ENABLE_OBJC_WEAK' => 'YES'
58-
}
58+
}
5959
end
60+
61+
s.test_spec 'integration' do |int_tests|
62+
int_tests.platforms = {:ios => '8.0', :osx => '10.11', :tvos => '10.0'}
63+
int_tests.source_files = 'Example/InstanceID/IntegrationTests/*.[mh]'
64+
int_tests.resources = 'Example/InstanceID/Resources/**/*'
65+
int_tests.requires_app_host = true
66+
if ENV['FIR_IID_INTEGRATION_TESTS_REQUIRED'] && ENV['FIR_IID_INTEGRATION_TESTS_REQUIRED'] == '1' then
67+
int_tests.pod_target_xcconfig = {
68+
'GCC_PREPROCESSOR_DEFINITIONS' =>
69+
'FIR_IID_INTEGRATION_TESTS_REQUIRED=1'
70+
}
71+
end
72+
end
73+
6074
end

scripts/install_prereqs.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ function install_secrets() {
4949
fis_resources_dir=FirebaseInstallations/Source/Tests/Resources/
5050
mkdir -p "$fis_resources_dir"
5151
cp Secrets/Installations/GoogleService-Info.plist "$fis_resources_dir"
52+
53+
# FirebaseInstanceID
54+
iid_resources_dir=Example/InstanceID/Resources/
55+
mkdir -p "$iid_resources_dir"
56+
cp Secrets/Installations/GoogleService-Info.plist "$iid_resources_dir"
5257
fi
5358
}
5459

@@ -90,6 +95,10 @@ case "$PROJECT-$PLATFORM-$METHOD" in
9095
install_secrets
9196
;;
9297

98+
InstanceID*)
99+
install_secrets
100+
;;
101+
93102
InAppMessaging-*-xcodebuild)
94103
gem install xcpretty
95104
bundle exec pod install --project-directory=InAppMessaging/Example --repo-update

0 commit comments

Comments
 (0)