Skip to content

Commit

Permalink
adding unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calebjenkins committed Nov 9, 2024
1 parent dcad739 commit f0e9dde
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/WebAPI/Calebs.WebAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<PackageOutputPath>../../nupkg</PackageOutputPath>
<Version>1.0.0</Version>
<IsPackable>true</IsPackable>
<PackageProjectUrl>https://github.com/calebjenkins/calebs.webapi/</PackageProjectUrl>
<RepositoryUrl>https://github.com/calebjenkins/Calebs.WebAPI.git</RepositoryUrl>
</PropertyGroup>

<ItemGroup>
Expand All @@ -33,8 +35,8 @@
</ItemGroup>

<ItemGroup>
<None Include="..\..\README.md" Pack="true" PackagePath=""/>
<None Include="..\..\LICENSE" Pack="true" PackagePath=""/>
<None Include="..\..\README.md" Pack="true" PackagePath="" />
<None Include="..\..\LICENSE" Pack="true" PackagePath="" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion src/WebAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using FakeAuth; // Using FakeAuth so I can send a 403 without setting up all the needed auth infrastructure
using Calebs.Extensions.Console;

//namespace Calebs.WebAPI;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthentication().AddFakeAuth();

Expand Down Expand Up @@ -50,7 +52,7 @@ IResult Check_Secure(HttpContext context)
{
if (context.Request.Headers.Keys.Contains("bearer")) // && context.Request.Headers["bearer"] == TOKEN)
{
return Results.Ok(new { Results = "Suceess!" });
return Results.Ok(new { Results = "Success!" });
}

return Results.Forbid();
Expand All @@ -77,3 +79,7 @@ async Task<string> StreamToString(Stream stream)
var result = await reader.ReadToEndAsync();
return result;
}

public partial class Program
{ } // needed for test visability

14 changes: 0 additions & 14 deletions src/WebAPITests/UnitTest1.cs

This file was deleted.

30 changes: 30 additions & 0 deletions src/WebAPITests/WebAPIApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using FakeAuth;


namespace Calebs.WebAPI.Tests;

internal class WebAPIApplication : WebApplicationFactory<Program>
{
private readonly string _environment;

public WebAPIApplication(string environment = "Development")
{
_environment = environment;
}

protected override IHost CreateHost(IHostBuilder builder)
{
builder.UseEnvironment(_environment);

// Add mock/test services to the builder here
builder.ConfigureServices(services =>
{
// nothing to change
});

return base.CreateHost(builder);
}
}
74 changes: 74 additions & 0 deletions src/WebAPITests/WebAPITests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.AspNetCore.Mvc.Testing;
using Calebs.WebAPI;
using Microsoft.VisualStudio.TestPlatform.TestHost;
using System.Net;
using System.Xml.Linq;


namespace Calebs.WebAPI.Tests;

public class WebAPITests
{
private WebAPIApplication _server;
private HttpClient _client;
private const string _token = "fakeToken123xyz";

public WebAPITests()
{
_server = new WebAPIApplication();
_client = _server.CreateClient();
}

[Fact]
public async Task Hello_Should_Return_HelloWorld()
{
var response = await _client.GetAsync("/hello/");

response.StatusCode.Should().Be(HttpStatusCode.OK);
var value = await response.Content.ReadAsStringAsync();
value.Should().Be("hello world");
}
[Theory]
[InlineData ("Joe")]
[InlineData ("Bill")]
[InlineData ("Fred the great!")]
[InlineData ("Elizabeth")]
public async Task Hello_NAME_Should_return_name(string Name)
{
var response = await _client.GetAsync($"/hello/{ Name }/");

response.StatusCode.Should().Be(HttpStatusCode.OK);
var value = await response.Content.ReadAsStringAsync();
value.Should().Be($"hello {Name}");
}

[Fact]
public async Task Secure_No_Token_Should_Deny()
{
var response = await _client.GetAsync($"/secure");
response.StatusCode.Should().Be(HttpStatusCode.Forbidden);
}

[Fact]
public async Task FakeToken_Should_return_token()
{
var response = await _client.GetAsync($"/FakeToken");
response.StatusCode.Should().Be(HttpStatusCode.OK);
var value = await response.Content.ReadAsStringAsync();
value.Should().Be(_token);
}

[Fact]
public async Task Secure_with_Token_should_return_token()
{
var newClient = _server.CreateClient();
newClient.DefaultRequestHeaders.Add("bearer", _token);
var response = await newClient.GetAsync($"/Secure");

response.StatusCode.Should().Be(HttpStatusCode.OK);
var value = await response.Content.ReadAsStringAsync();
value.Should().Contain("Success!");
}


}

0 comments on commit f0e9dde

Please sign in to comment.