Skip to content

Commit 0b2d8fc

Browse files
author
tfuda
committed
315 Create an ad-hoc Permission Set that grants read access to Account, Contact and Lead and use this to reproduce the perms that used to be granted by the "Read Only" profile.
1 parent 46e2e79 commit 0b2d8fc

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

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

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,76 @@
2626

2727
@isTest
2828
private class fflib_SecurityUtilsTest {
29+
30+
@TestSetup
31+
static void testSetup() {
32+
// #315 Create a Permission Set that grants "Read" access to Account, Contact and Lead. We will use this in
33+
// Spring '21 orgs that lack the "Read Only" Profile. See:
34+
// https://help.salesforce.com/articleView?id=release-notes.rn_profiles_and_perms_read_only_new.htm&release=230&type=5).
35+
PermissionSet ps = new PermissionSet(Label = 'Read Only Permission Set', Name = 'ReadOnlyPermissionSet');
36+
insert ps;
37+
38+
// Grant Read access to the SObjects we use for CRUD tests
39+
List<ObjectPermissions> objectPerms = new List<ObjectPermissions>();
40+
objectPerms.add(createObjectPermissions(ps.Id, 'Account', false, true, false, false));
41+
objectPerms.add(createObjectPermissions(ps.Id, 'Contact', false, true, false, false));
42+
objectPerms.add(createObjectPermissions(ps.Id, 'Lead', false, true, false, false));
43+
insert objectPerms;
44+
45+
// Grant Read/Edit access to the SObject fields we use for FLS tests
46+
List<FieldPermissions> fieldPerms = new List<FieldPermissions>();
47+
fieldPerms.add(createFieldPermissions(ps.Id, 'Contact', 'Birthdate', true, false));
48+
fieldPerms.add(createFieldPermissions(ps.Id, 'Contact', 'Email', true, false));
49+
insert fieldPerms;
50+
}
51+
52+
static Profile getProfile(String profileName) {
53+
return [SELECT Id, Name FROM Profile WHERE Name = :profileName];
54+
}
55+
56+
static ObjectPermissions createObjectPermissions(
57+
Id permSetId, String objectType, Boolean canCreate, Boolean canRead, Boolean canUpdate, Boolean canDelete
58+
) {
59+
return new ObjectPermissions(
60+
ParentId = permSetId,
61+
SobjectType = objectType,
62+
PermissionsCreate = canCreate,
63+
PermissionsRead = canRead,
64+
PermissionsEdit = canUpdate,
65+
PermissionsDelete = canDelete
66+
);
67+
}
68+
69+
static FieldPermissions createFieldPermissions(
70+
Id permSetId, String objectType, String fieldName, Boolean canRead, Boolean canEdit
71+
) {
72+
return new FieldPermissions(
73+
ParentId = permSetId,
74+
SobjectType = objectType,
75+
Field = objectType + '.' + fieldName,
76+
PermissionsRead = canRead,
77+
PermissionsEdit = canEdit
78+
);
79+
}
80+
2981
static User setupTestUser(String profileName){
82+
Profile p;
83+
Boolean usedMinimumAccessProfile = false;
84+
if (profileName == 'Read Only') {
85+
try {
86+
p = getProfile(profileName);
87+
} catch (QueryException ex) {
88+
if (ex.getMessage().contains('List has no rows for assignment to SObject')) {
89+
// #315 If the "Read Only" Profile is absent, then assume it's a Spring '21 org and see if there's a
90+
// "Minimum Access - Salesforce" Profile we can use instead.
91+
p = getProfile('Minimum Access - Salesforce');
92+
usedMinimumAccessProfile = true;
93+
}
94+
}
95+
} else {
96+
p = getProfile(profileName);
97+
}
98+
3099
//username global uniqueness is still enforced in tests
31100
//make sure we get something unique to avoid issues with parallel tests
32101
String uniqueness = DateTime.now()+':'+Math.random();
@@ -35,8 +104,7 @@ private class fflib_SecurityUtilsTest {
35104
}catch(Exception e){
36105
uniqueness += e.getStackTraceString(); //includes the top level test method name without having to pass it
37106
}
38-
Profile p = [SELECT id, Name FROM Profile WHERE Name = :profileName];
39-
User result = new User(
107+
User usr = new User(
40108
username=UserInfo.getUserId()+'.'+uniqueness.HashCode()+'@'+UserInfo.getOrganizationId()+'.sfdcOrg',
41109
alias = 'testExec',
42110
@@ -47,8 +115,15 @@ private class fflib_SecurityUtilsTest {
47115
profileid = p.Id,
48116
timezonesidkey='America/Los_Angeles'
49117
);
50-
insert result;
51-
return result;
118+
insert usr;
119+
120+
if (usedMinimumAccessProfile) {
121+
// #315 We need to assign the Perm Set to grant Account "Read" access
122+
PermissionSet accountReadPS = [SELECT Id FROM PermissionSet WHERE Name = 'ReadOnlyPermissionSet'];
123+
PermissionSetAssignment psa = new PermissionSetAssignment(AssigneeId = usr.Id, PermissionSetId = accountReadPS.Id);
124+
insert psa;
125+
}
126+
return usr;
52127
}
53128

54129
@isTest
@@ -176,8 +251,8 @@ private class fflib_SecurityUtilsTest {
176251
Contact.SObjectType,
177252
new List<String>{
178253
'LastName',
179-
'accountId',
180-
'ownerId'
254+
'eMaiL',
255+
'BirthDATE'
181256
}
182257
);
183258
}catch(fflib_SecurityUtils.SecurityException e){

0 commit comments

Comments
 (0)