Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Allow removing claim value in test? #160

Closed
sommmen opened this issue Apr 10, 2024 · 1 comment
Closed

[feat] Allow removing claim value in test? #160

sommmen opened this issue Apr 10, 2024 · 1 comment
Milestone

Comments

@sommmen
Copy link

sommmen commented Apr 10, 2024

Hi,

I have role based authorization, my claims would look like this;

{
    "role": [
        "Employee",
        "Admin",
         ...
    ]
}

In a test i'd like to remove this role so i can test that asp.net correctly returns Forbidden, but i can't seem to get that to work becuase i can only remove a claim by key;

await Host.Scenario(c =>
{
    c.RemoveClaim("Role"); // Would remove all roles, not a specific role.
    c.Get.Url(url);
    c.StatusCodeShouldBe(HttpStatusCode.Forbidden);
});

Could Alba support configuring the claim values on a per test basis?
In my case i'd like to only remove the 'Employee' role.

For the sake of completeness, the Host is setup in a base class, using MsTest :

public abstract class IntegrationTestBase
{
    protected static IAlbaHost Host { get; set; } = null!;
    
    [ClassInitialize(InheritanceBehavior.BeforeEachDerivedClass)]
    public static async Task ClassInitialize(TestContext context)
    {
        
        var jwtSecurityStub = new JwtSecurityStub()
            .With(JwtRegisteredClaimNames.Email, "runner@webapi.tests");

        var claimsByRole = RoleManagerExtensions.GetRoleClaims();

        foreach (var (role, claims) in claimsByRole)
        {
            jwtSecurityStub = jwtSecurityStub.With("role", role);

            foreach (var claim in claims)
            {
                jwtSecurityStub = jwtSecurityStub.With(claim);
            }
        }

        Host = await AlbaHost.For<Program>(x =>
        {
            x.ConfigureServices((_, services) =>
            {
                for (var i = services.Count - 1; i >= 0; i--)
                {
                    var service = services[i];

                    Type[] implementationTypesToRemove =
                    [
                        // Hosted services that do stuff on startup
                        typeof(VersionHostedService),
                        typeof(DbContextWarmUpHostedService),
                        typeof(YarpConfiguratorHostedService),

                        // Remove auth so the stub can pick this up
                        typeof(ConfigureJwtBearerOptions)
                    ];

                    if (implementationTypesToRemove.Contains(service.ImplementationType))
                    {
                        services.RemoveAt(i);
                    }
                }

                services.ConfigureOptions<ConfigureJwtBearerOptionsMock>();
            });
        }, jwtSecurityStub);
    }


    [ClassCleanup]
    public void ClassCleanup()
    {
        Host.Dispose();
    }
}

I may be able to push out a PR, if you'd like me to do that, please provide me some steps on how you want this to work.

@Hawxy Hawxy modified the milestone: v8 May 15, 2024
@Hawxy
Copy link
Collaborator

Hawxy commented May 15, 2024

Sorry, I missed this issue when you first posted it. This is unsupportable, claims don't have a type once created, they're just strings. I'd recommend writing an extension that removes the claim and adds the change you want (claims can be removed & added as part of the same scenario).

@Hawxy Hawxy closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants