JasperFx/alba

Alba 5 - Stub JWT Security doesn't work with XUnit Theories

asmorger opened this issue · 3 comments

When attempting to use the new JWT stub security, it all works great for individual unit tests, but the behavior seems unstable for Theory scenarios. The first test of the theory succeeds, but subsequent runs will fail with authentication errors.

I'm working within an environment where I upgraded from Alba 4 to 5 and theories work great in other scenarios - they just don't seem to work with the stub JWT configuration.

I've been attempting to reproduce this in Alba.Testing and as yet have been unsuccessful, which indicates it might be something with my test setup. I'm not convinced, but I'm willing to be persuaded :)

Since I can't see what the actual request content created by Alba (might be reason to come up with a solution to #30, at least for a debug experience), I'm debugging a custom IAuthorizationRequirement to determine what claims are being passed along to my API. The first test of the theory contains the expected, configured claims in the ClaimsPrincipal, but the subsequent tests do not. It's very strange.

Setup:
image

Theory:
image

Claims Principal of the first request:
image

Claims Principal of the subsequent requests:
image

For what it's worth, I implemented a thin IAlbaExtension wrapping my previous, copy-pasta'd-off-the-Internet FakeJwtManager solution I was previously using and it works just fine doing a host.BeforeEach to set the authorization header. Maybe it's an interplay between XUnit Collection Fixtures and the JwtSecurityStub?

It seems super strange (and possibly telling) that I can implement a separate path and it works. 🤷‍♂️

@asmorger The JwtSecurityStub uses a before each hook in Alba. It should be happening every time you call a Scenario() method. I have no explanation for you, but I'm also pretty dubious this is an Alba issue. Sorry, but I'm going to close this.