I had been under the impression that omitting a sharing declaration was equivalent to using "without sharing", but that appears to not always be true.
For example, if I have legacy code like this...
// No sharing declaration here
global class MyWebservice {
webservice static List<Account> getAccount(String accountName) {
return new List<Account>([SELECT Id FROM Account WHERE Name = :accountName]);
}
webservice static void updateAccount(Account acc) {
update acc;
}
}
... if I try to test in the following way, I get failures:
private class MyWebserviceTest {
@TestSetup
static void makeData() {
insert new Account(Name = 'Test Account');
TestUtil.insertPartnerUser();
}
@isTest
static void testGetAccount() {
User partner = TestUtil.getPartnerUser();
System.runAs(partner) {
List<Account> result = MyWebservice.getAccount('Test Account');
// AssertException: expected 1, actual 0
// The partner user doesn't have visibility to the Account, even
// though we aren't explicitly using "with sharing"
System.assertEquals(1, result.size());
}
}
@isTest
static void testUpdateAccount() {
User partner = TestUtil.getPartnerUser();
User systemAdmin = [
SELECT Id FROM User
WHERE Profile.Name = 'System Administrator'
AND IsActive = TRUE
LIMIT 1
];
Account acc = MyWebservice.getAccount('Test Account')[0];
acc.OwnerId = systemAdmin.Id;
System.runAs(partner) {
// INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY
// i.e. the partner doesn't have visibility to the system admin user
// and isn't allowed to assign that user as the record owner
MyWebservice.updateAccount(acc);
}
}
}
Adding "without sharing" at the top of the MyWebservice
class resolves the failures. But I'm a little leery of making these types of changes because I don't have a comprehensive view of what additional access this grants.
What does adding "without sharing" to a class that previously had no sharing declaration do in terms of user-based restrictions, beyond the differences I've stumbled on above?