Skip to content

Commit

Permalink
[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
jongpie committed Oct 15, 2024
1 parent 8f018fb commit 10f2cf2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config/linters/lint-staged.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
'sfdx-project.json': () => {
return `npm run package:version:number:fix`;
},
'*.{apex,cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}': filenames => filenames.map(filename => `prettier --write '${filename}'`),
'*.{apex,cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}': filenames => filenames.map(filename => `prettier --write '${filename}'`),
'**/lwc/**': filenames => {
return [`eslint --config ./config/linters/.eslintrc.json ${filenames.join(' ')} --fix`];
// FIXME this command should only run tests for the changed LWCs (instead of running tests for all LWCs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public without sharing class LoggerSObjectProxy {
* class acts as a substitute for a `Schema.AuthSession` record to provide the abilility to mock the data during unit & integration tests.
*/
@SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions')
public class AuthSession {
public class AuthSession extends SObjectProxy {
public Id Id;
public String LoginType;
public Id LoginHistoryId;
Expand All @@ -36,7 +36,7 @@ public without sharing class LoggerSObjectProxy {
* class acts as a substitute for a `Schema.LoginHistory` record to provide the abilility to mock the data during unit & integration tests.
*/
@SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions')
public class LoginHistory {
public class LoginHistory extends SObjectProxy {
public String Application;
public String Browser;
public String Platform;
Expand All @@ -50,7 +50,7 @@ public without sharing class LoggerSObjectProxy {
* strongly-typed references to fields (properties).
*/
@SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions')
public class Network {
public class Network extends SObjectProxy {
public String Id;
public String Name;
public String UrlPathPrefix;
Expand All @@ -63,7 +63,7 @@ public without sharing class LoggerSObjectProxy {
* strongly-typed references to fields (properties).
*/
@SuppressWarnings('PMD.FieldNamingConventions, PMD.VariableNamingConventions')
public class OmniProcess {
public class OmniProcess extends SObjectProxy {
public Id CreatedById;
public Schema.User CreatedBy;
public Datetime CreatedDate;
Expand All @@ -74,4 +74,10 @@ public without sharing class LoggerSObjectProxy {
public String OmniProcessType;
public String UniqueName;
}

private abstract class SObjectProxy {
public String serialize() {
return System.JSON.serializePretty(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,12 @@ private class LogEntryEventHandler_Tests {
System.Assert.isFalse(LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY);
MockLoggerEngineDataSelector mockSelector = new MockLoggerEngineDataSelector();
LoggerEngineDataSelector.setMock(mockSelector);
LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(null);
LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory();
mockLoginHistoryProxy.Application = 'Application';
mockLoginHistoryProxy.Browser = 'Browser';
mockLoginHistoryProxy.Platform = 'Platform';
mockLoginHistoryProxy.UserId = System.UserInfo.getUserId();
LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(null);
LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession();
mockAuthSessionProxy.Id = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType);
mockAuthSessionProxy.LoginHistory = mockLoginHistoryProxy;
mockAuthSessionProxy.LoginHistoryId = LoggerMockDataCreator.createId(Schema.LoginHistory.SObjectType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,12 @@ private class LogEntryEventBuilder_Tests {
System.Assert.isTrue(LoggerParameter.QUERY_AUTH_SESSION_DATA_SYNCHRONOUSLY);
MockLoggerEngineDataSelector mockSelector = new MockLoggerEngineDataSelector();
LoggerEngineDataSelector.setMock(mockSelector);
LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory(null);
LoggerSObjectProxy.LoginHistory mockLoginHistoryProxy = new LoggerSObjectProxy.LoginHistory();
mockLoginHistoryProxy.Application = 'Application';
mockLoginHistoryProxy.Browser = 'Browser';
mockLoginHistoryProxy.Platform = 'Platform';
mockLoginHistoryProxy.UserId = System.UserInfo.getUserId();
LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession(null);
LoggerSObjectProxy.AuthSession mockAuthSessionProxy = new LoggerSObjectProxy.AuthSession();
mockAuthSessionProxy.Id = LoggerMockDataCreator.createId(Schema.AuthSession.SObjectType);
mockAuthSessionProxy.LoginHistory = mockLoginHistoryProxy;
mockAuthSessionProxy.LoginHistoryId = LoggerMockDataCreator.createId(Schema.LoginHistory.SObjectType);
Expand Down Expand Up @@ -1789,24 +1789,23 @@ private class LogEntryEventBuilder_Tests {
}

@IsTest
static void it_should_set_network_fields_for_experience_site_user_using_stub_network_record() {
String mockNetworkId = LoggerMockDataCreator.createId(Schema.Account.SObjectType);
String mockNetworkName = 'An Account record, substituting for a Network record';
String mockNetworkUrlPrefix = 'some string value for URL prefix';
LoggerSObjectProxy.NETWORK_FIELD_NAMES.urlPathPrefixField = Schema.Account.Description.toString();
Schema.Account stubNetworkRecord = new Schema.Account(Description = mockNetworkUrlPrefix, Id = mockNetworkId, Name = mockNetworkName);
LogEntryEventBuilder.networkId = stubNetworkRecord.Id;
static void it_should_set_network_fields_for_experience_site_user_using_mock_network_proxy() {
LoggerSObjectProxy.Network mockNetworkProxy = new LoggerSObjectProxy.Network();
mockNetworkProxy.Id = LoggerMockDataCreator.createId(Schema.Account.SObjectType);
mockNetworkProxy.Name = 'Some Network Name';
mockNetworkProxy.UrlPathPrefix = 'some string value for URL prefix';
LogEntryEventBuilder.networkId = mockNetworkProxy.Id;
LoggerEngineDataSelector.useMocks();
LoggerEngineDataSelector.mockNetworkRecords.add(stubNetworkRecord);
LoggerEngineDataSelector.mockNetworkProxies.add(mockNetworkProxy);

LogEntryEvent__e logEntryEvent = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true).getLogEntryEvent();

System.Assert.areEqual(mockNetworkId, logEntryEvent.NetworkId__c);
System.Assert.areEqual(mockNetworkName, logEntryEvent.NetworkName__c);
System.Assert.areEqual(System.Network.getLoginUrl(mockNetworkId), logEntryEvent.NetworkLoginUrl__c);
System.Assert.areEqual(System.Network.getLogoutUrl(mockNetworkId), logEntryEvent.NetworkLogoutUrl__c);
System.Assert.areEqual(System.Network.getSelfRegUrl(mockNetworkId), logEntryEvent.NetworkSelfRegistrationUrl__c);
System.Assert.areEqual(mockNetworkUrlPrefix, logEntryEvent.NetworkUrlPathPrefix__c);
System.Assert.areEqual(mockNetworkProxy.Id, logEntryEvent.NetworkId__c);
System.Assert.areEqual(mockNetworkProxy.Name, logEntryEvent.NetworkName__c);
System.Assert.areEqual(System.Network.getLoginUrl(mockNetworkProxy.Id), logEntryEvent.NetworkLoginUrl__c);
System.Assert.areEqual(System.Network.getLogoutUrl(mockNetworkProxy.Id), logEntryEvent.NetworkLogoutUrl__c);
System.Assert.areEqual(System.Network.getSelfRegUrl(mockNetworkProxy.Id), logEntryEvent.NetworkSelfRegistrationUrl__c);
System.Assert.areEqual(mockNetworkProxy.UrlPathPrefix, logEntryEvent.NetworkUrlPathPrefix__c);
}

@IsTest
Expand All @@ -1815,13 +1814,17 @@ private class LogEntryEventBuilder_Tests {
return;
}

SObject queriedNetworkRecord = System.Database.query('SELECT Id, Name, UrlPathPrefix FROM Network LIMIT 1');
// Ideally, this type of test would run using System.runAs() and a test user setup for the queried Network record
// But, setting up a test user in a test context can run into a lot of issues due to custom fields, validation rules, etc.
// that have been deployed to an org. So, just use the mockNetworkRecords variable, even though this is a real Network record
SObject queriedNetworkRecord = System.Database.query('SELECT Id, Name, UrlPathPrefix FROM Network LIMIT 1');
LoggerSObjectProxy.Network networkProxy = (LoggerSObjectProxy.Network) System.JSON.deserialize(
System.JSON.serialize(queriedNetworkRecord),
LoggerSObjectProxy.Network.class
);
LogEntryEventBuilder.networkId = queriedNetworkRecord.Id;
LoggerEngineDataSelector.useMocks();
LoggerEngineDataSelector.mockNetworkRecords.add(queriedNetworkRecord);
LoggerEngineDataSelector.mockNetworkProxies.add(networkProxy);

LogEntryEvent__e logEntryEvent = new LogEntryEventBuilder(getUserSettings(), System.LoggingLevel.INFO, true).getLogEntryEvent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ private class LoggerSObjectProxy_Tests {
private static final Boolean IS_EXPERIENCE_CLOUD_ENABLED = System.Type.forName('Schema.Network') != null;
private static final Boolean IS_OMNISTUDIO_ENABLED = System.Type.forName('Schema.OmniProcess') != null;

@IsTest
static void it_supports_constructing_proxy_classes() {
// This test is a bit silly, it's primarily here because the other tests uses JSON.deserialize(),
// which doesn't provided code coverage
LoggerSObjectProxy.AuthSession authSessionProxy = new LoggerSObjectProxy.AuthSession();
LoggerSObjectProxy.LoginHistory loginHistoryProxy = new LoggerSObjectProxy.LoginHistory();
LoggerSObjectProxy.Network networkProxy = new LoggerSObjectProxy.Network();
LoggerSObjectProxy.OmniProcess omniProcessProxy = new LoggerSObjectProxy.OmniProcess();

System.Assert.areEqual(System.JSON.serializePretty(authSessionProxy), authSessionProxy.serialize());
System.Assert.areEqual(System.JSON.serializePretty(loginHistoryProxy), loginHistoryProxy.serialize());
System.Assert.areEqual(System.JSON.serializePretty(networkProxy), networkProxy.serialize());
System.Assert.areEqual(System.JSON.serializePretty(omniProcessProxy), omniProcessProxy.serialize());
}

@IsTest
static void it_correctly_deserializes_auth_session_records_to_proxies() {
List<Schema.AuthSession> authSessionRecords = [
Expand Down Expand Up @@ -39,10 +54,10 @@ private class LoggerSObjectProxy_Tests {
for (Integer i = 0; i < authSessionRecords.size(); i++) {
Schema.AuthSession authSessionRecord = authSessionRecords.get(i);
LoggerSObjectProxy.AuthSession authSessionProxy = authSessionProxies.get(i);
System.Assert.areEqual(authSessionRecord.LoginHistory.Application, authSessionProxy.LoginHistory.Application);
System.Assert.areEqual(authSessionRecord.LoginHistory.Browser, authSessionProxy.LoginHistory.Browser);
System.Assert.areEqual(authSessionRecord.LoginHistory.Platform, authSessionProxy.LoginHistory.Platform);
System.Assert.areEqual(authSessionRecord.LoginHistory.UserId, authSessionProxy.LoginHistory.UserId);
System.Assert.areEqual(authSessionRecord.LoginHistory?.Application, authSessionProxy.LoginHistory?.Application);
System.Assert.areEqual(authSessionRecord.LoginHistory?.Browser, authSessionProxy.LoginHistory?.Browser);
System.Assert.areEqual(authSessionRecord.LoginHistory?.Platform, authSessionProxy.LoginHistory?.Platform);
System.Assert.areEqual(authSessionRecord.LoginHistory?.UserId, authSessionProxy.LoginHistory?.UserId);
System.Assert.areEqual(authSessionRecord.LoginHistoryId, authSessionProxy.LoginHistoryId);
System.Assert.areEqual(authSessionRecord.LoginType, authSessionProxy.LoginType);
System.Assert.areEqual(authSessionRecord.LogoutUrl, authSessionProxy.LogoutUrl);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"plugin:version:create:logger-admin-dashboard": "sf package version create --json --package \"Nebula Logger - Core Plugin - Logger Admin Dashboard\" --skip-ancestor-check --code-coverage --installation-key-bypass --wait 30",
"plugin:version:create:slack": "sf package version create --json --package \"Nebula Logger - Core Plugin - Slack\" --skip-ancestor-check --code-coverage --installation-key-bypass --wait 30",
"prepare": "husky install && chmod +x ./.husky/pre-commit",
"prettier:fix": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
"prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,xml,yaml,yml}\"",
"prettier:fix": "prettier --write \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}\"",
"prettier:verify": "prettier --list-different \"**/*.{cls,cmp,component,css,html,js,json,md,page,trigger,yaml,yml}\"",
"scan:fix:lwc": "eslint --config ./config/linters/.eslintrc.json **/lwc/** --fix",
"scan": "npm run scan:apex && npm run scan:lwc",
"scan:apex": "sf scanner:run --pmdconfig ./config/linters/pmd-ruleset.xml --target ./nebula-logger/ --engine pmd --severity-threshold 3",
Expand Down

0 comments on commit 10f2cf2

Please sign in to comment.