Skip to content
39 changes: 36 additions & 3 deletions GoogleSignIn/Sources/GIDEMMSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ typedef NS_ENUM(NSInteger, ErrorCode) {
ErrorCodeAppVerificationRequired,
};

@interface GIDEMMSupport ()

+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary;

@end

@implementation GIDEMMSupport

- (instancetype)init {
Expand Down Expand Up @@ -115,9 +122,11 @@ + (NSDictionary *)parametersWithParameters:(NSDictionary *)parameters
#pragma mark - GTMAuthSessionDelegate

- (nullable NSDictionary<NSString *,NSString *> *)
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
return [GIDEMMSupport updatedEMMParametersWithParameters:
authSession.authState.lastTokenResponse.additionalParameters];
additionalTokenRefreshParametersForAuthSession:(GTMAuthSession *)authSession {
NSDictionary *additionalParameters = authSession.authState.lastTokenResponse.additionalParameters;
NSDictionary *updatedAdditionalParameters =
[GIDEMMSupport updatedEMMParametersWithParameters:additionalParameters];
return [GIDEMMSupport dictionaryWithStringValuesFromDictionary:updatedAdditionalParameters];
}

- (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
Expand All @@ -128,6 +137,30 @@ - (void)updateErrorForAuthSession:(GTMAuthSession *)authSession
}];
}

#pragma mark - Private Helpers

+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary {
NSMutableDictionary<NSString *, NSString *> *stringifiedDictionary =
[NSMutableDictionary dictionaryWithCapacity:originalDictionary.count];

[originalDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
if ([value isKindOfClass:[NSString class]]) {
stringifiedDictionary[key] = value;
return;
}
if ([value isKindOfClass:[NSNumber class]]) {
if (CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID()) {
stringifiedDictionary[key] = [value boolValue] ? @"true" : @"false";
} else {
stringifiedDictionary[key] = [value stringValue];
}
return;
}
}];
return stringifiedDictionary;
}

@end

NS_ASSUME_NONNULL_END
Expand Down
79 changes: 79 additions & 0 deletions GoogleSignIn/Tests/Unit/GIDEMMSupportTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
static NSString *const kDeviceOSKey = @"device_os";
static NSString *const kEMMPasscodeInfoKey = @"emm_passcode_info";

@interface GIDEMMSupport (Private)
Comment thread
AkshatG6 marked this conversation as resolved.
Outdated
+ (NSDictionary<NSString *, NSString *> *)
dictionaryWithStringValuesFromDictionary:(NSDictionary *)originalDictionary;
@end

@interface GIDEMMSupportTest : XCTestCase
// The view controller that has been presented, if any.
@property(nonatomic, strong, nullable) UIViewController *presentedViewController;
Expand Down Expand Up @@ -274,6 +279,80 @@ - (void)testHandleTokenFetchEMMError_errorIsNotEMM {
[self waitForExpectations:@[ called ] timeout:1];
}

# pragma mark - String Conversion Tests

- (void)testStringConversion_withAnyNumber_isConvertedToString {
NSDictionary *inputDictionary = @{ @"number_key": @12345 };

NSDictionary *resultDictionary = [GIDEMMSupport
Comment thread
AkshatG6 marked this conversation as resolved.
Outdated
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]],
@"The value should be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"number_key"], @"12345",
@"The NSNumber should be converted to a string.");
}

- (void)testStringConversion_withNumberOne_isConvertedToString {
NSDictionary *inputDictionary = @{ @"number_key": @1 };

NSDictionary *resultDictionary = [GIDEMMSupport
Comment thread
AkshatG6 marked this conversation as resolved.
Outdated
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]],
@"The value should be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"number_key"], @"1",
@"The NSNumber should be converted to a string.");
}

- (void)testStringConversion_withNumberZero_isConvertedToString {
NSDictionary *inputDictionary = @{ @"number_key": @0};

NSDictionary *resultDictionary = [GIDEMMSupport
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"number_key"] isKindOfClass:[NSString class]],
@"The value should be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"number_key"], @"0",
@"The NSNumber should be converted to a string.");
}

- (void)testStringConversion_withBooleanYes_isConvertedToTrueString {
NSDictionary *inputDictionary = @{ @"bool_key": @YES };

NSDictionary *resultDictionary = [GIDEMMSupport
Comment thread
AkshatG6 marked this conversation as resolved.
Outdated
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"bool_key"] isKindOfClass:[NSString class]],
@"The value should be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"bool_key"], @"true",
@"The boolean YES should be converted to the string 'true'.");
}

- (void)testStringConversion_withBooleanNo_isConvertedToFalseString {
NSDictionary *inputDictionary = @{ @"bool_key": @NO };

NSDictionary *resultDictionary = [GIDEMMSupport
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"bool_key"] isKindOfClass:[NSString class]],
@"The value should be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"bool_key"], @"false",
@"The boolean NO should be converted to the string 'false'.");
}

- (void)testStringConversion_withString_remainsUnchanged {
NSDictionary *inputDictionary = @{ @"string_key": @"hello" };

NSDictionary *resultDictionary = [GIDEMMSupport
dictionaryWithStringValuesFromDictionary:inputDictionary];

XCTAssertTrue([resultDictionary[@"string_key"] isKindOfClass:[NSString class]],
@"The value should still be an NSString.");
XCTAssertEqualObjects(resultDictionary[@"string_key"], @"hello",
@"The original string value should be preserved.");
}

# pragma mark - Helpers

- (NSString *)systemVersion {
Expand Down