|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | + */ |
| 16 | + |
| 17 | +#import <XCTest/XCTest.h> |
| 18 | + |
| 19 | +#import "FIRAuthApiTestsBase.h" |
| 20 | + |
| 21 | +/** The user name string for Custom Auth testing account. */ |
| 22 | +static NSString *const kCustomAuthTestingAccountUserID = KCUSTOM_AUTH_USER_ID; |
| 23 | + |
| 24 | +/** The url for obtaining a valid custom token string used to test Custom Auth. */ |
| 25 | +static NSString *const kCustomTokenUrl = KCUSTOM_AUTH_TOKEN_URL; |
| 26 | + |
| 27 | +/** The url for obtaining an expired but valid custom token string used to test Custom Auth failure. |
| 28 | + */ |
| 29 | +static NSString *const kExpiredCustomTokenUrl = KCUSTOM_AUTH_TOKEN_EXPIRED_URL; |
| 30 | + |
| 31 | +/** The invalid custom token string for testing Custom Auth. */ |
| 32 | +static NSString *const kInvalidCustomToken = @"invalid token."; |
| 33 | + |
| 34 | +/** Error message for invalid custom token sign in. */ |
| 35 | +NSString *kInvalidTokenErrorMessage = |
| 36 | + @"Invalid assertion format. 3 dot separated segments required."; |
| 37 | + |
| 38 | +@interface CustomAuthTests : FIRAuthApiTestsBase |
| 39 | + |
| 40 | +@end |
| 41 | + |
| 42 | +@implementation CustomAuthTests |
| 43 | + |
| 44 | +- (void)testSignInWithValidCustomAuthToken { |
| 45 | + FIRAuth *auth = [FIRAuth auth]; |
| 46 | + if (!auth) { |
| 47 | + XCTFail(@"Could not obtain auth object."); |
| 48 | + } |
| 49 | + |
| 50 | + NSError *error; |
| 51 | + NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl] |
| 52 | + encoding:NSUTF8StringEncoding |
| 53 | + error:&error]; |
| 54 | + if (!customToken) { |
| 55 | + XCTFail(@"There was an error retrieving the custom token: %@", error); |
| 56 | + } |
| 57 | + NSLog(@"The valid token is: %@", customToken); |
| 58 | + |
| 59 | + XCTestExpectation *expectation = |
| 60 | + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; |
| 61 | + |
| 62 | + [auth signInWithCustomToken:customToken |
| 63 | + completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { |
| 64 | + if (error) { |
| 65 | + NSLog(@"Valid token sign in error: %@", error); |
| 66 | + } |
| 67 | + [expectation fulfill]; |
| 68 | + }]; |
| 69 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 70 | + handler:^(NSError *error) { |
| 71 | + if (error != nil) { |
| 72 | + XCTFail(@"Failed to wait for expectations " |
| 73 | + @"in CustomAuthToken sign in. Error: %@", |
| 74 | + error.localizedDescription); |
| 75 | + } |
| 76 | + }]; |
| 77 | + |
| 78 | + XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID); |
| 79 | +} |
| 80 | + |
| 81 | +- (void)testSignInWithValidCustomAuthExpiredToken { |
| 82 | + FIRAuth *auth = [FIRAuth auth]; |
| 83 | + if (!auth) { |
| 84 | + XCTFail(@"Could not obtain auth object."); |
| 85 | + } |
| 86 | + |
| 87 | + NSError *error; |
| 88 | + NSString *customToken = |
| 89 | + [NSString stringWithContentsOfURL:[NSURL URLWithString:kExpiredCustomTokenUrl] |
| 90 | + encoding:NSUTF8StringEncoding |
| 91 | + error:&error]; |
| 92 | + if (!customToken) { |
| 93 | + XCTFail(@"There was an error retrieving the custom token: %@", error); |
| 94 | + } |
| 95 | + XCTestExpectation *expectation = |
| 96 | + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; |
| 97 | + |
| 98 | + __block NSError *apiError; |
| 99 | + [auth signInWithCustomToken:customToken |
| 100 | + completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { |
| 101 | + if (error) { |
| 102 | + apiError = error; |
| 103 | + } |
| 104 | + [expectation fulfill]; |
| 105 | + }]; |
| 106 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 107 | + handler:^(NSError *error) { |
| 108 | + if (error != nil) { |
| 109 | + XCTFail(@"Failed to wait for expectations " |
| 110 | + @"in CustomAuthToken sign in. Error: %@", |
| 111 | + error.localizedDescription); |
| 112 | + } |
| 113 | + }]; |
| 114 | + |
| 115 | + XCTAssertNil(auth.currentUser); |
| 116 | + XCTAssertEqual(apiError.code, FIRAuthErrorCodeInvalidCustomToken); |
| 117 | +} |
| 118 | + |
| 119 | +- (void)testSignInWithInvalidCustomAuthToken { |
| 120 | + FIRAuth *auth = [FIRAuth auth]; |
| 121 | + if (!auth) { |
| 122 | + XCTFail(@"Could not obtain auth object."); |
| 123 | + } |
| 124 | + XCTestExpectation *expectation = |
| 125 | + [self expectationWithDescription:@"Invalid CustomAuthToken sign-in finished."]; |
| 126 | + |
| 127 | + [auth signInWithCustomToken:kInvalidCustomToken |
| 128 | + completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { |
| 129 | + XCTAssertEqualObjects(error.localizedDescription, kInvalidTokenErrorMessage); |
| 130 | + [expectation fulfill]; |
| 131 | + }]; |
| 132 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 133 | + handler:^(NSError *error) { |
| 134 | + if (error != nil) { |
| 135 | + XCTFail(@"Failed to wait for expectations " |
| 136 | + @"in CustomAuthToken sign in. Error: %@", |
| 137 | + error.localizedDescription); |
| 138 | + } |
| 139 | + }]; |
| 140 | +} |
| 141 | + |
| 142 | +- (void)testInMemoryUserAfterSignOut { |
| 143 | + FIRAuth *auth = [FIRAuth auth]; |
| 144 | + if (!auth) { |
| 145 | + XCTFail(@"Could not obtain auth object."); |
| 146 | + } |
| 147 | + NSError *error; |
| 148 | + NSString *customToken = [NSString stringWithContentsOfURL:[NSURL URLWithString:kCustomTokenUrl] |
| 149 | + encoding:NSUTF8StringEncoding |
| 150 | + error:&error]; |
| 151 | + if (!customToken) { |
| 152 | + XCTFail(@"There was an error retrieving the custom token: %@", error); |
| 153 | + } |
| 154 | + XCTestExpectation *expectation = |
| 155 | + [self expectationWithDescription:@"CustomAuthToken sign-in finished."]; |
| 156 | + __block NSError *rpcError; |
| 157 | + [auth signInWithCustomToken:customToken |
| 158 | + completion:^(FIRAuthDataResult *_Nullable result, NSError *_Nullable error) { |
| 159 | + if (error) { |
| 160 | + rpcError = error; |
| 161 | + } |
| 162 | + [expectation fulfill]; |
| 163 | + }]; |
| 164 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout |
| 165 | + handler:^(NSError *error) { |
| 166 | + if (error != nil) { |
| 167 | + XCTFail(@"Failed to wait for expectations " |
| 168 | + @"in CustomAuthToken sign in. Error: %@", |
| 169 | + error.localizedDescription); |
| 170 | + } |
| 171 | + }]; |
| 172 | + XCTAssertEqualObjects(auth.currentUser.uid, kCustomAuthTestingAccountUserID); |
| 173 | + XCTAssertNil(rpcError); |
| 174 | + FIRUser *inMemoryUser = auth.currentUser; |
| 175 | + XCTestExpectation *expectation1 = [self expectationWithDescription:@"Profile data change."]; |
| 176 | + [auth signOut:NULL]; |
| 177 | + rpcError = nil; |
| 178 | + NSString *newEmailAddress = [self fakeRandomEmail]; |
| 179 | + XCTAssertNotEqualObjects(newEmailAddress, inMemoryUser.email); |
| 180 | + [inMemoryUser updateEmail:newEmailAddress |
| 181 | + completion:^(NSError *_Nullable error) { |
| 182 | + rpcError = error; |
| 183 | + [expectation1 fulfill]; |
| 184 | + }]; |
| 185 | + [self waitForExpectationsWithTimeout:kExpectationsTimeout handler:nil]; |
| 186 | + XCTAssertEqualObjects(inMemoryUser.email, newEmailAddress); |
| 187 | + XCTAssertNil(rpcError); |
| 188 | + XCTAssertNil(auth.currentUser); |
| 189 | +} |
| 190 | + |
| 191 | +@end |
0 commit comments