mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-07-24 22:21:30 +00:00
Basic testing framework
This commit is contained in:
parent
bce6d1e2ef
commit
5052d19656
8 changed files with 97 additions and 4 deletions
11
ProjectLighthouse.Tests/DatabaseFact.cs
Normal file
11
ProjectLighthouse.Tests/DatabaseFact.cs
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
using ProjectLighthouse.Types;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests {
|
||||||
|
public sealed class DatabaseFact : FactAttribute {
|
||||||
|
public DatabaseFact() {
|
||||||
|
ServerSettings.DbConnectionString = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse";
|
||||||
|
if(!ServerSettings.DbConnected) Skip = "Database not available";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
ProjectLighthouse.Tests/LighthouseTest.cs
Normal file
19
ProjectLighthouse.Tests/LighthouseTest.cs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.TestHost;
|
||||||
|
using ProjectLighthouse.Types;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests {
|
||||||
|
public class LighthouseTest {
|
||||||
|
public readonly TestServer Server;
|
||||||
|
public readonly HttpClient Client;
|
||||||
|
|
||||||
|
public LighthouseTest() {
|
||||||
|
this.Server = new TestServer(new WebHostBuilder()
|
||||||
|
.UseStartup<Startup>());
|
||||||
|
this.Client = this.Server.CreateClient();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
29
ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj
Normal file
29
ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
|
||||||
|
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.11" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="3.0.2">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
|
@ -0,0 +1,2 @@
|
||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=tests/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
28
ProjectLighthouse.Tests/Tests/AuthenticationTest.cs
Normal file
28
ProjectLighthouse.Tests/Tests/AuthenticationTest.cs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Tests {
|
||||||
|
public class AuthenticationTest : LighthouseTest {
|
||||||
|
[Fact]
|
||||||
|
public async Task ShouldReturnErrorOnNoPostData() {
|
||||||
|
var response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", null!);
|
||||||
|
Assert.False(response.IsSuccessStatusCode);
|
||||||
|
#if NET6_0_OR_GREATER
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.BadRequest);
|
||||||
|
#else
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.NotAcceptable);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
[DatabaseFact]
|
||||||
|
public async Task ShouldAuthenticateWithValidData() {
|
||||||
|
const char nullChar = (char)0x00;
|
||||||
|
const char sepChar = (char)0x20;
|
||||||
|
|
||||||
|
var response = await this.Client.PostAsync("/LITTLEBIGPLANETPS3_XML/login", new StringContent($"{nullChar}{sepChar}jvyden{nullChar}"));
|
||||||
|
Assert.True(response.StatusCode == HttpStatusCode.OK);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,8 @@
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse", "ProjectLighthouse\ProjectLighthouse.csproj", "{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse", "ProjectLighthouse\ProjectLighthouse.csproj", "{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Tests", "ProjectLighthouse.Tests\ProjectLighthouse.Tests.csproj", "{AFC74569-B289-4ACC-B21C-313A3A62C017}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
@ -12,5 +14,9 @@ Global
|
||||||
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Release|Any CPU.Build.0 = Release|Any CPU
|
{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{AFC74569-B289-4ACC-B21C-313A3A62C017}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AFC74569-B289-4ACC-B21C-313A3A62C017}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AFC74569-B289-4ACC-B21C-313A3A62C017}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AFC74569-B289-4ACC-B21C-313A3A62C017}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|
|
@ -14,9 +14,6 @@ namespace ProjectLighthouse.Controllers {
|
||||||
public class LoginController : ControllerBase {
|
public class LoginController : ControllerBase {
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Login() {
|
public async Task<IActionResult> Login() {
|
||||||
if(!this.Request.Query.TryGetValue("titleID", out StringValues _))
|
|
||||||
return this.BadRequest("");
|
|
||||||
|
|
||||||
string body = await new StreamReader(Request.Body).ReadToEndAsync();
|
string body = await new StreamReader(Request.Body).ReadToEndAsync();
|
||||||
|
|
||||||
LoginData loginData;
|
LoginData loginData;
|
||||||
|
|
|
@ -16,10 +16,11 @@ namespace ProjectLighthouse.Types {
|
||||||
public static string DbConnectionString {
|
public static string DbConnectionString {
|
||||||
get {
|
get {
|
||||||
if(dbConnectionString == null) {
|
if(dbConnectionString == null) {
|
||||||
return dbConnectionString = Environment.GetEnvironmentVariable("LIGHTHOUSE_DB_CONNECTION_STRING") ?? "";
|
return dbConnectionString = Environment.GetEnvironmentVariable("") ?? "";
|
||||||
}
|
}
|
||||||
return dbConnectionString;
|
return dbConnectionString;
|
||||||
}
|
}
|
||||||
|
set => dbConnectionString = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool DbConnected {
|
public static bool DbConnected {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue