How to use dynamic roles with @TestSecurity

233 Views Asked by At

I would like to use dynamic roles in my @TestSecurity annotation.

My resource:

    @Path("/web-app")
    @Authenticated
    public class MyProtectedResource{

        @GET
        @Path("my-path") 
        @RolesAllowed({ "admin", "user" }) 
        public String getSomething() {
            // return something;
        }    
    }

So instead of doing this (which works fine):

@QuarkusTest
@TestHTTPEndpoint(MyProtectedResource.class)
public class TestMyProtectedResource{

    @Test
    @TestSecurity(user = "testUser", roles = { "admin" })
    public void myTest() {
        // Some test
    }

}

I would like to do this:

@QuarkusTest
@TestHTTPEndpoint(MyProtectedResource.class)
public class TestMyProtectedResource{

    private String[] allRoles = ["admin", "user"] 

    @Test
    @TestSecurity(user = "testUser", roles = allRoles )
    public void myTest() {
        // Some test
    }    
}

but the roles attribute needs to be a constant. Is there another way of doing this? Ideally I would like to run the same test multiple times with different sets of roles.

Otherwise I would have to duplicate quite a bit of code to test all interesting role combinations

0

There are 0 best solutions below