GIDAuthorizationResponseHelperTest.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2024 Google LLC
  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. #import <XCTest/XCTest.h>
  17. #import "GoogleSignIn/Sources/GIDAuthorizationResponse/GIDAuthorizationResponseHelper.h"
  18. #import "GoogleSignIn/Sources/GIDAuthorizationResponse/Fake/GIDAuthorizationResponseHandlingFake.h"
  19. #import "GoogleSignIn/Sources/GIDAuthorizationResponse/Implementations/GIDAuthorizationResponseHandler.h"
  20. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  21. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  22. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifyAccountDetail.h"
  23. #import "GoogleSignIn/Sources/GIDAuthFlow.h"
  24. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
  25. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  26. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  27. /// The EMM support version
  28. static NSString *const kEMMVersion = @"1";
  29. /// Time interval to use for an expiring access token.
  30. static NSTimeInterval kAccessTokenExpiration = 20;
  31. @interface GIDAuthorizationResponseHelperTest : XCTestCase
  32. @end
  33. @implementation GIDAuthorizationResponseHelperTest
  34. - (void)testInitWithAuthorizationResponseHandler {
  35. GIDAuthorizationResponseHandler *responseHandler =
  36. [[GIDAuthorizationResponseHandler alloc] initWithAuthorizationResponse:nil
  37. emmSupport:nil
  38. flowName:GIDFlowNameVerifyAccountDetail
  39. configuration:nil
  40. error:nil];
  41. GIDAuthorizationResponseHelper *responseHelper =
  42. [[GIDAuthorizationResponseHelper alloc] initWithAuthorizationResponseHandler:responseHandler];
  43. XCTAssertNotNil(responseHelper);
  44. XCTAssertNotNil(responseHelper.responseHandler);
  45. }
  46. - (void)testFetchTokenWithAuthFlow {
  47. OIDTokenResponse *tokenResponse =
  48. [OIDTokenResponse testInstanceWithAccessTokenExpiration:@(kAccessTokenExpiration)];
  49. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  50. GIDAuthorizationResponseHandlingFake *responseHandler =
  51. [[GIDAuthorizationResponseHandlingFake alloc] initWithAuthState:authState error:nil];
  52. GIDAuthorizationResponseHelper *responseHelper =
  53. [[GIDAuthorizationResponseHelper alloc] initWithAuthorizationResponseHandler:responseHandler];
  54. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] initWithAuthState:authState
  55. error:nil
  56. emmSupport:nil
  57. profileData:nil];
  58. [responseHelper fetchTokenWithAuthFlow:authFlow];
  59. XCTAssertNotNil(authFlow);
  60. XCTAssertEqual(authFlow.authState, authState);
  61. XCTAssertNil(authFlow.error);
  62. }
  63. - (void)testSuccessfulGenerateAuthFlowFromAuthorizationResponse {
  64. OIDTokenResponse *tokenResponse =
  65. [OIDTokenResponse testInstanceWithAccessTokenExpiration:@(kAccessTokenExpiration)];
  66. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  67. GIDAuthorizationResponseHandlingFake *responseHandler =
  68. [[GIDAuthorizationResponseHandlingFake alloc] initWithAuthState:authState error:nil];
  69. GIDAuthFlow *authFlow = [responseHandler generateAuthFlowFromAuthorizationResponse];
  70. XCTAssertNotNil(authFlow);
  71. XCTAssertEqual(authFlow.authState, authState);
  72. XCTAssertNil(authFlow.error);
  73. }
  74. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  75. - (void)testGenerateAuthFlowFromAuthorizationResponse_noCode {
  76. NSDictionary<NSString *, NSString *> *errorDict =
  77. @{ NSLocalizedDescriptionKey : @"Unknown error" };
  78. NSError *expectedError = [NSError errorWithDomain:kGIDVerifyErrorDomain
  79. code:GIDVerifyErrorCodeUnknown
  80. userInfo:errorDict];
  81. OIDAuthorizationResponse *authorizationResponse =
  82. [OIDAuthorizationResponse testInstanceNoAuthCodeWithAdditionalParameters:nil
  83. errorString:nil];
  84. GIDAuthorizationResponseHandler *responseHandler =
  85. [[GIDAuthorizationResponseHandler alloc] initWithAuthorizationResponse:authorizationResponse
  86. emmSupport:nil
  87. flowName:GIDFlowNameVerifyAccountDetail
  88. configuration:nil
  89. error:nil];
  90. GIDAuthFlow *authFlow = [responseHandler generateAuthFlowFromAuthorizationResponse];
  91. XCTAssertNotNil(authFlow);
  92. XCTAssertNil(authFlow.authState);
  93. XCTAssertNotNil(authFlow.error);
  94. XCTAssertEqualObjects(authFlow.error, expectedError);
  95. }
  96. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  97. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  98. - (void)testGenerateAuthFlowWithMissingAuthorizationResponse {
  99. NSError *error = [NSError errorWithDomain:kGIDVerifyErrorDomain
  100. code:GIDVerifyErrorCodeUnknown
  101. userInfo:nil];
  102. NSString *errorString = [error localizedDescription];
  103. NSDictionary<NSString *, NSString *> *errorDict = @{ NSLocalizedDescriptionKey : errorString };
  104. NSError *expectedError = [NSError errorWithDomain:kGIDVerifyErrorDomain
  105. code:GIDVerifyErrorCodeUnknown
  106. userInfo:errorDict];
  107. GIDAuthorizationResponseHandler *responseHandler =
  108. [[GIDAuthorizationResponseHandler alloc] initWithAuthorizationResponse:nil
  109. emmSupport:nil
  110. flowName:GIDFlowNameVerifyAccountDetail
  111. configuration:nil
  112. error:error];
  113. GIDAuthFlow *authFlow = [responseHandler generateAuthFlowFromAuthorizationResponse];
  114. XCTAssertNotNil(authFlow);
  115. XCTAssertNil(authFlow.authState);
  116. XCTAssertNotNil(authFlow.error);
  117. XCTAssertEqualObjects(authFlow.error, expectedError);
  118. }
  119. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  120. - (void)testMaybeFetchToken_authFlowError {
  121. NSError *expectedError = [NSError errorWithDomain:kGIDSignInErrorDomain
  122. code:kGIDSignInErrorCodeUnknown
  123. userInfo:nil];
  124. OIDTokenResponse *tokenResponse =
  125. [OIDTokenResponse testInstanceWithAccessTokenExpiration:@(kAccessTokenExpiration)];
  126. OIDAuthState *authState = [OIDAuthState testInstanceWithTokenResponse:tokenResponse];
  127. GIDAuthorizationResponseHandlingFake *responseHandler =
  128. [[GIDAuthorizationResponseHandlingFake alloc] initWithAuthState:authState error:nil];
  129. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] initWithAuthState:nil
  130. error:expectedError
  131. emmSupport:nil
  132. profileData:nil];
  133. [responseHandler maybeFetchToken:authFlow];
  134. XCTAssertNil(authFlow.authState);
  135. XCTAssertNotNil(authFlow.error);
  136. XCTAssertEqualObjects(authFlow.error, expectedError);
  137. }
  138. - (void)testMaybeFetchToken_noRefresh {
  139. NSError *expectedError = [NSError errorWithDomain:kGIDSignInErrorDomain
  140. code:kGIDSignInErrorCodeUnknown
  141. userInfo:nil];
  142. OIDAuthState *authState = [OIDAuthState testInstance];
  143. GIDAuthorizationResponseHandlingFake *responseHandler =
  144. [[GIDAuthorizationResponseHandlingFake alloc] initWithAuthState:authState error:nil];
  145. GIDAuthFlow *authFlow = [[GIDAuthFlow alloc] initWithAuthState:authState
  146. error:expectedError
  147. emmSupport:nil
  148. profileData:nil];
  149. [responseHandler maybeFetchToken:authFlow];
  150. XCTAssertNotNil(authFlow.authState);
  151. XCTAssertEqualObjects(authFlow.authState, authState);
  152. XCTAssertNotNil(authFlow.error);
  153. XCTAssertEqualObjects(authFlow.error, expectedError);
  154. }
  155. @end