Skip to content

Commit

Permalink
Fixed LogEntryEventHandler to properly set Log__c.ParentSessionId__c …
Browse files Browse the repository at this point in the history
…when LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY is false
  • Loading branch information
jongpie committed Apr 23, 2024
1 parent 92cc363 commit ef0b5e9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ public without sharing class LogEntryEventHandler extends LoggerSObjectHandler {
log.LoginPlatform__c = matchingAuthSessionProxy.LoginHistory.Platform;
log.LoginType__c = matchingAuthSessionProxy.LoginType;
log.LogoutUrl__c = matchingAuthSessionProxy.LogoutUrl;
log.ParentSessionId__c = matchingAuthSessionProxy.ParentId;
log.SessionId__c = matchingAuthSessionProxy.Id;
log.SessionSecurityLevel__c = matchingAuthSessionProxy.SessionSecurityLevel;
log.SessionType__c = matchingAuthSessionProxy.SessionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,26 +587,43 @@ private class LogEntryEventHandler_Tests {
logEntryEvent.SessionType__c = null;
logEntryEvent.SourceIp__c = null;
LoggerTestConfigurator.setMock(new LoggerParameter__mdt(DeveloperName = 'QueryAuthSessionDataSynchronously', Value__c = String.valueOf(false)));
System.Assert.isFalse(LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY);
MockLoggerEngineDataSelector mockSelector = new MockLoggerEngineDataSelector();
LoggerEngineDataSelector.setMock(mockSelector);
LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(null);
mockLoginHistoryProxy.Application = 'Application';
mockLoginHistoryProxy.Browser = 'Browser';
mockLoginHistoryProxy.Platform = 'Platform';
mockLoginHistoryProxy.UserId = System.UserInfo.getUserId();
LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(null);
mockAuthSessionProxy.Id = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType);
mockAuthSessionProxy.LoginHistory = mockLoginHistoryProxy;
mockAuthSessionProxy.LoginHistoryId = LoggerMockDataCreator.createId(Schema.LoginHistory.SObjectType);
mockAuthSessionProxy.LoginType = 'LoginType';
mockAuthSessionProxy.LogoutUrl = 'LogoutUrl';
mockAuthSessionProxy.ParentId = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType);
mockAuthSessionProxy.SessionSecurityLevel = 'SessionSecurityLevel';
mockAuthSessionProxy.SessionType = 'SessionType';
mockAuthSessionProxy.SourceIp = 'SourceIp';
mockAuthSessionProxy.UsersId = System.UserInfo.getUserId();
mockSelector.setCachedAuthSessionProxy(mockAuthSessionProxy);
System.Assert.areEqual(0, mockSelector.getCachedAuthSessionQueryCount());

LoggerMockDataStore.getEventBus().publishRecord(logEntryEvent);
LoggerMockDataStore.getEventBus().deliver(new LogEntryEventHandler());

List<Id> userIds = new List<Id>{ logEntryEvent.LoggedById__c };
LoggerSObjectProxy.AuthSession expectedAuthSessionProxy = LoggerEngineDataSelector.getInstance()
.getAuthSessionProxies(userIds)
.get(logEntryEvent.LoggedById__c);
Log__c log = getLog();
System.Assert.areEqual(expectedAuthSessionProxy?.LoginHistory.Application, log.LoginApplication__c);
System.Assert.areEqual(expectedAuthSessionProxy?.LoginHistory.Browser, log.LoginBrowser__c);
System.Assert.areEqual(expectedAuthSessionProxy?.LoginHistoryId, log.LoginHistoryId__c);
System.Assert.areEqual(expectedAuthSessionProxy?.LoginHistory.Platform, log.LoginPlatform__c);
System.Assert.areEqual(expectedAuthSessionProxy?.LoginType, log.LoginType__c);
System.Assert.areEqual(expectedAuthSessionProxy?.LogoutUrl, log.LogoutUrl__c);
System.Assert.areEqual(expectedAuthSessionProxy?.Id, log.SessionId__c);
System.Assert.areEqual(expectedAuthSessionProxy?.ParentId, log.ParentSessionId__c);
System.Assert.areEqual(expectedAuthSessionProxy?.SessionSecurityLevel, log.SessionSecurityLevel__c);
System.Assert.areEqual(expectedAuthSessionProxy?.SessionType, log.SessionType__c);
System.Assert.areEqual(expectedAuthSessionProxy?.SourceIp, log.SourceIp__c);
System.Assert.areEqual(mockAuthSessionProxy.Id, log.SessionId__c);
System.Assert.areEqual(mockAuthSessionProxy.LoginHistory.Application, log.LoginApplication__c);
System.Assert.areEqual(mockAuthSessionProxy.LoginHistory.Browser, log.LoginBrowser__c);
System.Assert.areEqual(mockAuthSessionProxy.LoginHistory.Platform, log.LoginPlatform__c);
System.Assert.areEqual(mockAuthSessionProxy.LoginHistoryId, log.LoginHistoryId__c);
System.Assert.areEqual(mockAuthSessionProxy.LoginType, log.LoginType__c);
System.Assert.areEqual(mockAuthSessionProxy.LogoutUrl, log.LogoutUrl__c);
System.Assert.areEqual(mockAuthSessionProxy.ParentId, log.ParentSessionId__c);
System.Assert.areEqual(mockAuthSessionProxy.SessionSecurityLevel, log.SessionSecurityLevel__c);
System.Assert.areEqual(mockAuthSessionProxy.SessionType, log.SessionType__c);
System.Assert.areEqual(mockAuthSessionProxy.SourceIp, log.SourceIp__c);
}

@IsTest
Expand Down Expand Up @@ -1793,4 +1810,25 @@ private class LogEntryEventHandler_Tests {
);
System.Assert.areEqual(logEntryEvent.TriggerSObjectType__c, logEntry.TriggerSObjectType__c, 'logEntry.TriggerSObjectType__c was not properly set');
}

private class MockLoggerEngineDataSelector extends LoggerEngineDataSelector {
private Integer authSessionProxiesQueryCount = 0;
private LoggerSObjectProxy.AuthSession mockAuthSessionProxy;

public Integer getCachedAuthSessionQueryCount() {
return authSessionProxiesQueryCount;
}

public override Map<Id, LoggerSObjectProxy.AuthSession> getAuthSessionProxies(List<Id> userIds) {
this.authSessionProxiesQueryCount++;
if (this.mockAuthSessionProxy != null) {
return new Map<Id, LoggerSObjectProxy.AuthSession>{ mockAuthSessionProxy.UsersId => mockAuthSessionProxy };
}
return super.getAuthSessionProxies(userIds);
}

public void setCachedAuthSessionProxy(LoggerSObjectProxy.AuthSession mockAuthSessionProxy) {
this.mockAuthSessionProxy = mockAuthSessionProxy;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,10 @@ private class LogEntryEventBuilder_Tests {
private Integer cachedUserQueryCount = 0;
private LoggerSObjectProxy.AuthSession mockAuthSessionProxy;

public Integer getCachedAuthSessionQueryCount() {
return cachedAuthSessionQueryCount;
}

public override LoggerSObjectProxy.AuthSession getCachedAuthSessionProxy() {
this.cachedAuthSessionQueryCount++;
if (this.mockAuthSessionProxy != null) {
Expand All @@ -1870,10 +1874,6 @@ private class LogEntryEventBuilder_Tests {
this.mockAuthSessionProxy = mockAuthSessionProxy;
}

public Integer getCachedAuthSessionQueryCount() {
return cachedAuthSessionQueryCount;
}

public override Schema.Organization getCachedOrganization() {
this.cachedOrganizationQueryCount++;
return super.getCachedOrganization();
Expand Down

0 comments on commit ef0b5e9

Please sign in to comment.