Let's pretend I am working on a Managed Package app that is soon going to Security Review. I have used all the latest and greatest native features (I know some of them are not GA yet) like as user
and WITH USER_MODE
to ensure CRUD/FLS and Sharing security.
To avoid overlooking something, I use positive and negative test methods using System.runAs()
.
Business logic:
public void execute() {
List<CustomObject__c> records = [SELECT int_Field__C FROM CustomObject__c
WHERE txt_Field__c = NULL WITH USER_MODE];
for(CustomObject__c record : packages) {
record.int_Field__C = calculate(record);
}
update as user records;
}
Apex Test:
@IsTest
private static void failsWhenCalledByStandardUser() {
// Setup
Exception expectedException = null;
// Exercise
try {
System.runAs(PERSONAS.standardUser) {
new BusinessLogic().execute();
}
}
catch(Exception ex) {
expectedException = ex;
}
// Verify
System.assertNotEquals(null, expectedException);
}
Real source code can be found here: https://github.com/rsoesemann/salesforce-isv-cockpit/blob/master/force-app/main/default/classes/SetupPackageNamespaces_Test.cls
Am I right that this indeed should be done to a big extend to finally prove Sharing/CRUD/FLS security of our code?