Skip to content

Commit cc9f2cb

Browse files
committed
refactor: Remove Extensions methods for TestContext and moved them to Testcontext directly
1 parent 8e256ff commit cc9f2cb

File tree

12 files changed

+84
-94
lines changed

12 files changed

+84
-94
lines changed

MIGRATION.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This document describes the changes that need to be made to migrate from bUnit 1
44
## Removal of `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods
55
The `GetChangesSinceFirstRender` and `GetChangesSinceLastRender` methods have been removed from `RenderedComponent<TComponent>`. There is no one-to-one replacement for these methods, but the general idea is to select the HTML in question via `Find` and assert against that.
66

7-
Alternatively, the `IRenderFragment` still offers the `OnMarkupUpdated` event, which can be used to assert against the markup after a render.
7+
Alternatively, the `IRenderedFragment` still offers the `OnMarkupUpdated` event, which can be used to assert against the markup after a render.
88

9-
## Removal of `IsNullOrEmpty` extension method on `IEnumerable<T>` and `CreateLogger` on `IserviceProvider`
9+
## Removal of `IsNullOrEmpty` extension method on `IEnumerable<T>` and `CreateLogger` on `IServiceProvider`
1010
The `IsNullOrEmpty` extension method on `IEnumerable<T>` has been removed, as well as the `CreateLogger` extension method on `IServiceProvider`. These extension methods are pretty common and conflict with other libraries. These methods can be recreated like this:
1111

1212
```csharp
@@ -29,10 +29,11 @@ The `bunit.core` and `bunit.web` packages have been merged into a single `bunit`
2929
## Removal of unneeded abstraction
3030

3131
### `IRenderedComponentBase<TComponent>` and `RenderedFragmentBase`
32-
`IRenderedComponentBase<TComponent>` and `RenderedFragmentBase` have been removed. They were used to provide a common base class for `RenderedComponent<TComponent>` and `RenderedFragment`, but this is no longer needed (due to the merge of the project). If you used either of these interfaces, you should replace them with `RenderedComponent<TComponent>` and `RenderedFragment` respectively.
32+
`IRenderedComponentBase<TComponent>`, `IRenderedComponent<TComponent>`, `IRenderedFragmentBase`, `IRenderedFragment` and `RenderedFragmentBase` have been removed.
33+
If you used either of these types, you should replace them with `RenderedComponent<TComponent>` or `RenderedFragment` respectively.
3334

34-
### `WebTestRender` merged into `TestRender`
35-
The `WebTestRender` class has been merged into the `TestRender` class. If you used `WebTestRender`, you should replace it with `TestRender`.
35+
### `WebTestRender` merged into `BunitTestRender`
36+
The `WebTestRender` class has been merged into the `TestRender` class. If you used `WebTestRender`, you should replace it with `BunitTestRender`.
3637

3738
## Renamed `Fake` to `Bunit` in many test doubles
3839
The `Fake` prefix has been replaced with `Bunit` in many test doubles. For example, `FakeNavigationManager` is now `BunitNavigationManager`. If you reference any of these types explicitly, you need to update your code.

docs/samples/tests/xunit/InjectAuthServiceTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class InjectAuthServiceTest : TestContext
1010
public void Test001()
1111
{
1212
// arrange
13-
var authContext = this.AddAuthorization();
13+
var authContext = AddAuthorization();
1414
authContext.SetAuthorized("TestUserName", AuthorizationState.Authorized);
1515

1616
// act
@@ -24,7 +24,7 @@ public void Test001()
2424
public void Test002()
2525
{
2626
// arrange
27-
var authContext = this.AddAuthorization();
27+
var authContext = AddAuthorization();
2828

2929
// act
3030
var cut = Render<InjectAuthService>();

docs/samples/tests/xunit/UserInfoTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class UserInfoTest : TestContext
99
public void Test001()
1010
{
1111
// Arrange
12-
this.AddAuthorization();
12+
AddAuthorization();
1313

1414
// Act
1515
var cut = Render<UserInfo>();
@@ -23,7 +23,7 @@ public void Test001()
2323
public void Test004()
2424
{
2525
// Arrange
26-
var authContext = this.AddAuthorization();
26+
var authContext = AddAuthorization();
2727
authContext.SetAuthorizing();
2828

2929
// Act
@@ -38,7 +38,7 @@ public void Test004()
3838
public void Test002()
3939
{
4040
// Arrange
41-
var authContext = this.AddAuthorization();
41+
var authContext = AddAuthorization();
4242
authContext.SetAuthorized("TEST USER", AuthorizationState.Unauthorized);
4343

4444
// Act
@@ -53,7 +53,7 @@ public void Test002()
5353
public void Test003()
5454
{
5555
// Arrange
56-
var authContext = this.AddAuthorization();
56+
var authContext = AddAuthorization();
5757
authContext.SetAuthorized("TEST USER");
5858

5959
// Act

docs/samples/tests/xunit/UserRightsTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class UserRightsTest : TestContext
1111
public void Test001()
1212
{
1313
// Arrange
14-
var authContext = this.AddAuthorization();
14+
var authContext = AddAuthorization();
1515
authContext.SetAuthorized("TEST USER");
1616

1717
// Act
@@ -26,7 +26,7 @@ public void Test001()
2626
public void Test002()
2727
{
2828
// Arrange
29-
var authContext = this.AddAuthorization();
29+
var authContext = AddAuthorization();
3030
authContext.SetAuthorized("TEST USER");
3131
authContext.SetRoles("superuser");
3232

@@ -44,7 +44,7 @@ public void Test002()
4444
public void Test003()
4545
{
4646
// Arrange
47-
var authContext = this.AddAuthorization();
47+
var authContext = AddAuthorization();
4848
authContext.SetAuthorized("TEST USER");
4949
authContext.SetRoles("admin", "superuser");
5050

@@ -63,7 +63,7 @@ public void Test003()
6363
public void Test004()
6464
{
6565
// Arrange
66-
var authContext = this.AddAuthorization();
66+
var authContext = AddAuthorization();
6767
authContext.SetAuthorized("TEST USER");
6868
authContext.SetPolicies("content-editor");
6969

@@ -81,7 +81,7 @@ public void Test004()
8181
public void Test0041()
8282
{
8383
// Arrange
84-
var authContext = this.AddAuthorization();
84+
var authContext = AddAuthorization();
8585
authContext.SetAuthorized("TEST USER");
8686
authContext.SetPolicies("content-editor", "approver");
8787

@@ -99,7 +99,7 @@ public void Test0041()
9999
public void Test006()
100100
{
101101
// Arrange
102-
var authContext = this.AddAuthorization();
102+
var authContext = AddAuthorization();
103103
authContext.SetAuthorized("TEST USER");
104104
authContext.SetClaims(
105105
new Claim(ClaimTypes.Email, "[email protected]"),
@@ -121,7 +121,7 @@ public void Test006()
121121
public void Test005()
122122
{
123123
// Arrange
124-
var authContext = this.AddAuthorization();
124+
var authContext = AddAuthorization();
125125
authContext.SetAuthorized("TEST USER");
126126
authContext.SetRoles("admin", "superuser");
127127
authContext.SetPolicies("content-editor");
@@ -144,7 +144,7 @@ public void Test005()
144144
public void Test007()
145145
{
146146
// Arrange
147-
var authContext = this.AddAuthorization();
147+
var authContext = AddAuthorization();
148148
authContext.SetAuthorized("TEST USER");
149149
authContext.SetAuthenticationType("custom-auth-type");
150150

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +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 [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(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.
18+
bUnit's authentication and authorization implementation is easily available by calling [`AddAuthorization()`](xref:Bunit.TestContext.AddAuthorization(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.BunitAuthorizationContext> type that allows you to control the authentication and authorization state for a test.
1919

2020
> [!NOTE]
21-
> If your test class inherits directly from bUnit's <xref:Bunit.TestContext> then you need to call the [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(Bunit.TestContext)) method on `this`, since `AddAuthorization()` is an extension method, otherwise it wont be available. E.g.: `this.AddAuthorization()`.
21+
> If your test class inherits directly from bUnit's <xref:Bunit.TestContext> then you need to call the [`AddAuthorization()`](xref:Bunit.TestContext.AddAuthorization(Bunit.TestContext)) method on `this`, since `AddAuthorization()` is an extension method, otherwise it wont be available. E.g.: `AddAuthorization()`.
2222
2323
The following sections show how to set each of these states in a test.
2424

@@ -46,23 +46,23 @@ To set the state to authenticating and authorizing, do the following:
4646

4747
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=25&end=34&highlight=3)]
4848

49-
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticating and authorizing state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorizing> method.
49+
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.BunitAuthorizationContext> is used to set the authenticating and authorizing state through the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetAuthorizing> method.
5050

5151
### Authenticated and unauthorized state
5252

5353
To set the state to authenticated and unauthorized, do the following:
5454

5555
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=40&end=49&highlight=3)]
5656

57-
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticated and unauthorized state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
57+
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.BunitAuthorizationContext> is used to set the authenticated and unauthorized state through the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
5858

5959
### Authenticated and authorized state
6060

6161
To set the state to authenticated and authorized, do the following:
6262

6363
[!code-csharp[UserInfoTest.cs](../../../samples/tests/xunit/UserInfoTest.cs?start=55&end=64&highlight=3)]
6464

65-
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.TestAuthorizationContext> is used to set the authenticated and authorized state through the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
65+
After calling `AddAuthorization()`, the returned <xref:Bunit.TestDoubles.BunitAuthorizationContext> is used to set the authenticated and authorized state through the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetAuthorized(System.String,Bunit.TestDoubles.AuthorizationState)> method.
6666

6767
Note that the second parameter, `AuthorizationState`, is optional, and defaults to `AuthorizationState.Authorized` if not specified.
6868

@@ -80,7 +80,7 @@ To specify one or more roles for the authenticated and authorized user, do the f
8080

8181
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=28&end=40&highlight=4)]
8282

83-
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetRoles(System.String[])> method is used to specify a single role. To specify multiple roles, do the following:
83+
The highlighted line shows how the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetRoles(System.String[])> method is used to specify a single role. To specify multiple roles, do the following:
8484

8585
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=46&end=59&highlight=4)]
8686

@@ -90,7 +90,7 @@ To specify one or more policies for the authenticated and authorized user, do th
9090

9191
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=65&end=78&highlight=5)]
9292

93-
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetPolicies(System.String[])> method is used to specify one policy. To specify multiple policies, do the following:
93+
The highlighted line shows how the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetPolicies(System.String[])> method is used to specify one policy. To specify multiple policies, do the following:
9494

9595
[!code-csharp[](../../../samples/tests/xunit/UserRightsTest.cs?start=91&end=91)]
9696

@@ -100,7 +100,7 @@ To specify one or more claims for the authenticated and authorized user, do the
100100

101101
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=101&end=117&highlight=4-7)]
102102

103-
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetClaims(System.Security.Claims.Claim[])> method is used to pass two instances of the `Claim` type.
103+
The highlighted line shows how the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetClaims(System.Security.Claims.Claim[])> method is used to pass two instances of the `Claim` type.
104104

105105
### Example of passing both roles, claims, and policies
106106

@@ -116,4 +116,4 @@ To specify a authentication type for the authenticated and authorized user, do t
116116

117117
[!code-csharp[UserRightsTest.cs](../../../samples/tests/xunit/UserRightsTest.cs?start=146&end=158&highlight=4)]
118118

119-
The highlighted line shows how the <xref:Bunit.TestDoubles.TestAuthorizationContext.SetAuthenticationType(System.String)> method is used to change the `Identity.AuthenticationType` of the user.
119+
The highlighted line shows how the <xref:Bunit.TestDoubles.BunitAuthorizationContext.SetAuthenticationType(System.String)> method is used to change the `Identity.AuthenticationType` of the user.

src/bunit/TestContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Bunit;
77
/// <summary>
88
/// A test context is a factory that makes it possible to create components under tests.
99
/// </summary>
10-
public class TestContext : IDisposable
10+
public partial class TestContext : IDisposable
1111
{
1212
private bool disposed;
1313
private BunitRenderer? bunitRenderer;

src/bunit/TestDoubles/Authorization/TestAuthorizationContext.cs renamed to src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Bunit.TestDoubles;
88
/// Root authorization service that manages different authentication/authorization state
99
/// in the system.
1010
/// </summary>
11-
public class TestAuthorizationContext
11+
public class BunitAuthorizationContext
1212
{
1313
private readonly BunitAuthorizationService authService = new();
1414
private readonly BunitAuthorizationPolicyProvider policyProvider = new();
@@ -51,22 +51,23 @@ public class TestAuthorizationContext
5151
public string PolicySchemeName { get; set; } = "TestScheme";
5252

5353
/// <summary>
54-
/// Registers authorization services with the specified service provider.
54+
/// Creates a new instance of <see cref="BunitAuthorizationContext"/>.
5555
/// </summary>
56-
/// <param name="services">Service provider to use.</param>
57-
public void RegisterAuthorizationServices(IServiceCollection services)
56+
internal BunitAuthorizationContext(TestServiceProvider services)
5857
{
5958
services.AddSingleton<IAuthorizationService>(authService);
6059
services.AddSingleton<IAuthorizationPolicyProvider>(policyProvider);
6160
services.AddSingleton<AuthenticationStateProvider>(authProvider);
61+
62+
authService.SetAuthorizationState(AuthorizationState.Unauthorized);
6263
}
6364

6465
/// <summary>
6566
/// Authenticates the user with specified name and authorization state.
6667
/// </summary>
6768
/// <param name="userName">User name for the principal identity.</param>
6869
/// <param name="state">Authorization state.</param>
69-
public TestAuthorizationContext SetAuthorized(string userName, AuthorizationState state = AuthorizationState.Authorized)
70+
public BunitAuthorizationContext SetAuthorized(string userName, AuthorizationState state = AuthorizationState.Authorized)
7071
{
7172
IsAuthenticated = true;
7273
UserName = userName;
@@ -83,7 +84,7 @@ public TestAuthorizationContext SetAuthorized(string userName, AuthorizationStat
8384
/// <summary>
8485
/// Puts the authorization services into the authorizing state.
8586
/// </summary>
86-
public TestAuthorizationContext SetAuthorizing()
87+
public BunitAuthorizationContext SetAuthorizing()
8788
{
8889
IsAuthenticated = false;
8990
Roles = Array.Empty<string>();
@@ -99,7 +100,7 @@ public TestAuthorizationContext SetAuthorizing()
99100
/// <summary>
100101
/// Puts the authorization services into an unauthenticated and unauthorized state.
101102
/// </summary>
102-
public TestAuthorizationContext SetNotAuthorized()
103+
public BunitAuthorizationContext SetNotAuthorized()
103104
{
104105
IsAuthenticated = false;
105106
Roles = Array.Empty<string>();
@@ -116,7 +117,7 @@ public TestAuthorizationContext SetNotAuthorized()
116117
/// Sets the user roles in this context..
117118
/// </summary>
118119
/// <param name="roles">Roles for the claims principal.</param>
119-
public TestAuthorizationContext SetRoles(params string[] roles)
120+
public BunitAuthorizationContext SetRoles(params string[] roles)
120121
{
121122
Roles = roles;
122123
authService.SetRoles(Roles);
@@ -129,7 +130,7 @@ public TestAuthorizationContext SetRoles(params string[] roles)
129130
/// Sets the authorization policies supported for the current user.
130131
/// </summary>
131132
/// <param name="policies">Supported authorization policies.</param>
132-
public TestAuthorizationContext SetPolicies(params string[] policies)
133+
public BunitAuthorizationContext SetPolicies(params string[] policies)
133134
{
134135
Policies = policies;
135136
policyProvider.SetPolicyScheme(PolicySchemeName);
@@ -142,7 +143,7 @@ public TestAuthorizationContext SetPolicies(params string[] policies)
142143
/// Sets the claims on the current user/principal.
143144
/// </summary>
144145
/// <param name="claims">Claims to set.</param>
145-
public TestAuthorizationContext SetClaims(params Claim[] claims)
146+
public BunitAuthorizationContext SetClaims(params Claim[] claims)
146147
{
147148
Claims = claims;
148149
authProvider.TriggerAuthenticationStateChanged(UserName, Roles, Claims);
@@ -154,7 +155,7 @@ public TestAuthorizationContext SetClaims(params Claim[] claims)
154155
/// Sets the Identity.AuthenticationType for the current user/principa;.
155156
/// </summary>
156157
/// <param name="authenticationType">The authentication type to set.</param>
157-
public TestAuthorizationContext SetAuthenticationType(string authenticationType)
158+
public BunitAuthorizationContext SetAuthenticationType(string authenticationType)
158159
{
159160
this.authProvider.TriggerAuthenticationStateChanged(this.UserName, this.Roles, this.Claims, authenticationType);
160161
return this;
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1+
using Bunit.TestDoubles;
12
using Microsoft.AspNetCore.Components.Authorization;
23
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
34

4-
namespace Bunit.TestDoubles;
5+
namespace Bunit;
56

6-
/// <summary>
7-
/// Helper methods for registering the Authentication/Authorization services with
8-
/// a <see cref="TestServiceProvider"/>.
9-
/// </summary>
10-
public static class BunitAuthorizationExtensions
7+
public partial class TestContext
118
{
129
/// <summary>
1310
/// Adds the appropriate Blazor authentication and authorization services to the <see cref="TestServiceProvider"/> to enable
1411
/// an authenticated user, as well as adding the <see cref="CascadingAuthenticationState"/> component to the
1512
/// test contexts render tree.
1613
/// </summary>
17-
public static TestAuthorizationContext AddAuthorization(this TestContext context)
14+
public BunitAuthorizationContext AddAuthorization()
1815
{
19-
ArgumentNullException.ThrowIfNull(context);
20-
21-
context.RenderTree.TryAdd<CascadingAuthenticationState>();
22-
context.Services.AddSingleton<BunitSignOutSessionStateManager>();
16+
RenderTree.TryAdd<CascadingAuthenticationState>();
17+
Services.AddSingleton<BunitSignOutSessionStateManager>();
2318
#pragma warning disable CS0618
24-
context.Services.AddSingleton<SignOutSessionStateManager>(s => s.GetRequiredService<BunitSignOutSessionStateManager>());
19+
Services.AddSingleton<SignOutSessionStateManager>(s => s.GetRequiredService<BunitSignOutSessionStateManager>());
2520
#pragma warning restore CS0618
26-
var authCtx = new TestAuthorizationContext();
27-
authCtx.SetNotAuthorized();
28-
authCtx.RegisterAuthorizationServices(context.Services);
21+
var authCtx = new BunitAuthorizationContext(Services);
2922
return authCtx;
3023
}
3124
}

0 commit comments

Comments
 (0)