I'm missing something obvious but we have a PostInstall script that makes a PermissionSetAssignment to the installer to give an Admin permission. The test class is failing since, as is documented in Salesforce InstallHandler Docs, the special install user is not accessible in tests.
The error is on the insert psalist
;
So how do I test that the InstallHandler successfully assigned the Permission Set? Thought of a Login Flow but given that this runs everytime a user logs in its overkill for a one-time process and is the purpose of having a Post Install script.
global void onInstall(InstallContext context) {
Map<String, Object> inputs = new Map<String, Object>();
if (context.previousVersion() == null) {
// ID of user installing package. Part of InstallHandler Interface
Id UserId = context.installerId();
// Getting System Administrator PermissionSet
List<PermissionSet> PermissionSetToAssignList = [
SELECT Id, Name
FROM PermissionSet
WHERE Name = 'provNpiAdministrator' AND NamespacePrefix = 'prov'
];
List<PermissionSetAssignment> psalist = new List<PermissionSetAssignment>();
PermissionSet PermissionSetToAssign = PermissionSetToAssignList[0];
PermissionSetAssignment psa = new PermissionSetAssignment();
psa.PermissionSetId = PermissionSetToAssign.Id;
psa.AssigneeId = UserId;
psalist.add(psa);
insert psalist;
} else if (context.isUpgrade()) {
}
if (context.isPush()) {
}
}
}
@isTest
class PostInstallClassNpiTest {
@isTest
static void onInstallTest() {
PostInstallClassNpi postinstall = new PostInstallClassNpi();
Test.testInstall(postinstall, null);
List<PermissionSetAssignment> psa = [
SELECT id, PermissionSetId
FROM PermissionSetAssignment
WHERE PermissionSet.Name = 'provNpiAdministrator'
];
System.assertEquals(psa.size(), 1, 'PermissionSetAssignment found');
// Test.testInstall(postinstall, new Version(1, 0), true);
}
}