Base solution for your next web application
Open Closed

Test with SqlLite throws exception on PermissionChecker.AuthorizeAsync #10042


User avatar
0
BigCDogCrew created

AspNetZero v8.6 MVC/jQuery .Net Core Abp.TestBase v5.5

When I connect my unit tests to a SQLServer database, I am able to call application services with appropriate permissions. When I switch to using the in-memory SQLLite database, the [MyProject]AuthorizationHelper.PermissionChecker.AuthorizeAsync throws an exception.

As far as I can tell, there is either (A) an issue with the data being different betweeen the two databases or (B) an issue with the Abp code/handlers associated with SQLLite.

I have meticulously recreated the SQLServer database in the [MyProject].Test.Base.TestData.TestDataBuilder population of the SQLLite instance. So, I'm now leaning toward option B.

	public class TestDataBuilder : ITestDataBuilder
	{
		protected readonly PGSAspectDbContext _context;
		protected readonly int _tenantId;
		protected readonly RoleManager roleManager;
		protected readonly UserManager userManager;
		protected readonly TenantManager tenantManager;
		protected readonly ISessionTenantFilterList sessionTenantFilterList;

		public TestDataBuilder(PGSAspectDbContext context, int tenantId, TenantManager tenantManager, UserManager userManager, RoleManager roleManager, ISessionTenantFilterList sessionTenantFilterList)
		{
			_context = context;
			_tenantId = tenantId;
			this.roleManager = roleManager;
			this.userManager = userManager;
			this.tenantManager = tenantManager;
			this.sessionTenantFilterList = sessionTenantFilterList;
		}

		public virtual void Create()
		{
			_context.SaveChanges();

			new TestData_AbpEditions(_context).Create();
			new TestData_AbpUsersHost(userManager, roleManager).Create(); _context.SaveChanges(); 
			new TestData_AbpTenants(tenantManager, roleManager).Create(); _context.SaveChanges(); // Prerequisites: Edition
			new TestData_AbpSettings(_context).Create();                                          // Prerequisites: Tenant
			new TestData_AbpFeatures(_context).Create();                                          // Prerequisites: Edition, Tenant
			new TestData_AbpRoles(_context).Create();                                             // Prerequisites: Tenant
			new TestData_AbpUsers(userManager, roleManager).Create();    _context.SaveChanges();  // Prerequisites: Tenant, Roles
			new TestData_AbpUserAccounts(_context).Create();                                      // Prerequisites: Tenant, Users
			new TestData_AbpPermissions(_context).Create();                                       // Prerequisites: Tenant, Roles, Users
			
			new TestData_Clients(_context).Create();
			new TestData_AbpOrganizationUnits(_context, sessionTenantFilterList).Create();

			_context.SaveChanges();
		}
	}

NOTE: We are using MultiTenancy and we are highly integrated into the OrganizationalUnits. The ISessionTenantFilterList is our set of custom filters that allow us to expose data up and down the OrganizationalUnit hierarchy based upon Edition, Organziational Level, and TEntity. (I am certain this issue is unrelated to our custom filters, though I have not yet ruled out the possibility that it is related to MultiTenancy.)

Here is the test that triggers the error....

		[MultiTenantFact]
		public async Task Test_Create()
		{
			//Arrange
			LoginAsTenant("xxx", "admin");
			var mockInput = new FakeCreateOrEditStoreDto();

			//Act
			await _storesAppService.CreateOrEdit(mockInput);

			//Assert
			UsingDbContext(context =>
			{
				var store = context.Stores.FirstOrDefault(e => e.UID == mockInput.UID);
				store.ShouldNotBeNull();
				store.Name.ShouldBe(mockInput.Name);
			});
		}

The error reiceved looks like this...

I noticed that trapping the breakpoint BEFORE execution of PermissionChecker, there are no ActiveDbContexts (see below).

AFTER execution/exception, an ActiveDbContext reference has been set, but there are no permissions available (see below).

If I click on the Results View, the context is refreshed and the permissions become available (see below).

When I change to using LoginAsHost("admin"), I get a different but seemingly related error (see below).

I am probably doing something stupid, but any insights that you can offer are greatly appreciated.


3 Answer(s)
  • User Avatar
    0
    BigCDogCrew created

    Here is some additional information that may help.

    When I run the GetTenants_Test, the LoginAsHost works fine but PermissionChecker throws an exception...even though the AbpSession shows that I am logged in as the Host Admin. See images below.

  • User Avatar
    0
    BigCDogCrew created

    I figured, just maybe the AuthorizationProvider is not being loaded. So, I added this line to thePreInitialize() of [MyProject]TestBaseModule. <br>

    Configuration.Authorization.Providers.Add<AppAuthorizationProvider>();
    

    This gave me an error: <br>

    Message: 
        Abp.AbpInitializationException : Duplicate permission name detected for Pages.Accounts
      Stack Trace: 
        PermissionDictionary.AddPermissionRecursively(Permission permission)
        PermissionDictionary.AddPermissionRecursively(Permission permission)
        PermissionDictionary.AddAllPermissions()
        PermissionManager.Initialize()
        AbpKernelModule.PostInitialize()
        <>c.<StartModules>b__15_2(AbpModuleInfo module)
        List`1.ForEach(Action`1 action)
        AbpModuleManager.StartModules()
        AbpBootstrapper.Initialize()
        AbpIntegratedTestBase`1.InitializeAbp()
        AbpIntegratedTestBase`1.ctor(Boolean initializeAbp, IIocManager localIocManager)
        AppTestBase`1.ctor() line 40
        AppTestBase.ctor()
        TenantAppService_Tests.ctor() line 30
    
    

    So, we know for certain the AuthorizationProvider is being loaded.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Normally, TenantRoleAndUserBuilder.cs class (aspnet-core/src/MyCompanyName.AbpZeroTemplate.EntityFrameworkCore/Migrations/Seed/Tenants/TenantRoleAndUserBuilder.cs), grants permissions for the Default tenant admin.

    Could you debug your test and see if this class is executed ? It must be executed before every test execution. You can also grant permissions in your test for specific users so you can easil call app services.