Skip to content

Commit 01d3a16

Browse files
committed
Fixes #440 - changes the few tests that are exercising CRUD/FLS enforcement to prefer the 'Minimum Access - Salesforce' profile since that is a more reliable Profile in most modern orgs
1 parent a685f9a commit 01d3a16

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

sfdx-source/apex-common/test/classes/fflib_SObjectSelectorTest.cls

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private with sharing class fflib_SObjectSelectorTest
378378
static void toSOQL_When_UserModeAndUserCannnotReadObject_Expect_QueryException(){
379379
AccessLevelOpportunitySelector sel = new AccessLevelOpportunitySelector();
380380

381-
User u = getLimitedReadOnlyUser();
381+
User u = getMinimumAccessUser();
382382
System.runAs(u){
383383
try{
384384
System.debug(sel.newQueryFactory().toSOQL());
@@ -399,7 +399,7 @@ private with sharing class fflib_SObjectSelectorTest
399399
static void toSOQL_When_SystemModeAndUserCannnotReadObject_Expect_Success(){
400400
AccessLevelOpportunitySelector sel = new AccessLevelOpportunitySelector(fflib_SObjectSelector.DataAccess.SYSTEM_MODE);
401401

402-
User u = getLimitedReadOnlyUser();
402+
User u = getMinimumAccessUser();
403403
System.runAs(u){
404404
sel.selectSObjectsById(new Set<Id>{fflib_IDGenerator.generate(Opportunity.SObjectType)});
405405
}
@@ -410,7 +410,7 @@ private with sharing class fflib_SObjectSelectorTest
410410
AccessLevelAccountSelector sel = new AccessLevelAccountSelector();
411411

412412
//Account has Read access by the limited read only user but no FLS access
413-
User u = getLimitedReadOnlyUser();
413+
User u = getMinimumAccessUser();
414414
System.runAs(u){
415415
try{
416416
System.debug(sel.newQueryFactory().toSOQL());
@@ -433,7 +433,7 @@ private with sharing class fflib_SObjectSelectorTest
433433
AccessLevelAccountSelector sel = new AccessLevelAccountSelector(fflib_SObjectSelector.DataAccess.SYSTEM_MODE);
434434

435435
//Account has Read access by the limited read only user but no FLS access
436-
User u = getLimitedReadOnlyUser();
436+
User u = getMinimumAccessUser();
437437
System.runAs(u){
438438
sel.selectSObjectsById(new Set<Id>{fflib_IDGenerator.generate(Account.SObjectType)});
439439
}
@@ -651,8 +651,8 @@ private with sharing class fflib_SObjectSelectorTest
651651
return testUser;
652652
}
653653

654-
private static User getLimitedReadOnlyUser(){
655-
return fflib_SecurityUtilsTest.setupTestUser('Read Only');
654+
private static User getMinimumAccessUser(){
655+
return fflib_SecurityUtilsTest.setupTestUser(true);
656656
}
657657

658658
@IsTest

sfdx-source/apex-common/test/classes/fflib_SecurityUtilsTest.cls

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,20 @@ private class fflib_SecurityUtilsTest {
7979
}
8080

8181
@TestVisible
82-
static User setupTestUser(String profileName){
82+
static User setupTestUser(Boolean limitedAccess){
8383
Profile p;
84-
Boolean applyReadOnlyPermissionSet = false;
85-
if (profileName == 'Read Only') {
86-
applyReadOnlyPermissionSet = true;
84+
85+
if (limitedAccess) {
8786
try {
88-
p = getProfile(profileName);
87+
p = getProfile('Minimum Access - Salesforce');
8988
} catch (QueryException ex) {
9089
if (ex.getMessage().contains('List has no rows for assignment to SObject')) {
91-
// #315 If the "Read Only" Profile is absent, then assume it's a Spring '21 org and see if there's a
92-
// "Minimum Access - Salesforce" Profile we can use instead.
93-
p = getProfile('Minimum Access - Salesforce');
90+
//#440 - not ideal, but we'll fall back to the rather liberally permissioned 'Read Only' profile that exists in very old orgs
91+
p = getProfile('Read Only');
9492
}
9593
}
9694
} else {
97-
p = getProfile(profileName);
95+
p = getProfile('System Administrator');
9896
}
9997

10098
//username global uniqueness is still enforced in tests
@@ -118,7 +116,7 @@ private class fflib_SecurityUtilsTest {
118116
);
119117
insert usr;
120118

121-
if (applyReadOnlyPermissionSet) {
119+
if (limitedAccess) {
122120
// #315 We need to assign the Perm Set to grant Account "Read" access
123121
PermissionSet accountReadPS = [SELECT Id FROM PermissionSet WHERE Name = 'ReadOnlyPermissionSet'];
124122
PermissionSetAssignment psa = new PermissionSetAssignment(AssigneeId = usr.Id, PermissionSetId = accountReadPS.Id);
@@ -129,7 +127,7 @@ private class fflib_SecurityUtilsTest {
129127

130128
@isTest
131129
static void readonly_field_access() {
132-
User testUser = setupTestUser('Read Only');
130+
User testUser = setupTestUser(true);
133131
System.runAs(testUser){
134132
{
135133
fflib_SecurityUtils.SecurityException ex;
@@ -172,7 +170,7 @@ private class fflib_SecurityUtilsTest {
172170

173171
@isTest
174172
static void readonly_object_access() {
175-
User testUser = setupTestUser('Read Only');
173+
User testUser = setupTestUser(true);
176174
System.runAs(testUser){
177175
{
178176
fflib_SecurityUtils.SecurityException ex;
@@ -226,7 +224,7 @@ private class fflib_SecurityUtilsTest {
226224

227225
@isTest
228226
static void readonly_objectAndField_access() {
229-
User testUser = setupTestUser('Read Only');
227+
User testUser = setupTestUser(true);
230228
System.runAs(testUser){
231229
{
232230
fflib_SecurityUtils.SecurityException ex;
@@ -311,7 +309,7 @@ private class fflib_SecurityUtilsTest {
311309

312310
@isTest
313311
static void sysadmin_objectAndField_access() {
314-
User testUser = setupTestUser('System Administrator');
312+
User testUser = setupTestUser(false);
315313
System.runAs(testUser){
316314
fflib_SecurityUtils.checkInsert(
317315
Account.SObjectType,

0 commit comments

Comments
 (0)