|
| 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 |
0 commit comments