11/*
2- * Copyright 2020 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- */
2+ * Copyright 2020 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+ */
1616
17- #import < XCTest/XCTest.h>
1817#import < OCMock/OCMock.h>
18+ #import < XCTest/XCTest.h>
1919
2020#import " FIRIAMClientInfoFetcher.h"
2121
2222#import < FirebaseInstallations/FIRInstallations.h>
23+ #import < FirebaseInstallations/FIRInstallationsAuthTokenResult.h>
24+
25+ @interface FIRInstallationsAuthTokenResult (Tests)
26+ - (instancetype )initWithToken : (NSString *)token expirationDate : (NSDate *)expirationDate ;
27+ @end
2328
2429@interface FIRIAMClientInfoFetcherTests : XCTestCase
2530
31+ @property (nonatomic , strong ) FIRIAMClientInfoFetcher *clientInfoFetcher;
32+
2633@end
2734
2835@implementation FIRIAMClientInfoFetcherTests
2936
37+ - (NSDate *)future {
38+ return [[NSDate date ] dateByAddingTimeInterval: 10000 ];
39+ }
40+
3041- (void )setUp {
31- [self mockInstanceIDMethodForTokenAndIdentity: @" hey_im_token" tokenError: nil identity: @" hey_im" identityError: nil ];
42+ // Set up for the happy bath where both FID and FIS token are returned.
43+ FIRInstallationsAuthTokenResult *tokenResult =
44+ [[FIRInstallationsAuthTokenResult alloc ] initWithToken: @" mock_token"
45+ expirationDate: [self future ]];
46+ id mockInstallations = [self mockInstanceIDMethodForTokenAndIdentity: tokenResult
47+ tokenError: nil
48+ identity: @" mock_id"
49+ identityError: nil ];
50+
51+ self.clientInfoFetcher =
52+ [[FIRIAMClientInfoFetcher alloc ] initWithFirebaseInstallations: mockInstallations];
53+ }
54+
55+ - (void )testReturnsBothFIDAndToken {
56+ [self .clientInfoFetcher
57+ fetchFirebaseInstallationDataWithProjectNumber: @" my project number"
58+ withCompletion: ^(NSString *_Nullable FID,
59+ NSString *_Nullable FISToken,
60+ NSError *_Nullable error) {
61+ XCTAssertEqualObjects (FID, @" mock_id" );
62+ XCTAssertEqualObjects (FISToken, @" mock_token" );
63+ XCTAssertNil (error);
64+ }];
3265}
3366
34- - (void )tearDown {
35- // Put teardown code here. This method is called after the invocation of each test method in the class.
67+ - (void )testReturnsTokenButNotFID {
68+ // Mock error where no installations ID was fetched.
69+ NSError *error =
70+ [NSError errorWithDomain: @" com.mock.installations"
71+ code: 0
72+ userInfo: @{NSLocalizedDescriptionKey : @" Installations couldn't return FID" }];
73+
74+ FIRInstallationsAuthTokenResult *tokenResult =
75+ [[FIRInstallationsAuthTokenResult alloc ] initWithToken: @" mock_token"
76+ expirationDate: [self future ]];
77+ id mockInstallations = [self mockInstanceIDMethodForTokenAndIdentity: tokenResult
78+ tokenError: nil
79+ identity: nil
80+ identityError: error];
81+
82+ self.clientInfoFetcher =
83+ [[FIRIAMClientInfoFetcher alloc ] initWithFirebaseInstallations: mockInstallations];
84+
85+ [self .clientInfoFetcher
86+ fetchFirebaseInstallationDataWithProjectNumber: @" my project number"
87+ withCompletion: ^(NSString *_Nullable FID,
88+ NSString *_Nullable FISToken,
89+ NSError *_Nullable error) {
90+ // FID should be nil.
91+ XCTAssertNil (FID);
92+ // FIS token is still passed.
93+ XCTAssertEqualObjects (FISToken, @" mock_token" );
94+ // Validate error gets propagated.
95+ XCTAssertEqualObjects (error.localizedDescription ,
96+ @" Installations couldn't return FID" );
97+ }];
3698}
3799
38- - (void )testExample {
39- // This is an example of a functional test case.
40- // Use XCTAssert and related functions to verify your tests produce the correct results.
100+ - (void )testDoesntReturnToken {
101+ // Mock error where no auth token was fetched.
102+ NSError *error = [NSError
103+ errorWithDomain: @" com.mock.installations"
104+ code: 0
105+ userInfo: @{NSLocalizedDescriptionKey : @" Installations couldn't return FIS token" }];
106+
107+ id mockInstallations = [self mockInstanceIDMethodForTokenAndIdentity: nil
108+ tokenError: error
109+ identity: nil
110+ identityError: nil ];
111+
112+ self.clientInfoFetcher =
113+ [[FIRIAMClientInfoFetcher alloc ] initWithFirebaseInstallations: mockInstallations];
114+
115+ OCMReject ([mockInstallations installationIDWithCompletion: [OCMArg any ]]);
116+
117+ [self .clientInfoFetcher
118+ fetchFirebaseInstallationDataWithProjectNumber: @" my project number"
119+ withCompletion: ^(NSString *_Nullable FID,
120+ NSString *_Nullable FISToken,
121+ NSError *_Nullable error) {
122+ XCTAssertNil (FID);
123+ XCTAssertNil (FISToken);
124+ // Validate error gets propagated.
125+ XCTAssertEqualObjects (
126+ error.localizedDescription ,
127+ @" Installations couldn't return FIS token" );
128+ }];
41129}
42130
43- // Mock instance ID methods.
44- - (void )mockInstanceIDMethodForTokenAndIdentity : (nullable NSString *) token
45- tokenError : (nullable NSError *)tokenError
46- identity : (nullable NSString *)identity
47- identityError : (nullable NSError *)identityError {
48- // Mock the installations retreival method.
131+ // Mock FIRInstallations methods.
132+ - (id )mockInstanceIDMethodForTokenAndIdentity :
133+ (nullable FIRInstallationsAuthTokenResult *)tokenResult
134+ tokenError : (nullable NSError *)tokenError
135+ identity : (nullable NSString *)identity
136+ identityError : (nullable NSError *) identityError {
49137 id installationsMock = OCMClassMock ([FIRInstallations class ]);
50138 OCMStub ([installationsMock
51139 installationIDWithCompletion: ([OCMArg
@@ -54,10 +142,11 @@ - (void)mockInstanceIDMethodForTokenAndIdentity:(nullable NSString *)token
54142 : [NSNull null ]),
55143 nil ])]);
56144 OCMStub ([installationsMock
57- authTokenWithCompletion: ([OCMArg invokeBlockWithArgs: (identity ? identity : [NSNull null ]),
58- (identityError ? identityError
59- : [NSNull null ]),
60- nil ])]);
145+ authTokenWithCompletion: ([OCMArg
146+ invokeBlockWithArgs: (tokenResult ? tokenResult : [NSNull null ]),
147+ (tokenError ? tokenError : [NSNull null ]),
148+ nil ])]);
149+ return installationsMock;
61150}
62151
63152@end
0 commit comments