https://github.com/godeltech/godeltech.xunit.auth
https://github.com/godeltech/godeltech.xunit.auth
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/godeltech/godeltech.xunit.auth
- Owner: GodelTech
- License: mit
- Created: 2023-11-03T14:35:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-02T08:26:58.000Z (11 months ago)
- Last Synced: 2025-01-27T01:41:40.421Z (4 months ago)
- Language: C#
- Size: 74.2 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GodelTech.XUnit.Auth
## Overview
`GodelTech.XUnit.Auth` contains helpers to assert routes in project.
## Example of usage Test JSON Web Token
`AppTestFixture.cs`
```c#
public class AppTestFixture : WebApplicationFactory
{
protected override void ConfigureWebHost([NotNull] IWebHostBuilder builder)
{
builder
.UseSetting("https_port", "8080")
.ConfigureTestJwtToken(); // Configures Test JSON Web Token
}
}
````ControllerTests.cs`
```c#
public sealed class FakeControllerTests : IDisposable
{
private readonly AppTestFixture _fixture;
private readonly HttpClient _client;public FakeControllerTests(ITestOutputHelper output)
{
_fixture = new AppTestFixture
{
Output = output
};_client = _fixture.CreateClient(
new WebApplicationFactoryClientOptions
{
BaseAddress = new Uri("https://localhost:8080")
}
);
}public void Dispose()
{
_client.Dispose();
_fixture.Dispose();
}[Fact]
public async Task GetListAsync_ReturnsOkWithList()
{
// Arrange
var expectedResult = FakeController.Items;// Act
var result = await _client
.WithJwtBearerToken() // Adds Test JSON Web Token to HttpClient
.GetAsync(new Uri("/fakes", UriKind.Relative));// Assert
Assert.Equal(HttpStatusCode.OK, result.StatusCode);var resultValue = await result.Content.ReadFromJsonAsync>();
Assert.NotNull(resultValue);
Assert.Equal(2, resultValue.Count);
resultValue.Should().BeEquivalentTo(expectedResult);
}
}
```You can check example of usage in Integration Tests for this library here: https://github.com/GodelTech/GodelTech.XUnit.Auth/tree/main/test/GodelTech.XUnit.Auth.IntegrationTests.
## Exmaple of usage RouteTests
```c#
public sealed class RouteTests : IDisposable
{
private readonly AppTestFixture _fixture;
private readonly HttpClient _client;public RouteTests(ITestOutputHelper output)
{
_fixture = new AppTestFixture
{
Output = output
};_client = _fixture.CreateClient(
new WebApplicationFactoryClientOptions
{
BaseAddress = new Uri("https://localhost:8080"),
AllowAutoRedirect = false
}
);
}public void Dispose()
{
_client.Dispose();
_fixture.Dispose();
}private static readonly IList Routes = new List
{
// fakes
new AuthorizedRoute("/fakes", HttpMethod.Get),
new AuthorizedRoute(
"/fakes/{id}",
HttpMethod.Get,
new RouteValueDictionary
{
{ "id", 1 }
}
),
new AuthorizedRoute("/fakes", HttpMethod.Post),
new AuthorizedRoute(
"/fakes/{id}",
HttpMethod.Put,
new RouteValueDictionary
{
{ "id", 1 }
}
),
new AuthorizedRoute(
"/fakes/{id}",
HttpMethod.Delete,
new RouteValueDictionary
{
{ "id", 1 }
}
),
// openFakes
new AllowAnonymousRoute(
"/openFakes",
HttpMethod.Get
)
};public static IEnumerable CheckRoutesMemberData => Routes
.Select(
route => new object[]
{
route
}
);[Theory]
[MemberData(nameof(CheckRoutesMemberData))]
public async Task CheckRoutes(Routes.RouteBase route)
{
// Arrange & Act & Assert
await AssertRoute.CheckAsync(
route,
_fixture.GetEndpoints(),
_fixture.GeTemplateBinderFactory(),
_client
);
}[Fact]
public void CheckEndpoints()
{
// Arrange & Act & Assert
AssertEndpoint.Check(
_fixture.GetEndpoints(),
Routes
);
}
}
```You can check example of usage in Integration Tests for this library here: https://github.com/GodelTech/GodelTech.XUnit.Auth/tree/main/test/GodelTech.XUnit.Auth.IntegrationTests.