diff --git a/src/WebAPI/Calebs.WebAPI.csproj b/src/WebAPI/Calebs.WebAPI.csproj index e2112f1..7db3ba3 100644 --- a/src/WebAPI/Calebs.WebAPI.csproj +++ b/src/WebAPI/Calebs.WebAPI.csproj @@ -15,6 +15,8 @@ ../../nupkg 1.0.0 true + https://github.com/calebjenkins/calebs.webapi/ + https://github.com/calebjenkins/Calebs.WebAPI.git @@ -33,8 +35,8 @@ - - + + diff --git a/src/WebAPI/Program.cs b/src/WebAPI/Program.cs index 8f2a99e..b669a5a 100644 --- a/src/WebAPI/Program.cs +++ b/src/WebAPI/Program.cs @@ -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(); @@ -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(); @@ -77,3 +79,7 @@ async Task StreamToString(Stream stream) var result = await reader.ReadToEndAsync(); return result; } + +public partial class Program +{ } // needed for test visability + diff --git a/src/WebAPITests/UnitTest1.cs b/src/WebAPITests/UnitTest1.cs deleted file mode 100644 index 0206d95..0000000 --- a/src/WebAPITests/UnitTest1.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Mvc.Testing; -//using WebAPI; - -namespace WebAPITests -{ - public class WebAPITests - { - [Fact] - public void Hello_Should_Return_HelloWorld() - { - // var app = new WebApplicationFactory<>(); - } - } -} \ No newline at end of file diff --git a/src/WebAPITests/WebAPIApplication.cs b/src/WebAPITests/WebAPIApplication.cs new file mode 100644 index 0000000..31d37ee --- /dev/null +++ b/src/WebAPITests/WebAPIApplication.cs @@ -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 +{ + 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); + } +} diff --git a/src/WebAPITests/WebAPITests.cs b/src/WebAPITests/WebAPITests.cs new file mode 100644 index 0000000..bee77bd --- /dev/null +++ b/src/WebAPITests/WebAPITests.cs @@ -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!"); + } + + +}