From 67d0d3d7b149eb0fecd1db0f3e1e098c9c95ae0f Mon Sep 17 00:00:00 2001 From: sudokoko Date: Fri, 5 Apr 2024 09:54:03 -0400 Subject: [PATCH] Prevent duplicate profile pins --- .../Controllers/UserController.cs | 2 +- .../Unit/Controllers/UserControllerTests.cs | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs b/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs index cbed989be..7019cdd76 100644 --- a/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs +++ b/ProjectLighthouse.Servers.GameServer/Controllers/UserController.cs @@ -180,7 +180,7 @@ public async Task UpdateMyPins() // Sometimes the update gets called periodically as pin progress updates via playing, // may not affect equipped profile pins however, so check before setting it. string currentPins = user.Pins; - string newPins = string.Join(",", pinJson.ProfilePins); + string newPins = string.Join(",", pinJson.ProfilePins.Distinct()); if (string.Equals(currentPins, newPins)) return this.Ok("[{\"StatusCode\":200}]"); diff --git a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/UserControllerTests.cs b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/UserControllerTests.cs index e7228b2cf..3347db1e6 100644 --- a/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/UserControllerTests.cs +++ b/ProjectLighthouse.Tests.GameApiTests/Unit/Controllers/UserControllerTests.cs @@ -177,4 +177,29 @@ public async Task UpdateMyPins_ShouldNotSave_WhenPinsAreEqual() Assert.Equal(expectedPins, dbMock.Users.First().Pins); Assert.Equal(expectedResponse, pinsResponse); } + + [Fact] + public async Task UpdateMyPins_ShouldRemove_DuplicatePins() + { + UserEntity entity = MockHelper.GetUnitTestUser(); + entity.Pins = "1234"; + List users = new() + { + entity, + }; + await using DatabaseContext dbMock = await MockHelper.GetTestDatabase(users); + + UserController userController = new(dbMock); + userController.SetupTestController("{\"profile_pins\": [1234, 1234]}"); + + const string expectedPins = "1234"; + const string expectedResponse = "[{\"StatusCode\":200}]"; + + IActionResult result = await userController.UpdateMyPins(); + + string pinsResponse = result.CastTo(); + + Assert.Equal(expectedPins, dbMock.Users.First().Pins); + Assert.Equal(expectedResponse, pinsResponse); + } } \ No newline at end of file