Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
ebc8530
Created fake helper classes to assist with testing Blazor authenticat…
DarthPedro Jun 15, 2020
16b7585
Added extension methods to add appropriate authentication/authorizati…
DarthPedro Jun 16, 2020
f92a850
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro Jun 16, 2020
80baed4
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro Jun 16, 2020
35b9b5c
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro Jun 16, 2020
ffbc120
Merge pull request #1 from egil/dev
DarthPedro Jun 16, 2020
308914a
committing this to be able to switch
DarthPedro Jun 16, 2020
f6f4471
Created fake helper classes to assist with testing Blazor authenticat…
DarthPedro Jun 15, 2020
f804a1e
Added extension methods to add appropriate authentication/authorizati…
DarthPedro Jun 16, 2020
b0931ec
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro Jun 16, 2020
6deb8c5
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro Jun 16, 2020
908d219
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro Jun 16, 2020
7316fb0
Merge branch 'dev-authorization-fakes' of https://github.com/DarthPed…
DarthPedro Jun 16, 2020
6ef2e83
Added documentation for fake authorization services and how to use them.
DarthPedro Jun 17, 2020
a316b3e
Fixed code formatting in documentation.
DarthPedro Jun 17, 2020
e753deb
Update docs/site/docs/mocking/mocking-auth.md
DarthPedro Jun 17, 2020
bd1dcbb
Update src/bunit.web/TestDoubles/Authorization/FakeAuthenticationStat…
DarthPedro Jun 17, 2020
94e1d77
Update docs/site/docs/mocking/mocking-auth.md
DarthPedro Jun 17, 2020
402b38d
Update src/bunit.web.tests/TestDoubles/Authorization/FakeAuthorizatio…
DarthPedro Jun 17, 2020
fa5d230
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplay.razor
DarthPedro Jun 17, 2020
9f4b4fe
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
8defb66
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
b28ad7b
Update src/bunit.web.tests/TestDoubles/Authorization/SimpleAuthView.r…
DarthPedro Jun 17, 2020
45280c6
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
2f8c99e
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
94f8035
Merge pull request #3 from egil/dev
DarthPedro Jun 17, 2020
9ed6e40
Merge branch 'dev' of https://github.com/DarthPedro/bUnit into dev
DarthPedro Jun 17, 2020
9bad52b
Created fake helper classes to assist with testing Blazor authenticat…
DarthPedro Jun 15, 2020
e49a77e
Added extension methods to add appropriate authentication/authorizati…
DarthPedro Jun 16, 2020
534144c
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro Jun 16, 2020
2e86791
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro Jun 16, 2020
084b0f1
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro Jun 16, 2020
a3929c4
Update src/bunit.web/TestDoubles/Authorization/FakeAuthenticationStat…
DarthPedro Jun 17, 2020
c8cf267
Update src/bunit.web.tests/TestDoubles/Authorization/FakeAuthorizatio…
DarthPedro Jun 17, 2020
232efd9
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplay.razor
DarthPedro Jun 17, 2020
1d1a577
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
05799b2
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
d979d30
Update src/bunit.web.tests/TestDoubles/Authorization/SimpleAuthView.r…
DarthPedro Jun 17, 2020
cb0442d
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
4dea2f4
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro Jun 17, 2020
2b6939c
committing merge with dev doc changes
DarthPedro Jun 17, 2020
99bf847
Removed this prefixes from usage in code.
DarthPedro Jun 17, 2020
d4eda28
Rename SimpleAuthViewTest to AuthorizationTests to better describe wh…
DarthPedro Jun 17, 2020
086dbc6
Added AuthorizationState enum to encapsulate the states. Changed API …
DarthPedro Jun 17, 2020
ecb1f9a
Renamed FakeAuthorizationExtensions.AddAuthorization to AddTestAuthor…
DarthPedro Jun 17, 2020
50306e5
Replaced FakeAuthorizationService.NextResult to be CurrentState with …
DarthPedro Jun 17, 2020
561575c
Added simplifying methods to FakeAuthorizationExtensions to UpdateTes…
DarthPedro Jun 17, 2020
6579e98
Added factory method to simplify creating ClaimsPrincipal when needed.
DarthPedro Jun 17, 2020
e9e95d9
Update docs/site/docs/test-doubles/faking-auth.md
DarthPedro Jun 17, 2020
c6f69b1
Update docs/site/docs/test-doubles/faking-auth.md
DarthPedro Jun 17, 2020
be481cd
Update src/bunit.web/TestDoubles/Authorization/MissingFakeAuthorizati…
DarthPedro Jun 17, 2020
821573e
Fix build failure caused by IList property with a setter.
DarthPedro Jun 17, 2020
82bd35d
Moved SimpleAuthTest to bunit.testassets project. Fixed tests to refe…
DarthPedro Jun 17, 2020
3f6c598
Add an explicit SetAuthorizationState to FakeAuthorizationService.
DarthPedro Jun 18, 2020
ffcae94
Hide the CurrentState property because it's an implementation detail.
DarthPedro Jun 18, 2020
11629f6
Made policy scheme name a settable property with a default to TestSch…
DarthPedro Jun 18, 2020
97dd96f
Created an AuthorizationAdaptor to wrap some of the auth services and…
DarthPedro Jun 18, 2020
e707b5e
Renamed AuthorizationAdaptor to TestAuthorizationContext and minor cl…
DarthPedro Jun 18, 2020
6ac02d1
Changed Roles properties to be non-nullable in TestAuthorizationConte…
DarthPedro Jun 18, 2020
a5f09a3
Renamed TestAuthorizationContext methods to SetAuthorized and SetNotA…
DarthPedro Jun 18, 2020
2e513d2
Update CHANGELOG.md with PR comments
DarthPedro Jun 18, 2020
bb9448a
Minor clean ups from PR comments.
DarthPedro Jun 18, 2020
55e34f7
Added check to property setters in FakeIdentity and FakeAuthorization…
DarthPedro Jun 18, 2020
cd70c01
Exposed AuthorizeCalls through TestAuthorizationContext for callers t…
DarthPedro Jun 18, 2020
08b6405
Small style tweaks to code, spelling, etc.
egil Jun 19, 2020
295a3b1
Merge branch 'dev' into dev-authorization-fakes
egil Jun 19, 2020
a189d58
Added back .Render() to test removed during experiments
egil Jun 19, 2020
06f3faa
Additional simplifications of code, update to changelog
egil Jun 19, 2020
b7171ce
Fixed tests and code that broke during refactor
egil Jun 19, 2020
0862a8f
Added empty string check to PolicySchemeName. Removed AuthorizeCalls
DarthPedro Jun 19, 2020
a818c2f
Added documentation sample code to the bunit.docs.samples project. Up…
DarthPedro Jun 19, 2020
3e4b04d
Merge branch 'dev' into dev-authorization-fakes
egil Jun 20, 2020
ac59ecf
Made roles list in SetAuthorized a params string[] roles, making it e…
DarthPedro Jun 20, 2020
c67cfd4
Added TestAuthorizationContext.SetAuthorizationPolicies as way for ca…
DarthPedro Jun 20, 2020
e206bf0
Fix up sample build break.
DarthPedro Jun 20, 2020
a944488
Added SimpleAuthViewWithPolicy component to validate testing with Aut…
DarthPedro Jun 20, 2020
a88b2aa
Added sample code and unit tests for using AuthorizeView with Policy.
DarthPedro Jun 20, 2020
4f5fc66
changed role names, and removed ConfigureAwait calls from test classes.
DarthPedro Jun 22, 2020
10fdc3c
Made CreatePrincipal internal
DarthPedro Jun 22, 2020
ed87615
Added SetRoles method, and made the TestAuthorizationContext methods …
DarthPedro Jun 22, 2020
4b518bd
Updated samples to the TestAuthorizationContext api changes.
DarthPedro Jun 22, 2020
26a21ac
Updated docs to match the style of the other pages
egil Jun 24, 2020
92b06e3
Added docs samples
egil Jun 24, 2020
41725b4
Tests (which breaks) for missing or invalid roles and policies
egil Jun 24, 2020
4c65bb1
removed src/bunit.testassets/SampleComponents/SimpleWithJsRuntimeDep.…
egil Jun 24, 2020
2851cdb
Readded SimpleWithJSRuntimeDep.razor
egil Jun 24, 2020
5bfd207
Fix FakeAuthorizationService to validate checks for policies and role…
DarthPedro Jun 24, 2020
7e6081e
Added SetAuthorizing method and state implementation to get Authorize…
DarthPedro Jun 24, 2020
315ea0f
Added TestAuthorizationContext.SetClaims and implementation in servic…
DarthPedro Jun 24, 2020
38c8aef
Clean up from PR comments.
DarthPedro Jun 24, 2020
97953dc
Added negative test for AuthorizeView without defined claims.
DarthPedro Jun 24, 2020
4bb2386
Update docs/samples/components/UserInfo.razor
DarthPedro Jun 24, 2020
be8f9dd
Changed code to remove null pointer exception if authorizing state wa…
egil Jun 25, 2020
c561fe1
Docs: added authorizing example to tests, fixed spelling and gramma
egil Jun 25, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,45 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
List of new features.

- Authorization fakes added to make it much easier to test components that use authentication and authorization. By [@DarthPedro](https://github.com/DarthPedro) in [#151](https://github.com/egil/bUnit/pull/151).

#### Authorization Fakes
Added authentication/authorization fake services to make it easy to test Blazor components that use authorization either through code or the AuthorizeView component.
You just need to call the AddTestAuthorization method on your TestContext.Services collection.

First define your component that uses AuthorizeView to render differently based on the user's authorization state:
```
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization

<CascadingAuthenticationState>
<AuthorizeView>
<Authorized>
Authorized!
</Authorized>
<NotAuthorized>
Not authorized?
</NotAuthorized>
</AuthorizeView>
</CascadingAuthenticationState>
```
Then define your test method to specify the authorization state with a user name, and run your test.
```c#
public void Test002()
{
// arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("TestUser", AuthorizationState.Authorized);

// act
var cut = ctx.RenderComponent<SimpleAuthView>();

// assert
cut.MarkupMatches("Authorized!");
}
```

### Changed
List of changes in existing functionality.

Expand Down
24 changes: 24 additions & 0 deletions docs/samples/components/InjectAuthService.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider

<p>Test Component</p>

@if (isAuthenticated)
{
<p>User: @userName</p>
}

@code{
bool isAuthenticated = false;
string userName;

protected override async Task OnInitializedAsync()
{
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync().ConfigureAwait(false);
if (state != null)
{
this.isAuthenticated = state.User.Identity.IsAuthenticated;
this.userName = state.User.Identity.Name;
}
}
}
37 changes: 37 additions & 0 deletions docs/samples/components/UserInfo.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@using Microsoft.AspNetCore.Components.Authorization
@inject AuthenticationStateProvider AuthenticationStateProvider

@if (isAuthenticated)
{
<h1>Welcome @userName</h1>
}
@if (!isAuthenticated)
{
<h1>Please log in!</h1>
}
<CascadingAuthenticationState>
<AuthorizeView>
<Authorized>
<p>State: Authorized</p>
</Authorized>
<Authorizing>
<p>State: Authorizing</p>
</Authorizing>
<NotAuthorized>
<p>State: Not authorized</p>
</NotAuthorized>
</AuthorizeView>
</CascadingAuthenticationState>

@code
{
bool isAuthenticated = false;
string userName;

protected override async Task OnParametersSetAsync()
{
var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
isAuthenticated = state.User.Identity.IsAuthenticated;
userName = state.User.Identity.Name;
}
}
36 changes: 36 additions & 0 deletions docs/samples/components/UserRights.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@using Microsoft.AspNetCore.Components.Authorization
@using System.Security.Claims
@using System.Globalization

<CascadingAuthenticationState>
<AuthorizeView>
<h1>Hi @context.User.Identity.Name, you have these claims and rights:</h1>
</AuthorizeView>
<ul>
<AuthorizeView>
@foreach (var claim in @context.User.FindAll(x => x.Type != ClaimTypes.Name))
{
<li>@GetClaimName(claim): @claim.Value</li>
}
</AuthorizeView>
<AuthorizeView Roles="superuser">
<li>You have the role SUPER USER</li>
</AuthorizeView>
<AuthorizeView Roles="admin">
<li>You have the role ADMIN</li>
</AuthorizeView>
<AuthorizeView Policy="content-editor">
<li>You are a CONTENT EDITOR</li>
</AuthorizeView>
</ul>
</CascadingAuthenticationState>

@code
{
private static string GetClaimName(Claim claim)
{
var claimType = new Uri(claim.Type);
var name = claimType.Segments.Last();
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(name);
}
}
1 change: 1 addition & 0 deletions docs/samples/components/bunit.docs.samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
</ItemGroup>

Expand Down
38 changes: 38 additions & 0 deletions docs/samples/tests/xunit/InjectAuthServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Bunit.TestDoubles.Authorization;
using System;
using Xunit;

namespace Bunit.Docs.Samples
{
public class InjectAuthServiceTest
{
[Fact(DisplayName = "Use AuthenticationStateProvider service with authenticated and authorized user")]
public void Test001()
{
// arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("TestUserName", AuthorizationState.Authorized);

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

// assert
Assert.Contains("<p>User: TestUserName</p>", cut.Markup, StringComparison.InvariantCulture);
}

[Fact(DisplayName = "Use AuthenticationStateProvider service with unauthenticated and unauthorized user")]
public void Test002()
{
// arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();

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

// assert
Assert.DoesNotContain("User:", cut.Markup, StringComparison.InvariantCulture);
}
}
}
71 changes: 71 additions & 0 deletions docs/samples/tests/xunit/UserInfoTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Bunit.TestDoubles.Authorization;
using Xunit;

namespace Bunit.Docs.Samples
{
public class UserInfoTest
{
[Fact(DisplayName = "UserInfo with unauthenticated user")]
public void Test001()
{
// Arrange
using var ctx = new TestContext();
ctx.Services.AddTestAuthorization();

// Act
var cut = ctx.RenderComponent<UserInfo>();

// Assert
cut.MarkupMatches(@"<h1>Please log in!</h1>
<p>State: Not authorized</p>");
}

[Fact(DisplayName = "UserInfo while authorizing user")]
public void Test004()
{
// Arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorizing();

// Act
var cut = ctx.RenderComponent<UserInfo>();

// Assert
cut.MarkupMatches(@"<h1>Please log in!</h1>
<p>State: Authorizing</p>");
}

[Fact(DisplayName = "UserInfo with authenticated but unauthorized user")]
public void Test002()
{
// Arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("TEST USER", AuthorizationState.Unauthorized);

// Act
var cut = ctx.RenderComponent<UserInfo>();

// Assert
cut.MarkupMatches(@"<h1>Welcome TEST USER</h1>
<p>State: Not authorized</p>");
}

[Fact(DisplayName = "UserInfo with authenticated and authorized user")]
public void Test003()
{
// Arrange
using var ctx = new TestContext();
var authContext = ctx.Services.AddTestAuthorization();
authContext.SetAuthorized("TEST USER");

// Act
var cut = ctx.RenderComponent<UserInfo>();

// Assert
cut.MarkupMatches(@"<h1>Welcome TEST USER</h1>
<p>State: Authorized</p>");
}
}
}
Loading