diff options
author | Zsika Phillip <protocol86@users.noreply.github.com> | 2017-08-24 13:58:50 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-24 13:58:50 -0700 |
commit | fb1b4ba5879e4118d73dc70f83d7678407418870 (patch) | |
tree | 1b00ea36ab7ea6fa6fc8ca95cbe7be69dbe0f992 /Example/Auth | |
parent | 1a857fe48fe32c9768143a81b009fe62a34fd0ec (diff) |
Add get Auth domain RPC (#214)
* Add get Auth domain RPC
* Fixes indentation
* Addresses comments
* ran tests on Example/Firebase.xcodeproj
Diffstat (limited to 'Example/Auth')
4 files changed, 218 insertions, 3 deletions
diff --git a/Example/Auth/Tests/FIRAuthBackendRPCImplementationTests.m b/Example/Auth/Tests/FIRAuthBackendRPCImplementationTests.m index cf26546..4a718cd 100644 --- a/Example/Auth/Tests/FIRAuthBackendRPCImplementationTests.m +++ b/Example/Auth/Tests/FIRAuthBackendRPCImplementationTests.m @@ -236,6 +236,10 @@ static NSString *const kTestValue = @"TestValue"; return [NSURL URLWithString:kFakeRequestURL]; } +- (BOOL)containsPostBody { + return YES; +} + - (FIRAuthRequestConfiguration *)requestConfiguration { FIRAuthRequestConfiguration *fakeConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kFakeAPIkey]; diff --git a/Example/Auth/Tests/FIRFakeBackendRPCIssuer.m b/Example/Auth/Tests/FIRFakeBackendRPCIssuer.m index bdc7706..0dbbf23 100644 --- a/Example/Auth/Tests/FIRFakeBackendRPCIssuer.m +++ b/Example/Auth/Tests/FIRFakeBackendRPCIssuer.m @@ -34,9 +34,11 @@ static NSString *const kFakeErrorDomain = @"fake domain"; contentType:(NSString *)contentType completionHandler:(FIRAuthBackendRPCIssuerCompletionHandler)handler { _requestURL = [URL copy]; - _requestData = body; - NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:body options:0 error:nil]; - _decodedRequest = JSON; + if (body) { + _requestData = body; + NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:body options:0 error:nil]; + _decodedRequest = JSON; + } _contentType = contentType; _handler = handler; } diff --git a/Example/Auth/Tests/FIRGetProjectConfigRequestTests.m b/Example/Auth/Tests/FIRGetProjectConfigRequestTests.m new file mode 100644 index 0000000..4c163e8 --- /dev/null +++ b/Example/Auth/Tests/FIRGetProjectConfigRequestTests.m @@ -0,0 +1,73 @@ +/* + * 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> + +#import "FIRAuthBackend.h" +#import "FIRGetProjectConfigRequest.h" +#import "FIRGetProjectConfigResponse.h" +#import "FIRFakeBackendRPCIssuer.h" + +/** @var kTestAPIKey + @brief Fake API key used for testing. + */ +static NSString *const kTestAPIKey = @"APIKey"; + +@interface FIRGetProjectConfigRequestTests : XCTestCase +@end + +@implementation FIRGetProjectConfigRequestTests { + /** @var _RPCIssuer + @brief This backend RPC issuer is used to fake network responses for each test in the suite. + In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it. + */ + FIRFakeBackendRPCIssuer *_RPCIssuer; +} + +- (void)setUp { + [super setUp]; + FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init]; + [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer]; + _RPCIssuer = RPCIssuer; +} + +- (void)tearDown { + _RPCIssuer = nil; + [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil]; + [super tearDown]; +} + +/** @fn testGetProjectConfigRequest + @brief Tests get project config request. + */ +- (void)testGetProjectConfigRequest { + FIRAuthRequestConfiguration *requestConfiguration = + [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey]; + FIRGetProjectConfigRequest *request = + [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration]; + + [FIRAuthBackend getProjectConfig:request + callback:^(FIRGetProjectConfigResponse *_Nullable response, + NSError *_Nullable error) { + + + }]; + XCTAssertFalse([request containsPostBody]); + // Confirm that the quest has no decoded body as it is get request. + XCTAssertNil(_RPCIssuer.decodedRequest); +} + +@end diff --git a/Example/Auth/Tests/FIRGetProjectConfigResponseTests.m b/Example/Auth/Tests/FIRGetProjectConfigResponseTests.m new file mode 100644 index 0000000..f8f11ef --- /dev/null +++ b/Example/Auth/Tests/FIRGetProjectConfigResponseTests.m @@ -0,0 +1,136 @@ +/* + * 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> + +#import "FIRAuthBackend.h" +#import "FIRAuthErrors.h" +#import "FIRAuthInternalErrors.h" +#import "FIRGetProjectConfigRequest.h" +#import "FIRGetProjectConfigResponse.h" +#import "FIRFakeBackendRPCIssuer.h" + +/** @var kTestAPIKey + @brief Fake API key used for testing. + */ +static NSString *const kTestAPIKey = @"APIKey"; + +/** @var kTestProjectID + @brief Fake project ID used for testing. + */ +static NSString *const kTestProjectID = @"21141651616"; + +/** @var kTestDomain1 + @brief Fake whitelisted domain used for testing. + */ +static NSString *const kTestDomain1 = @"localhost"; + +/** @var kTestDomain2 + @brief Fake whitelisted domain used for testing. + */ +static NSString *const kTestDomain2 = @"example.firebaseapp.com"; + +/** @var kMissingAPIKeyErrorMessage + @brief The error message the server would respond with if the API Key was missing. + */ +static NSString *const kMissingAPIKeyErrorMessage = @"MISSING_API_KEY"; + +@interface FIRGetProjectConfigResponseTests : XCTestCase +@end + +@implementation FIRGetProjectConfigResponseTests { + /** @var _RPCIssuer + @brief This backend RPC issuer is used to fake network responses for each test in the suite. + In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it. + */ + FIRFakeBackendRPCIssuer *_RPCIssuer; +} + +- (void)setUp { + [super setUp]; + FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init]; + [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer]; + _RPCIssuer = RPCIssuer; +} + +- (void)tearDown { + _RPCIssuer = nil; + [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil]; + [super tearDown]; +} + +/** @fn testMissingAPIKeyError + @brief This test simulates a missing API key error. Since the API key is provided to the backend + from the auth library this error should map to an internal error. + */ +- (void)testMissingAPIKeyError { + FIRAuthRequestConfiguration *requestConfiguration = + [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey]; + FIRGetProjectConfigRequest *request = + [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration]; + + __block BOOL callbackInvoked; + __block FIRGetProjectConfigResponse *RPCResponse; + __block NSError *RPCError; + [FIRAuthBackend getProjectConfig:request + callback:^(FIRGetProjectConfigResponse *_Nullable response, + NSError *_Nullable error) { + callbackInvoked = YES; + RPCResponse = response; + RPCError = error; + }]; + + [_RPCIssuer respondWithServerErrorMessage:kMissingAPIKeyErrorMessage]; + + XCTAssert(callbackInvoked); + XCTAssertNotNil(RPCError); + XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain); + XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError); + XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]); +} + +/** @fn testSuccessFulGetProjectConfigRequest + @brief This test simulates a successful @c getProjectConfig Flow. + */ +- (void)testSuccessFulGetProjectConfigRequest { + FIRAuthRequestConfiguration *requestConfiguration = + [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey]; + FIRGetProjectConfigRequest *request = + [[FIRGetProjectConfigRequest alloc] initWithRequestConfiguration:requestConfiguration]; + + __block BOOL callbackInvoked; + __block FIRGetProjectConfigResponse *RPCResponse; + __block NSError *RPCError; + [FIRAuthBackend getProjectConfig:request + callback:^(FIRGetProjectConfigResponse *_Nullable response, + NSError *_Nullable error) { + callbackInvoked = YES; + RPCResponse = response; + RPCError = error; + }]; + + [_RPCIssuer respondWithJSON:@{ + @"projectId": kTestProjectID, + @"authorizedDomains": @[ kTestDomain1, kTestDomain2 ] + }]; + XCTAssert(callbackInvoked); + XCTAssertNil(RPCError); + XCTAssertEqualObjects(kTestProjectID, RPCResponse.projectID); + XCTAssertEqualObjects(kTestDomain1, RPCResponse.authorizedDomains[0]); + XCTAssertEqualObjects(kTestDomain2, RPCResponse.authorizedDomains[1]); +} + +@end |