Files
firebase-ios-sdk/Functions/Example/IntegrationTests/FIRIntegrationTests.m
T

235 lines
9.1 KiB
Objective-C
Raw Normal View History

2018-03-20 11:59:03 -07:00
// Copyright 2017 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>
2018-03-20 11:59:03 -07:00
#import <FirebaseCore/FIROptions.h>
2018-12-06 13:30:08 -05:00
#import "FIRAuthInteropFake.h"
2018-03-20 11:59:03 -07:00
#import "FIRError.h"
#import "FIRFunctions+Internal.h"
#import "FIRFunctions.h"
#import "FIRHTTPSCallable.h"
#import "FUNFakeInstanceID.h"
2018-12-06 13:30:08 -05:00
// Project ID used by these tests.
static NSString *const kDefaultProjectID = @"functions-integration-test";
2018-12-06 13:30:08 -05:00
2018-03-20 11:59:03 -07:00
@interface FIRIntegrationTests : XCTestCase {
FIRFunctions *_functions;
NSString *_projectID;
BOOL _useLocalhost;
2018-03-20 11:59:03 -07:00
}
@end
@implementation FIRIntegrationTests
- (void)setUp {
[super setUp];
_projectID = kDefaultProjectID;
_useLocalhost = YES;
// Check for configuration of a prod project via GoogleServices-Info.plist.
FIROptions *options = [FIROptions defaultOptions];
if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
_projectID = options.projectID;
_useLocalhost = NO;
}
2018-12-06 13:30:08 -05:00
_functions = [[FIRFunctions alloc]
initWithProjectID:_projectID
2018-12-06 13:30:08 -05:00
region:@"us-central1"
auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]];
if (_useLocalhost) {
[_functions useLocalhost];
}
2018-03-20 11:59:03 -07:00
}
- (void)tearDown {
[super tearDown];
}
- (void)testData {
NSDictionary *data = @{
@"bool" : @YES,
@"int" : @2,
@"long" : @9876543210L,
2018-03-20 11:59:03 -07:00
@"string" : @"four",
@"array" : @[ @5, @6 ],
@"null" : [NSNull null],
};
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"dataTest"];
[function callWithObject:data
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertEqualObjects(@"stub response", result.data[@"message"]);
XCTAssertEqualObjects(@42, result.data[@"code"]);
XCTAssertEqualObjects(@420L, result.data[@"long"]);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testScalar {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
[function callWithObject:@17
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertEqualObjects(@76, result.data);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testToken {
// Recreate _functions with a token.
2018-12-06 13:30:08 -05:00
FIRFunctions *functions = [[FIRFunctions alloc]
initWithProjectID:_projectID
2018-12-06 13:30:08 -05:00
region:@"us-central1"
auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]];
if (_useLocalhost) {
[functions useLocalhost];
}
2018-03-20 11:59:03 -07:00
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [functions HTTPSCallableWithName:@"tokenTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertEqualObjects(@{}, result.data);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testInstanceID {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"instanceIdTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(error);
XCTAssertEqualObjects(@{}, result.data);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testNull {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"nullTest"];
[function callWithObject:[NSNull null]
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertEqualObjects([NSNull null], result.data);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
// Test the version with no arguments.
2018-12-06 13:30:08 -05:00
expectation = [[XCTestExpectation alloc] init];
2018-03-20 11:59:03 -07:00
[function
callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertEqualObjects([NSNull null], result.data);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testMissingResult {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"missingResultTest"];
[function
callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
XCTAssertEqualObjects(@"Response is missing data field.", error.localizedDescription);
2018-03-20 11:59:03 -07:00
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testUnhandledError {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unhandledErrorTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testUnknownError {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unknownErrorTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
XCTAssertEqualObjects(@"INTERNAL", error.localizedDescription);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testExplicitError {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"explicitErrorTest"];
[function
callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeOutOfRange, error.code);
XCTAssertEqualObjects(@"explicit nope", error.userInfo[NSLocalizedDescriptionKey]);
NSDictionary *expectedDetails = @{@"start" : @10, @"end" : @20, @"long" : @30L};
XCTAssertEqualObjects(expectedDetails, error.userInfo[FIRFunctionsErrorDetailsKey]);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testHttpError {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
[function callWithObject:@{}
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
[expectation fulfill];
}];
2018-03-20 11:59:03 -07:00
[self waitForExpectations:@[ expectation ] timeout:10];
}
- (void)testTimeout {
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"timeoutTest"];
function.timeoutInterval = 0.05;
[function
callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(error);
XCTAssertEqual(FIRFunctionsErrorCodeDeadlineExceeded, error.code);
XCTAssertEqualObjects(@"DEADLINE EXCEEDED", error.userInfo[NSLocalizedDescriptionKey]);
XCTAssertNil(error.userInfo[FIRFunctionsErrorDetailsKey]);
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:10];
}
2018-03-20 11:59:03 -07:00
@end