-
-
Notifications
You must be signed in to change notification settings - Fork 116
Dev authorization fakes #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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 16b7585
Added extension methods to add appropriate authentication/authorizati…
DarthPedro f92a850
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro 80baed4
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro 35b9b5c
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro ffbc120
Merge pull request #1 from egil/dev
DarthPedro 308914a
committing this to be able to switch
DarthPedro f6f4471
Created fake helper classes to assist with testing Blazor authenticat…
DarthPedro f804a1e
Added extension methods to add appropriate authentication/authorizati…
DarthPedro b0931ec
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro 6deb8c5
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro 908d219
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro 7316fb0
Merge branch 'dev-authorization-fakes' of https://github.com/DarthPed…
DarthPedro 6ef2e83
Added documentation for fake authorization services and how to use them.
DarthPedro a316b3e
Fixed code formatting in documentation.
DarthPedro e753deb
Update docs/site/docs/mocking/mocking-auth.md
DarthPedro bd1dcbb
Update src/bunit.web/TestDoubles/Authorization/FakeAuthenticationStat…
DarthPedro 94e1d77
Update docs/site/docs/mocking/mocking-auth.md
DarthPedro 402b38d
Update src/bunit.web.tests/TestDoubles/Authorization/FakeAuthorizatio…
DarthPedro fa5d230
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplay.razor
DarthPedro 9f4b4fe
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 8defb66
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro b28ad7b
Update src/bunit.web.tests/TestDoubles/Authorization/SimpleAuthView.r…
DarthPedro 45280c6
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 2f8c99e
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 94f8035
Merge pull request #3 from egil/dev
DarthPedro 9ed6e40
Merge branch 'dev' of https://github.com/DarthPedro/bUnit into dev
DarthPedro 9bad52b
Created fake helper classes to assist with testing Blazor authenticat…
DarthPedro e49a77e
Added extension methods to add appropriate authentication/authorizati…
DarthPedro 534144c
Added component tests that use AuthorizeView and the BUnit authorizat…
DarthPedro 2e86791
Updated changelog and minor cleanup for SimpleAuthView and test.
DarthPedro 084b0f1
Added placeholders for AuthenticationStateProvider and IAuthorization…
DarthPedro a3929c4
Update src/bunit.web/TestDoubles/Authorization/FakeAuthenticationStat…
DarthPedro c8cf267
Update src/bunit.web.tests/TestDoubles/Authorization/FakeAuthorizatio…
DarthPedro 232efd9
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplay.razor
DarthPedro 1d1a577
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 05799b2
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro d979d30
Update src/bunit.web.tests/TestDoubles/Authorization/SimpleAuthView.r…
DarthPedro cb0442d
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 4dea2f4
Update src/bunit.web.tests/TestDoubles/Authorization/LoginDisplayTest.cs
DarthPedro 2b6939c
committing merge with dev doc changes
DarthPedro 99bf847
Removed this prefixes from usage in code.
DarthPedro d4eda28
Rename SimpleAuthViewTest to AuthorizationTests to better describe wh…
DarthPedro 086dbc6
Added AuthorizationState enum to encapsulate the states. Changed API …
DarthPedro ecb1f9a
Renamed FakeAuthorizationExtensions.AddAuthorization to AddTestAuthor…
DarthPedro 50306e5
Replaced FakeAuthorizationService.NextResult to be CurrentState with …
DarthPedro 561575c
Added simplifying methods to FakeAuthorizationExtensions to UpdateTes…
DarthPedro 6579e98
Added factory method to simplify creating ClaimsPrincipal when needed.
DarthPedro e9e95d9
Update docs/site/docs/test-doubles/faking-auth.md
DarthPedro c6f69b1
Update docs/site/docs/test-doubles/faking-auth.md
DarthPedro be481cd
Update src/bunit.web/TestDoubles/Authorization/MissingFakeAuthorizati…
DarthPedro 821573e
Fix build failure caused by IList property with a setter.
DarthPedro 82bd35d
Moved SimpleAuthTest to bunit.testassets project. Fixed tests to refe…
DarthPedro 3f6c598
Add an explicit SetAuthorizationState to FakeAuthorizationService.
DarthPedro ffcae94
Hide the CurrentState property because it's an implementation detail.
DarthPedro 11629f6
Made policy scheme name a settable property with a default to TestSch…
DarthPedro 97dd96f
Created an AuthorizationAdaptor to wrap some of the auth services and…
DarthPedro e707b5e
Renamed AuthorizationAdaptor to TestAuthorizationContext and minor cl…
DarthPedro 6ac02d1
Changed Roles properties to be non-nullable in TestAuthorizationConte…
DarthPedro a5f09a3
Renamed TestAuthorizationContext methods to SetAuthorized and SetNotA…
DarthPedro 2e513d2
Update CHANGELOG.md with PR comments
DarthPedro bb9448a
Minor clean ups from PR comments.
DarthPedro 55e34f7
Added check to property setters in FakeIdentity and FakeAuthorization…
DarthPedro cd70c01
Exposed AuthorizeCalls through TestAuthorizationContext for callers t…
DarthPedro 08b6405
Small style tweaks to code, spelling, etc.
egil 295a3b1
Merge branch 'dev' into dev-authorization-fakes
egil a189d58
Added back .Render() to test removed during experiments
egil 06f3faa
Additional simplifications of code, update to changelog
egil b7171ce
Fixed tests and code that broke during refactor
egil 0862a8f
Added empty string check to PolicySchemeName. Removed AuthorizeCalls
DarthPedro a818c2f
Added documentation sample code to the bunit.docs.samples project. Up…
DarthPedro 3e4b04d
Merge branch 'dev' into dev-authorization-fakes
egil ac59ecf
Made roles list in SetAuthorized a params string[] roles, making it e…
DarthPedro c67cfd4
Added TestAuthorizationContext.SetAuthorizationPolicies as way for ca…
DarthPedro e206bf0
Fix up sample build break.
DarthPedro a944488
Added SimpleAuthViewWithPolicy component to validate testing with Aut…
DarthPedro a88b2aa
Added sample code and unit tests for using AuthorizeView with Policy.
DarthPedro 4f5fc66
changed role names, and removed ConfigureAwait calls from test classes.
DarthPedro 10fdc3c
Made CreatePrincipal internal
DarthPedro ed87615
Added SetRoles method, and made the TestAuthorizationContext methods …
DarthPedro 4b518bd
Updated samples to the TestAuthorizationContext api changes.
DarthPedro 26a21ac
Updated docs to match the style of the other pages
egil 92b06e3
Added docs samples
egil 41725b4
Tests (which breaks) for missing or invalid roles and policies
egil 4c65bb1
removed src/bunit.testassets/SampleComponents/SimpleWithJsRuntimeDep.…
egil 2851cdb
Readded SimpleWithJSRuntimeDep.razor
egil 5bfd207
Fix FakeAuthorizationService to validate checks for policies and role…
DarthPedro 7e6081e
Added SetAuthorizing method and state implementation to get Authorize…
DarthPedro 315ea0f
Added TestAuthorizationContext.SetClaims and implementation in servic…
DarthPedro 38c8aef
Clean up from PR comments.
DarthPedro 97953dc
Added negative test for AuthorizeView without defined claims.
DarthPedro 4bb2386
Update docs/samples/components/UserInfo.razor
DarthPedro be8f9dd
Changed code to remove null pointer exception if authorizing state wa…
egil c561fe1
Docs: added authorizing example to tests, fixed spelling and gramma
egil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.