Skip to content

Commit 2f0ab2d

Browse files
authored
Merge pull request #238 from egil/feature/171-auto-cascading-auth
AddTestAuthorization adds CascadingAuthenticationState to render tree
2 parents 92779eb + 0effaba commit 2f0ab2d

File tree

13 files changed

+97
-116
lines changed

13 files changed

+97
-116
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ List of changes in existing functionality.
7373

7474
- Marked MarkupMatches methods as assertion methods to stop SonarSource analyzers complaining about missing assertions in tests. By [@egil](https://github.com/egil) in [#229](https://github.com/egil/bUnit/pull/229).
7575

76+
- `AddTestAuthorization` now extends `TestContext` instead of `TestServiceProvider`, and also automatically adds the `CascadingAuthenticationState` component to the root render tree. [@egil](https://github.com/egil) in [#237](https://github.com/egil/bUnit/pull/367).
77+
7678
### Deprecated
7779
List of soon-to-be removed features.
7880

docs/samples/components/UserInfo.razor

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@
99
{
1010
<h1>Please log in!</h1>
1111
}
12-
<CascadingAuthenticationState>
13-
<AuthorizeView>
14-
<Authorized>
15-
<p>State: Authorized</p>
16-
</Authorized>
17-
<Authorizing>
18-
<p>State: Authorizing</p>
19-
</Authorizing>
20-
<NotAuthorized>
21-
<p>State: Not authorized</p>
22-
</NotAuthorized>
23-
</AuthorizeView>
24-
</CascadingAuthenticationState>
25-
12+
<AuthorizeView>
13+
<Authorized>
14+
<p>State: Authorized</p>
15+
</Authorized>
16+
<Authorizing>
17+
<p>State: Authorizing</p>
18+
</Authorizing>
19+
<NotAuthorized>
20+
<p>State: Not authorized</p>
21+
</NotAuthorized>
22+
</AuthorizeView>
2623
@code
2724
{
2825
bool isAuthenticated = false;

docs/samples/components/UserRights.razor

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22
@using System.Security.Claims
33
@using System.Globalization
44

5-
<CascadingAuthenticationState>
5+
<AuthorizeView>
6+
<h1>Hi @context.User.Identity.Name, you have these claims and rights:</h1>
7+
</AuthorizeView>
8+
<ul>
69
<AuthorizeView>
7-
<h1>Hi @context.User.Identity.Name, you have these claims and rights:</h1>
10+
@foreach (var claim in @context.User.FindAll(x => x.Type != ClaimTypes.Name))
11+
{
12+
<li>@GetClaimName(claim): @claim.Value</li>
13+
}
814
</AuthorizeView>
9-
<ul>
10-
<AuthorizeView>
11-
@foreach (var claim in @context.User.FindAll(x => x.Type != ClaimTypes.Name))
12-
{
13-
<li>@GetClaimName(claim): @claim.Value</li>
14-
}
15-
</AuthorizeView>
16-
<AuthorizeView Roles="superuser">
17-
<li>You have the role SUPER USER</li>
18-
</AuthorizeView>
19-
<AuthorizeView Roles="admin">
20-
<li>You have the role ADMIN</li>
21-
</AuthorizeView>
22-
<AuthorizeView Policy="content-editor">
23-
<li>You are a CONTENT EDITOR</li>
24-
</AuthorizeView>
25-
</ul>
26-
</CascadingAuthenticationState>
15+
<AuthorizeView Roles="superuser">
16+
<li>You have the role SUPER USER</li>
17+
</AuthorizeView>
18+
<AuthorizeView Roles="admin">
19+
<li>You have the role ADMIN</li>
20+
</AuthorizeView>
21+
<AuthorizeView Policy="content-editor">
22+
<li>You are a CONTENT EDITOR</li>
23+
</AuthorizeView>
24+
</ul>
2725

2826
@code
2927
{

docs/samples/tests/xunit/InjectAuthServiceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public void Test001()
1111
{
1212
// arrange
1313
using var ctx = new TestContext();
14-
var authContext = ctx.Services.AddTestAuthorization();
14+
var authContext = ctx.AddTestAuthorization();
1515
authContext.SetAuthorized("TestUserName", AuthorizationState.Authorized);
1616

1717
// act
@@ -26,7 +26,7 @@ public void Test002()
2626
{
2727
// arrange
2828
using var ctx = new TestContext();
29-
var authContext = ctx.Services.AddTestAuthorization();
29+
var authContext = ctx.AddTestAuthorization();
3030

3131
// act
3232
var cut = ctx.RenderComponent<InjectAuthService>();

docs/samples/tests/xunit/UserInfoTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void Test001()
1010
{
1111
// Arrange
1212
using var ctx = new TestContext();
13-
ctx.Services.AddTestAuthorization();
13+
ctx.AddTestAuthorization();
1414

1515
// Act
1616
var cut = ctx.RenderComponent<UserInfo>();
@@ -25,7 +25,7 @@ public void Test004()
2525
{
2626
// Arrange
2727
using var ctx = new TestContext();
28-
var authContext = ctx.Services.AddTestAuthorization();
28+
var authContext = ctx.AddTestAuthorization();
2929
authContext.SetAuthorizing();
3030

3131
// Act
@@ -41,7 +41,7 @@ public void Test002()
4141
{
4242
// Arrange
4343
using var ctx = new TestContext();
44-
var authContext = ctx.Services.AddTestAuthorization();
44+
var authContext = ctx.AddTestAuthorization();
4545
authContext.SetAuthorized("TEST USER", AuthorizationState.Unauthorized);
4646

4747
// Act
@@ -57,7 +57,7 @@ public void Test003()
5757
{
5858
// Arrange
5959
using var ctx = new TestContext();
60-
var authContext = ctx.Services.AddTestAuthorization();
60+
var authContext = ctx.AddTestAuthorization();
6161
authContext.SetAuthorized("TEST USER");
6262

6363
// Act

docs/samples/tests/xunit/UserRightsTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void Test001()
1212
{
1313
// Arrange
1414
using var ctx = new TestContext();
15-
var authContext = ctx.Services.AddTestAuthorization();
15+
var authContext = ctx.AddTestAuthorization();
1616
authContext.SetAuthorized("TEST USER");
1717

1818
// Act
@@ -28,7 +28,7 @@ public void Test002()
2828
{
2929
// Arrange
3030
using var ctx = new TestContext();
31-
var authContext = ctx.Services.AddTestAuthorization();
31+
var authContext = ctx.AddTestAuthorization();
3232
authContext.SetAuthorized("TEST USER");
3333
authContext.SetRoles("superuser");
3434

@@ -47,7 +47,7 @@ public void Test003()
4747
{
4848
// Arrange
4949
using var ctx = new TestContext();
50-
var authContext = ctx.Services.AddTestAuthorization();
50+
var authContext = ctx.AddTestAuthorization();
5151
authContext.SetAuthorized("TEST USER");
5252
authContext.SetRoles("admin", "superuser");
5353

@@ -67,7 +67,7 @@ public void Test004()
6767
{
6868
// Arrange
6969
using var ctx = new TestContext();
70-
var authContext = ctx.Services.AddTestAuthorization();
70+
var authContext = ctx.AddTestAuthorization();
7171
authContext.SetAuthorized("TEST USER");
7272
authContext.SetPolicies("content-editor");
7373

@@ -86,7 +86,7 @@ public void Test0041()
8686
{
8787
// Arrange
8888
using var ctx = new TestContext();
89-
var authContext = ctx.Services.AddTestAuthorization();
89+
var authContext = ctx.AddTestAuthorization();
9090
authContext.SetAuthorized("TEST USER");
9191
authContext.SetPolicies("content-editor", "approver");
9292

@@ -105,7 +105,7 @@ public void Test006()
105105
{
106106
// Arrange
107107
using var ctx = new TestContext();
108-
var authContext = ctx.Services.AddTestAuthorization();
108+
var authContext = ctx.AddTestAuthorization();
109109
authContext.SetAuthorized("TEST USER");
110110
authContext.SetClaims(
111111
new Claim(ClaimTypes.Email, "[email protected]"),
@@ -128,7 +128,7 @@ public void Test005()
128128
{
129129
// Arrange
130130
using var ctx = new TestContext();
131-
var authContext = ctx.Services.AddTestAuthorization();
131+
var authContext = ctx.AddTestAuthorization();
132132
authContext.SetAuthorized("TEST USER");
133133
authContext.SetRoles("admin", "superuser");
134134
authContext.SetPolicies("content-editor");

docs/site/docs/test-doubles/faking-auth.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ The test implementation of Blazor's authentication and authorization can be put
1515
- **Authenticated** and **authorized**
1616
- **Authenticated** and **authorized** with one or more **roles**, **claims**, and/or **policies**
1717

18-
bUnit's authentication and authorization implementation is easily available by calling [`AddTestAuthorization()`](xref:Bunit.TestDoubles.FakeAuthorizationExtensions.AddTestAuthorization(Bunit.TestServiceProvider)) on a test context's `Services` collection. This returns an instance of the <xref:Bunit.TestDoubles.TestAuthorizationContext> type that allows you to control the authentication and authorization state for a test.
18+
bUnit's authentication and authorization implementation is easily available by calling [`AddTestAuthorization()`](xref:Bunit.TestDoubles.FakeAuthorizationExtensions.AddTestAuthorization(Bunit.TestContext)) on a test context. This adds the necessary services to the `Services` collection and the `CascadingAuthenticationState` component to the [root render tree](xref:root-render-tree). The method returns an instance of the <xref:Bunit.TestDoubles.TestAuthorizationContext> type that allows you to control the authentication and authorization state for a test.
19+
20+
> [!NOTE]
21+
> If your inherits directly from bUnit's <xref:Bunit.TestContext> as described in <xref:writing-csharp-tests#remove-boilerplate-code-from-tests>, then you need to call the [`AddTestAuthorization()`](xref:Bunit.TestDoubles.FakeAuthorizationExtensions.AddTestAuthorization(Bunit.TestContext)) method like so: `this.AddTestAuthorization()`.
1922
2023
The following sections will show how to set each of these states in a test.
2124

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.Components.Authorization;
2+
13
namespace Bunit.TestDoubles
24
{
35
/// <summary>
@@ -7,14 +9,17 @@ namespace Bunit.TestDoubles
79
public static class FakeAuthorizationExtensions
810
{
911
/// <summary>
10-
/// Adds the appropriate auth services to the <see cref="TestServiceProvider"/> to enable
11-
/// an authenticated user.
12+
/// Adds the appropriate Blazor authentication and authorization services to the <see cref="TestServiceProvider"/> to enable
13+
/// an authenticated user, as well as adding the <see cref="CascadingAuthenticationState"/> component to the
14+
/// <see cref="TestContext.RenderTree"/>.
1215
/// </summary>
13-
public static TestAuthorizationContext AddTestAuthorization(this TestServiceProvider serviceProvider)
16+
public static TestAuthorizationContext AddTestAuthorization(this TestContext context)
1417
{
18+
context.RenderTree.TryAdd<CascadingAuthenticationState>();
19+
1520
var authCtx = new TestAuthorizationContext();
1621
authCtx.SetNotAuthorized();
17-
authCtx.RegisterAuthorizationServices(serviceProvider);
22+
authCtx.RegisterAuthorizationServices(context.Services);
1823
return authCtx;
1924
}
2025
}

tests/bunit.testassets/SampleComponents/SimpleAuthViewWithClaims.razor

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
@using System.Security.Claims
33
@inject AuthenticationStateProvider AuthenticationStateProvider
44

5-
<CascadingAuthenticationState>
6-
<AuthorizeView>
7-
<div>Authorized!</div>
8-
<div>Name: @userName</div>
9-
@if (hasUserEmail)
10-
{
11-
<div>Email: @userEmail</div>
12-
}
13-
@if (hasUserId)
14-
{
15-
<div>Id: @userId</div>
16-
}
17-
</AuthorizeView>
18-
</CascadingAuthenticationState>
5+
<AuthorizeView>
6+
<div>Authorized!</div>
7+
<div>Name: @userName</div>
8+
@if (hasUserEmail)
9+
{
10+
<div>Email: @userEmail</div>
11+
}
12+
@if (hasUserId)
13+
{
14+
<div>Id: @userId</div>
15+
}
16+
</AuthorizeView>
1917

2018
@code {
2119
string userName = "";
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
@using Microsoft.AspNetCore.Components.Authorization
22

3-
<CascadingAuthenticationState>
4-
<AuthorizeView Policy="ContentViewer">
5-
Authorized for content viewers.
6-
</AuthorizeView>
7-
</CascadingAuthenticationState>
3+
<AuthorizeView Policy="ContentViewer">
4+
Authorized for content viewers.
5+
</AuthorizeView>

0 commit comments

Comments
 (0)