Base solution for your next web application
Open Closed

MultiTenancy in Tests #1938


User avatar
0
marcosli created

Hi,

In the latest version of AspNetZero, version 1.13.0.0, if i set the MultiTenancy to false, my application permissions are not created in the Tests project when the seed is executed.

public abstract class AppTestBase : AbpIntegratedTestBase<MyModule>
{
        protected AppTestBase()
        {
                // Here when MultiTenancy is false, TenantId can't be assigned to null, consequently this is will cause a problem in the seed methods. I'm going to explain in another piece of code
                AbpSession.TenantId = null;
        }
}

The real problems in Seed methods: Take a look in the coments to understand what is happening 1)

public class HostRoleAndUserCreator
{
        private void CreateHostRoleAndUsers()
        {
            //Admin role for host

            var adminRoleForHost = _context.Roles.FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);
            if (adminRoleForHost == null)
            {
                adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin) { IsStatic = true, IsDefault = true });
                _context.SaveChanges();
                // After saving the changes the adminRoleForHost.TenantId will be set to 1 even we have passed null to the constructor parameter because AbpSession.TenantId is 1 and not null
            }
        }
}
public class TenantRoleAndUserBuilder
{
        private void CreateRolesAndUsers()
        {
            //Admin role

            // Here this query will find the admin role to TenantId == 1, so the seed won´t generated my permissions needed to run my tests (i mean the tests not created by aspnetzero template by default)
            var adminRole = _context.Roles.FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin);
            if (adminRole == null)
            {
                // Here is generated the permissions
                ...
            }
        }
}

The workaround that I've created is to set MultiTenancy to true like this:

protected AppTestBase()
        {
            // ....
            var multiTenancy = Resolve<IMultiTenancyConfig>();
            if (!multiTenancy.IsEnabled)
            {
                multiTenancy.IsEnabled = true;
            }
            // ....
        }

Maybe that issue is a bug in TestAbpSession?

Thanks.


2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Thank you for reporting. We'll test it to understand better.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    For now your solution seems good. Because some of our unit tests are written as if multiTenancy is enabled.

    I have created an issue about this problem <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/issues/392">https://github.com/aspnetzero/aspnet-zero/issues/392</a>. We will try to fix it.

    Thanks again for informing us about it.