GIDAuthorizationFlowProcessorTest.m 5.8 KB

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