Fix authentication (mostly) and file upload tests failing

This commit is contained in:
jvyden 2021-12-20 01:05:23 -05:00
parent 8289e6c04e
commit 0d682dceeb
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 25 additions and 13 deletions

View file

@ -4,9 +4,11 @@ using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests
{
public sealed class DatabaseFact : FactAttribute
public sealed class DatabaseFactAttribute : FactAttribute
{
public DatabaseFact()
private static readonly object migrateLock = new();
public DatabaseFactAttribute()
{
ServerSettings.Instance = new ServerSettings();
ServerSettings.Instance.DbConnectionString = "server=127.0.0.1;uid=root;pwd=lighthouse;database=lighthouse";
@ -15,10 +17,13 @@ namespace LBPUnion.ProjectLighthouse.Tests
this.Skip = "Database not available";
}
else
{
lock(migrateLock)
{
using Database database = new();
database.Database.Migrate();
}
}
}
}
}

View file

@ -1,7 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using LBPUnion.ProjectLighthouse.Helpers;
@ -25,9 +25,13 @@ namespace LBPUnion.ProjectLighthouse.Tests
this.Client = this.Server.CreateClient();
}
public async Task<HttpResponseMessage> AuthenticateResponse(int number = 0, bool createUser = true)
public async Task<HttpResponseMessage> AuthenticateResponse(int number = -1, bool createUser = true)
{
if (number == -1)
{
number = new Random().Next();
}
const string username = "unitTestUser";
if (createUser)
{
@ -65,8 +69,8 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task<HttpResponseMessage> UploadFileEndpointRequest(string filePath)
{
byte[] bytes = Encoding.UTF8.GetBytes(await File.ReadAllTextAsync(filePath));
string hash = HashHelper.Sha1Hash(bytes);
byte[] bytes = await File.ReadAllBytesAsync(filePath);
string hash = HashHelper.Sha1Hash(bytes).ToLower();
return await this.Client.PostAsync($"/LITTLEBIGPLANETPS3_XML/upload/{hash}", new ByteArrayContent(bytes));
}

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
@ -18,6 +19,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task ShouldNotAcceptScript()
{
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestScript.ff");
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
Assert.False(response.IsSuccessStatusCode);
}
@ -25,6 +27,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task ShouldNotAcceptFarc()
{
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestFarc.farc");
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
Assert.False(response.IsSuccessStatusCode);
}
@ -32,6 +35,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task ShouldNotAcceptGarbage()
{
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestGarbage.bin");
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
Assert.False(response.IsSuccessStatusCode);
}
@ -39,6 +43,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task ShouldAcceptTexture()
{
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestTexture.tex");
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
Assert.True(response.IsSuccessStatusCode);
}
@ -46,6 +51,7 @@ namespace LBPUnion.ProjectLighthouse.Tests
public async Task ShouldAcceptLevel()
{
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestLevel.lvl");
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
Assert.True(response.IsSuccessStatusCode);
}
}

View file

@ -73,6 +73,7 @@ namespace LBPUnion.ProjectLighthouse
GameToken gameToken = new()
{
UserToken = HashHelper.GenerateAuthToken(),
User = user,
UserId = user.UserId,
UserLocation = userLocation,
GameVersion = GameVersionHelper.FromTitleId(titleId),

View file

@ -33,10 +33,6 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<_ContentIncludedByDefault Remove="Pages\Admin\Index.cshtml"/>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;"/>
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;"/>