Open Closed

Testing with Fake Users and Permissions Issues #3019


User avatar
0
  • ABP Framework version: v5.2.1
  • UI type: MVC
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): yes

So, we're trying to setup testing for the devs so that when they create the db there are a ton of fake users covering a variety of our real world scenarios. Creating the users works fine but we're having a heck of a time setting up permissions so that when devs login to those accounts, they can use the system.

We'll create a tenant:

var tenant = await _tenantManager.CreateAsync(name: tenantName, editionId: editionId);
tenant = await _tenantRepository.InsertAsync(tenant, true);

Then we'll create the admin user:

IdentityUser user = new(_guidGenerator.Create(), userName ?? faker.Internet.UserName(), faker.Internet.Email(), tenantId: tenant.Id);
var createStatus = await _userManager.CreateAsync(user, CabMDConsts.AdminPasswordDefaultValue);
if (createStatus.Succeeded)
{
    var roleResult = await _userManager.AddToRoleAsync(user, "admin");
    if (roleResult.Succeeded) {
        // just in case we need to specify?
        var permissions = await _permissionManager.GetAllForRoleAsync(role);
        foreach (var p in permissions.Where(p => p.IsGranted))
        {
            await _permissionManager.SetForUserAsync(user.Id, p.Name, true);
        }
    } else {
        throw new InvalidOperationException($"Could not add user {user.UserName} to role {role}: {string.Join(", ", roleResult.Errors.Select(x => x.Description))}");
    }
}

But when we login as these users, there's no menu and they can't do anything within in the app. Looking at the menu contributor, I'm seeing this:

I can see in the AbpUserRoles table that there is an entry seemingly associating that "admin" role to the user:

Any ideas? How can we seed fake users who have default roles and the permissions associated with those roles for testing?


1 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Can you check if the role exists in the authenticated claims?

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11