mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-05 15:42: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
|
@ -1,66 +0,0 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Settings;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class AuthenticationTests : LighthouseTest
|
||||
{
|
||||
[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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class DatabaseTests : LighthouseTest
|
||||
{
|
||||
[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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using LBPUnion.ProjectLighthouse.Types.Files;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class FileTypeTests
|
||||
{
|
||||
[Fact]
|
||||
public void ShouldRecognizeLevel()
|
||||
{
|
||||
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestLevel.lvl"));
|
||||
Assert.True(file.FileType == LbpFileType.Level);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRecognizeScript()
|
||||
{
|
||||
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestScript.ff"));
|
||||
Assert.True(file.FileType == LbpFileType.Script);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRecognizeTexture()
|
||||
{
|
||||
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestTexture.tex"));
|
||||
Assert.True(file.FileType == LbpFileType.Texture);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRecognizeFileArchive()
|
||||
{
|
||||
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
|
||||
Assert.True(file.FileType == LbpFileType.FileArchive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldNotRecognizeFileArchiveAsScript()
|
||||
{
|
||||
LbpFile file = new(File.ReadAllBytes("ExampleFiles/TestFarc.farc"));
|
||||
Assert.False(file.FileType == LbpFileType.Script);
|
||||
Assert.True(file.FileType == LbpFileType.FileArchive);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRecognizeNothingAsUnknown()
|
||||
{
|
||||
LbpFile file = new(Array.Empty<byte>());
|
||||
Assert.True(file.FileType == LbpFileType.Unknown);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldRecognizeGarbageAsUnknown()
|
||||
{
|
||||
LbpFile file = new(Encoding.ASCII.GetBytes("free pc only $900"));
|
||||
Assert.True(file.FileType == LbpFileType.Unknown);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class MatchTests : LighthouseTest
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using LBPUnion.ProjectLighthouse.Serialization;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class SerializerTests : LighthouseTest
|
||||
{
|
||||
[Fact]
|
||||
public void BlankElementWorks()
|
||||
{
|
||||
Assert.Equal("<test></test>", LbpSerializer.BlankElement("test"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StringElementWorks()
|
||||
{
|
||||
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement("test", "asd"));
|
||||
Assert.Equal("<test>asd</test>", LbpSerializer.StringElement(new KeyValuePair<string, object>("test", "asd")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TaggedStringElementWorks()
|
||||
{
|
||||
Assert.Equal("<test foo=\"bar\">asd</test>", LbpSerializer.TaggedStringElement("test", "asd", "foo", "bar"));
|
||||
Assert.Equal
|
||||
(
|
||||
"<test foo=\"bar\">asd</test>",
|
||||
LbpSerializer.TaggedStringElement(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ElementsWorks()
|
||||
{
|
||||
Assert.Equal
|
||||
(
|
||||
"<test>asd</test><foo>bar</foo>",
|
||||
LbpSerializer.Elements(new KeyValuePair<string, object>("test", "asd"), new KeyValuePair<string, object>("foo", "bar"))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using LBPUnion.ProjectLighthouse.Types.Levels;
|
||||
using LBPUnion.ProjectLighthouse.Types.Profiles;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class SlotTests : LighthouseTest
|
||||
{
|
||||
[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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,58 +0,0 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Tests
|
||||
{
|
||||
public class UploadTests : LighthouseTest
|
||||
{
|
||||
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