2019-01-22 14:09:48 -05:00
|
|
|
// Copyright 2019 Google
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
|
|
|
|
#import "FIRAuthInteropFake.h"
|
2020-06-19 12:14:22 -07:00
|
|
|
#import "Functions/FirebaseFunctions/FUNContext.h"
|
2020-09-03 14:45:56 -04:00
|
|
|
#import "SharedTestUtilities/FIRMessagingInteropFake.h"
|
2019-01-22 14:09:48 -05:00
|
|
|
|
|
|
|
|
@interface FUNContextProviderTests : XCTestCase
|
2020-09-03 14:45:56 -04:00
|
|
|
@property(nonatomic) FIRMessagingInteropFake *messagingFake;
|
2019-01-22 14:09:48 -05:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FUNContextProviderTests
|
|
|
|
|
|
2020-09-03 14:45:56 -04:00
|
|
|
- (void)setUp {
|
|
|
|
|
self.messagingFake = [[FIRMessagingInteropFake alloc] init];
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 14:09:48 -05:00
|
|
|
- (void)testContextWithAuth {
|
|
|
|
|
FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:@"token"
|
|
|
|
|
userID:@"userID"
|
|
|
|
|
error:nil];
|
2020-09-03 14:45:56 -04:00
|
|
|
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
|
|
|
|
|
messaging:self.messagingFake];
|
2019-01-22 14:09:48 -05:00
|
|
|
XCTestExpectation *expectation =
|
|
|
|
|
[self expectationWithDescription:@"Context should have auth keys."];
|
|
|
|
|
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
|
|
|
|
|
XCTAssert([context.authToken isEqualToString:@"token"]);
|
2020-09-03 14:45:56 -04:00
|
|
|
XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
|
2019-01-22 14:09:48 -05:00
|
|
|
XCTAssertNil(error);
|
|
|
|
|
[expectation fulfill];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self waitForExpectations:@[ expectation ] timeout:0.1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testContextWithAuthError {
|
|
|
|
|
NSError *authError = [[NSError alloc] initWithDomain:@"com.functions.tests" code:4 userInfo:nil];
|
|
|
|
|
FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:nil
|
|
|
|
|
userID:nil
|
|
|
|
|
error:authError];
|
2020-09-03 14:45:56 -04:00
|
|
|
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
|
|
|
|
|
messaging:self.messagingFake];
|
2019-01-22 14:09:48 -05:00
|
|
|
XCTestExpectation *expectation =
|
|
|
|
|
[self expectationWithDescription:@"Completion handler should fail with Auth error."];
|
|
|
|
|
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
|
|
|
|
|
XCTAssertNil(context);
|
|
|
|
|
XCTAssertEqual(error, auth.error);
|
|
|
|
|
[expectation fulfill];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self waitForExpectations:@[ expectation ] timeout:0.1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testContextWithoutAuth {
|
2020-09-03 14:45:56 -04:00
|
|
|
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil messaging:nil];
|
2019-01-22 14:09:48 -05:00
|
|
|
XCTestExpectation *expectation =
|
|
|
|
|
[self expectationWithDescription:@"Completion handler should succeed without Auth."];
|
|
|
|
|
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
|
|
|
|
|
XCTAssertNil(error);
|
|
|
|
|
XCTAssertNil(context.authToken);
|
2020-09-03 14:45:56 -04:00
|
|
|
XCTAssertNil(context.FCMToken);
|
2019-01-22 14:09:48 -05:00
|
|
|
[expectation fulfill];
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
[self waitForExpectations:@[ expectation ] timeout:0.1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|