diff --git a/ProjectLighthouse.Tests/DatabaseFact.cs b/ProjectLighthouse.Tests/DatabaseFact.cs new file mode 100644 index 00000000..ecb8216f --- /dev/null +++ b/ProjectLighthouse.Tests/DatabaseFact.cs @@ -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"; + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse.Tests/LighthouseTest.cs b/ProjectLighthouse.Tests/LighthouseTest.cs new file mode 100644 index 00000000..0cacb69c --- /dev/null +++ b/ProjectLighthouse.Tests/LighthouseTest.cs @@ -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()); + this.Client = this.Server.CreateClient(); + + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj new file mode 100644 index 00000000..cd72611b --- /dev/null +++ b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj @@ -0,0 +1,29 @@ + + + + enable + + false + + net5.0;net6.0 + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj.DotSettings b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj.DotSettings new file mode 100644 index 00000000..eccc8454 --- /dev/null +++ b/ProjectLighthouse.Tests/ProjectLighthouse.Tests.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/ProjectLighthouse.Tests/Tests/AuthenticationTest.cs b/ProjectLighthouse.Tests/Tests/AuthenticationTest.cs new file mode 100644 index 00000000..e06ee7b3 --- /dev/null +++ b/ProjectLighthouse.Tests/Tests/AuthenticationTest.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse.sln b/ProjectLighthouse.sln index 12322461..96db324b 100644 --- a/ProjectLighthouse.sln +++ b/ProjectLighthouse.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse", "ProjectLighthouse\ProjectLighthouse.csproj", "{C6CFD4AD-47ED-4C86-B0C4-A4216D82E0DC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProjectLighthouse.Tests", "ProjectLighthouse.Tests\ProjectLighthouse.Tests.csproj", "{AFC74569-B289-4ACC-B21C-313A3A62C017}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.Release|Any CPU.ActiveCfg = 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 EndGlobal diff --git a/ProjectLighthouse/Controllers/LoginController.cs b/ProjectLighthouse/Controllers/LoginController.cs index 5b137aa4..dfb6a0fa 100644 --- a/ProjectLighthouse/Controllers/LoginController.cs +++ b/ProjectLighthouse/Controllers/LoginController.cs @@ -14,9 +14,6 @@ namespace ProjectLighthouse.Controllers { public class LoginController : ControllerBase { [HttpPost] public async Task Login() { - if(!this.Request.Query.TryGetValue("titleID", out StringValues _)) - return this.BadRequest(""); - string body = await new StreamReader(Request.Body).ReadToEndAsync(); LoginData loginData; diff --git a/ProjectLighthouse/Types/ServerSettings.cs b/ProjectLighthouse/Types/ServerSettings.cs index 39fc79d9..bde98a9c 100644 --- a/ProjectLighthouse/Types/ServerSettings.cs +++ b/ProjectLighthouse/Types/ServerSettings.cs @@ -16,10 +16,11 @@ namespace ProjectLighthouse.Types { public static string DbConnectionString { get { if(dbConnectionString == null) { - return dbConnectionString = Environment.GetEnvironmentVariable("LIGHTHOUSE_DB_CONNECTION_STRING") ?? ""; + return dbConnectionString = Environment.GetEnvironmentVariable("") ?? ""; } return dbConnectionString; } + set => dbConnectionString = value; } public static bool DbConnected {