-
Notifications
You must be signed in to change notification settings - Fork 64
/
SFDCAccessControllerTest.cls
319 lines (284 loc) · 17.6 KB
/
SFDCAccessControllerTest.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2010 - Salesforce.com
*
* The Apex ESAPI implementation is published by Salesforce.com under the New BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Yoel Gluck (securecloud .at. salesforce.com) <a href="http://www.salesforce.com">Salesforce.com</a>
* @created 2010
*/
/**
* This class contains unit tests for validating the behavior of Apex classes
* and triggers.
*
* Unit tests are class methods that verify whether a particular piece
* of code is working properly. Unit test methods take no arguments,
* commit no data to the database, and are flagged with the testMethod
* keyword in the method definition.
*
* All test methods in an organization are executed whenever Apex code is deployed
* to a production organization to confirm correctness, ensure code
* coverage, and prevent regressions. All Apex classes are
* required to have at least 75% code coverage in order to be deployed
* to a production organization. In addition, all triggers must have some code coverage.
*
* The @isTest class annotation indicates this class only contains test
* methods. Classes defined with the @isTest annotation do not count against
* the organization size limit for all Apex scripts.
*
* See the Apex Language Reference for more information about Testing and Code Coverage.
*/
@isTest
private class SFDCAccessControllerTest {
static SFDCAccessController accessController = new SFDCAccessController();
@isTest
private static void testAccessControlRunAs() {
String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
// This code runs as the system user
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User'];
User u = new User(
Alias = 'standt',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Testing',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
UserName = uniqueUserName
);
System.runAs(u) {
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
accessController.assertAuthorizedToCreate(Schema.Survey__c.SobjectType, new List<Schema.SObjectField>{ Schema.Survey__c.fields.Name });
System.assert(false, 'Should not succeed as Survey__c is not accessible to standard user');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_Create, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
Boolean retVal = accessController.checkAuthorizedToCreate(Schema.Survey__c.SobjectType, new List<Schema.SObjectField>{ Schema.Survey__c.fields.Name }, false);
System.assertEquals(false, retVal);
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_Create, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
accessController.assertAuthorizedToView(Schema.Survey__c.SobjectType, new List<Schema.SObjectField>{ Schema.Survey__c.fields.Name });
System.assert(false, 'Should not succeed as Survey__c is not accessible to standard user');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_READ, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
Boolean retVal = accessController.checkAuthorizedToView(Schema.Survey__c.SobjectType, new List<Schema.SObjectField>{ Schema.Survey__c.fields.Name }, false);
System.assertEquals(false, retVal);
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_READ, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
Boolean retVal = accessController.checkAuthorizedToView(Schema.Survey__c.SobjectType, new List<String>{ 'Name' }, false);
System.assertEquals(false, retVal);
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_READ, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
try {
System.debug(Schema.Survey__c.SobjectType.getDescribe().isAccessible());
Boolean retVal = accessController.checkAuthorizedToView(Schema.Survey__c.SobjectType, new List<String>{ 'Name' }, true);
System.assertEquals(false, retVal);
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_READ, ex.getExceptionReason());
System.assertEquals('Survey__c', ex.getExceptionObject());
}
}
}
@isTest
private static void testAccessControl() {
Contact c = new Contact();
List<Schema.SObjectField> contactLastModifiedDate = new List<Schema.SobjectField>{ Schema.Contact.fields.LastModifiedDate };
Boolean setAuditFieldsEnabled = Schema.SObjectType.Profile.fields.getMap().containsKey('PermissionsCreateAuditFields');
// if permission is available, it might be possible to set LastModifiedDate on create
if (!setAuditFieldsEnabled) {
System.assertEquals(false, accessController.isAuthorizedToCreate(Schema.Contact.SobjectType, contactLastModifiedDate));
System.assertEquals(false, accessController.isAuthorizedToCreate(Schema.Contact.SobjectType, new List<String>{ 'LastModifiedDate' }));
try {
accessController.assertAuthorizedToCreate(Schema.Contact.SobjectType, contactLastModifiedDate);
System.assert(false, 'Should not succeed in saving as LastModified date is not creatable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_CREATE, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals('LastModifiedDate', ex.getExceptionField());
}
}
// can never modify lastmodified date
System.assertEquals(false, accessController.isAuthorizedToUpdate(Schema.Contact.SobjectType, contactLastModifiedDate));
System.assertEquals(false, accessController.isAuthorizedToUpdate(Schema.Contact.SobjectType, new List<String>{ 'LastModifiedDate' }));
try {
accessController.assertAuthorizedToUpdate(Schema.Contact.SobjectType, contactLastModifiedDate);
System.assert(false, 'Should not succeed in saving as Lastmodified date is not updateable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_UPDATE, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals('LastModifiedDate', ex.getExceptionField());
}
try {
accessController.assertAuthorizedToUpdate(Schema.Contact.SobjectType, new List<String>{ 'LastModifiedDate' });
System.assert(false, 'Should not succeed in saving as Lastmodified date is not updateable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_UPDATE, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals('LastModifiedDate', ex.getExceptionField());
}
try {
accessController.assertAuthorizedToDelete(Schema.Contact.SobjectType);
System.assert(true, 'Should succeed in deleting contact object');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_DELETE, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
}
try {
accessController.assertAuthorizedToDelete(Schema.ApexLog.SobjectType);
System.assert(false, 'Should not succeed as ApexLog is not deletable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_DELETE, ex.getExceptionReason());
System.assertEquals('ApexLog', ex.getExceptionObject());
}
try {
accessController.assertAuthorizedToCreate(Schema.Contact.SobjectType, new List<String>{ 'LastModifiedDate' });
System.assert(false, 'Should not succeed in saving as Lastmodified date is not creatable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_CREATE, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals('LastModifiedDate', ex.getExceptionField());
}
try {
accessController.assertAuthorizedToCreate(Schema.ApexLog.SobjectType, new List<Schema.SObjectField>{ Schema.ApexLog.fields.Status });
System.assert(false, 'Should not succeed in saving as ApexLog is not creatable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_CREATE, ex.getExceptionReason());
System.assertEquals('ApexLog', ex.getExceptionObject());
System.assertEquals(null, ex.getExceptionField());
}
try {
accessController.assertAuthorizedToUpdate(Schema.ApexLog.SobjectType, new List<Schema.SObjectField>{ Schema.ApexLog.fields.Status });
System.assert(false, 'Should not succeed in saving as ApexLog is not updateable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_UPDATE, ex.getExceptionReason());
System.assertEquals('ApexLog', ex.getExceptionObject());
System.assertEquals(null, ex.getExceptionField());
}
try {
accessController.assertAuthorizedToCreate(Schema.ApexLog.SobjectType, new List<String>{ 'Status' });
System.assert(false, 'Should not succeed in saving as ApexLog is not creatable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_CREATE, ex.getExceptionReason());
System.assertEquals('ApexLog', ex.getExceptionObject());
System.assertEquals(null, ex.getExceptionField());
}
try {
accessController.assertAuthorizedToUpdate(Schema.ApexLog.SobjectType, new List<String>{ 'Status' });
System.assert(false, 'Should not succeed in saving as ApexLog is not updateable');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.OBJECT_ACCESS_VIOLATION, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.NO_UPDATE, ex.getExceptionReason());
System.assertEquals('ApexLog', ex.getExceptionObject());
System.assertEquals(null, ex.getExceptionField());
}
System.assertEquals(false, accessController.isAuthorizedToCreate(Schema.ApexLog.SobjectType, new List<Schema.SObjectField>{ Schema.ApexLog.fields.Status }));
System.assertEquals(false, accessController.isAuthorizedToCreate(Schema.ApexLog.SobjectType, new List<String>{ 'Status' }));
System.assertEquals(false, accessController.isAuthorizedToUpdate(Schema.ApexLog.SobjectType, new List<Schema.SObjectField>{ Schema.ApexLog.fields.Status }));
System.assertEquals(false, accessController.isAuthorizedToUpdate(Schema.ApexLog.SobjectType, new List<String>{ 'Status' }));
System.assertEquals(Contact.SobjectType.getDescribe().isDeletable(), accessController.isAuthorizedToDelete(Schema.Contact.SobjectType));
List<Schema.SobjectField> createableFields = accessController.getCreatableFields(c);
List<Schema.SobjectField> updateableFields = accessController.getUpdateableFields(c);
List<Schema.SobjectField> viewableFields = accessController.getViewableFields(c);
System.assertEquals(createableFields, accessController.getCreatableFields(Contact.getSObjectType()));
System.assertEquals(updateableFields, accessController.getUpdateableFields(Contact.getSObjectType()));
System.assertEquals(viewableFields, accessController.getViewableFields(Contact.getSObjectType()));
// will throw exception if in bad state
accessController.assertAuthorizedToView(Schema.Contact.SobjectType, viewableFields);
accessController.assertAuthorizedToCreate(Schema.Contact.SobjectType, createableFields);
accessController.assertAuthorizedToUpdate(Schema.Contact.SobjectType, updateableFields);
System.assert(accessController.isAuthorizedToView(Schema.Contact.SobjectType, viewableFields), 'should be able to view');
System.assert(accessController.isAuthorizedToCreate(Schema.Contact.SobjectType, createableFields), 'should be able to view');
System.assert(accessController.isAuthorizedToUpdate(Schema.Contact.SobjectType, updateableFields), 'should be able to update');
System.assert(accessController.isAuthorizedToView(Schema.Contact.SobjectType, new List<String>{ 'LastName' }), 'should be able to view');
System.assert(accessController.isAuthorizedToCreate(Schema.Contact.SobjectType, new List<String>{ 'LastName' }), 'should be able to create');
System.assert(accessController.isAuthorizedToUpdate(Schema.Contact.SobjectType, new List<String>{ 'LastName' }), 'should be able to update');
String badFieldName = 'SomeCrazyBadFieldName';
try {
System.assert(accessController.isAuthorizedToView(Schema.Contact.SobjectType, new List<String>{ 'LastName', badFieldName }), 'should be able to view');
System.assert(false, 'Should not come here');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_NOT_FOUND, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.GENERIC, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals(badFieldName, ex.getExceptionField());
}
try {
System.assert(accessController.isAuthorizedToCreate(Schema.Contact.SobjectType, new List<String>{ 'LastName', badFieldName }), 'should be able to view');
System.assert(false, 'Should not come here');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_NOT_FOUND, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.GENERIC, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals(badFieldName, ex.getExceptionField());
}
try {
System.assert(accessController.isAuthorizedToUpdate(Schema.Contact.SobjectType, new List<String>{ 'LastName', badFieldName }), 'should be able to view');
System.assert(false, 'Should not come here');
} catch (SFDCAccessControlException ex) {
System.assert(!String.isBlank(ex.getText()));
System.assertEquals(SFDCAccessControlException.ExceptionType.FIELD_NOT_FOUND, ex.getExceptionType());
System.assertEquals(SFDCAccessControlException.ExceptionReason.GENERIC, ex.getExceptionReason());
System.assertEquals('Contact', ex.getExceptionObject());
System.assertEquals(badFieldName, ex.getExceptionField());
}
}
/**/
}