GIDAccountDetailTest.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2024 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. #if TARGET_OS_IOS
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifyAccountDetail.h"
  18. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDVerifiableAccountDetail.h"
  19. #import "GoogleSignIn/Sources/GIDSignIn_Private.h"
  20. #import "GoogleSignIn/Sources/GIDGoogleUser_Private.h"
  21. #import "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
  22. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  23. // Exception Reasons
  24. static NSString * const kSchemesNotSupportedExceptionReason =
  25. @"Your app is missing support for the following URL schemes: fakeclientid";
  26. static NSString * const kClientIDMissingExceptionReason =
  27. @"You must specify |clientID| in |GIDConfiguration|";
  28. static NSString * const kMissingCurrentUserExceptionReason =
  29. @"|currentUser| must be set to verify.";
  30. static NSString * const kMissingPresentingViewControllerExceptionReason =
  31. @"|presentingViewController| must be set.";
  32. static NSString * const kClientId = @"FakeClientID";
  33. static NSString * const kServerClientId = @"FakeServerClientID";
  34. static NSString * const kOpenIDRealm = @"FakeRealm";
  35. static NSString * const kFakeHostedDomain = @"fakehosteddomain.com";
  36. @interface GIDAccountDetailTests : XCTestCase
  37. // The |UIViewController| object being tested.
  38. @property UIViewController *presentingViewController;
  39. // Fake [NSBundle mainBundle].
  40. @property GIDFakeMainBundle *fakeMainBundle;
  41. // The |GIDVerifyAccountDetail| object being tested.
  42. @property GIDVerifyAccountDetail *verifyAccountDetail;
  43. // The list of account details when testing [GIDVerifiableAccountDetail].
  44. @property NSArray<GIDVerifiableAccountDetail *> *verifiableAccountDetails;
  45. @end
  46. @implementation GIDAccountDetailTests
  47. #pragma mark - Lifecycle
  48. - (void)setUp {
  49. [super setUp];
  50. _presentingViewController = [[UIViewController alloc] init];
  51. _verifyAccountDetail = [[GIDVerifyAccountDetail alloc] init];
  52. GIDVerifiableAccountDetail *ageOver18Detail =
  53. [[GIDVerifiableAccountDetail alloc] initWithAccountDetailType:GIDAccountDetailTypeAgeOver18];
  54. _verifiableAccountDetails = @[ageOver18Detail];
  55. _fakeMainBundle = [[GIDFakeMainBundle alloc] init];
  56. }
  57. #pragma mark - Tests
  58. - (void)testInit {
  59. [_fakeMainBundle startFakingWithClientID:kClientId];
  60. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc] init];
  61. XCTAssertNotNil(verifyAccountDetail.configuration);
  62. XCTAssertEqual(verifyAccountDetail.configuration.clientID, kClientId);
  63. XCTAssertNil(verifyAccountDetail.configuration.serverClientID);
  64. XCTAssertNil(verifyAccountDetail.configuration.hostedDomain);
  65. XCTAssertNil(verifyAccountDetail.configuration.openIDRealm);
  66. }
  67. - (void)testInit_noConfig {
  68. [_fakeMainBundle startFakingWithClientID:kClientId];
  69. [_fakeMainBundle fakeWithClientID:nil
  70. serverClientID:nil
  71. hostedDomain:nil
  72. openIDRealm:nil];
  73. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc] init];
  74. XCTAssertNil(verifyAccountDetail.configuration);
  75. }
  76. - (void)testInit_fullConfig {
  77. [_fakeMainBundle startFakingWithClientID:kClientId];
  78. [_fakeMainBundle fakeWithClientID:kClientId
  79. serverClientID:kServerClientId
  80. hostedDomain:kFakeHostedDomain
  81. openIDRealm:kOpenIDRealm];
  82. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc] init];
  83. XCTAssertNotNil(verifyAccountDetail.configuration);
  84. XCTAssertEqual(verifyAccountDetail.configuration.clientID, kClientId);
  85. XCTAssertEqual(verifyAccountDetail.configuration.serverClientID, kServerClientId);
  86. XCTAssertEqual(verifyAccountDetail.configuration.hostedDomain, kFakeHostedDomain);
  87. XCTAssertEqual(verifyAccountDetail.configuration.openIDRealm, kOpenIDRealm);
  88. }
  89. - (void)testInit_invalidConfig {
  90. [_fakeMainBundle startFakingWithClientID:kClientId];
  91. [_fakeMainBundle fakeWithClientID:@[ @"bad", @"config", @"values" ]
  92. serverClientID:nil
  93. hostedDomain:nil
  94. openIDRealm:nil];
  95. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc] init];
  96. XCTAssertNil(verifyAccountDetail.configuration);
  97. }
  98. - (void)testInitWithConfig_noConfig {
  99. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc] initWithConfig:nil];
  100. XCTAssertNil(verifyAccountDetail.configuration);
  101. }
  102. - (void)testInitWithConfig_fullConfig {
  103. GIDConfiguration *configuration = [[GIDConfiguration alloc] initWithClientID:kClientId
  104. serverClientID:kServerClientId
  105. hostedDomain:kFakeHostedDomain
  106. openIDRealm:kOpenIDRealm];
  107. GIDVerifyAccountDetail *verifyAccountDetail = [[GIDVerifyAccountDetail alloc]
  108. initWithConfig:configuration];
  109. XCTAssertNotNil(verifyAccountDetail.configuration);
  110. XCTAssertEqual(verifyAccountDetail.configuration.clientID, kClientId);
  111. XCTAssertEqual(verifyAccountDetail.configuration.serverClientID, kServerClientId);
  112. XCTAssertEqual(verifyAccountDetail.configuration.hostedDomain, kFakeHostedDomain);
  113. XCTAssertEqual(verifyAccountDetail.configuration.openIDRealm, kOpenIDRealm);
  114. }
  115. - (void)testCurrentUserException {
  116. #pragma GCC diagnostic push
  117. #pragma GCC diagnostic ignored "-Wnonnull"
  118. _verifyAccountDetail.configuration = [[GIDConfiguration alloc] initWithClientID:nil];
  119. #pragma GCC diagnostic pop
  120. OIDAuthState *authState = [OIDAuthState testInstance];
  121. GIDSignIn.sharedInstance.currentUser = nil;
  122. @try {
  123. [_verifyAccountDetail verifyAccountDetails:_verifiableAccountDetails
  124. presentingViewController:_presentingViewController
  125. completion:nil];
  126. } @catch (NSException *exception) {
  127. XCTAssertEqual(exception.name, NSInvalidArgumentException);
  128. XCTAssertEqualObjects(exception.reason, kMissingCurrentUserExceptionReason);
  129. }
  130. }
  131. - (void)testPresentingViewControllerException {
  132. #pragma GCC diagnostic push
  133. #pragma GCC diagnostic ignored "-Wnonnull"
  134. _verifyAccountDetail.configuration = [[GIDConfiguration alloc] initWithClientID:kClientId];
  135. #pragma GCC diagnostic pop
  136. _presentingViewController = nil;
  137. OIDAuthState *authState = [OIDAuthState testInstance];
  138. GIDSignIn.sharedInstance.currentUser = [[GIDGoogleUser alloc] initWithAuthState:authState
  139. profileData:nil];
  140. @try {
  141. [_verifyAccountDetail verifyAccountDetails:_verifiableAccountDetails
  142. presentingViewController:_presentingViewController
  143. completion:nil];
  144. } @catch (NSException *exception) {
  145. XCTAssertEqual(exception.name, NSInvalidArgumentException);
  146. XCTAssertEqualObjects(exception.reason, kMissingPresentingViewControllerExceptionReason);
  147. }
  148. }
  149. - (void)testClientIDMissingException {
  150. #pragma GCC diagnostic push
  151. #pragma GCC diagnostic ignored "-Wnonnull"
  152. _verifyAccountDetail.configuration = [[GIDConfiguration alloc] initWithClientID:nil];
  153. #pragma GCC diagnostic pop
  154. OIDAuthState *authState = [OIDAuthState testInstance];
  155. GIDSignIn.sharedInstance.currentUser = [[GIDGoogleUser alloc] initWithAuthState:authState
  156. profileData:nil];
  157. @try {
  158. [_verifyAccountDetail verifyAccountDetails:_verifiableAccountDetails
  159. presentingViewController:_presentingViewController
  160. completion:nil];
  161. } @catch (NSException *exception) {
  162. XCTAssertEqual(exception.name, NSInvalidArgumentException);
  163. XCTAssertEqualObjects(exception.reason, kClientIDMissingExceptionReason);
  164. }
  165. }
  166. - (void)testSchemesNotSupportedException {
  167. #pragma GCC diagnostic push
  168. #pragma GCC diagnostic ignored "-Wnonnull"
  169. _verifyAccountDetail.configuration = [[GIDConfiguration alloc] initWithClientID:kClientId];
  170. #pragma GCC diagnostic pop
  171. OIDAuthState *authState = [OIDAuthState testInstance];
  172. GIDSignIn.sharedInstance.currentUser = [[GIDGoogleUser alloc] initWithAuthState:authState
  173. profileData:nil];
  174. @try {
  175. [_verifyAccountDetail verifyAccountDetails:_verifiableAccountDetails
  176. presentingViewController:_presentingViewController
  177. completion:nil];
  178. } @catch (NSException *exception) {
  179. XCTAssertEqual(exception.name, NSInvalidArgumentException);
  180. XCTAssertEqualObjects(exception.reason, kSchemesNotSupportedExceptionReason);
  181. }
  182. }
  183. @end
  184. #endif // TARGET_OS_IOS