mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-02 14:12:27 +00:00
Split normal tests from game api tests
This commit is contained in:
parent
68eb6aeb3e
commit
1fbabe0000
11 changed files with 74 additions and 14 deletions
67
ProjectLighthouse.Tests.GameApiTests/AuthenticationTests.cs
Normal file
67
ProjectLighthouse.Tests.GameApiTests/AuthenticationTests.cs
Normal file
|
@ -0,0 +1,67 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests
|
||||
{
|
||||
public class AuthenticationTests : LighthouseServerTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task ShouldReturnErrorOnNoPostData()
|
||||
{
|
||||
HttpResponseMessage 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 ShouldReturnWithValidData()
|
||||
{
|
||||
HttpResponseMessage response = await this.AuthenticateResponse();
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
string responseContent = await response.Content.ReadAsStringAsync();
|
||||
Assert.Contains("MM_AUTH=", responseContent);
|
||||
Assert.Contains(ServerStatics.ServerName, responseContent);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task CanSerializeBack()
|
||||
{
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
Assert.NotNull(loginResult);
|
||||
Assert.NotNull(loginResult.AuthTicket);
|
||||
Assert.NotNull(loginResult.LbpEnvVer);
|
||||
|
||||
Assert.Contains("MM_AUTH=", loginResult.AuthTicket);
|
||||
Assert.Equal(ServerStatics.ServerName, loginResult.LbpEnvVer);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task CanUseToken()
|
||||
{
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage response = await this.AuthenticatedRequest("/LITTLEBIGPLANETPS3_XML/enterLevel/1", loginResult.AuthTicket);
|
||||
string responseContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldReturnForbiddenWhenNotAuthenticated()
|
||||
{
|
||||
HttpResponseMessage response = await this.Client.GetAsync("/LITTLEBIGPLANETPS3_XML/announce");
|
||||
Assert.False(response.IsSuccessStatusCode);
|
||||
Assert.True(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
}
|
||||
}
|
||||
}
|
30
ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs
Normal file
30
ProjectLighthouse.Tests.GameApiTests/DatabaseTests.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests
|
||||
{
|
||||
public class DatabaseTests : LighthouseServerTest
|
||||
{
|
||||
[DatabaseFact]
|
||||
public async Task CanCreateUserTwice()
|
||||
{
|
||||
await using Database database = new();
|
||||
int rand = new Random().Next();
|
||||
|
||||
User userA = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken());
|
||||
User userB = await database.CreateUser("createUserTwiceTest" + rand, HashHelper.GenerateAuthToken());
|
||||
|
||||
Assert.NotNull(userA);
|
||||
Assert.NotNull(userB);
|
||||
|
||||
await database.RemoveUser(userA); // Only remove userA since userA and userB are the same user
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
62
ProjectLighthouse.Tests.GameApiTests/MatchTests.cs
Normal file
62
ProjectLighthouse.Tests.GameApiTests/MatchTests.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests
|
||||
{
|
||||
public class MatchTests : LighthouseServerTest
|
||||
{
|
||||
private static readonly SemaphoreSlim semaphore = new(1, 1);
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldRejectEmptyData()
|
||||
{
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest("LITTLEBIGPLANETPS3_XML/match", Array.Empty<byte>(), loginResult.AuthTicket);
|
||||
|
||||
semaphore.Release();
|
||||
Assert.False(result.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldReturnOk()
|
||||
{
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest
|
||||
("LITTLEBIGPLANETPS3_XML/match", Encoding.ASCII.GetBytes("[UpdateMyPlayerData,[\"Player\":\"1984\"]]"), loginResult.AuthTicket);
|
||||
|
||||
semaphore.Release();
|
||||
Assert.True(result.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[DatabaseFact]
|
||||
public async Task ShouldIncrementPlayerCount()
|
||||
{
|
||||
LoginResult loginResult = await this.Authenticate(new Random().Next());
|
||||
|
||||
await semaphore.WaitAsync();
|
||||
|
||||
int oldPlayerCount = await StatisticsHelper.RecentMatches();
|
||||
|
||||
HttpResponseMessage result = await this.AuthenticatedUploadDataRequest
|
||||
("LITTLEBIGPLANETPS3_XML/match", Encoding.ASCII.GetBytes("[UpdateMyPlayerData,[\"Player\":\"1984\"]]"), loginResult.AuthTicket);
|
||||
|
||||
Assert.True(result.IsSuccessStatusCode);
|
||||
|
||||
int playerCount = await StatisticsHelper.RecentMatches();
|
||||
|
||||
semaphore.Release();
|
||||
Assert.Equal(oldPlayerCount + 1, playerCount);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0"/>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0"/>
|
||||
<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.1.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ProjectLighthouse.Tests\ProjectLighthouse.Tests.csproj"/>
|
||||
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="..\ProjectLighthouse.Tests\ExampleFiles\**">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<ProjectReference Include="..\ProjectLighthouse\ProjectLighthouse.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
93
ProjectLighthouse.Tests.GameApiTests/SlotTests.cs
Normal file
93
ProjectLighthouse.Tests.GameApiTests/SlotTests.cs
Normal file
|
@ -0,0 +1,93 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests
|
||||
{
|
||||
public class SlotTests : LighthouseServerTest
|
||||
{
|
||||
[DatabaseFact]
|
||||
public async Task ShouldOnlyShowUsersLevels()
|
||||
{
|
||||
await using Database database = new();
|
||||
|
||||
Random r = new();
|
||||
|
||||
User userA = await database.CreateUser($"unitTestUser{r.Next()}", HashHelper.GenerateAuthToken());
|
||||
User userB = await database.CreateUser($"unitTestUser{r.Next()}", HashHelper.GenerateAuthToken());
|
||||
|
||||
Location l = new()
|
||||
{
|
||||
X = 0,
|
||||
Y = 0,
|
||||
};
|
||||
database.Locations.Add(l);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
Slot slotA = new()
|
||||
{
|
||||
Creator = userA,
|
||||
CreatorId = userA.UserId,
|
||||
Name = "slotA",
|
||||
Location = l,
|
||||
LocationId = l.Id,
|
||||
ResourceCollection = "",
|
||||
};
|
||||
|
||||
Slot slotB = new()
|
||||
{
|
||||
Creator = userB,
|
||||
CreatorId = userB.UserId,
|
||||
Name = "slotB",
|
||||
Location = l,
|
||||
LocationId = l.Id,
|
||||
ResourceCollection = "",
|
||||
};
|
||||
|
||||
database.Slots.Add(slotA);
|
||||
database.Slots.Add(slotB);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
// XmlSerializer serializer = new(typeof(Slot));
|
||||
// Slot slot = (Slot)serializer.Deserialize(new StringReader(bodyString));
|
||||
|
||||
LoginResult loginResult = await this.Authenticate();
|
||||
|
||||
HttpResponseMessage respMessageA = await this.AuthenticatedRequest
|
||||
($"LITTLEBIGPLANETPS3_XML/slots/by?u={userA.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket);
|
||||
HttpResponseMessage respMessageB = await this.AuthenticatedRequest
|
||||
($"LITTLEBIGPLANETPS3_XML/slots/by?u={userB.Username}&pageStart=1&pageSize=1", loginResult.AuthTicket);
|
||||
|
||||
Assert.True(respMessageA.IsSuccessStatusCode);
|
||||
Assert.True(respMessageB.IsSuccessStatusCode);
|
||||
|
||||
string respA = await respMessageA.Content.ReadAsStringAsync();
|
||||
string respB = await respMessageB.Content.ReadAsStringAsync();
|
||||
|
||||
Assert.False(string.IsNullOrEmpty(respA));
|
||||
Assert.False(string.IsNullOrEmpty(respB));
|
||||
|
||||
Assert.NotEqual(respA, respB);
|
||||
Assert.DoesNotContain(respA, "slotB");
|
||||
Assert.DoesNotContain(respB, "slotA");
|
||||
|
||||
// Cleanup
|
||||
|
||||
database.Slots.Remove(slotA);
|
||||
database.Slots.Remove(slotB);
|
||||
|
||||
await database.RemoveUser(userA);
|
||||
await database.RemoveUser(userB);
|
||||
|
||||
await database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
59
ProjectLighthouse.Tests.GameApiTests/UploadTests.cs
Normal file
59
ProjectLighthouse.Tests.GameApiTests/UploadTests.cs
Normal file
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Tests;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests
|
||||
{
|
||||
public class UploadTests : LighthouseServerTest
|
||||
{
|
||||
public UploadTests()
|
||||
{
|
||||
string assetsDirectory = Path.Combine(Environment.CurrentDirectory, "r");
|
||||
if (Directory.Exists(assetsDirectory)) Directory.Delete(assetsDirectory, true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptScript()
|
||||
{
|
||||
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestScript.ff");
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
Assert.False(response.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptFarc()
|
||||
{
|
||||
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestFarc.farc");
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
Assert.False(response.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldNotAcceptGarbage()
|
||||
{
|
||||
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestGarbage.bin");
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
Assert.False(response.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldAcceptTexture()
|
||||
{
|
||||
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestTexture.tex");
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ShouldAcceptLevel()
|
||||
{
|
||||
HttpResponseMessage response = await this.UploadFileEndpointRequest("ExampleFiles/TestLevel.lvl");
|
||||
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
|
||||
Assert.True(response.IsSuccessStatusCode);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue