test(blogs): seed mapped JWT authors
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
d86ea87d59
commit
e62080c7c6
1 changed files with 12 additions and 4 deletions
|
|
@ -24,12 +24,19 @@ IClassFixture<MappedClaimsBlogsWebServerFixture>
|
||||||
_fixture = fixture;
|
_fixture = fixture;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ResetDatabase()
|
private void ResetDatabase(string authenticatedUserId)
|
||||||
{
|
{
|
||||||
using var scope = _fixture.Services.CreateScope();
|
using var scope = _fixture.Services.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||||
db.Database.EnsureDeleted();
|
db.Database.EnsureDeleted();
|
||||||
db.Database.EnsureCreated();
|
db.Database.EnsureCreated();
|
||||||
|
db.Users.Add(new ApplicationUser
|
||||||
|
{
|
||||||
|
Id = authenticatedUserId,
|
||||||
|
UserName = authenticatedUserId,
|
||||||
|
Email = $"{authenticatedUserId}@example.test"
|
||||||
|
});
|
||||||
|
db.SaveChanges();
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpClient NewClient(string subject = "tester")
|
private HttpClient NewClient(string subject = "tester")
|
||||||
|
|
@ -69,7 +76,7 @@ IClassFixture<MappedClaimsBlogsWebServerFixture>
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task PostBlog_with_mapped_sub_claim_sets_AuthorId_from_authenticated_user()
|
public async Task PostBlog_with_mapped_sub_claim_sets_AuthorId_from_authenticated_user()
|
||||||
{
|
{
|
||||||
ResetDatabase();
|
ResetDatabase("mapped-user");
|
||||||
using var http = NewClient(subject: "mapped-user");
|
using var http = NewClient(subject: "mapped-user");
|
||||||
|
|
||||||
var draft = new BlogPost
|
var draft = new BlogPost
|
||||||
|
|
@ -91,12 +98,13 @@ IClassFixture<MappedClaimsBlogsWebServerFixture>
|
||||||
var created = await response.Content.ReadFromJsonAsync<BlogPost>(TestContext.Current.CancellationToken);
|
var created = await response.Content.ReadFromJsonAsync<BlogPost>(TestContext.Current.CancellationToken);
|
||||||
Assert.NotNull(created);
|
Assert.NotNull(created);
|
||||||
Assert.Equal("mapped-user", created!.AuthorId);
|
Assert.Equal("mapped-user", created!.AuthorId);
|
||||||
|
Assert.Equal("mapped-user", created.Author?.UserName);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task PutBlog_with_mapped_sub_claim_allows_owner_to_update()
|
public async Task PutBlog_with_mapped_sub_claim_allows_owner_to_update()
|
||||||
{
|
{
|
||||||
ResetDatabase();
|
ResetDatabase("mapped-owner");
|
||||||
using var http = NewClient(subject: "mapped-owner");
|
using var http = NewClient(subject: "mapped-owner");
|
||||||
|
|
||||||
var createdResponse = await http.PostAsJsonAsync(_fixture.BlogSpotUrl(), new BlogPost
|
var createdResponse = await http.PostAsJsonAsync(_fixture.BlogSpotUrl(), new BlogPost
|
||||||
|
|
@ -129,7 +137,7 @@ IClassFixture<MappedClaimsBlogsWebServerFixture>
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task PutBlog_with_mapped_sub_claim_rejects_non_owner()
|
public async Task PutBlog_with_mapped_sub_claim_rejects_non_owner()
|
||||||
{
|
{
|
||||||
ResetDatabase();
|
ResetDatabase("mapped-owner");
|
||||||
using var ownerHttp = NewClient(subject: "mapped-owner");
|
using var ownerHttp = NewClient(subject: "mapped-owner");
|
||||||
|
|
||||||
var createdResponse = await ownerHttp.PostAsJsonAsync(_fixture.BlogSpotUrl(), new BlogPost
|
var createdResponse = await ownerHttp.PostAsJsonAsync(_fixture.BlogSpotUrl(), new BlogPost
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue