GIDProfileDataFetcherTest.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 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/GIDDataFetcher/Implementations/Fakes/GIDFakeDataFetcher.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. GIDFakeDataFetcher *_dataFetcher;
  28. }
  29. - (void)setUp {
  30. [super setUp];
  31. _dataFetcher = [[GIDFakeDataFetcher alloc] init];
  32. _profileDataFetcher = [[GIDProfileDataFetcher alloc] initWithDataFetcher:_dataFetcher];
  33. }
  34. - (void)testFetchProfileData_outOfAuthState_success {
  35. OIDTokenResponse *tokenResponse =
  36. [OIDTokenResponse testInstanceWithIDToken:[OIDTokenResponse fatIDToken]
  37. accessToken:kAccessToken
  38. expiresIn:@(300)
  39. refreshToken:kRefreshToken
  40. tokenRequest:nil];
  41. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  42. XCTestExpectation *expectation =
  43. [self expectationWithDescription:@"Callback called with no error"];
  44. [_profileDataFetcher fetchProfileDataWithAuthState:authState
  45. completion:^(GIDProfileData *profileData, NSError *error) {
  46. XCTAssertNil(error);
  47. XCTAssertEqualObjects(profileData.name, kFatName);
  48. XCTAssertEqualObjects(profileData.givenName, kFatGivenName);
  49. XCTAssertEqualObjects(profileData.familyName, kFatFamilyName);
  50. XCTAssertTrue(profileData.hasImage);
  51. [expectation fulfill];
  52. }];
  53. [self waitForExpectationsWithTimeout:1 handler:nil];
  54. }
  55. - (void)testFetchProfileData_fromInfoURL_fetchingError {
  56. OIDTokenResponse *tokenResponse =
  57. [OIDTokenResponse testInstanceWithIDToken:nil
  58. accessToken:kAccessToken
  59. expiresIn:@(300)
  60. refreshToken:kRefreshToken
  61. tokenRequest:nil];
  62. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  63. GIDDataFetcherTestBlock testBlock =
  64. ^(GIDDataFetcherFakeResponse response) {
  65. NSError *fetchingError = [self error];
  66. response(nil, fetchingError);
  67. };
  68. [_dataFetcher setTestBlock:testBlock];
  69. XCTestExpectation *expectation =
  70. [self expectationWithDescription:@"Callback called with error"];
  71. [_profileDataFetcher fetchProfileDataWithAuthState:authState
  72. completion:^(GIDProfileData *profileData, NSError *error) {
  73. XCTAssertNil(profileData);
  74. XCTAssertNotNil(error);
  75. XCTAssertEqualObjects(error.domain, kErrorDomain);
  76. XCTAssertEqual(error.code, kErrorCode);
  77. [expectation fulfill];
  78. }];
  79. [self waitForExpectationsWithTimeout:1 handler:nil];
  80. }
  81. // Fetch the NSData successfully but can not parse it.
  82. - (void)testFetchProfileData_fromInfoURL_parsingError {
  83. OIDTokenResponse *tokenResponse =
  84. [OIDTokenResponse testInstanceWithIDToken:nil
  85. accessToken:kAccessToken
  86. expiresIn:@(300)
  87. refreshToken:kRefreshToken
  88. tokenRequest:nil];
  89. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  90. GIDDataFetcherTestBlock testBlock =
  91. ^(GIDDataFetcherFakeResponse response) {
  92. NSData *data = [[NSData alloc] init];
  93. response(data, nil);
  94. };
  95. [_dataFetcher setTestBlock:testBlock];
  96. XCTestExpectation *expectation =
  97. [self expectationWithDescription:@"Callback called with error"];
  98. [_profileDataFetcher fetchProfileDataWithAuthState:authState
  99. completion:^(GIDProfileData *profileData, NSError *error) {
  100. XCTAssertNil(profileData);
  101. XCTAssertNotNil(error);
  102. XCTAssertEqualObjects(error.domain, NSCocoaErrorDomain);
  103. [expectation fulfill];
  104. }];
  105. [self waitForExpectationsWithTimeout:1 handler:nil];
  106. }
  107. #pragma mark - Helpers
  108. - (NSError *)error {
  109. return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
  110. }
  111. @end