-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,677 additions
and
834 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -88,6 +88,25 @@ the JWT tokens with a real Open Id Connect server **so you can test your service | |
The `JwtSecurityStub` will also honor the `WithClaim()` method to add additional claims on a scenario by scenario basis | ||
as shown in the previous section. | ||
|
||
## Override a specific scheme | ||
|
||
Both `AuthenticationSecurityStub` and `JwtSecuritySnub` will replace all authentication schemes by default. If you only want a single scheme to be replaced, | ||
you can pass the scheme name via the constructor: | ||
|
||
<!-- snippet: sample_bootstrapping_with_stub_scheme_extension --> | ||
<a id='snippet-sample_bootstrapping_with_stub_scheme_extension'></a> | ||
```cs | ||
// Stub out an individual scheme | ||
var securityStub = new AuthenticationStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub); | ||
``` | ||
<sup><a href='https://github.com/JasperFx/alba/blob/master/src/Alba.Testing/Security/web_api_authentication_with_individual_stub.cs#L14-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrapping_with_stub_scheme_extension' title='Start of snippet'>anchor</a></sup> | ||
<!-- endSnippet --> | ||
|
||
## Integration with JWT Authentication | ||
|
||
::: tip | ||
|
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
63 changes: 63 additions & 0 deletions
63
src/Alba.Testing/Security/web_api_authentication_with_individual_stub.cs
This file contains 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,63 @@ | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Alba.Security; | ||
using Microsoft.IdentityModel.JsonWebTokens; | ||
using Xunit; | ||
|
||
namespace Alba.Testing.Security; | ||
|
||
public class web_api_authentication_with_individual_stub | ||
{ | ||
[Fact] | ||
public async Task can_stub_individual_scheme() | ||
{ | ||
#region sample_bootstrapping_with_stub_scheme_extension | ||
// Stub out an individual scheme | ||
var securityStub = new AuthenticationStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub); | ||
#endregion | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBe(HttpStatusCode.Unauthorized); | ||
}); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task can_stub_individual_scheme_jwt() | ||
{ | ||
// This is a Alba extension that can "stub" out authentication | ||
var securityStub = new JwtSecurityStub("custom") | ||
.With("foo", "bar") | ||
.With(JwtRegisteredClaimNames.Email, "[email protected]") | ||
.WithName("jeremy"); | ||
|
||
// We're calling your real web service's configuration | ||
await using var host = await AlbaHost.For<WebAppSecuredWithJwt.Program>(securityStub); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity2"); | ||
s.StatusCodeShouldBeOk(); | ||
}); | ||
|
||
await host.Scenario(s => | ||
{ | ||
s.Get.Url("/identity"); | ||
s.StatusCodeShouldBe(HttpStatusCode.Unauthorized); | ||
}); | ||
|
||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -111,7 +111,6 @@ public async Task can_modify_claims_per_scenario() | |
} | ||
|
||
#endregion | ||
|
||
|
||
} | ||
} |
This file contains 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 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 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 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