Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
antrix1989 committed Sep 26, 2024
1 parent 59e19f3 commit 97385d4
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,32 @@

#import <XCTest/XCTest.h>
#import "MSIDBrokerOperationBrowserNativeMessageRequest.h"
#import "MSIDJsonSerializableFactory.h"

@interface MSIDBrokerOperationBrowserNativeMockRequest : MSIDBaseBrokerOperationRequest <MSIDJsonSerializable>

@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

Expand All @@ -34,6 +60,7 @@ @implementation MSIDBrokerOperationBrowserNativeMessageRequestTests

- (void)setUp
{
[MSIDJsonSerializableFactory registerClass:MSIDBrokerOperationBrowserNativeMockRequest.class forClassType:@"BrowserNativeMockRequest"];
}

- (void)tearDown
Expand All @@ -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
29 changes: 29 additions & 0 deletions IdentityCore/tests/MSIDBrowserNativeMessageGetTokenRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 97385d4

Please sign in to comment.