Skip to content

Commit 8235d93

Browse files
committed
Tweaks to AddAuthorizationContext to allow it to be used in razor tests
1 parent f1c95e2 commit 8235d93

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/bunit.web/TestDoubles/Authorization/FakeAuthorizationExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ public static class FakeAuthorizationExtensions
1313
/// an authenticated user, as well as adding the <see cref="CascadingAuthenticationState"/> component to the
1414
/// test contexts render tree.
1515
/// </summary>
16-
public static TestAuthorizationContext AddTestAuthorization(this TestContext context)
16+
public static TestAuthorizationContext AddTestAuthorization(this TestContextBase context)
1717
{
1818
context.RenderTree.TryAdd<CascadingAuthenticationState>();
19-
2019
var authCtx = new TestAuthorizationContext();
2120
authCtx.SetNotAuthorized();
2221
authCtx.RegisterAuthorizationServices(context.Services);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
@inherits TestComponentBase
2+
3+
<Fixture Test="CanAddFakeAuthToContext" Description="Fake Auth can be added to Fixture">
4+
<ComponentUnderTest>
5+
<SimpleAuthView />
6+
</ComponentUnderTest>
7+
8+
@code
9+
{
10+
void CanAddFakeAuthToContext(Fixture fixture)
11+
{
12+
var authContext = fixture.AddTestAuthorization();
13+
authContext.SetAuthorized("TestUser", AuthorizationState.Authorized);
14+
15+
var cut = fixture.GetComponentUnderTest<SimpleAuthView>();
16+
17+
cut.MarkupMatches("Authorized!");
18+
}
19+
}
20+
</Fixture>
21+
22+
<SnapshotTest Setup="SetupFakeAuthToContext" Description="Fake Auth can be added to SnapshotTest">
23+
<TestInput><SimpleAuthView /></TestInput>
24+
<ExpectedOutput>Authorized!</ExpectedOutput>
25+
26+
@code
27+
{
28+
void SetupFakeAuthToContext(SnapshotTest ctx)
29+
{
30+
var authContext = ctx.AddTestAuthorization();
31+
authContext.SetAuthorized("TestUser", AuthorizationState.Authorized);
32+
}
33+
}
34+
</SnapshotTest>

0 commit comments

Comments
 (0)