Skip to content

Commit 6954d28

Browse files
committed
Add user moderation/photo/matching API tests
1 parent f35cc73 commit 6954d28

6 files changed

Lines changed: 312 additions & 1 deletion

File tree

Refresh.Interfaces.APIv3/Documentation/Descriptions/SharedParamDescriptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Refresh.Interfaces.APIv3.Documentation.Descriptions;
55
/// </summary>
66
public static class SharedParamDescriptions
77
{
8-
public const string UserIdParam = "The UUID or (user)name of the user.";
8+
public const string UserIdParam = "The UUID or username of the user.";
99
public const string UserIdTypeParam = "The type of ID used to specify the user. Can be 'uuid', 'username' or 'name'.";
1010
}

RefreshTests.GameServer/TestContext.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,35 @@ public GameLevel CreateLevel(GameUser author, string title, string description,
142142
return level;
143143
}
144144

145+
public void CreatePhotoWithSubject(GameUser author, string imageHash)
146+
{
147+
// TODO: Return newly created GamePhoto
148+
if (this.Database.GetAssetFromHash(imageHash) == null)
149+
{
150+
this.Database.AddAssetToDatabase(new()
151+
{
152+
AssetHash = imageHash,
153+
});
154+
}
155+
156+
this.Database.UploadPhoto(new()
157+
{
158+
AuthorName = author.Username,
159+
SmallHash = imageHash,
160+
MediumHash = imageHash,
161+
LargeHash = imageHash,
162+
PhotoSubjects =
163+
[
164+
new()
165+
{
166+
Username = author.Username,
167+
DisplayName = author.Username,
168+
BoundsList = "10,10,10,10"
169+
}
170+
]
171+
}, author);
172+
}
173+
145174
public void FillLeaderboard(GameLevel level, int count, byte type)
146175
{
147176
for (byte i = 0; i < count; i++)

RefreshTests.GameServer/Tests/ApiV3/AdminUserEditApiTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ public void MayEditOtherUsersProfile(GameUserRole actorRole)
5555
}
5656
}
5757

58+
[Test]
59+
public void EditsUserByUuidAndName()
60+
{
61+
using TestContext context = this.GetServer();
62+
GameUser mod = context.CreateUser(role: GameUserRole.Moderator);
63+
HttpClient client = context.GetAuthenticatedClient(TokenType.Api, mod);
64+
65+
GameUser player = context.CreateUser(role: GameUserRole.User);
66+
ApiAdminUpdateUserRequest request = new()
67+
{
68+
Description = "poo"
69+
};
70+
ApiResponse<ApiExtendedGameUserResponse>? response = client.PatchData<ApiExtendedGameUserResponse>($"/api/v3/admin/users/uuid/{player.UserId}", request);
71+
Assert.That(response?.Data, Is.Not.Null);
72+
Assert.That(response!.Data!.Description, Is.EqualTo(request.Description));
73+
74+
request = new()
75+
{
76+
Description = "pee"
77+
};
78+
response = client.PatchData<ApiExtendedGameUserResponse>($"/api/v3/admin/users/username/{player.Username}", request);
79+
Assert.That(response?.Data, Is.Not.Null);
80+
Assert.That(response!.Data!.Description, Is.EqualTo(request.Description));
81+
82+
request = new()
83+
{
84+
Description = "lmao"
85+
};
86+
response = client.PatchData<ApiExtendedGameUserResponse>($"/api/v3/admin/users/name/{player.Username}", request);
87+
Assert.That(response?.Data, Is.Not.Null);
88+
Assert.That(response!.Data!.Description, Is.EqualTo(request.Description));
89+
}
90+
5891
[Test]
5992
[TestCase(GameUserRole.Restricted)]
6093
[TestCase(GameUserRole.User)]
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using Refresh.Database.Models.Authentication;
2+
using Refresh.Database.Models.Users;
3+
using RefreshTests.GameServer.Extensions;
4+
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Request.Authentication;
5+
using Refresh.Common.Helpers;
6+
using System.Security.Cryptography;
7+
using Refresh.Interfaces.Game.Types.UserData;
8+
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Response.Admin;
9+
using Refresh.Interfaces.APIv3.Endpoints.ApiTypes;
10+
using Refresh.Common.Extensions;
11+
12+
namespace RefreshTests.GameServer.Tests.ApiV3;
13+
14+
public class AdminUserManagementApiTests : GameServerTest
15+
{
16+
[Test]
17+
public async Task ResetsUsersPasswordByUuidAndName()
18+
{
19+
using TestContext context = this.GetServer();
20+
GameUser mod = context.CreateUser(role: GameUserRole.Moderator);
21+
HttpClient client = context.GetAuthenticatedClient(TokenType.Api, mod);
22+
23+
GameUser player1 = context.CreateUser(role: GameUserRole.User);
24+
ApiResetPasswordRequest request = new()
25+
{
26+
PasswordSha512 = HexHelper.BytesToHexString(SHA512.HashData("poo"u8))
27+
};
28+
HttpResponseMessage response = await client.PutAsync($"/api/v3/admin/users/uuid/{player1.UserId}/resetPassword", new StringContent(request.AsJson()));
29+
Assert.That(response.IsSuccessStatusCode, Is.True);
30+
31+
context.Database.Refresh();
32+
GameUser? updated1 = context.Database.GetUserByObjectId(player1.UserId);
33+
Assert.That(updated1, Is.Not.Null);
34+
Assert.That(updated1!.ShouldResetPassword, Is.True);
35+
36+
GameUser player2 = context.CreateUser(role: GameUserRole.User);
37+
request = new()
38+
{
39+
PasswordSha512 = HexHelper.BytesToHexString(SHA512.HashData("poo"u8))
40+
};
41+
response = await client.PutAsync($"/api/v3/admin/users/uuid/{player2.UserId}/resetPassword", new StringContent(request.AsJson()));
42+
Assert.That(response.IsSuccessStatusCode, Is.True);
43+
44+
context.Database.Refresh();
45+
GameUser? updated2 = context.Database.GetUserByObjectId(player2.UserId);
46+
Assert.That(updated2, Is.Not.Null);
47+
Assert.That(updated2!.ShouldResetPassword, Is.True);
48+
49+
GameUser player3 = context.CreateUser(role: GameUserRole.User);
50+
request = new()
51+
{
52+
PasswordSha512 = HexHelper.BytesToHexString(SHA512.HashData("poo"u8))
53+
};
54+
response = await client.PutAsync($"/api/v3/admin/users/uuid/{player3.UserId}/resetPassword", new StringContent(request.AsJson()));
55+
Assert.That(response.IsSuccessStatusCode, Is.True);
56+
57+
context.Database.Refresh();
58+
GameUser? updated3 = context.Database.GetUserByObjectId(player3.UserId);
59+
Assert.That(updated3, Is.Not.Null);
60+
Assert.That(updated3!.ShouldResetPassword, Is.True);
61+
}
62+
63+
[Test]
64+
public async Task GetsAndResetsUserPlanetsByUuidAndName()
65+
{
66+
using TestContext context = this.GetServer();
67+
GameUser mod = context.CreateUser(role: GameUserRole.Moderator);
68+
HttpClient client = context.GetAuthenticatedClient(TokenType.Api, mod);
69+
GameUser player = context.CreateUser(role: GameUserRole.User);
70+
71+
// UUID
72+
context.Database.UpdateUserData(player, new SerializedUpdateData()
73+
{
74+
PlanetsHash = "lol"
75+
}, TokenGame.LittleBigPlanet2);
76+
77+
ApiResponse<ApiAdminUserPlanetsResponse>? planetResponse = client.GetData<ApiAdminUserPlanetsResponse>($"/api/v3/admin/users/uuid/{player.UserId}/planets");
78+
Assert.That(planetResponse?.Data, Is.Not.Null);
79+
Assert.That(planetResponse!.Data!.Lbp2PlanetsHash, Is.EqualTo("lol"));
80+
81+
HttpResponseMessage resetResponse = await client.DeleteAsync($"/api/v3/admin/users/uuid/{player.UserId}/planets");
82+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
83+
84+
context.Database.Refresh();
85+
86+
GameUser? updated = context.Database.GetUserByObjectId(player.UserId);
87+
Assert.That(updated, Is.Not.Null);
88+
Assert.That(updated!.Lbp2PlanetsHash.IsBlankHash(), Is.True);
89+
90+
// name
91+
context.Database.UpdateUserData(updated, new SerializedUpdateData()
92+
{
93+
PlanetsHash = "lol"
94+
}, TokenGame.LittleBigPlanet3);
95+
96+
context.Database.Refresh();
97+
98+
planetResponse = client.GetData<ApiAdminUserPlanetsResponse>($"/api/v3/admin/users/name/{updated.Username}/planets");
99+
Assert.That(planetResponse?.Data, Is.Not.Null);
100+
Assert.That(planetResponse!.Data!.Lbp3PlanetsHash, Is.EqualTo("lol"));
101+
102+
resetResponse = await client.DeleteAsync($"/api/v3/admin/users/name/{updated.Username}/planets");
103+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
104+
105+
context.Database.Refresh();
106+
107+
updated = context.Database.GetUserByObjectId(updated.UserId);
108+
Assert.That(updated, Is.Not.Null);
109+
Assert.That(updated!.Lbp3PlanetsHash.IsBlankHash(), Is.True);
110+
111+
// username
112+
context.Database.UpdateUserData(updated, new SerializedUpdateData()
113+
{
114+
PlanetsHash = "lol"
115+
}, TokenGame.LittleBigPlanetVita);
116+
117+
context.Database.Refresh();
118+
119+
planetResponse = client.GetData<ApiAdminUserPlanetsResponse>($"/api/v3/admin/users/username/{updated.Username}/planets");
120+
Assert.That(planetResponse?.Data, Is.Not.Null);
121+
Assert.That(planetResponse!.Data!.VitaPlanetsHash, Is.EqualTo("lol"));
122+
123+
resetResponse = await client.DeleteAsync($"/api/v3/admin/users/username/{updated.Username}/planets");
124+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
125+
126+
context.Database.Refresh();
127+
128+
updated = context.Database.GetUserByObjectId(updated.UserId);
129+
Assert.That(updated, Is.Not.Null);
130+
Assert.That(updated!.VitaPlanetsHash.IsBlankHash(), Is.True);
131+
}
132+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Refresh.Database.Models.Authentication;
2+
using Refresh.Database.Models.Users;
3+
using Refresh.Core.Configuration;
4+
using Refresh.Core.Services;
5+
using Refresh.Core.Types.Matching;
6+
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Response.Users.Rooms;
7+
using Refresh.Interfaces.APIv3.Endpoints.ApiTypes;
8+
using RefreshTests.GameServer.Extensions;
9+
10+
namespace RefreshTests.GameServer.Tests.ApiV3;
11+
12+
public class MatchingApiTests : GameServerTest
13+
{
14+
[Test]
15+
public void GetsRoomByUserUuidAndName()
16+
{
17+
using TestContext context = this.GetServer();
18+
GameUser player = context.CreateUser();
19+
GameServerConfig config = context.Server.Value.GameServerConfig;
20+
config.PermitShowingOnlineUsers = true;
21+
22+
MatchService match = context.GetService<MatchService>();
23+
match.Initialize();
24+
GameRoom room = match.CreateRoomByPlayer(player, TokenPlatform.PS3, TokenGame.LittleBigPlanet2, NatType.Open);
25+
26+
ApiResponse<ApiGameRoomResponse>? response = context.Http.GetData<ApiGameRoomResponse>($"/api/v3/rooms/uuid/{player.UserId}");
27+
Assert.That(response?.Data, Is.Not.Null);
28+
Assert.That(response!.Data!.RoomId.ToString(), Is.EqualTo(room.RoomId.ToString()));
29+
30+
response = context.Http.GetData<ApiGameRoomResponse>($"/api/v3/rooms/username/{player.Username}");
31+
Assert.That(response?.Data, Is.Not.Null);
32+
Assert.That(response!.Data!.RoomId.ToString(), Is.EqualTo(room.RoomId.ToString()));
33+
34+
response = context.Http.GetData<ApiGameRoomResponse>($"/api/v3/rooms/name/{player.Username}");
35+
Assert.That(response?.Data, Is.Not.Null);
36+
Assert.That(response!.Data!.RoomId.ToString(), Is.EqualTo(room.RoomId.ToString()));
37+
}
38+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using Refresh.Database.Models.Users;
2+
using Refresh.Interfaces.APIv3.Endpoints.ApiTypes;
3+
using RefreshTests.GameServer.Extensions;
4+
using Refresh.Interfaces.APIv3.Endpoints.DataTypes.Response.Users.Photos;
5+
using Refresh.Database.Models.Authentication;
6+
7+
namespace RefreshTests.GameServer.Tests.ApiV3;
8+
9+
public class PhotoApiTests : GameServerTest
10+
{
11+
private const string TEST_IMAGE_HASH = "0ec63b140374ba704a58fa0c743cb357683313dd";
12+
13+
[Test]
14+
public void GetsPhotosByUserUuidAndName()
15+
{
16+
using TestContext context = this.GetServer();
17+
GameUser player = context.CreateUser();
18+
context.CreatePhotoWithSubject(player, TEST_IMAGE_HASH);
19+
20+
ApiListResponse<ApiGamePhotoResponse>? response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/by/uuid/{player.UserId}");
21+
Assert.That(response?.Data, Is.Not.Null);
22+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
23+
24+
response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/by/username/{player.Username}");
25+
Assert.That(response?.Data, Is.Not.Null);
26+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
27+
28+
response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/by/name/{player.Username}");
29+
Assert.That(response?.Data, Is.Not.Null);
30+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
31+
}
32+
33+
[Test]
34+
public void GetsPhotosWithUserUuidAndName()
35+
{
36+
using TestContext context = this.GetServer();
37+
GameUser player = context.CreateUser();
38+
context.CreatePhotoWithSubject(player, TEST_IMAGE_HASH);
39+
40+
ApiListResponse<ApiGamePhotoResponse>? response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/with/uuid/{player.UserId}");
41+
Assert.That(response?.Data, Is.Not.Null);
42+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
43+
44+
response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/with/username/{player.Username}");
45+
Assert.That(response?.Data, Is.Not.Null);
46+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
47+
48+
response = context.Http.GetList<ApiGamePhotoResponse>($"/api/v3/photos/with/name/{player.Username}");
49+
Assert.That(response?.Data, Is.Not.Null);
50+
Assert.That(response!.Data!.Count, Is.EqualTo(1));
51+
}
52+
53+
[Test]
54+
public async Task DeletesAllPhotosByUserUuidAndName()
55+
{
56+
using TestContext context = this.GetServer();
57+
GameUser player = context.CreateUser(role: GameUserRole.User);
58+
GameUser mod = context.CreateUser(role: GameUserRole.Moderator);
59+
HttpClient client = context.GetAuthenticatedClient(TokenType.Api, mod);
60+
61+
// UUID
62+
context.CreatePhotoWithSubject(player, TEST_IMAGE_HASH);
63+
HttpResponseMessage resetResponse = await client.DeleteAsync($"/api/v3/admin/users/uuid/{player.UserId}/photos");
64+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
65+
Assert.That(context.Database.GetTotalPhotosByUser(player), Is.Zero);
66+
67+
// name
68+
context.CreatePhotoWithSubject(player, TEST_IMAGE_HASH);
69+
resetResponse = await client.DeleteAsync($"/api/v3/admin/users/name/{player.Username}/photos");
70+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
71+
Assert.That(context.Database.GetTotalPhotosByUser(player), Is.Zero);
72+
73+
// username
74+
context.CreatePhotoWithSubject(player, TEST_IMAGE_HASH);
75+
resetResponse = await client.DeleteAsync($"/api/v3/admin/users/username/{player.Username}/photos");
76+
Assert.That(resetResponse.IsSuccessStatusCode, Is.True);
77+
Assert.That(context.Database.GetTotalPhotosByUser(player), Is.Zero);
78+
}
79+
}

0 commit comments

Comments
 (0)