GIDSignInInternalOptionsTest.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2021 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. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
  17. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDTokenClaim.h"
  18. #ifdef SWIFT_PACKAGE
  19. @import OCMock;
  20. #else
  21. #import <OCMock/OCMock.h>
  22. #endif
  23. @interface GIDSignInInternalOptionsTest : XCTestCase
  24. @end
  25. @implementation GIDSignInInternalOptionsTest
  26. - (void)testDefaultOptions {
  27. id configuration = OCMStrictClassMock([GIDConfiguration class]);
  28. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  29. id presentingViewController = OCMStrictClassMock([UIViewController class]);
  30. #elif TARGET_OS_OSX
  31. id presentingWindow = OCMStrictClassMock([NSWindow class]);
  32. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  33. NSString *loginHint = @"login_hint";
  34. GIDSignInCompletion completion = ^(GIDSignInResult *_Nullable signInResult,
  35. NSError * _Nullable error) {};
  36. GIDSignInInternalOptions *options =
  37. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  38. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  39. presentingViewController:presentingViewController
  40. #elif TARGET_OS_OSX
  41. presentingWindow:presentingWindow
  42. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  43. loginHint:loginHint
  44. addScopesFlow:NO
  45. completion:completion];
  46. XCTAssertTrue(options.interactive);
  47. XCTAssertFalse(options.continuation);
  48. XCTAssertFalse(options.addScopesFlow);
  49. XCTAssertNil(options.extraParams);
  50. OCMVerifyAll(configuration);
  51. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  52. OCMVerifyAll(presentingViewController);
  53. #elif TARGET_OS_OSX
  54. OCMVerifyAll(presentingWindow);
  55. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  56. }
  57. - (void)testDefaultOptions_withAllParameters_initializesPropertiesCorrectly {
  58. id configuration = OCMStrictClassMock([GIDConfiguration class]);
  59. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  60. id presentingViewController = OCMStrictClassMock([UIViewController class]);
  61. #elif TARGET_OS_OSX
  62. id presentingWindow = OCMStrictClassMock([NSWindow class]);
  63. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  64. NSString *loginHint = @"login_hint";
  65. NSArray<NSString *> *scopes = @[@"scope1", @"scope2"];
  66. NSString *nonce = @"test_nonce";
  67. NSSet<GIDTokenClaim *> *tokenClaims = [NSSet setWithObject:[GIDTokenClaim authTimeClaim]];
  68. NSArray<NSString *> *expectedScopes = @[@"scope1", @"scope2", @"email", @"profile"];
  69. GIDSignInCompletion completion = ^(GIDSignInResult *_Nullable signInResult,
  70. NSError * _Nullable error) {};
  71. GIDSignInInternalOptions *options =
  72. [GIDSignInInternalOptions defaultOptionsWithConfiguration:configuration
  73. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  74. presentingViewController:presentingViewController
  75. #elif TARGET_OS_OSX
  76. presentingWindow:presentingWindow
  77. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  78. loginHint:loginHint
  79. addScopesFlow:NO
  80. scopes:scopes
  81. nonce:nonce
  82. tokenClaims:tokenClaims
  83. completion:completion];
  84. XCTAssertTrue(options.interactive);
  85. XCTAssertFalse(options.continuation);
  86. XCTAssertFalse(options.addScopesFlow);
  87. XCTAssertNil(options.extraParams);
  88. // Convert arrays to sets for comparison to make the test order-independent.
  89. XCTAssertEqualObjects([NSSet setWithArray:options.scopes], [NSSet setWithArray:expectedScopes]);
  90. XCTAssertEqualObjects(options.nonce, nonce);
  91. XCTAssertEqualObjects(options.tokenClaims, tokenClaims);
  92. XCTAssertNil(options.tokenClaimsAsJSON);
  93. OCMVerifyAll(configuration);
  94. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  95. OCMVerifyAll(presentingViewController);
  96. #elif TARGET_OS_OSX
  97. OCMVerifyAll(presentingWindow);
  98. #endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST
  99. }
  100. - (void)testSilentOptions {
  101. GIDSignInCompletion completion = ^(GIDSignInResult *_Nullable signInResult,
  102. NSError * _Nullable error) {};
  103. GIDSignInInternalOptions *options = [GIDSignInInternalOptions silentOptionsWithCompletion:completion];
  104. XCTAssertFalse(options.interactive);
  105. XCTAssertFalse(options.continuation);
  106. XCTAssertNil(options.extraParams);
  107. XCTAssertEqual(options.completion, completion);
  108. }
  109. @end