-
Notifications
You must be signed in to change notification settings - Fork 28
Example: Security Role
A user with the security role Scheduler should not be able to create a lead.
The following test ensures that the specification is met. Note that the TypeDeclarations.cs file should be included in the solution and the namespace XrmMockup.TypeDeclarations should be referenced. The code shown in this example will use our early-bound context generator, XrmContext. We use this, since it has some great features compared to CrmSvcUtil. For a comparison, look at https://github.com/delegateas/XrmContext/wiki/functionality
[TestMethod]
public void TestCreateSecurity() {
using (var context = new Xrm(orgAdminUIService)) {
var orgUser = new SystemUser();
orgUser.Id = orgAdminUIService.Create(orgUser);
var businessunit = new BusinessUnit();
businessunit.Id = orgAdminUIService.Create(businessunit);
var user = crm.CreateUser(orgAdminUIService, businessunit.ToEntityReference(), SR.Scheduler);
var service = crm.CreateOrganizationService(user.Id);
try {
service.Create(new Lead());
Assert.Fail();
} catch (Exception e) {
Assert.IsInstanceOfType(e, typeof(FaultException));
}
}
}
When a new businessunit is created, it automatically gets security roles attached to it. Since security roles should only be added to the root businessunit, the roles are simply copied from there. All security roles from your CRM instance are available in the SR struct. These roles can be passed to the CreateUser or CreateTeam function, which returns a new record, where the security roles are set. An organization service can be created by providing the id of a systemuser. This service allows the developer to test whether the user can do certain actions within the context of the application, where all of the actions are automatically checked by XrmMockup for validity.
Getting started
General
Examples
Contribute