| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- // Copyright 2022 Google LLC
- //
- // 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>
- // Test module imports
- @import GoogleSignIn;
- #import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/Fakes/GIDFakeDataFetcher.h"
- #import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
- #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
- #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
- static NSString *const kErrorDomain = @"ERROR_DOMAIN";
- static NSInteger const kErrorCode = 400;
- @interface GIDProfileDataFetcherTest : XCTestCase
- @end
- @implementation GIDProfileDataFetcherTest {
- GIDProfileDataFetcher *_profileDataFetcher;
- GIDFakeDataFetcher *_dataFetcher;
- }
- - (void)setUp {
- [super setUp];
- _dataFetcher = [[GIDFakeDataFetcher alloc] init];
- _profileDataFetcher = [[GIDProfileDataFetcher alloc] initWithDataFetcher:_dataFetcher];
- }
- - (void)testFetchProfileData_outOfAuthState_success {
- OIDTokenResponse *tokenResponse =
- [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken]
- accessToken:kAccessToken
- expiresIn:@(300)
- refreshToken:kRefreshToken
- tokenRequest:nil];
-
- OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
-
- XCTestExpectation *expectation =
- [self expectationWithDescription:@"Callback called with no error"];
-
- [_profileDataFetcher fetchProfileDataWithAuthState:authState
- completion:^(GIDProfileData *profileData, NSError *error) {
- XCTAssertNil(error);
- XCTAssertEqualObjects(profileData.name, kFatName);
- XCTAssertEqualObjects(profileData.givenName, kFatGivenName);
- XCTAssertEqualObjects(profileData.familyName, kFatFamilyName);
- XCTAssertTrue(profileData.hasImage);
- [expectation fulfill];
- }];
-
- [self waitForExpectationsWithTimeout:1 handler:nil];
- }
- - (void)testFetchProfileData_fromInfoURL_fetchingError {
- OIDTokenResponse *tokenResponse =
- [OIDTokenResponse testInstanceWithIDToken:nil
- accessToken:kAccessToken
- expiresIn:@(300)
- refreshToken:kRefreshToken
- tokenRequest:nil];
-
- OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
-
- GIDDataFetcherTestBlock testBlock =
- ^(GIDDataFetcherFakeResponse response) {
- NSError *fetchingError = [self error];
- response(nil, fetchingError);
- };
- [_dataFetcher setTestBlock:testBlock];
-
- XCTestExpectation *expectation =
- [self expectationWithDescription:@"Callback called with error"];
-
- [_profileDataFetcher fetchProfileDataWithAuthState:authState
- completion:^(GIDProfileData *profileData, NSError *error) {
- XCTAssertNil(profileData);
- XCTAssertNotNil(error);
- XCTAssertEqualObjects(error.domain, kErrorDomain);
- XCTAssertEqual(error.code, kErrorCode);
- [expectation fulfill];
- }];
-
- [self waitForExpectationsWithTimeout:1 handler:nil];
- }
- // Fetch the NSData successfully but can not parse it.
- - (void)testFetchProfileData_fromInfoURL_parsingError {
- OIDTokenResponse *tokenResponse =
- [OIDTokenResponse testInstanceWithIDToken:nil
- accessToken:kAccessToken
- expiresIn:@(300)
- refreshToken:kRefreshToken
- tokenRequest:nil];
-
- OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
-
- GIDDataFetcherTestBlock testBlock =
- ^(GIDDataFetcherFakeResponse response) {
- NSData *data = [[NSData alloc] init];
- response(data, nil);
- };
- [_dataFetcher setTestBlock:testBlock];
-
- XCTestExpectation *expectation =
- [self expectationWithDescription:@"Callback called with error"];
-
- [_profileDataFetcher fetchProfileDataWithAuthState:authState
- completion:^(GIDProfileData *profileData, NSError *error) {
- XCTAssertNil(profileData);
- XCTAssertNotNil(error);
- XCTAssertEqualObjects(error.domain, NSCocoaErrorDomain);
- [expectation fulfill];
- }];
-
- [self waitForExpectationsWithTimeout:1 handler:nil];
- }
- #pragma mark - Helpers
- - (NSError *)error {
- return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
- }
- @end
|