GIDAuthorizationUtilTest.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <TargetConditionals.h>
  15. #import <XCTest/XCTest.h>
  16. #import "GoogleSignIn/Sources/GIDAuthorizationUtil.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  18. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  19. #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
  20. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  21. #ifdef SWIFT_PACKAGE
  22. @import AppAuth;
  23. #else
  24. #import <AppAuth/AppAuth.h>
  25. #endif
  26. NS_ASSUME_NONNULL_BEGIN
  27. static NSString * const kClientId = @"FakeClientID";
  28. static NSString * const kUserEmail = @"FakeUserEmail";
  29. static NSString * const kServerClientId = @"FakeServerClientID";
  30. static NSString * const kScopeBirthday = @"birthday";
  31. static NSString * const kScopeEmail = @"email";
  32. static NSString * const kScopeProfile = @"profile";
  33. @interface GIDAuthorizationUtilTest : XCTestCase
  34. @end
  35. @implementation GIDAuthorizationUtilTest {
  36. GIDConfiguration *_configuration;
  37. }
  38. - (void)setUp {
  39. [super setUp];
  40. _configuration = [[GIDConfiguration alloc] initWithClientID:kClientId
  41. serverClientID:kServerClientId
  42. hostedDomain:kHostedDomain
  43. openIDRealm:nil];
  44. }
  45. - (void)testCreateAuthorizationRequest_signInFlow {
  46. GIDSignInInternalOptions *options =
  47. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  48. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  49. presentingViewController:nil
  50. #elif TARGET_OS_OSX
  51. presentingWindow:nil
  52. #endif // TARGET_OS_OSX
  53. loginHint:kUserEmail
  54. addScopesFlow:NO
  55. completion:nil];
  56. OIDAuthorizationRequest *request =
  57. [GIDAuthorizationUtil authorizationRequestWithOptions:options
  58. emmSupport:nil];
  59. NSDictionary<NSString *, NSObject *> *params = request.additionalParameters;
  60. XCTAssertEqualObjects(params[kIncludeGrantedScopesParameter], @"true");
  61. XCTAssertEqualObjects(params[kSDKVersionLoggingParameter], GIDVersion());
  62. XCTAssertEqualObjects(params[kEnvironmentLoggingParameter], GIDEnvironment());
  63. XCTAssertEqualObjects(params[kLoginHintParameter], kUserEmail, @"login hint should match");
  64. XCTAssertEqualObjects(params[kHostedDomainParameter], kHostedDomain,
  65. @"hosted domain should match");
  66. XCTAssertEqualObjects(params[kAudienceParameter], kServerClientId, @"client ID should match");
  67. NSArray<NSString *> *defaultScopes = @[kScopeEmail, kScopeProfile];
  68. NSString *expectedScopeString = [defaultScopes componentsJoinedByString:@" "];
  69. XCTAssertEqualObjects(request.scope, expectedScopeString);
  70. }
  71. - (void)testCreateAuthorizationRequest_additionalScopes {
  72. NSArray<NSString *> *addtionalScopes = @[kScopeBirthday];
  73. GIDSignInInternalOptions *options =
  74. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  75. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  76. presentingViewController:nil
  77. #elif TARGET_OS_OSX
  78. presentingWindow:nil
  79. #endif // TARGET_OS_OSX
  80. loginHint:kUserEmail
  81. addScopesFlow:NO
  82. scopes:addtionalScopes
  83. completion:nil];
  84. OIDAuthorizationRequest *request =
  85. [GIDAuthorizationUtil authorizationRequestWithOptions:options
  86. emmSupport:nil];
  87. NSArray<NSString *> *expectedScopes = @[kScopeBirthday, kScopeEmail, kScopeProfile];
  88. NSString *expectedScopeString = [expectedScopes componentsJoinedByString:@" "];
  89. XCTAssertEqualObjects(request.scope, expectedScopeString);
  90. }
  91. - (void)testCreateAuthrizationRequest_addScopes {
  92. NSArray<NSString *> *scopes = @[kScopeEmail, kScopeProfile, kScopeBirthday];
  93. GIDSignInInternalOptions *options =
  94. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  95. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  96. presentingViewController:nil
  97. #elif TARGET_OS_OSX
  98. presentingWindow:nil
  99. #endif // TARGET_OS_OSX
  100. loginHint:kUserEmail
  101. addScopesFlow:YES
  102. scopes:scopes
  103. completion:nil];
  104. OIDAuthorizationRequest *request =
  105. [GIDAuthorizationUtil authorizationRequestWithOptions:options
  106. emmSupport:nil];
  107. NSString *expectedScopeString = [scopes componentsJoinedByString:@" "];
  108. XCTAssertEqualObjects(request.scope, expectedScopeString);
  109. }
  110. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  111. - (void)testCreateAuthorizationRequest_signInFlow_EMM {
  112. GIDSignInInternalOptions *options =
  113. [GIDSignInInternalOptions defaultOptionsWithConfiguration:_configuration
  114. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  115. presentingViewController:nil
  116. #elif TARGET_OS_OSX
  117. presentingWindow:nil
  118. #endif // TARGET_OS_OSX
  119. loginHint:kUserEmail
  120. addScopesFlow:NO
  121. completion:nil];
  122. OIDAuthorizationRequest *request =
  123. [GIDAuthorizationUtil authorizationRequestWithOptions:options
  124. emmSupport:kEMMVersion];
  125. NSString *systemName = [UIDevice currentDevice].systemName;
  126. if ([systemName isEqualToString:@"iPhone OS"]) {
  127. systemName = @"iOS";
  128. }
  129. NSString *expectedOSVersion = [NSString stringWithFormat:@"%@ %@",
  130. systemName, [UIDevice currentDevice].systemVersion];
  131. NSDictionary<NSString *, NSObject *> *authParams = request.additionalParameters;
  132. BOOL isEligibleForEMM = [UIDevice currentDevice].systemVersion.integerValue >= 9;
  133. if (isEligibleForEMM) {
  134. XCTAssertEqualObjects(authParams[@"emm_support"], kEMMVersion,
  135. @"EMM support should match in auth request");
  136. XCTAssertEqualObjects(authParams[@"device_os"], expectedOSVersion,
  137. @"OS version should match in auth request");
  138. } else {
  139. XCTAssertNil(authParams[@"emm_support"],
  140. @"EMM support should not be in auth request for unsupported OS");
  141. XCTAssertNil(authParams[@"device_os"],
  142. @"OS version should not be in auth request for unsupported OS");
  143. }
  144. }
  145. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  146. - (void)testUnionScopes_success {
  147. NSArray<NSString *> *scopes = @[kScopeEmail, kScopeProfile];
  148. NSArray<NSString *> *newScopes = @[kScopeBirthday];
  149. NSError *error;
  150. NSArray<NSString *> *allScopes =
  151. [GIDAuthorizationUtil resolvedScopesFromGrantedScoped:scopes
  152. withNewScopes:newScopes
  153. error:&error];
  154. NSArray<NSString *> *expectedScopes = @[kScopeEmail, kScopeProfile, kScopeBirthday];
  155. XCTAssertEqualObjects(allScopes, expectedScopes);
  156. XCTAssertNil(error);
  157. }
  158. - (void)testUnionScopes_addExistingScopes_error {
  159. NSArray<NSString *> *scopes = @[kScopeEmail, kScopeProfile, kScopeBirthday];
  160. NSArray<NSString *> *newScopes = @[kScopeBirthday];
  161. NSError *error;
  162. NSArray<NSString *> *allScopes =
  163. [GIDAuthorizationUtil resolvedScopesFromGrantedScoped:scopes
  164. withNewScopes:newScopes
  165. error:&error];
  166. XCTAssertNil(allScopes);
  167. XCTAssertEqualObjects(error.domain, kGIDSignInErrorDomain);
  168. XCTAssertEqual(error.code, kGIDSignInErrorCodeScopesAlreadyGranted);
  169. }
  170. @end
  171. NS_ASSUME_NONNULL_END