GIDProfileDataFetcherTest.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. // Test module imports
  16. @import GoogleSignIn;
  17. #import "GoogleSignIn/Sources/GIDHTTPFetcher/Implementations/Fakes/GIDFakeHTTPFetcher.h"
  18. #import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/GIDProfileDataFetcher.h"
  19. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  20. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  21. static NSString *const kErrorDomain = @"ERROR_DOMAIN";
  22. static NSInteger const kErrorCode = 400;
  23. @interface GIDProfileDataFetcherTest : XCTestCase
  24. @end
  25. @implementation GIDProfileDataFetcherTest {
  26. GIDProfileDataFetcher *_profileDataFetcher;
  27. GIDFakeHTTPFetcher *_httpFetcher;
  28. }
  29. - (void)setUp {
  30. [super setUp];
  31. _httpFetcher = [[GIDFakeHTTPFetcher alloc] init];
  32. _profileDataFetcher = [[GIDProfileDataFetcher alloc] initWithHTTPFetcher:_httpFetcher];
  33. }
  34. // Extracts the `GIDProfileData` out of a fat ID token.
  35. - (void)testFetchProfileData_fatIDToken_success {
  36. NSString *fatIDToken = [OIDTokenResponse fatIDToken];
  37. OIDTokenResponse *tokenResponse =
  38. [OIDTokenResponse testInstanceWithIDToken:fatIDToken
  39. accessToken:kAccessToken
  40. expiresIn:@(300)
  41. refreshToken:kRefreshToken
  42. tokenRequest:nil];
  43. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  44. GIDHTTPFetcherTestBlock testBlock =
  45. ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
  46. XCTFail(@"_httpFetcher should not be invoked");
  47. };
  48. [_httpFetcher setTestBlock:testBlock];
  49. XCTestExpectation *expectation =
  50. [self expectationWithDescription:@"completion is invoked"];
  51. [_profileDataFetcher
  52. fetchProfileDataWithAuthState:authState
  53. completion:^(GIDProfileData *profileData, NSError *error) {
  54. XCTAssertNil(error);
  55. XCTAssertEqualObjects(profileData.name, kFatName);
  56. XCTAssertEqualObjects(profileData.givenName, kFatGivenName);
  57. XCTAssertEqualObjects(profileData.familyName, kFatFamilyName);
  58. XCTAssertTrue(profileData.hasImage);
  59. [expectation fulfill];
  60. }];
  61. [self waitForExpectationsWithTimeout:1 handler:nil];
  62. }
  63. // Can not extracts the `GIDProfileData` out of a non-fat ID token. Try to fetch it from the
  64. // UserInfo endpoint and get an error.
  65. - (void)testFetchProfileData_nonFatIDToken_fetchingError {
  66. NSString *nonFatIDToken = [OIDTokenResponse idToken];
  67. OIDTokenResponse *tokenResponse =
  68. [OIDTokenResponse testInstanceWithIDToken:nonFatIDToken
  69. accessToken:kAccessToken
  70. expiresIn:@(300)
  71. refreshToken:kRefreshToken
  72. tokenRequest:nil];
  73. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  74. NSError *fetchingError = [self error];
  75. [self setGIDHTTPFetcherTestBlockWithData:nil error:fetchingError];
  76. XCTestExpectation *completionExpectation =
  77. [self expectationWithDescription:@"completion is invoked"];
  78. [_profileDataFetcher
  79. fetchProfileDataWithAuthState:authState
  80. completion:^(GIDProfileData *profileData, NSError *error) {
  81. XCTAssertNil(profileData);
  82. XCTAssertNotNil(error);
  83. XCTAssertEqualObjects(error.domain, kErrorDomain);
  84. XCTAssertEqual(error.code, kErrorCode);
  85. [completionExpectation fulfill];
  86. }];
  87. [self waitForExpectationsWithTimeout:1 handler:nil];
  88. }
  89. // There is no ID token. Try to fetch it from the UserInfo endpoint and get an error.
  90. - (void)testFetchProfileData_noIDToken_fetchingError {
  91. OIDTokenResponse *tokenResponse =
  92. [OIDTokenResponse testInstanceWithIDToken:nil
  93. accessToken:kAccessToken
  94. expiresIn:@(300)
  95. refreshToken:kRefreshToken
  96. tokenRequest:nil];
  97. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  98. NSError *fetchingError = [self error];
  99. [self setGIDHTTPFetcherTestBlockWithData:nil error:fetchingError];
  100. XCTestExpectation *completionExpectation =
  101. [self expectationWithDescription:@"completion is invoked"];
  102. [_profileDataFetcher
  103. fetchProfileDataWithAuthState:authState
  104. completion:^(GIDProfileData *profileData, NSError *error) {
  105. XCTAssertNil(profileData);
  106. XCTAssertNotNil(error);
  107. XCTAssertEqualObjects(error.domain, kErrorDomain);
  108. XCTAssertEqual(error.code, kErrorCode);
  109. [completionExpectation fulfill];
  110. }];
  111. [self waitForExpectationsWithTimeout:1 handler:nil];
  112. }
  113. // Fetch the NSData from the UserInfo endpoint successfully but can not parse it.
  114. - (void)testFetchProfileData_noIDToken_parsingError {
  115. OIDTokenResponse *tokenResponse =
  116. [OIDTokenResponse testInstanceWithIDToken:nil
  117. accessToken:kAccessToken
  118. expiresIn:@(300)
  119. refreshToken:kRefreshToken
  120. tokenRequest:nil];
  121. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  122. NSData *data = [[NSData alloc] init];
  123. [self setGIDHTTPFetcherTestBlockWithData:data error:nil];
  124. XCTestExpectation *completionExpectation =
  125. [self expectationWithDescription:@"completion is invoked"];
  126. [_profileDataFetcher
  127. fetchProfileDataWithAuthState:authState
  128. completion:^(GIDProfileData *profileData, NSError *error) {
  129. XCTAssertNil(profileData);
  130. XCTAssertNotNil(error);
  131. XCTAssertEqualObjects(error.domain, NSCocoaErrorDomain);
  132. [completionExpectation fulfill];
  133. }];
  134. [self waitForExpectationsWithTimeout:1 handler:nil];
  135. }
  136. #pragma mark - Helpers
  137. - (NSError *)error {
  138. return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
  139. }
  140. /// Set the return value from the UserInfo endpoint.
  141. - (void)setGIDHTTPFetcherTestBlockWithData:(NSData *)data
  142. error:(NSError *)error {
  143. XCTestExpectation *fetcherExpectation =
  144. [self expectationWithDescription:@"_httpFetcher is invoked"];
  145. GIDHTTPFetcherTestBlock testBlock =
  146. ^(NSURLRequest *request, GIDHTTPFetcherFakeResponseProviderBlock responseProvider) {
  147. responseProvider(data, error);
  148. [fetcherExpectation fulfill];
  149. };
  150. [_httpFetcher setTestBlock:testBlock];
  151. }
  152. @end