GIDAuthorizationFlowProcessorTest.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
  15. #import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
  16. #import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
  17. #import <XCTest/XCTest.h>
  18. #ifdef SWIFT_PACKAGE
  19. @import AppAuth;
  20. @import OCMock;
  21. #else
  22. #import <AppAuth/AppAuth.h>
  23. #import <OCMock/OCMock.h>
  24. #endif
  25. static NSString *const kFakeURL = @"www.fakeURL.com";
  26. static NSString *const kErrorDomain = @"ERROR_DOMAIN";
  27. static NSInteger const kErrorCode = 400;
  28. @interface GIDAuthorizationFlowProcessorTest : XCTestCase {
  29. GIDAuthorizationFlowProcessor *_authorizationFlowProcessor;
  30. id _authorizationServiceMock;
  31. id _externalUserAgentSession;
  32. }
  33. @end
  34. @implementation GIDAuthorizationFlowProcessorTest
  35. - (void)setUp {
  36. [super setUp];
  37. _authorizationFlowProcessor = [[GIDAuthorizationFlowProcessor alloc] init];
  38. _externalUserAgentSession = OCMProtocolMock(@protocol(OIDExternalUserAgentSession));
  39. _authorizationServiceMock = OCMClassMock([OIDAuthorizationService class]);
  40. OIDAuthorizationResponse *response = [OIDAuthorizationResponse testInstance];
  41. NSError *error = [self error];
  42. OCMStub([_authorizationServiceMock
  43. presentAuthorizationRequest:[OCMArg any]
  44. #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
  45. presentingViewController:[OCMArg any]
  46. #elif TARGET_OS_OSX
  47. presentingWindow:[OCMArg any]
  48. #endif // TARGET_OS_OSX
  49. callback:([OCMArg invokeBlockWithArgs:response, error, nil])
  50. ]).andReturn(_externalUserAgentSession);
  51. }
  52. - (void)testStartAndCancelAuthorizationFlow_success {
  53. XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
  54. GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
  55. [_authorizationFlowProcessor startWithOptions:options
  56. emmSupport:nil
  57. completion:^(OIDAuthorizationResponse *authorizationResponse,
  58. NSError *error) {
  59. [expectation fulfill];
  60. }];
  61. [self waitForExpectationsWithTimeout:1 handler:nil];
  62. XCTAssertTrue(_authorizationFlowProcessor.isStarted);
  63. [_authorizationFlowProcessor cancelAuthenticationFlow];
  64. XCTAssertFalse(_authorizationFlowProcessor.isStarted);
  65. }
  66. - (void)testStartAndResumeAuthorizationFlow_success {
  67. XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
  68. GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
  69. [_authorizationFlowProcessor startWithOptions:options
  70. emmSupport:nil
  71. completion:^(OIDAuthorizationResponse *authorizationResponse,
  72. NSError *error) {
  73. [expectation fulfill];
  74. }];
  75. [self waitForExpectationsWithTimeout:1 handler:nil];
  76. XCTAssertTrue(_authorizationFlowProcessor.isStarted);
  77. OCMStub([_externalUserAgentSession resumeExternalUserAgentFlowWithURL:[OCMArg any]])
  78. .andReturn(YES);
  79. NSURL *url = [[NSURL alloc] initWithString:kFakeURL];
  80. [_authorizationFlowProcessor resumeExternalUserAgentFlowWithURL:url];
  81. XCTAssertFalse(_authorizationFlowProcessor.isStarted);
  82. }
  83. - (void)testStartAndFailToResumeAuthorizationFlow {
  84. XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
  85. GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
  86. [_authorizationFlowProcessor startWithOptions:options
  87. emmSupport:nil
  88. completion:^(OIDAuthorizationResponse *authorizationResponse,
  89. NSError *error) {
  90. [expectation fulfill];
  91. }];
  92. [self waitForExpectationsWithTimeout:1 handler:nil];
  93. XCTAssertTrue(_authorizationFlowProcessor.isStarted);
  94. OCMStub([_externalUserAgentSession resumeExternalUserAgentFlowWithURL:[OCMArg any]])
  95. .andReturn(NO);
  96. NSURL *url = [[NSURL alloc] initWithString:kFakeURL];
  97. [_authorizationFlowProcessor resumeExternalUserAgentFlowWithURL:url];
  98. XCTAssertTrue(_authorizationFlowProcessor.isStarted);
  99. }
  100. #pragma mark - Helpers
  101. - (NSError *)error {
  102. return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
  103. }
  104. @end