-
Notifications
You must be signed in to change notification settings - Fork 247
Add unit tests for GIDEMMSupport #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Alex-4-Git
merged 5 commits into
GIDGoogleUser-restructure
from
pin-AddTestForGIDEMMSupport
Oct 10, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| /* | ||
| * Copyright 2022 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 <TargetConditionals.h> | ||
|
|
||
| #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
|
|
||
| #import <XCTest/XCTest.h> | ||
|
|
||
| #import "GoogleSignIn/Sources/GIDEMMSupport.h" | ||
|
|
||
| #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h" | ||
|
|
||
| #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h" | ||
| #import "GoogleSignIn/Sources/GIDMDMPasscodeState.h" | ||
|
|
||
| #ifdef SWIFT_PACKAGE | ||
| @import AppAuth; | ||
| @import GoogleUtilities_MethodSwizzler; | ||
| @import GoogleUtilities_SwizzlerTestHelpers; | ||
| @import OCMock; | ||
| #else | ||
| #import <GoogleUtilities/GULSwizzler.h> | ||
| #import <GoogleUtilities/GULSwizzler+Unswizzle.h> | ||
| #import <AppAuth/OIDError.h> | ||
| #import <OCMock/OCMock.h> | ||
| #endif | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| // The system name in old iOS versions. | ||
| static NSString *const kOldIOSName = @"iPhone OS"; | ||
|
|
||
| // The system name in new iOS versions. | ||
| static NSString *const kNewIOSName = @"iOS"; | ||
|
|
||
| // They keys in EMM dictionary. | ||
| static NSString *const kEMMKey = @"emm_support"; | ||
| static NSString *const kDeviceOSKey = @"device_os"; | ||
| static NSString *const kEMMPasscodeInfoKey = @"emm_passcode_info"; | ||
|
|
||
| @interface GIDEMMSupportTest : XCTestCase | ||
| @end | ||
|
|
||
| @implementation GIDEMMSupportTest | ||
|
|
||
| - (void)testUpdatedEMMParametersWithParameters_NoEMMKey { | ||
| NSDictionary *originalParameters = @{ | ||
| @"not_emm_support_key" : @"xyz", | ||
| }; | ||
|
|
||
| NSDictionary *updatedEMMParameters = | ||
| [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters]; | ||
|
|
||
| XCTAssertEqualObjects(updatedEMMParameters, originalParameters); | ||
| } | ||
|
|
||
| - (void)testUpdateEMMParametersWithParameters_systemName { | ||
| [GULSwizzler swizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO | ||
| withBlock:^(id sender) { return kNewIOSName; }]; | ||
|
|
||
| NSDictionary *originalParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| }; | ||
|
|
||
| NSDictionary *updatedEMMParameters = | ||
| [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters]; | ||
|
|
||
| NSDictionary *expectedParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]] | ||
| }; | ||
|
|
||
| XCTAssertEqualObjects(updatedEMMParameters, expectedParameters); | ||
|
|
||
| [self addTeardownBlock:^{ | ||
| [GULSwizzler unswizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO]; | ||
| }]; | ||
| } | ||
|
|
||
| // When the systemName is @"iPhone OS" we still get "iOS". | ||
| - (void)testUpdateEMMParametersWithParameters_systemNameNormalization { | ||
| [GULSwizzler swizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO | ||
| withBlock:^(id sender) { return kOldIOSName; }]; | ||
|
|
||
| NSDictionary *originalParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| }; | ||
|
|
||
| NSDictionary *updatedEMMParameters = | ||
| [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters]; | ||
|
|
||
| NSDictionary *expectedParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]] | ||
| }; | ||
|
|
||
| XCTAssertEqualObjects(updatedEMMParameters, expectedParameters); | ||
|
|
||
| [self addTeardownBlock:^{ | ||
| [GULSwizzler unswizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO]; | ||
| }]; | ||
| } | ||
|
|
||
| - (void)testUpdateEMMParametersWithParameters_passcodInfo { | ||
| [GULSwizzler swizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO | ||
| withBlock:^(id sender) { return kOldIOSName; }]; | ||
|
|
||
| NSDictionary *originalParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| kDeviceOSKey : @"old one", | ||
| kEMMPasscodeInfoKey : @"something", | ||
| }; | ||
|
|
||
| NSDictionary *updatedEMMParameters = | ||
| [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters]; | ||
|
|
||
| NSDictionary *expectedParameters = @{ | ||
| kEMMKey : @"xyz", | ||
| kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]], | ||
| kEMMPasscodeInfoKey : [GIDMDMPasscodeState passcodeState].info, | ||
| }; | ||
|
|
||
| XCTAssertEqualObjects(updatedEMMParameters, expectedParameters); | ||
|
|
||
| [self addTeardownBlock:^{ | ||
| [GULSwizzler unswizzleClass:[UIDevice class] | ||
| selector:@selector(systemName) | ||
| isClassSelector:NO]; | ||
| }]; | ||
|
|
||
| } | ||
|
|
||
| - (void)testHandleTokenFetchEMMError_errorIsEMM { | ||
| // Set expectations. | ||
| NSDictionary *errorJSON = @{ @"error" : @"EMM Specific Error" }; | ||
| NSError *emmError = [NSError errorWithDomain:@"anydomain" | ||
| code:12345 | ||
| userInfo:@{ OIDOAuthErrorResponseErrorKey : errorJSON }]; | ||
| id mockEMMErrorHandler = OCMStrictClassMock([GIDEMMErrorHandler class]); | ||
| [[[mockEMMErrorHandler stub] andReturn:mockEMMErrorHandler] sharedInstance]; | ||
| __block void (^savedCompletion)(void); | ||
| [[[mockEMMErrorHandler stub] andReturnValue:@YES] | ||
| handleErrorFromResponse:errorJSON completion:[OCMArg checkWithBlock:^(id arg) { | ||
| savedCompletion = arg; | ||
| return YES; | ||
| }]]; | ||
|
|
||
| XCTestExpectation *notCalled = [self expectationWithDescription:@"Callback is not called"]; | ||
| notCalled.inverted = YES; | ||
| XCTestExpectation *called = [self expectationWithDescription:@"Callback is called"]; | ||
|
|
||
| [GIDEMMSupport handleTokenFetchEMMError:emmError completion:^(NSError *error) { | ||
| [notCalled fulfill]; | ||
| [called fulfill]; | ||
| XCTAssertEqualObjects(error.domain, kGIDSignInErrorDomain); | ||
| XCTAssertEqual(error.code, kGIDSignInErrorCodeEMM); | ||
| }]; | ||
|
|
||
| [self waitForExpectations:@[ notCalled ] timeout:1]; | ||
| savedCompletion(); | ||
| [self waitForExpectations:@[ called ] timeout:1]; | ||
| } | ||
|
|
||
| - (void)testHandleTokenFetchEMMError_errorIsNotEMM { | ||
| // Set expectations. | ||
| NSDictionary *errorJSON = @{ @"error" : @"Not EMM Specific Error" }; | ||
| NSError *emmError = [NSError errorWithDomain:@"anydomain" | ||
| code:12345 | ||
| userInfo:@{ OIDOAuthErrorResponseErrorKey : errorJSON }]; | ||
| id mockEMMErrorHandler = OCMStrictClassMock([GIDEMMErrorHandler class]); | ||
| [[[mockEMMErrorHandler stub] andReturn:mockEMMErrorHandler] sharedInstance]; | ||
| __block void (^savedCompletion)(void); | ||
| [[[mockEMMErrorHandler stub] andReturnValue:@NO] | ||
| handleErrorFromResponse:errorJSON completion:[OCMArg checkWithBlock:^(id arg) { | ||
| savedCompletion = arg; | ||
| return YES; | ||
| }]]; | ||
|
|
||
| XCTestExpectation *notCalled = [self expectationWithDescription:@"Callback is not called"]; | ||
| notCalled.inverted = YES; | ||
| XCTestExpectation *called = [self expectationWithDescription:@"Callback is called"]; | ||
|
|
||
| [GIDEMMSupport handleTokenFetchEMMError:emmError completion:^(NSError *error) { | ||
| [notCalled fulfill]; | ||
| [called fulfill]; | ||
| XCTAssertEqualObjects(error.domain, @"anydomain"); | ||
| XCTAssertEqual(error.code, 12345); | ||
| }]; | ||
|
|
||
| [self waitForExpectations:@[ notCalled ] timeout:1]; | ||
| savedCompletion(); | ||
Alex-4-Git marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [self waitForExpectations:@[ called ] timeout:1]; | ||
| } | ||
|
|
||
| # pragma mark - Helpers | ||
|
|
||
| - (NSString *)systemVersion { | ||
| return [UIDevice currentDevice].systemVersion; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END | ||
|
|
||
| #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.