From f1a0ce454c5a72f76aabe326e51152c9608e4df6 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Tue, 31 Dec 2024 15:14:43 +0100 Subject: [PATCH 1/7] [Backend]Test player creation/update --- .../java/io/openbas/rest/PlayerApiTest.java | 166 ++++++++++++++++++ .../utils/fixtures/OrganizationFixture.java | 13 ++ .../openbas/utils/fixtures/PlayerFixture.java | 17 ++ 3 files changed, 196 insertions(+) create mode 100644 openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java create mode 100644 openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java create mode 100644 openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java new file mode 100644 index 0000000000..9f5e8914de --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -0,0 +1,166 @@ +package io.openbas.rest; + +import static io.openbas.rest.user.PlayerApi.PLAYER_URI; +import static io.openbas.utils.JsonUtils.asJsonString; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import com.jayway.jsonpath.JsonPath; +import io.openbas.database.model.Organization; +import io.openbas.database.model.Tag; +import io.openbas.database.model.User; +import io.openbas.database.repository.OrganizationRepository; +import io.openbas.database.repository.TagRepository; +import io.openbas.database.repository.UserRepository; +import io.openbas.rest.user.form.player.PlayerInput; +import io.openbas.utils.fixtures.OrganizationFixture; +import io.openbas.utils.fixtures.PlayerFixture; +import io.openbas.utils.fixtures.TagFixture; +import io.openbas.utils.mockUser.WithMockAdminUser; +import io.openbas.utils.mockUser.WithMockPlannerUser; +import jakarta.servlet.ServletException; +import java.util.List; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +@TestInstance(PER_CLASS) +public class PlayerApiTest { + + static User PLAYER; + static PlayerInput PLAYER_INPUT; + static Tag TAG; + static Organization ORGANIZATION; + + @Autowired private MockMvc mvc; + + @Value("${openbas.admin.email:#{null}}") + private String adminEmail; + + @Autowired private OrganizationRepository organizationRepository; + + @Autowired private TagRepository tagRepository; + @Autowired private UserRepository userRepository; + + @BeforeAll + void beforeAll() { + ORGANIZATION = organizationRepository.save(OrganizationFixture.createOrganization()); + TAG = tagRepository.save(TagFixture.getTag()); + PLAYER_INPUT = PlayerFixture.createPlayer(); + PLAYER_INPUT.setOrganizationId(ORGANIZATION.getId()); + PLAYER_INPUT.setTagIds(List.of(TAG.getId())); + } + + @AfterAll + void afterAll() { + organizationRepository.delete(ORGANIZATION); + tagRepository.delete(TAG); + } + + @DisplayName("Creation of a player") + @Test + @WithMockAdminUser + void createPlayerTest() throws Exception { + // --EXECUTE-- + String response = + mvc.perform( + post(PLAYER_URI) + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // --ASSERT-- + assertEquals("Firstname", JsonPath.read(response, "$.user_firstname")); + + // --THEN-- + userRepository.deleteById(JsonPath.read(response, "$.user_id")); + } + + @DisplayName("Creation of a player with a simple user") + @Test + @WithMockPlannerUser + void createPlayerWithRestrictedUserTest() throws Exception { + // --EXECUTE-- + Exception exception = + assertThrows( + ServletException.class, + () -> + mvc.perform( + post(PLAYER_URI) + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON))); + + String expectedMessage = "User is restricted"; + String actualMessage = exception.getMessage(); + + // --ASSERT-- + assertTrue(actualMessage.contains(expectedMessage)); + } + + @DisplayName("Edition of a player") + @Test + @WithMockAdminUser + void updatePlayerTest() throws Exception { + // --PREPARE-- + User user = new User(); + user.setUpdateAttributes(PLAYER_INPUT); + PLAYER = userRepository.save(user); + String newFirstname = "updatedFirstname"; + PLAYER_INPUT.setFirstname(newFirstname); + + // --EXECUTE-- + String response = + mvc.perform( + put(PLAYER_URI + "/" + PLAYER.getId()) + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // --ASSERT-- + assertEquals("updatedFirstname", JsonPath.read(response, "$.user_firstname")); + // --THEN-- + userRepository.deleteById(JsonPath.read(response, "$.user_id")); + } + + @DisplayName("Edition of a player with a simple user") + @Test + @WithMockPlannerUser + void updatePlayerWithRestrictedUserTest() throws Exception { + + User user = userRepository.findByEmailIgnoreCase(adminEmail).orElseThrow(); + // --EXECUTE-- + Exception exception = + assertThrows( + ServletException.class, + () -> + mvc.perform( + put(PLAYER_URI + "/" + user.getId()) + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON))); + + String expectedMessage = "You dont have the right to update this user"; + String actualMessage = exception.getMessage(); + + // --ASSERT-- + assertTrue(actualMessage.contains(expectedMessage)); + } +} diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java new file mode 100644 index 0000000000..7c2b09f3f6 --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java @@ -0,0 +1,13 @@ +package io.openbas.utils.fixtures; + +import io.openbas.database.model.Organization; + +public class OrganizationFixture { + + public static Organization createOrganization() { + Organization organization = new Organization(); + organization.setName("Filigran test"); + organization.setDescription("Filigran test organization"); + return organization; + } +} diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java new file mode 100644 index 0000000000..5bdad0332a --- /dev/null +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java @@ -0,0 +1,17 @@ +package io.openbas.utils.fixtures; + +import io.openbas.rest.user.form.player.PlayerInput; + +public class PlayerFixture { + + public static PlayerInput createPlayer() { + PlayerInput player = new PlayerInput(); + player.setEmail("player@example.com"); + player.setFirstname("Firstname"); + player.setLastname("Lastname"); + player.setCountry("France"); + player.setPhone("+33123456789"); + player.setPgpKey("a123b"); + return player; + } +} From e5ded086d6b19b624caee9f78ac80c7b61c4c2f5 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 3 Jan 2025 12:40:18 +0100 Subject: [PATCH 2/7] Add upsert --- .../java/io/openbas/rest/PlayerApiTest.java | 70 +++++++++++++++++-- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index 9f5e8914de..21462055f2 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -22,7 +22,9 @@ import io.openbas.utils.mockUser.WithMockAdminUser; import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.servlet.ServletException; + import java.util.List; + import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -41,15 +43,19 @@ public class PlayerApiTest { static Tag TAG; static Organization ORGANIZATION; - @Autowired private MockMvc mvc; + @Autowired + private MockMvc mvc; @Value("${openbas.admin.email:#{null}}") private String adminEmail; - @Autowired private OrganizationRepository organizationRepository; + @Autowired + private OrganizationRepository organizationRepository; - @Autowired private TagRepository tagRepository; - @Autowired private UserRepository userRepository; + @Autowired + private TagRepository tagRepository; + @Autowired + private UserRepository userRepository; @BeforeAll void beforeAll() { @@ -111,6 +117,62 @@ void createPlayerWithRestrictedUserTest() throws Exception { assertTrue(actualMessage.contains(expectedMessage)); } + @DisplayName("Upsert of a player") + @Test + @WithMockAdminUser + void upsertPlayerTest() throws Exception { + // --PREPARE-- + User user = new User(); + user.setUpdateAttributes(PLAYER_INPUT); + PLAYER = userRepository.save(user); + String newFirstname = "updatedFirstname"; + PLAYER_INPUT.setFirstname(newFirstname); + + // --EXECUTE-- + String response = + mvc.perform( + post(PLAYER_URI + "/upsert") + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // --ASSERT-- + assertEquals("updatedFirstname", JsonPath.read(response, "$.user_firstname")); + // --THEN-- + userRepository.deleteById(JsonPath.read(response, "$.user_id")); + } + + @DisplayName("Upsert of a non existing player") + @Test + @WithMockAdminUser + void upsertNonExistingPlayerTest() throws Exception { + // --PREPARE-- + User user = new User(); + user.setEmail("admin@example.com"); + PLAYER = userRepository.save(user); + + // --EXECUTE-- + String response = + mvc.perform( + post(PLAYER_URI + "/upsert") + .content(asJsonString(PLAYER_INPUT)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // --ASSERT-- + assertEquals("Firstname", JsonPath.read(response, "$.user_firstname")); + // --THEN-- + userRepository.deleteById(JsonPath.read(response, "$.user_id")); + } + @DisplayName("Edition of a player") @Test @WithMockAdminUser From 12a10f532199dc60a2462327e3c2b1098c3accdd Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 3 Jan 2025 13:22:04 +0100 Subject: [PATCH 3/7] Add upsert --- .../test/java/io/openbas/rest/PlayerApiTest.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index 21462055f2..78bd7735b4 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -22,9 +22,7 @@ import io.openbas.utils.mockUser.WithMockAdminUser; import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.servlet.ServletException; - import java.util.List; - import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -43,19 +41,15 @@ public class PlayerApiTest { static Tag TAG; static Organization ORGANIZATION; - @Autowired - private MockMvc mvc; + @Autowired private MockMvc mvc; @Value("${openbas.admin.email:#{null}}") private String adminEmail; - @Autowired - private OrganizationRepository organizationRepository; + @Autowired private OrganizationRepository organizationRepository; - @Autowired - private TagRepository tagRepository; - @Autowired - private UserRepository userRepository; + @Autowired private TagRepository tagRepository; + @Autowired private UserRepository userRepository; @BeforeAll void beforeAll() { From 4154b50cb259c917fc92ec40f6a0aa6b477fca27 Mon Sep 17 00:00:00 2001 From: Johanah LEKEU Date: Fri, 3 Jan 2025 15:40:05 +0100 Subject: [PATCH 4/7] Resolve drone errors --- .../src/test/java/io/openbas/rest/PlayerApiTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index 78bd7735b4..6680e14c38 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -51,8 +51,8 @@ public class PlayerApiTest { @Autowired private TagRepository tagRepository; @Autowired private UserRepository userRepository; - @BeforeAll - void beforeAll() { + @BeforeEach + void beforeEach() { ORGANIZATION = organizationRepository.save(OrganizationFixture.createOrganization()); TAG = tagRepository.save(TagFixture.getTag()); PLAYER_INPUT = PlayerFixture.createPlayer(); @@ -60,8 +60,8 @@ void beforeAll() { PLAYER_INPUT.setTagIds(List.of(TAG.getId())); } - @AfterAll - void afterAll() { + @AfterEach + void afterEach() { organizationRepository.delete(ORGANIZATION); tagRepository.delete(TAG); } From 595c8928fbea9d3cc1a5bab18b7adfc757b41344 Mon Sep 17 00:00:00 2001 From: Romuald Lemesle Date: Fri, 3 Jan 2025 19:02:46 +0100 Subject: [PATCH 5/7] [backend] Add some tests --- .../java/io/openbas/rest/PlayerApiTest.java | 156 ++++++++++-------- .../utils/fixtures/OrganizationFixture.java | 4 +- .../openbas/utils/fixtures/PlayerFixture.java | 6 +- 3 files changed, 97 insertions(+), 69 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index 6680e14c38..2b665328ae 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -2,13 +2,14 @@ import static io.openbas.rest.user.PlayerApi.PLAYER_URI; import static io.openbas.utils.JsonUtils.asJsonString; +import static io.openbas.utils.fixtures.PlayerFixture.PLAYER_FIXTURE_FIRSTNAME; import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.jayway.jsonpath.JsonPath; +import io.openbas.IntegrationTest; import io.openbas.database.model.Organization; import io.openbas.database.model.Tag; import io.openbas.database.model.User; @@ -26,20 +27,15 @@ import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@SpringBootTest -@AutoConfigureMockMvc @TestInstance(PER_CLASS) -public class PlayerApiTest { +public class PlayerApiTest extends IntegrationTest { - static User PLAYER; - static PlayerInput PLAYER_INPUT; static Tag TAG; static Organization ORGANIZATION; + static User USER; @Autowired private MockMvc mvc; @@ -47,7 +43,6 @@ public class PlayerApiTest { private String adminEmail; @Autowired private OrganizationRepository organizationRepository; - @Autowired private TagRepository tagRepository; @Autowired private UserRepository userRepository; @@ -55,26 +50,27 @@ public class PlayerApiTest { void beforeEach() { ORGANIZATION = organizationRepository.save(OrganizationFixture.createOrganization()); TAG = tagRepository.save(TagFixture.getTag()); - PLAYER_INPUT = PlayerFixture.createPlayer(); - PLAYER_INPUT.setOrganizationId(ORGANIZATION.getId()); - PLAYER_INPUT.setTagIds(List.of(TAG.getId())); } @AfterEach void afterEach() { organizationRepository.delete(ORGANIZATION); tagRepository.delete(TAG); + userRepository.delete(USER); } - @DisplayName("Creation of a player") + @DisplayName("Given valid player input, should create a player successfully") @Test @WithMockAdminUser - void createPlayerTest() throws Exception { - // --EXECUTE-- + void given_validPlayerInput_should_createPlayerSuccessfully() throws Exception { + // -- PREPARE -- + PlayerInput playerInput = buildPlayerInput(); + + // -- EXECUTE -- String response = mvc.perform( post(PLAYER_URI) - .content(asJsonString(PLAYER_INPUT)) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -82,17 +78,22 @@ void createPlayerTest() throws Exception { .getResponse() .getContentAsString(); - // --ASSERT-- - assertEquals("Firstname", JsonPath.read(response, "$.user_firstname")); + // -- ASSERT -- + assertEquals(PLAYER_FIXTURE_FIRSTNAME, JsonPath.read(response, "$.user_firstname")); + assertEquals(TAG.getId(), JsonPath.read(response, "$.user_tags[0]")); + assertEquals(ORGANIZATION.getId(), JsonPath.read(response, "$.user_organization")); - // --THEN-- + // -- CLEAN -- userRepository.deleteById(JsonPath.read(response, "$.user_id")); } - @DisplayName("Creation of a player with a simple user") + @DisplayName("Given restricted user, should not allow creation of player") @Test @WithMockPlannerUser - void createPlayerWithRestrictedUserTest() throws Exception { + void given_restrictedUser_should_notAllowPlayerCreation() { + // -- PREPARE -- + PlayerInput playerInput = buildPlayerInput(); + // --EXECUTE-- Exception exception = assertThrows( @@ -100,33 +101,31 @@ void createPlayerWithRestrictedUserTest() throws Exception { () -> mvc.perform( post(PLAYER_URI) - .content(asJsonString(PLAYER_INPUT)) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON))); - String expectedMessage = "User is restricted"; - String actualMessage = exception.getMessage(); - // --ASSERT-- - assertTrue(actualMessage.contains(expectedMessage)); + assertTrue(exception.getMessage().contains("User is restricted")); } - @DisplayName("Upsert of a player") + @DisplayName("Given valid player input, should update player successfully") @Test @WithMockAdminUser - void upsertPlayerTest() throws Exception { + void given_validPlayerInput_should_updatePlayerSuccessfully() throws Exception { // --PREPARE-- + PlayerInput playerInput = buildPlayerInput(); User user = new User(); - user.setUpdateAttributes(PLAYER_INPUT); - PLAYER = userRepository.save(user); + user.setUpdateAttributes(playerInput); + USER = userRepository.save(user); String newFirstname = "updatedFirstname"; - PLAYER_INPUT.setFirstname(newFirstname); + playerInput.setFirstname(newFirstname); - // --EXECUTE-- + // -- EXECUTE -- String response = mvc.perform( post(PLAYER_URI + "/upsert") - .content(asJsonString(PLAYER_INPUT)) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -134,26 +133,22 @@ void upsertPlayerTest() throws Exception { .getResponse() .getContentAsString(); - // --ASSERT-- - assertEquals("updatedFirstname", JsonPath.read(response, "$.user_firstname")); - // --THEN-- - userRepository.deleteById(JsonPath.read(response, "$.user_id")); + // -- ASSERT -- + assertEquals(newFirstname, JsonPath.read(response, "$.user_firstname")); } - @DisplayName("Upsert of a non existing player") + @DisplayName("Given non-existing player input, should upsert successfully") @Test @WithMockAdminUser - void upsertNonExistingPlayerTest() throws Exception { + void given_nonExistingPlayerInput_should_upsertSuccessfully() throws Exception { // --PREPARE-- - User user = new User(); - user.setEmail("admin@example.com"); - PLAYER = userRepository.save(user); + PlayerInput playerInput = buildPlayerInput(); // --EXECUTE-- String response = mvc.perform( post(PLAYER_URI + "/upsert") - .content(asJsonString(PLAYER_INPUT)) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -162,27 +157,26 @@ void upsertNonExistingPlayerTest() throws Exception { .getContentAsString(); // --ASSERT-- - assertEquals("Firstname", JsonPath.read(response, "$.user_firstname")); - // --THEN-- - userRepository.deleteById(JsonPath.read(response, "$.user_id")); + assertEquals(PLAYER_FIXTURE_FIRSTNAME, JsonPath.read(response, "$.user_firstname")); } - @DisplayName("Edition of a player") + @DisplayName("Given valid player ID and input, should update player successfully") @Test @WithMockAdminUser - void updatePlayerTest() throws Exception { - // --PREPARE-- + void given_validPlayerIdAndInput_should_updatePlayerSuccessfully() throws Exception { + // -- PREPARE -- + PlayerInput playerInput = buildPlayerInput(); User user = new User(); - user.setUpdateAttributes(PLAYER_INPUT); - PLAYER = userRepository.save(user); + user.setUpdateAttributes(playerInput); + USER = userRepository.save(user); String newFirstname = "updatedFirstname"; - PLAYER_INPUT.setFirstname(newFirstname); + playerInput.setFirstname(newFirstname); // --EXECUTE-- String response = mvc.perform( - put(PLAYER_URI + "/" + PLAYER.getId()) - .content(asJsonString(PLAYER_INPUT)) + put(PLAYER_URI + "/" + user.getId()) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -192,31 +186,61 @@ void updatePlayerTest() throws Exception { // --ASSERT-- assertEquals("updatedFirstname", JsonPath.read(response, "$.user_firstname")); - // --THEN-- - userRepository.deleteById(JsonPath.read(response, "$.user_id")); } - @DisplayName("Edition of a player with a simple user") + @DisplayName("Given restricted user, should not allow updating a player") @Test @WithMockPlannerUser - void updatePlayerWithRestrictedUserTest() throws Exception { - + void given_restrictedUser_should_notAllowPlayerUpdate() { + // -- PREPARE -- + PlayerInput playerInput = buildPlayerInput(); User user = userRepository.findByEmailIgnoreCase(adminEmail).orElseThrow(); - // --EXECUTE-- + + // -- EXECUTE -- Exception exception = assertThrows( ServletException.class, () -> mvc.perform( put(PLAYER_URI + "/" + user.getId()) - .content(asJsonString(PLAYER_INPUT)) + .content(asJsonString(playerInput)) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON))); - String expectedMessage = "You dont have the right to update this user"; - String actualMessage = exception.getMessage(); - // --ASSERT-- - assertTrue(actualMessage.contains(expectedMessage)); + assertTrue(exception.getMessage().contains("You dont have the right to update this user")); + } + + @DisplayName("Given valid player ID, should delete player successfully") + @Test + @WithMockAdminUser + void given_validPlayerId_should_deletePlayerSuccessfully() throws Exception { + // -- PREPARE -- + PlayerInput playerInput = buildPlayerInput(); + User user = new User(); + user.setUpdateAttributes(playerInput); + USER = userRepository.save(user); + + // -- EXECUTE -- + mvc.perform( + delete(PLAYER_URI + "/" + USER.getId()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is2xxSuccessful()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + assertTrue(this.userRepository.findById(USER.getId()).isEmpty()); + } + + // -- PRIVATE -- + + private PlayerInput buildPlayerInput() { + PlayerInput player = PlayerFixture.createPlayerInput(); + player.setOrganizationId(ORGANIZATION.getId()); + player.setTagIds(List.of(TAG.getId())); + return player; } } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java index 7c2b09f3f6..3fa4a3a581 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/OrganizationFixture.java @@ -4,9 +4,11 @@ public class OrganizationFixture { + public static final String ORGANIZATION_FIXTURE_NAME = "Filigran test"; + public static Organization createOrganization() { Organization organization = new Organization(); - organization.setName("Filigran test"); + organization.setName(ORGANIZATION_FIXTURE_NAME); organization.setDescription("Filigran test organization"); return organization; } diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java index 5bdad0332a..b5dd832f6f 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java @@ -4,10 +4,12 @@ public class PlayerFixture { - public static PlayerInput createPlayer() { + public static final String PLAYER_FIXTURE_FIRSTNAME = "Firstname"; + + public static PlayerInput createPlayerInput() { PlayerInput player = new PlayerInput(); player.setEmail("player@example.com"); - player.setFirstname("Firstname"); + player.setFirstname(PLAYER_FIXTURE_FIRSTNAME); player.setLastname("Lastname"); player.setCountry("France"); player.setPhone("+33123456789"); From 52a9440130e7a0f25f8259328eed29cf547273e5 Mon Sep 17 00:00:00 2001 From: Romuald Lemesle Date: Fri, 3 Jan 2025 19:22:28 +0100 Subject: [PATCH 6/7] [backend] Fix tests --- openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java | 5 ++++- .../test/java/io/openbas/utils/fixtures/PlayerFixture.java | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index 2b665328ae..eb1541b677 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -31,7 +31,7 @@ import org.springframework.test.web.servlet.MockMvc; @TestInstance(PER_CLASS) -public class PlayerApiTest extends IntegrationTest { +class PlayerApiTest extends IntegrationTest { static Tag TAG; static Organization ORGANIZATION; @@ -158,6 +158,9 @@ void given_nonExistingPlayerInput_should_upsertSuccessfully() throws Exception { // --ASSERT-- assertEquals(PLAYER_FIXTURE_FIRSTNAME, JsonPath.read(response, "$.user_firstname")); + + // -- CLEAN -- + this.userRepository.deleteById(JsonPath.read(response, "$.user_id")); } @DisplayName("Given valid player ID and input, should update player successfully") diff --git a/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java index b5dd832f6f..9e2323da81 100644 --- a/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java +++ b/openbas-api/src/test/java/io/openbas/utils/fixtures/PlayerFixture.java @@ -5,10 +5,11 @@ public class PlayerFixture { public static final String PLAYER_FIXTURE_FIRSTNAME = "Firstname"; + public static final String PLAYER_FIXTURE_MAIL = "player@example.com"; public static PlayerInput createPlayerInput() { PlayerInput player = new PlayerInput(); - player.setEmail("player@example.com"); + player.setEmail(PLAYER_FIXTURE_MAIL); player.setFirstname(PLAYER_FIXTURE_FIRSTNAME); player.setLastname("Lastname"); player.setCountry("France"); From b46f0a0d665ad418d2f82541ec13fa6617634784 Mon Sep 17 00:00:00 2001 From: Romuald Lemesle Date: Mon, 6 Jan 2025 17:39:32 +0100 Subject: [PATCH 7/7] [backend] FIx --- .../java/io/openbas/rest/PlayerApiTest.java | 94 ++++++++++++------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java index eb1541b677..a9a06ae0d4 100644 --- a/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java +++ b/openbas-api/src/test/java/io/openbas/rest/PlayerApiTest.java @@ -1,5 +1,6 @@ package io.openbas.rest; +import static io.openbas.config.AppConfig.EMAIL_FORMAT; import static io.openbas.rest.user.PlayerApi.PLAYER_URI; import static io.openbas.utils.JsonUtils.asJsonString; import static io.openbas.utils.fixtures.PlayerFixture.PLAYER_FIXTURE_FIRSTNAME; @@ -24,19 +25,19 @@ import io.openbas.utils.mockUser.WithMockPlannerUser; import jakarta.servlet.ServletException; import java.util.List; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; @TestInstance(PER_CLASS) +@Transactional class PlayerApiTest extends IntegrationTest { - static Tag TAG; - static Organization ORGANIZATION; - static User USER; - @Autowired private MockMvc mvc; @Value("${openbas.admin.email:#{null}}") @@ -46,19 +47,6 @@ class PlayerApiTest extends IntegrationTest { @Autowired private TagRepository tagRepository; @Autowired private UserRepository userRepository; - @BeforeEach - void beforeEach() { - ORGANIZATION = organizationRepository.save(OrganizationFixture.createOrganization()); - TAG = tagRepository.save(TagFixture.getTag()); - } - - @AfterEach - void afterEach() { - organizationRepository.delete(ORGANIZATION); - tagRepository.delete(TAG); - userRepository.delete(USER); - } - @DisplayName("Given valid player input, should create a player successfully") @Test @WithMockAdminUser @@ -80,11 +68,32 @@ void given_validPlayerInput_should_createPlayerSuccessfully() throws Exception { // -- ASSERT -- assertEquals(PLAYER_FIXTURE_FIRSTNAME, JsonPath.read(response, "$.user_firstname")); - assertEquals(TAG.getId(), JsonPath.read(response, "$.user_tags[0]")); - assertEquals(ORGANIZATION.getId(), JsonPath.read(response, "$.user_organization")); + assertEquals(playerInput.getTagIds().getFirst(), JsonPath.read(response, "$.user_tags[0]")); + assertEquals(playerInput.getOrganizationId(), JsonPath.read(response, "$.user_organization")); + } + + @DisplayName("Given invalid email in player input, should throw exceptions") + @Test + @WithMockAdminUser + void given_invalidEmailInPlayerInput_should_throwExceptions() throws Exception { + // -- PREPARE -- + PlayerInput playerInput = new PlayerInput(); + playerInput.setEmail("email"); - // -- CLEAN -- - userRepository.deleteById(JsonPath.read(response, "$.user_id")); + // -- EXECUTE -- + String response = + mvc.perform( + post(PLAYER_URI) + .content(asJsonString(playerInput)) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().is4xxClientError()) + .andReturn() + .getResponse() + .getContentAsString(); + + // -- ASSERT -- + assertTrue(response.contains(EMAIL_FORMAT)); } @DisplayName("Given restricted user, should not allow creation of player") @@ -109,15 +118,15 @@ void given_restrictedUser_should_notAllowPlayerCreation() { assertTrue(exception.getMessage().contains("User is restricted")); } - @DisplayName("Given valid player input, should update player successfully") + @DisplayName("Given valid player input, should upsert player successfully") @Test @WithMockAdminUser - void given_validPlayerInput_should_updatePlayerSuccessfully() throws Exception { + void given_validPlayerInput_should_upsertPlayerSuccessfully() throws Exception { // --PREPARE-- PlayerInput playerInput = buildPlayerInput(); User user = new User(); user.setUpdateAttributes(playerInput); - USER = userRepository.save(user); + userRepository.save(user); String newFirstname = "updatedFirstname"; playerInput.setFirstname(newFirstname); @@ -158,9 +167,6 @@ void given_nonExistingPlayerInput_should_upsertSuccessfully() throws Exception { // --ASSERT-- assertEquals(PLAYER_FIXTURE_FIRSTNAME, JsonPath.read(response, "$.user_firstname")); - - // -- CLEAN -- - this.userRepository.deleteById(JsonPath.read(response, "$.user_id")); } @DisplayName("Given valid player ID and input, should update player successfully") @@ -171,7 +177,7 @@ void given_validPlayerIdAndInput_should_updatePlayerSuccessfully() throws Except PlayerInput playerInput = buildPlayerInput(); User user = new User(); user.setUpdateAttributes(playerInput); - USER = userRepository.save(user); + userRepository.save(user); String newFirstname = "updatedFirstname"; playerInput.setFirstname(newFirstname); @@ -222,11 +228,11 @@ void given_validPlayerId_should_deletePlayerSuccessfully() throws Exception { PlayerInput playerInput = buildPlayerInput(); User user = new User(); user.setUpdateAttributes(playerInput); - USER = userRepository.save(user); + user = userRepository.save(user); // -- EXECUTE -- mvc.perform( - delete(PLAYER_URI + "/" + USER.getId()) + delete(PLAYER_URI + "/" + user.getId()) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON)) .andExpect(status().is2xxSuccessful()) @@ -235,15 +241,35 @@ void given_validPlayerId_should_deletePlayerSuccessfully() throws Exception { .getContentAsString(); // -- ASSERT -- - assertTrue(this.userRepository.findById(USER.getId()).isEmpty()); + assertTrue(this.userRepository.findById(user.getId()).isEmpty()); + } + + @DisplayName("Given no existing player ID, should throw an exception") + @Test + @WithMockAdminUser + void given_notExistingAssetGroup_should_throwAnException() { + // -- PREPARE -- + String nonexistentAssetGroupId = "nonexistent-id"; + + // --EXECUTE-- + assertThrows( + ServletException.class, + () -> + mvc.perform( + delete(PLAYER_URI + "/" + nonexistentAssetGroupId) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON))); } // -- PRIVATE -- private PlayerInput buildPlayerInput() { + Organization organization = + organizationRepository.save(OrganizationFixture.createOrganization()); + Tag tag = tagRepository.save(TagFixture.getTag()); PlayerInput player = PlayerFixture.createPlayerInput(); - player.setOrganizationId(ORGANIZATION.getId()); - player.setTagIds(List.of(TAG.getId())); + player.setOrganizationId(organization.getId()); + player.setTagIds(List.of(tag.getId())); return player; } }