aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Auth/Tests/FIRAuthBackendCreateAuthURITests.m
blob: 5d403439c480eb9a33fe05d4e82ef9d390e09873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * 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 "FIRAuthErrorUtils.h"
#import "FIRAuthBackend.h"
#import "FIRCreateAuthURIRequest.h"
#import "FIRCreateAuthURIResponse.h"
#import "FIRFakeBackendRPCIssuer.h"

/** @var kTestIdentifier
    @brief A test value for @c FIRCreateAuthURIRequest.identifier
 */
static NSString *const kTestIdentifier = @"identifier_value";

/** @var kTestContinueURI
    @brief A test value for @c FIRCreateAuthURIRequest.continueURI
 */
static NSString *const kTestContinueURI = @"https://www.example.com/";

/** @var kTestAPIKey
    @brief A test value for @c FIRCreateAuthURIRequest.APIKey
 */
static NSString *const kTestAPIKey = @"apikey_value";

/** @var kTestExpectedRequestURL
    @brief The URL we are expecting should be requested by valid requests.
 */
static NSString *const kTestExpectedRequestURL =
    @"https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key=apikey_value";

/** @var kTestExpectedKind
    @brief The expected value for the "kind" parameter of a successful response.
 */
static NSString *const kTestExpectedKind = @"identitytoolkit#CreateAuthUriResponse";

/** @var kTestProviderID1
    @brief A valid value for a provider ID in the @c FIRCreateAuthURIResponse.allProviders array.
 */
static NSString *const kTestProviderID1 = @"google.com";

/** @var kTestProviderID2
    @brief A valid value for a provider ID in the @c FIRCreateAuthURIResponse.allProviders array.
 */
static NSString *const kTestProviderID2 = @"facebook.com";

/** @class FIRAuthBackendCreateAuthURITests
    @brief Unit tests for createAuthURI.
 */
@interface FIRAuthBackendCreateAuthURITests : XCTestCase
@end
@implementation FIRAuthBackendCreateAuthURITests

- (void)testRequestAndResponseEncoding {
  FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];

  FIRCreateAuthURIRequest *request =
      [[FIRCreateAuthURIRequest alloc] initWithIdentifier:kTestIdentifier
                                              continueURI:kTestContinueURI
                                                   APIKey:kTestAPIKey];

  __block FIRCreateAuthURIResponse *createAuthURIResponse;
  __block NSError *createAuthURIError;
  __block BOOL callbackInvoked;
  [FIRAuthBackend createAuthURI:request
                       callback:^(FIRCreateAuthURIResponse *_Nullable response,
                                  NSError *_Nullable error) {
    callbackInvoked = YES;
    createAuthURIResponse = response;
    createAuthURIError = error;
  }];

  XCTAssertEqualObjects(RPCIssuer.requestURL.absoluteString, kTestExpectedRequestURL);
  XCTAssertEqualObjects(RPCIssuer.decodedRequest[@"identifier"], kTestIdentifier);
  XCTAssertEqualObjects(RPCIssuer.decodedRequest[@"continueUri"], kTestContinueURI);

  [RPCIssuer respondWithJSON:@{
    @"kind" : kTestExpectedKind,
    @"allProviders" : @[ kTestProviderID1, kTestProviderID2 ]
  }];

  XCTAssert(callbackInvoked);
  XCTAssertNil(createAuthURIError);
  XCTAssertEqual(createAuthURIResponse.allProviders.count, 2);
  XCTAssertEqualObjects(createAuthURIResponse.allProviders[0], kTestProviderID1);
  XCTAssertEqualObjects(createAuthURIResponse.allProviders[1], kTestProviderID2);
}

@end