mirror of
https://github.com/CyberL1/Minecraft-Realms-Emulator.git
synced 2024-11-21 05:38:21 -05:00
create unit tests
This commit is contained in:
parent
c2f62fdffb
commit
92a9177d1f
@ -5,7 +5,9 @@ VisualStudioVersion = 17.8.34511.84
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Minecraft-Realms-Emulator", "Minecraft-Realms-Emulator\Minecraft-Realms-Emulator.csproj", "{A9A21AAF-EF1B-4723-9407-51FD6B831B98}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Panel", "Panel\Panel.csproj", "{DDDF0FE0-736D-47D5-8571-3DD7354B7917}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Panel", "Panel\Panel.csproj", "{DDDF0FE0-736D-47D5-8571-3DD7354B7917}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{AFF83C8C-B6CA-4707-A26E-82002822F820}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -21,6 +23,10 @@ Global
|
||||
{DDDF0FE0-736D-47D5-8571-3DD7354B7917}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DDDF0FE0-736D-47D5-8571-3DD7354B7917}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DDDF0FE0-736D-47D5-8571-3DD7354B7917}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AFF83C8C-B6CA-4707-A26E-82002822F820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AFF83C8C-B6CA-4707-A26E-82002822F820}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AFF83C8C-B6CA-4707-A26E-82002822F820}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AFF83C8C-B6CA-4707-A26E-82002822F820}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -25,4 +25,8 @@
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="UnitTests" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
31
UnitTests/UnitTests.csproj
Normal file
31
UnitTests/UnitTests.csproj
Normal file
@ -0,0 +1,31 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.10" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Minecraft-Realms-Emulator\Minecraft-Realms-Emulator.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
220
UnitTests/WorldsControllerTests.cs
Normal file
220
UnitTests/WorldsControllerTests.cs
Normal file
@ -0,0 +1,220 @@
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Minecraft_Realms_Emulator.Entities;
|
||||
using Minecraft_Realms_Emulator.Requests;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
[TestClass]
|
||||
public class WorldsControllerTests
|
||||
{
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("CONNECTION_STRING", "User Id=postgres;Password=postgres;Server=localhost;Port=5432;Database=Minecraft-Realms-Emulator;");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorlds_Always_ReturnsPlayerWorlds()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds");
|
||||
|
||||
Assert.IsTrue(response.IsSuccessStatusCode);
|
||||
Assert.IsNotNull(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorldsSnapshot_Always_ReturnsPlayerWorlds()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds/listUserWorldsOfType/any");
|
||||
|
||||
Assert.IsTrue(response.IsSuccessStatusCode);
|
||||
Assert.IsNotNull(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetPrereleaseWorlds_Always_ReturnsPrereleaseEligibleWorlds()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
client.DefaultRequestHeaders.Add("Is-Prerelease", "true");
|
||||
|
||||
var response = await client.GetAsync("worlds/listPrereleaseEligibleWorlds");
|
||||
|
||||
Assert.IsTrue(response.IsSuccessStatusCode);
|
||||
Assert.IsNotNull(await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorldById_NotInitialized_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds/1");
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "{\"errorCode\":400,\"errorMsg\":\"Initialize the world first\"}";
|
||||
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Initialize_NotInitializedAndOwner_ReturnsWorld()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
WorldCreateRequest request = new()
|
||||
{
|
||||
Name = "Testing",
|
||||
Description = "Created by unit tests"
|
||||
};
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("worlds/1/initialize", content);
|
||||
|
||||
Assert.IsTrue(response.IsSuccessStatusCode);
|
||||
|
||||
var jsonResponse = JsonConvert.DeserializeObject<WorldResponse>(await response.Content.ReadAsStringAsync());
|
||||
|
||||
string expectedResponse = $"{{\"id\":1,\"subscription\":{{\"id\":1,\"worldId\":1,\"world\":null,\"startDate\":\"{jsonResponse.Subscription.StartDate.ToString("O")}\",\"subscriptionType\":\"NORMAL\"}},\"owner\":\"testPlayer\",\"ownerUUID\":\"069a79f444e94726a5befca90e38aaf5\",\"name\":\"Testing\",\"motd\":\"Created by unit tests\",\"state\":\"OPEN\",\"worldType\":\"NORMAL\",\"players\":[],\"maxPlayers\":10,\"minigame\":null,\"activeSlot\":1,\"slots\":[{{\"id\":1,\"world\":null,\"slotId\":1,\"slotName\":\"\",\"version\":\"1.21.1\",\"gameMode\":0,\"difficulty\":2,\"spawnProtection\":0,\"forceGameMode\":false,\"pvp\":true,\"spawnAnimals\":true,\"spawnMonsters\":true,\"spawnNPCs\":true,\"commandBlocks\":false}}],\"member\":false,\"parentWorld\":null}}";
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorldById_IsInitializedAndOwner_ReturnsWorld()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds/1");
|
||||
Assert.IsTrue(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "{\"remoteSubscriptionId\":\"00000000-0000-0000-0000-000000000000\",\"daysLeft\":30,\"expired\":false,\"expiredTrial\":false,\"compatibility\":\"COMPATIBLE\",\"slots\":[{\"slotId\":1,\"options\":\"{\\\"slotName\\\":\\\"\\\",\\\"gameMode\\\":0,\\\"difficulty\\\":2,\\\"spawnProtection\\\":0,\\\"forceGameMode\\\":false,\\\"pvp\\\":true,\\\"spawnAnimals\\\":true,\\\"spawnMonsters\\\":true,\\\"spawnNPCs\\\":true,\\\"commandBlocks\\\":false,\\\"version\\\":\\\"1.21.1\\\",\\\"compatibility\\\":\\\"COMPATIBLE\\\"}\"}],\"activeVersion\":\"1.21.1\",\"parentWorldId\":null,\"parentWorldName\":null,\"minigameId\":null,\"minigameName\":null,\"minigameImage\":null,\"id\":1,\"subscription\":null,\"owner\":\"testPlayer\",\"ownerUUID\":\"069a79f444e94726a5befca90e38aaf5\",\"name\":\"Testing\",\"motd\":\"Created by unit tests\",\"state\":\"OPEN\",\"worldType\":\"NORMAL\",\"players\":[],\"maxPlayers\":10,\"minigame\":null,\"activeSlot\":1,\"member\":false,\"parentWorld\":null}";
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorldById_isInitializedAndNotOwner_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:d33b406631524e31805528e304f7401f;user=testPlayer2;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds/1");
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "You don't own this world";
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetWorldById_NotFound_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
var response = await client.GetAsync("worlds/2");
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "{\"errorCode\":404,\"errorMsg\":\"World not found\"}";
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Initialize_NotFound_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
WorldCreateRequest request = new()
|
||||
{
|
||||
Name = "Testing",
|
||||
Description = "Created by unit tests"
|
||||
};
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("worlds/2/initialize", content);
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "{\"errorCode\":404,\"errorMsg\":\"World not found\"}";
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task Initialize_AlreadyInitialized_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:069a79f444e94726a5befca90e38aaf5;user=testPlayer;version=1.21.1");
|
||||
|
||||
WorldCreateRequest request = new()
|
||||
{
|
||||
Name = "Testing",
|
||||
Description = "Created by unit tests"
|
||||
};
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("worlds/1/initialize", content);
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "{\"errorCode\":401,\"errorMsg\":\"World already initialized\"}";
|
||||
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
[TestMethod]
|
||||
public async Task Initialize_NotOwner_ReturnsError()
|
||||
{
|
||||
var factory = new WebApplicationFactory<Program>();
|
||||
var client = factory.CreateClient();
|
||||
|
||||
client.DefaultRequestHeaders.Add("Cookie", "sid=token:fakeToken:d33b406631524e31805528e304f7401f;user=testPlayer2;version=1.21.1");
|
||||
|
||||
WorldCreateRequest request = new()
|
||||
{
|
||||
Name = "Testing",
|
||||
Description = "Created by unit tests"
|
||||
};
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync("worlds/1/initialize", content);
|
||||
|
||||
Assert.IsFalse(response.IsSuccessStatusCode);
|
||||
|
||||
string expectedResponse = "You don't own this world";
|
||||
|
||||
Assert.AreEqual(expectedResponse, await response.Content.ReadAsStringAsync());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user