Bladeren bron

Add test for GIDAuthorizationFlowProcessor.

pinlu 3 jaren geleden
bovenliggende
commit
0e80a9db38

+ 2 - 2
GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/API/GIDAuthorizationFlowProcessor.h

@@ -32,13 +32,13 @@ NS_ASSUME_NONNULL_BEGIN
 ///     clientID, scopes, loginHint and extraParams.
 /// @param completion The block that is called on completion asynchronously.
 - (void)startWithOptions:(GIDSignInInternalOptions *)options
-              emmSupport:(NSString *)emmSupport
+              emmSupport:(nullable NSString *)emmSupport
               completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
                                    NSError *_Nullable error))completion;
 
 /// Handles the custom scheme URL opened by SFSafariViewController to cleanup UI on iOS 10.
 ///
-/// @param URL The redirect URL invoked by the server.
+/// @param url The redirect URL invoked by the server.
 /// @return YES if the passed URL matches the expected redirect URL and was consumed, NO otherwise.
 - (BOOL)resumeExternalUserAgentFlowWithURL:(NSURL *)url;
 

+ 22 - 6
GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.m

@@ -1,3 +1,19 @@
+/*
+ * Copyright 2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
 #import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
 
 #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDConfiguration.h"
@@ -38,14 +54,14 @@ static NSString *const kHostedDomainParameter = @"hd";
 }
 
 - (void)startWithOptions:(GIDSignInInternalOptions *)options
-              emmSupport:(NSString *)emmSupport
+              emmSupport:(nullable NSString *)emmSupport
               completion:(void (^)(OIDAuthorizationResponse *_Nullable authorizationResponse,
                                    NSError *_Nullable error))completion {
   GIDSignInCallbackSchemes *schemes =
       [[GIDSignInCallbackSchemes alloc] initWithClientIdentifier:options.configuration.clientID];
-  NSURL *redirectURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@:%@",
-                                             [schemes clientIdentifierScheme],
-                                             kBrowserCallbackPath]];
+  NSString *urlString = [NSString stringWithFormat:@"%@:%@",
+      [schemes clientIdentifierScheme], kBrowserCallbackPath];
+  NSURL *redirectURL = [NSURL URLWithString:urlString];
 
   NSMutableDictionary<NSString *, NSString *> *additionalParameters = [@{} mutableCopy];
   additionalParameters[kIncludeGrantedScopesParameter] = @"true";
@@ -90,8 +106,8 @@ static NSString *const kHostedDomainParameter = @"hd";
 #elif TARGET_OS_OSX
                  presentingWindow:options.presentingWindow
 #endif // TARGET_OS_OSX
-                        callback:^(OIDAuthorizationResponse *_Nullable authorizationResponse,
-                                   NSError *_Nullable error) {
+                         callback:^(OIDAuthorizationResponse *authorizationResponse,
+                                    NSError *error) {
     completion(authorizationResponse, error);
   }];
 }

+ 123 - 0
GoogleSignIn/Tests/Unit/GIDAuthorizationFlowProcessorTest.m

@@ -0,0 +1,123 @@
+// Copyright 2023 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#import "GoogleSignIn/Sources/GIDAuthorizationFlowProcessor/Implementations/GIDAuthorizationFlowProcessor.h"
+
+#import "GoogleSignIn/Sources/GIDSignInInternalOptions.h"
+#import "GoogleSignIn/Tests/Unit/OIDAuthorizationResponse+Testing.h"
+
+#import <XCTest/XCTest.h>
+
+#ifdef SWIFT_PACKAGE
+@import AppAuth;
+@import OCMock;
+#else
+#import <AppAuth/AppAuth.h>
+#import <OCMock/OCMock.h>
+#endif
+
+static NSString *const kFakeURL = @"www.fakeURL.com";
+static NSString *const kErrorDomain = @"ERROR_DOMAIN";
+static NSInteger const kErrorCode = 400;
+
+@interface GIDAuthorizationFlowProcessorTest : XCTestCase {
+  GIDAuthorizationFlowProcessor *_authorizationFlowProcessor;
+  id _authorizationServiceMock;
+  id _externalUserAgentSession;
+}
+
+@end
+
+@implementation GIDAuthorizationFlowProcessorTest
+
+- (void)setUp {
+  [super setUp];
+  
+  _authorizationFlowProcessor = [[GIDAuthorizationFlowProcessor alloc] init];
+  _externalUserAgentSession = OCMProtocolMock(@protocol(OIDExternalUserAgentSession));
+  _authorizationServiceMock = OCMClassMock([OIDAuthorizationService class]);
+  OIDAuthorizationResponse *response = [OIDAuthorizationResponse testInstance];
+  NSError *error = [self error];
+  OCMStub([_authorizationServiceMock
+      presentAuthorizationRequest:[OCMArg any]
+#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
+         presentingViewController:[OCMArg any]
+#elif TARGET_OS_OSX
+                 presentingWindow:[OCMArg any]
+#endif // TARGET_OS_OSX
+                         callback:([OCMArg invokeBlockWithArgs:response, error, nil])
+          ]).andReturn(_externalUserAgentSession);
+}
+
+- (void)testStartAndCancelAuthorizationFlow {
+  XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
+  GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
+  [_authorizationFlowProcessor startWithOptions:options
+                                     emmSupport:nil
+                                     completion:^(OIDAuthorizationResponse *authorizationResponse,
+                                                  NSError *error) {
+    [expectation fulfill];
+  }];
+  [self waitForExpectationsWithTimeout:1 handler:nil];
+  XCTAssertTrue(_authorizationFlowProcessor.isStarted);
+  
+  [_authorizationFlowProcessor cancelAuthenticationFlow];
+  XCTAssertFalse(_authorizationFlowProcessor.isStarted);
+}
+
+- (void)testStartAndResumeAuthorizationFlow_success {
+  OCMStub([_externalUserAgentSession resumeExternalUserAgentFlowWithURL:[OCMArg any]])
+    .andReturn(YES);
+  XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
+  GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
+  [_authorizationFlowProcessor startWithOptions:options
+                                     emmSupport:nil
+                                     completion:^(OIDAuthorizationResponse *authorizationResponse,
+                                                  NSError *error) {
+    [expectation fulfill];
+  }];
+  [self waitForExpectationsWithTimeout:1 handler:nil];
+  XCTAssertTrue(_authorizationFlowProcessor.isStarted);
+  
+  NSURL *url = [[NSURL alloc] initWithString:kFakeURL];
+  [_authorizationFlowProcessor resumeExternalUserAgentFlowWithURL:url];
+  XCTAssertFalse(_authorizationFlowProcessor.isStarted);
+}
+
+- (void)testStartAndResumeAuthorizationFlow_fail {
+  OCMStub([_externalUserAgentSession resumeExternalUserAgentFlowWithURL:[OCMArg any]])
+    .andReturn(NO);
+  XCTestExpectation *expectation = [self expectationWithDescription:@"completion is invoked."];
+  GIDSignInInternalOptions *options = [[GIDSignInInternalOptions alloc] init];
+  [_authorizationFlowProcessor startWithOptions:options
+                                     emmSupport:nil
+                                     completion:^(OIDAuthorizationResponse *authorizationResponse,
+                                                  NSError *error) {
+    [expectation fulfill];
+  }];
+  [self waitForExpectationsWithTimeout:1 handler:nil];
+  XCTAssertTrue(_authorizationFlowProcessor.isStarted);
+  
+  NSURL *url = [[NSURL alloc] initWithString:kFakeURL];
+  [_authorizationFlowProcessor resumeExternalUserAgentFlowWithURL:url];
+  XCTAssertTrue(_authorizationFlowProcessor.isStarted);
+}
+
+#pragma mark - Helpers
+
+- (NSError *)error {
+  return [NSError errorWithDomain:kErrorDomain code:kErrorCode userInfo:nil];
+}
+
+@end