Fix all tests

This commit is contained in:
jvyden 2021-11-22 17:40:25 -05:00
commit 84669201ab
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
5 changed files with 25 additions and 7 deletions

View file

@ -9,6 +9,7 @@ using LBPUnion.ProjectLighthouse.Serialization;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Tests
{
@ -25,9 +26,15 @@ namespace LBPUnion.ProjectLighthouse.Tests
this.Client = this.Server.CreateClient();
}
public async Task<HttpResponseMessage> AuthenticateResponse(int number = 0)
public async Task<HttpResponseMessage> AuthenticateResponse(int number = 0, bool createUser = true)
{
const string username = "unitTestUser";
if (createUser)
{
await using Database database = new();
if (await database.Users.FirstOrDefaultAsync(u => u.Username == $"{username}{number}") == null)
await database.CreateUser($"{username}{number}", HashHelper.BCryptHash($"unitTestPassword{number}"));
}
string stringContent = $"{LoginData.UsernamePrefix}{username}{number}{(char)0x00}";

View file

@ -49,10 +49,10 @@ namespace LBPUnion.ProjectLighthouse.Tests
{
LoginResult loginResult = await this.Authenticate();
HttpResponseMessage response = await this.AuthenticatedRequest("/LITTLEBIGPLANETPS3_XML/announce", loginResult.AuthTicket);
HttpResponseMessage response = await this.AuthenticatedRequest("/LITTLEBIGPLANETPS3_XML/enterLevel/1", loginResult.AuthTicket);
string responseContent = await response.Content.ReadAsStringAsync();
Assert.True(response.IsSuccessStatusCode);
Assert.False(response.StatusCode == HttpStatusCode.Forbidden);
}
[DatabaseFact]

View file

@ -3,6 +3,7 @@ using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Types;
using Xunit;
@ -36,7 +37,6 @@ namespace LBPUnion.ProjectLighthouse.Tests
semaphore.Release();
Assert.True(result.IsSuccessStatusCode);
}
public async Task<int> GetPlayerCount() => Convert.ToInt32(await this.Client.GetStringAsync("LITTLEBIGPLANETPS3_XML/totalPlayerCount"));
[DatabaseFact]
public async Task ShouldIncrementPlayerCount()
@ -45,14 +45,14 @@ namespace LBPUnion.ProjectLighthouse.Tests
await semaphore.WaitAsync();
int oldPlayerCount = await this.GetPlayerCount();
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 this.GetPlayerCount();
int playerCount = await StatisticsHelper.RecentMatches();
semaphore.Release();
Assert.Equal(oldPlayerCount + 1, playerCount);