From 97385d4d4a3418740cdcba902331d772415cd6e8 Mon Sep 17 00:00:00 2001 From: Sergey Demchenko Date: Thu, 26 Sep 2024 14:21:20 -0700 Subject: [PATCH] Add tests. --- ...okerOperationBrowserNativeMessageRequest.m | 13 ++- .../MSIDBrowserNativeMessageGetTokenRequest.m | 14 ++-- ...perationBrowserNativeMessageRequestTests.m | 80 +++++++++++++++++++ ...BrowserNativeMessageGetTokenRequestTests.m | 29 +++++++ 4 files changed, 124 insertions(+), 12 deletions(-) diff --git a/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrokerOperationBrowserNativeMessageRequest.m b/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrokerOperationBrowserNativeMessageRequest.m index d960d0fd3..acb86d188 100644 --- a/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrokerOperationBrowserNativeMessageRequest.m +++ b/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrokerOperationBrowserNativeMessageRequest.m @@ -30,6 +30,9 @@ NSString *const MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PAYLOAD_KEY = @"payload"; NSString *const MSID_BROWSER_NATIVE_MESSAGE_REQUEST_METHOD_KEY = @"method"; +NSString *const MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPTID_KEY = @"parent_process_teamId"; +NSString *const MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPBI_KEY = @"parent_process_bundle_identifier"; +NSString *const MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPLN_KEY = @"parent_process_localized_name"; @implementation MSIDBrokerOperationBrowserNativeMessageRequest @@ -113,9 +116,9 @@ - (instancetype)initWithJSONDictionary:(NSDictionary *)json error:(NSError *__au return nil; } - _parentProcessBundleIdentifier = [json msidStringObjectForKey:@"parent_process_bundle_identifier"]; - _parentProcessTeamId = [json msidStringObjectForKey:@"parent_process_teamId"]; - _parentProcessLocalizedName = [json msidStringObjectForKey:@"parent_process_localized_name"]; + _parentProcessTeamId = [json msidStringObjectForKey:MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPTID_KEY]; + _parentProcessBundleIdentifier = [json msidStringObjectForKey:MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPBI_KEY]; + _parentProcessLocalizedName = [json msidStringObjectForKey:MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPLN_KEY]; } return self; @@ -129,6 +132,10 @@ - (NSDictionary *)jsonDictionary json[MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PAYLOAD_KEY] = [self.payloadJson msidJSONSerializeWithContext:nil]; + json[MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPTID_KEY] = self.parentProcessTeamId; + json[MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPBI_KEY] = self.parentProcessBundleIdentifier; + json[MSID_BROWSER_NATIVE_MESSAGE_REQUEST_PPLN_KEY] = self.parentProcessLocalizedName; + return json; } diff --git a/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrowserNativeMessageGetTokenRequest.m b/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrowserNativeMessageGetTokenRequest.m index 222526d61..7e272587d 100644 --- a/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrowserNativeMessageGetTokenRequest.m +++ b/IdentityCore/src/broker_operation/request/browser_native_message_request/MSIDBrowserNativeMessageGetTokenRequest.m @@ -62,15 +62,11 @@ + (NSString *)operation - (NSString *)localizedApplicationInfo { - // If they are nil or empty -- return display name. - if ([NSString msidIsStringNilOrBlank:self.clientId] && [NSString msidIsStringNilOrBlank:self.redirectUri]) - { - return self.localizedCallerDisplayName; - } - - // Otherwise show clientId and redirect uri. - __auto_type clientId = self.clientId ?: NSLocalizedString(@"N/A", nil); - __auto_type redirectUri = self.redirectUri ?: NSLocalizedString(@"N/A", nil); + // clientId && redirectUri are requered params and should be validated during init. + NSParameterAssert(self.clientId); + NSParameterAssert(self.redirectUri); + __auto_type clientId = self.clientId ?: @""; + __auto_type redirectUri = self.redirectUri ?: @""; NSString *clientIdKey = NSLocalizedString(@"Client ID", nil); NSString *redirectUriKey = NSLocalizedString(@"Redirect URI", nil); diff --git a/IdentityCore/tests/MSIDBrokerOperationBrowserNativeMessageRequestTests.m b/IdentityCore/tests/MSIDBrokerOperationBrowserNativeMessageRequestTests.m index 4ace28298..66758457c 100644 --- a/IdentityCore/tests/MSIDBrokerOperationBrowserNativeMessageRequestTests.m +++ b/IdentityCore/tests/MSIDBrokerOperationBrowserNativeMessageRequestTests.m @@ -25,6 +25,32 @@ #import #import "MSIDBrokerOperationBrowserNativeMessageRequest.h" +#import "MSIDJsonSerializableFactory.h" + +@interface MSIDBrokerOperationBrowserNativeMockRequest : MSIDBaseBrokerOperationRequest + +@end + +@implementation MSIDBrokerOperationBrowserNativeMockRequest + +#pragma mark - MSIDBrokerOperationRequest + +- (NSString *)localizedApplicationInfo +{ + return @"mock_app_info"; +} + +- (instancetype)initWithJSONDictionary:(NSDictionary *)json error:(NSError *__autoreleasing *)error +{ + return [self init]; +} + +- (NSDictionary *)jsonDictionary +{ + return @{}; +} + +@end @interface MSIDBrokerOperationBrowserNativeMessageRequestTests : XCTestCase @@ -34,6 +60,7 @@ @implementation MSIDBrokerOperationBrowserNativeMessageRequestTests - (void)setUp { + [MSIDJsonSerializableFactory registerClass:MSIDBrokerOperationBrowserNativeMockRequest.class forClassType:@"BrowserNativeMockRequest"]; } - (void)tearDown @@ -60,12 +87,65 @@ - (void)testJsonDictionary_whenPayloadExist_shouldBeCorrect request.payloadJson = @{@"a": @"b"}; request.brokerKey = @"some key"; request.protocolVersion = 1; + request.parentProcessBundleIdentifier = @"com.qwe"; + request.parentProcessTeamId = @"12345"; + request.parentProcessLocalizedName = @"name1"; __auto_type expectedJson = @{@"broker_key": @"some key", @"msg_protocol_ver": @"1", + @"parent_process_bundle_identifier": @"com.qwe", + @"parent_process_localized_name": @"name1", + @"parent_process_teamId": @"12345", @"payload": @"{\"a\":\"b\"}"}; XCTAssertEqualObjects(expectedJson, [request jsonDictionary]); } +- (void)testInitWithJSONDictionary_whenAllValuesSet_shoudlInit +{ + __auto_type json = @{@"broker_key": @"some key", + @"msg_protocol_ver": @"1", + @"parent_process_bundle_identifier": @"com.qwe", + @"parent_process_teamId": @"12345", + @"parent_process_localized_name": @"name1", + @"payload": @"{\"method\":\"BrowserNativeMockRequest\"}"}; + + NSError *error; + __auto_type request = [[MSIDBrokerOperationBrowserNativeMessageRequest alloc] initWithJSONDictionary:json error:&error]; + + XCTAssertNil(error); + XCTAssertEqualObjects(request.brokerKey, @"some key"); + XCTAssertEqual(request.protocolVersion, 1); + XCTAssertEqualObjects(request.payloadJson, @{@"method":@"BrowserNativeMockRequest"}); + XCTAssertEqualObjects(request.parentProcessBundleIdentifier, @"com.qwe"); + XCTAssertEqualObjects(request.parentProcessTeamId, @"12345"); + XCTAssertEqualObjects(request.parentProcessLocalizedName, @"name1"); + XCTAssertEqualObjects(request.callerBundleIdentifier, @"com.qwe"); + XCTAssertEqualObjects(request.callerTeamIdentifier, @"12345"); + XCTAssertEqualObjects(request.localizedCallerDisplayName, @"name1"); + XCTAssertEqualObjects(request.localizedApplicationInfo, @"mock_app_info"); + +} + +- (void)testInitWithJSONDictionary_whenNoParentProcessInfo_shouldReturnNA +{ + __auto_type json = @{@"broker_key": @"some key", + @"msg_protocol_ver": @"1", + @"payload": @"{\"method\":\"GetCookies\"}"}; + + NSError *error; + __auto_type request = [[MSIDBrokerOperationBrowserNativeMessageRequest alloc] initWithJSONDictionary:json error:&error]; + + XCTAssertNil(error); + XCTAssertEqualObjects(request.brokerKey, @"some key"); + XCTAssertEqual(request.protocolVersion, 1); + XCTAssertEqualObjects(request.payloadJson, @{@"method":@"GetCookies"}); + XCTAssertEqualObjects(request.callerBundleIdentifier, @"N/A"); + XCTAssertEqualObjects(request.callerTeamIdentifier, @"N/A"); + XCTAssertEqualObjects(request.localizedCallerDisplayName, @"N/A"); + XCTAssertEqualObjects(request.localizedApplicationInfo, @"N/A"); + XCTAssertNil(request.parentProcessBundleIdentifier); + XCTAssertNil(request.parentProcessTeamId); + XCTAssertNil(request.parentProcessLocalizedName); +} @end diff --git a/IdentityCore/tests/MSIDBrowserNativeMessageGetTokenRequestTests.m b/IdentityCore/tests/MSIDBrowserNativeMessageGetTokenRequestTests.m index cf8f3bd85..e0c5d739d 100644 --- a/IdentityCore/tests/MSIDBrowserNativeMessageGetTokenRequestTests.m +++ b/IdentityCore/tests/MSIDBrowserNativeMessageGetTokenRequestTests.m @@ -167,4 +167,33 @@ - (void)testInitWithJSONDictionary_whenAccountIdInvalid_shouldFail } +- (void)testLocalizedApplicationInfo_whenClientIdAndRedirectUri_shouldReturnClientIDandRedirectUri +{ + __auto_type json = @{ + @"sender": @"https://login.microsoft.com", + @"request": @{ + @"accountId": @"uid.utid", + @"clientId": @"29a788ca-7bcf-4732-b23c-c8d294347e5b", + @"authority": @"https://login.microsoftonline.com/common", + @"scope": @"user.read openid profile offline_access", + @"redirectUri": @"https://login.microsoft.com", + @"correlationId": @"9BBCA391-33A9-4EC9-A00E-A0FBFA71013D", + @"prompt": @"login", + @"isSts": @(YES), + @"canShowUI": @(NO), + @"nonce": @"e98aba90-bc47-4ff9-8809-b6e1c7e7cd47", + @"state": @"state1", + @"loginHint": @"user@microsoft.com", + @"instance_aware": @(YES), + } + }; + + NSError *error; + __auto_type request = [[MSIDBrowserNativeMessageGetTokenRequest alloc] initWithJSONDictionary:json error:&error]; + + XCTAssertNil(error); + XCTAssertNotNil(request); + XCTAssertEqualObjects(@"Client ID: 29a788ca-7bcf-4732-b23c-c8d294347e5b Redirect URI: https://login.microsoft.com", request.localizedApplicationInfo); +} + @end