Skip to content

Update Chapter 07 Integration Tests to use FluentAssertions.Web extensions #4

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
130 changes: 59 additions & 71 deletions end/chapter07/AuthHandler/Integration.Tests/Integration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public async Task Auth_ShouldGetPastAuthEndpoint()

// Act
var response = await client.GetAsync("/api/Auth/testAuth");
response.IsSuccessStatusCode.Should().BeTrue("the request should be successful");

var content = await response.Content.ReadAsStringAsync();

response.Should().Be200Ok()
.And.MatchInContent("AuthController authorize is working!");
}

[Fact]
Expand All @@ -47,10 +46,9 @@ public async Task Auth_ShouldGetPastAuthEndpointJWT()

// Act
var response = await client.GetAsync("/api/Auth/testAuth");
response.IsSuccessStatusCode.Should().BeTrue("the request should be successful");

var content = await response.Content.ReadAsStringAsync();

response.Should().Be200Ok()
.And.MatchInContent("AuthController authorize is working!");
}

[Fact]
Expand All @@ -64,34 +62,21 @@ public async Task Claims_ShouldReturnCorrectAuthenticationDetails()
// Act
TestLogger.Log("Sending request to /api/Books/claims");
var response = await client.GetAsync("/api/Books/claims");
TestLogger.Log($"Response received. Status code: {response.StatusCode}");

var content = await response.Content.ReadAsStringAsync();
TestLogger.Log("Claims content returned:");
TestLogger.Log(content);

// Assert
response.IsSuccessStatusCode.Should().BeTrue("the request should be successful");

using (JsonDocument document = JsonDocument.Parse(content))
{
var root = document.RootElement;

root.GetProperty("isAuthenticated").GetBoolean().Should().BeTrue("the user should be authenticated");
root.GetProperty("authenticationType").GetString().Should().Be("Test", "the authentication type should match the test scheme");
root.GetProperty("name").GetString().Should().Be("testuser123", "the name should match the test user");

var claims = root.GetProperty("claims");
claims.GetArrayLength().Should().Be(2, "there should be two claims");

var claimValues = claims.EnumerateArray()
.Select(c => c.GetProperty("value").GetString())
.ToList();

claimValues.Should().AllBe("testuser123", "all claim values should be 'testuser123'");
}

TestLogger.Log("All assertions passed successfully");
TestLogger.Log($"Response received. Status code: {response.StatusCode}");
response.Should().Be200Ok()
.And.BeAs(new
{
isAuthenticated = true,
authenticationType = "Test",
name = "testuser123",
claims = new[]
{
new { type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", value= "testuser123"},
new { type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", value= "testuser123"},
}
});
}


Expand All @@ -101,10 +86,16 @@ public async Task Claims_ShouldReturnNameAndNameIdentifier()
var client = _factory.CreateAuthenticatedClient();

var response = await client.GetAsync("/api/Books/claims");
var content = await response.Content.ReadAsStringAsync();
response.IsSuccessStatusCode.Should().BeTrue();
TestLogger.Log("Claims content returned...");
TestLogger.Log(content);

response.Should().Be200Ok()
.And.BeAs(new
{
claims = new[]
{
new { type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", value= "testuser123"},
new { type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", value= "testuser123"},
}
});
}

[Fact]
Expand All @@ -118,24 +109,15 @@ public async Task Hi_ShouldReturnNameIdentifier()
var response = await client.GetAsync("/api/Books/hi");
TestLogger.Log($"Response received. Status code: {response.StatusCode}");

var content = await response.Content.ReadAsStringAsync();
TestLogger.Log($"Response content: {content}");

// Log response headers
TestLogger.Log("Response headers:");
foreach (var header in response.Headers)
{
TestLogger.Log($"{header.Key}: {string.Join(", ", header.Value)}");
}

// Assert
TestLogger.Log("Starting assertions");
response.IsSuccessStatusCode.Should().BeTrue($"Expected successful status code, but got {response.StatusCode}");
content.Should().Contain("#", $"Expected content to contain '#', but got: {content}");
content.Should().NotBe("Hello ", $"Expected content to not be exactly 'Hello ', but got: {content}");
content.Should().MatchRegex(@"Hello #\w+", $"Expected content to match 'Hello #<word>', but got: {content}");
content.Should().Be($"Hello #testuser123");
TestLogger.Log("Assertions completed");
response.Should().Be200Ok()
.And.Satisfy(async response =>
{
var content = await response.Content.ReadAsStringAsync();
content.Should().Contain("#", $"Expected content to contain '#', but got: {content}");
content.Should().NotBe("Hello ", $"Expected content to not be exactly 'Hello ', but got: {content}");
content.Should().MatchRegex(@"Hello #\w+", $"Expected content to match 'Hello #<word>', but got: {content}");
content.Should().Be($"Hello #testuser123");
});
}

[Fact]
Expand All @@ -150,19 +132,7 @@ public async Task Hi_EndpointIsOn()
var response = await client.GetAsync("/api/Books/hi");
Console.WriteLine($"Response received. Status code: {response.StatusCode}");

var content = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Response content: {content}");

// Log response headers
Console.WriteLine("Response headers:");
foreach (var header in response.Headers)
{
Console.WriteLine($"{header.Key}: {string.Join(", ", header.Value)}");
}

// Assert
response.IsSuccessStatusCode.Should().BeTrue($"Expected successful status code, but got {response.StatusCode}");
content.Should().Contain("Hello", $"Expected content to contain 'Hello', but got: {content}");
response.Should().Be200Ok().And.MatchInContent("*Hello*");
}

[Fact]
Expand All @@ -171,9 +141,27 @@ public async Task Claims_WithJWTShouldReturnNameAndNameIdentifier()
var client = _factory.CreateClientWithJwtAuth("testuser123", new[] { "Admin", "User" });

var response = await client.GetAsync("/api/Books/claims");
var content = await response.Content.ReadAsStringAsync();
response.IsSuccessStatusCode.Should().BeTrue();
TestLogger.Log("Claims content returned...");
TestLogger.Log(content);

response.Should().Be200Ok()
.And.Satisfy(givenModelStructure: new
{
isAuthenticated = default(bool),
authenticationType = default(string),
name = default(string),
claims = new[] { new { type = default(string), value = default(string) } }
}, model =>
{
model.Should().BeEquivalentTo(
new
{
isAuthenticated = true,
authenticationType = "AuthenticationTypes.Federation",
name = "testuser123"
});

model.claims.Should().NotBeNull().And.NotBeEmpty();
model.claims.Should().Contain(c => c.type == "iss" && c.value == "example-books.com");

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="FluentAssertions" Version="8.0.0-alpha.1" />
<PackageReference Include="FluentAssertions.Web" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down
2 changes: 1 addition & 1 deletion end/chapter07/AuthHandler/Unit.Tests/Unit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="7.1.0" />
<PackageReference Include="FluentAssertions" Version="8.0.0-alpha.1" />
<PackageReference Include="FluentAssertions.AspNetCore.Mvc" Version="4.2.0" />
<PackageReference Include="FluentAssertions.Web" Version="1.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.2" />
Expand Down