diff options
Diffstat (limited to 'Timeline.Tests')
-rw-r--r-- | Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs | 23 | ||||
-rw-r--r-- | Timeline.Tests/Properties/launchSettings.json | 27 | ||||
-rw-r--r-- | Timeline.Tests/Timeline.Tests.csproj | 27 | ||||
-rw-r--r-- | Timeline.Tests/UnitTest.cs | 58 |
4 files changed, 135 insertions, 0 deletions
diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs new file mode 100644 index 00000000..bb8fc71b --- /dev/null +++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.Logging; +using Xunit.Abstractions; + +namespace Timeline.Tests.Helpers +{ + public static class WebApplicationFactoryExtensions + { + public static WebApplicationFactory<TEntry> WithTestConfig<TEntry>(this WebApplicationFactory<TEntry> factory, ITestOutputHelper outputHelper) where TEntry : class + { + return factory.WithWebHostBuilder(builder => + { + builder + .UseEnvironment(EnvironmentConstants.TestEnvironmentName) + .ConfigureLogging(logging => + { + logging.AddXunit(outputHelper); + }); + }); + } + } +} diff --git a/Timeline.Tests/Properties/launchSettings.json b/Timeline.Tests/Properties/launchSettings.json new file mode 100644 index 00000000..0c1cae5d --- /dev/null +++ b/Timeline.Tests/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:11197/", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "Timeline.Tests": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:11199/;http://localhost:11198/" + } + } +}
\ No newline at end of file diff --git a/Timeline.Tests/Timeline.Tests.csproj b/Timeline.Tests/Timeline.Tests.csproj new file mode 100644 index 00000000..cbb8ab59 --- /dev/null +++ b/Timeline.Tests/Timeline.Tests.csproj @@ -0,0 +1,27 @@ +<Project Sdk="Microsoft.NET.Sdk.Web"> + + <PropertyGroup> + <TargetFramework>netcoreapp2.2</TargetFramework> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.AspNetCore.App" /> + <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" /> + <PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="2.2.0-rtm-35646" /> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> + <PackageReference Include="xunit" Version="2.4.1" /> + <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> + <PrivateAssets>all</PrivateAssets> + <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> + </PackageReference> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\Timeline\Timeline.csproj" /> + </ItemGroup> + + <ItemGroup> + <Folder Include="Properties\" /> + </ItemGroup> + +</Project> diff --git a/Timeline.Tests/UnitTest.cs b/Timeline.Tests/UnitTest.cs new file mode 100644 index 00000000..e201061b --- /dev/null +++ b/Timeline.Tests/UnitTest.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Mvc.Testing; +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Timeline.Controllers; +using Timeline.Tests.Helpers; +using Xunit; +using Xunit.Abstractions; + +namespace Timeline.Tests +{ + public class UnitTest : IClassFixture<WebApplicationFactory<Startup>> + { + private readonly WebApplicationFactory<Startup> _factory; + + public UnitTest(WebApplicationFactory<Startup> factory, ITestOutputHelper outputHelper) + { + _factory = factory.WithTestConfig(outputHelper); + } + + [Fact] + public async Task UnauthenticationTest() + { + using (var client = _factory.CreateDefaultClient()) + { + var response = await client.GetAsync("/api/SampleData/WeatherForecasts"); + + Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); + } + } + + [Fact] + public async Task AuthenticationTest() + { + using (var client = _factory.CreateDefaultClient()) + { + var response = await client.PostAsJsonAsync("/api/User/LogIn", new UserController.UserCredentials { Username = "crupest", Password = "yang0101" }); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var token = response.Headers.GetValues("Authorization").Single(); + + var request = new HttpRequestMessage + { + RequestUri = new Uri(client.BaseAddress, "/api/SampleData/WeatherForecasts"), + Method = HttpMethod.Get + }; + request.Headers.Add("Authorization", token); + + var response2 = await client.SendAsync(request); + + Assert.Equal(HttpStatusCode.OK, response2.StatusCode); + } + } + } +} |