Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

unit test in custom module #1529


User avatar
0
andry3ag created

Hi!

I try to run create unit test for a custom service that inherited from IApplicationService class with permission restrictions.

Here is the steps that I took:

public static class CalendarPermission {
        public const string ListEntry = "Administration.CalendarManagement.ListEntry";
    }

    public class CalendarAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var administration = context.CreatePermission("Administration");

            var calendarManagement = administration.CreateChildPermission("Administration.CalendarManagement");
            calendarManagement.CreateChildPermission(CalendarPermission.CreateEntry, null, true, null, MultiTenancySides.Tenant);
        }
    }
   public class CalenderModule : AbpModule
    {
        public override void PreInitialize() {
            Configuration.Authorization.Providers.Add<CalendarAuthorizationProvider>();
        }
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }


 public interface ICalendarService : IApplicationService
 {
        [HttpGet]
        Task<ListResultOutput<CalendarItemDto>> GetListForCurrentUser(DateTime minDate, DateTime maxDate);
 }
 
   public class CalendarService : ICalendarService {
        private readonly IRepository<CalenderItem> calenderRepository;

        [AbpAuthorize(CalendarPermission.ListEntry)]
        public async Task<ListResultOutput<CalendarItemDto>> GetListForCurrentUser(DateTime minDate, DateTime maxDate) { ...}
}

in the unit test, I do the following:

public class CalendarItem_Tests : AppTestBase {
     private ICalendarService calendarService;

     protected override void AddModules(ITypeList<AbpModule> modules) {
            base.AddModules(modules);
            modules.Add<CalenderModule.CalenderModule>();
        }
       public CalendarItem_Tests() {
            calendarService = Resolve<ICalendarService>();
        }
        [Fact]
        public async void Test_GetCalendarItems()
        {
            LoginAsHostAdmin();
           await calendarService.GetListForCurrentUser(DateTime.MinValue, DateTime.MaxValue);
        }
}

Below are the error messages:

Abp.Authorization.AbpAuthorizationException Required permissions are not granted. At least one of these permissions must be granted: Administration.CalendarManagement.ListEntry at Abp.Authorization.PermissionCheckerExtensions.<AuthorizeAsync>d__12.MoveNext() in D:\Halil\GitHub\aspnetboilerplate\src\Abp\Authorization\PermissionCheckerExtensions.cs:line 258 --- End of stack trace from previous location where exception was thrown ---

It seems that the PermissionFinder.GetAllPermissions in the TenantRoleAndUserBuilder class is not getting the permission Administration.CalendarManagement.ListEntry.

Much appreciated for any help!

derek


1 Answer(s)
  • User Avatar
    0
    andry3ag created

    I got it.

    1. you have to initialize the PermissionFinder properly as the following: PermissionFinder.GetAllPermissions(new AppAuthorizationProvider(false), new CalendarAuthorizationProvider()) since it won't pick up from the module PreInitialize method.
    2. login as tenant admin instead of hostadmin LoginAsDefaultTenantAdmin()

    Regards,

    derek