From 76e193b91efdb0e2562e29e19db02bbcd5fe2f22 Mon Sep 17 00:00:00 2001 From: Felix Hennig Date: Fri, 27 Sep 2024 13:05:10 +0100 Subject: [PATCH] Add test --- .../controller/GroupManagementController.kt | 2 +- .../GroupManagementControllerClient.kt | 7 +++ .../GroupManagementControllerTest.kt | 48 ++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/backend/src/main/kotlin/org/loculus/backend/controller/GroupManagementController.kt b/backend/src/main/kotlin/org/loculus/backend/controller/GroupManagementController.kt index decda074c..0bd149518 100644 --- a/backend/src/main/kotlin/org/loculus/backend/controller/GroupManagementController.kt +++ b/backend/src/main/kotlin/org/loculus/backend/controller/GroupManagementController.kt @@ -35,7 +35,7 @@ class GroupManagementController(private val groupManagementDatabaseService: Grou ): Group = groupManagementDatabaseService.createNewGroup(group, authenticatedUser) @Operation(description = "Edit a group. Only users part of the group can edit it. The updated group is returned.") - @ResponseStatus(HttpStatus.NO_CONTENT) + @ResponseStatus(HttpStatus.OK) @PutMapping("/groups/{groupId}", produces = [MediaType.APPLICATION_JSON_VALUE]) fun editGroup( @HiddenParam authenticatedUser: AuthenticatedUser, diff --git a/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerClient.kt b/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerClient.kt index 1794d877c..7d1da2c40 100644 --- a/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerClient.kt +++ b/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerClient.kt @@ -49,6 +49,13 @@ class GroupManagementControllerClient(private val mockMvc: MockMvc, private val .withAuth(jwt), ) + fun updateGroup(groupId: Int, group: NewGroup = NEW_GROUP, jwt: String? = jwtForDefaultUser): ResultActions = mockMvc.perform( + put("/groups/$groupId") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content(objectMapper.writeValueAsString(group)) + .withAuth(jwt), + ) + fun getGroupsOfUser(jwt: String? = jwtForDefaultUser): ResultActions = mockMvc.perform( get("/user/groups").withAuth(jwt), ) diff --git a/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerTest.kt b/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerTest.kt index 0afc314cf..151f63db2 100644 --- a/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerTest.kt +++ b/backend/src/test/kotlin/org/loculus/backend/controller/groupmanagement/GroupManagementControllerTest.kt @@ -11,6 +11,8 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.MethodSource import org.keycloak.representations.idm.UserRepresentation +import org.loculus.backend.api.Address +import org.loculus.backend.api.NewGroup import org.loculus.backend.controller.ALTERNATIVE_DEFAULT_GROUP import org.loculus.backend.controller.ALTERNATIVE_DEFAULT_GROUP_NAME import org.loculus.backend.controller.ALTERNATIVE_DEFAULT_USER_NAME @@ -41,8 +43,6 @@ class GroupManagementControllerTest(@Autowired private val client: GroupManageme every { keycloakAdapter.getUsersWithName(any()) } returns listOf(UserRepresentation()) } - // TODO add test somewhere here - @Test fun `GIVEN database preparation WHEN getting groups details THEN I get the default group with the default user`() { val defaultGroupId = client.createNewGroup(group = DEFAULT_GROUP, jwt = jwtForDefaultUser) @@ -106,6 +106,50 @@ class GroupManagementControllerTest(@Autowired private val client: GroupManageme .andExpect(jsonPath("\$.[0].contactEmail").value(NEW_GROUP.contactEmail)) } + @Test + fun `GIVEN a group is created WHEN I edit the group THEN the group information is updated`() { + val groupId = client.createNewGroup(group = DEFAULT_GROUP, jwt = jwtForDefaultUser) + .andExpect(status().isOk) + .andGetGroupId() + val newInfo = NewGroup( + groupName = "Updated group name", + institution = "Updated institution", + address = Address( + line1 = "Updated address line 1", + line2 = "Updated address line 2", + postalCode = "Updated post code", + city = "Updated city", + state = "Updated state", + country = "Updated country", + ), + contactEmail = "Updated email", + ) + client.updateGroup(groupId = groupId, group = newInfo, jwt = jwtForDefaultUser) + .andExpect(status().isOk) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(jsonPath("\$.groupName").value(newInfo.groupName)) + .andExpect(jsonPath("\$.institution").value(newInfo.institution)) + .andExpect(jsonPath("\$.address.line1").value(newInfo.address.line1)) + .andExpect(jsonPath("\$.address.line2").value(newInfo.address.line2)) + .andExpect(jsonPath("\$.address.city").value(newInfo.address.city)) + .andExpect(jsonPath("\$.address.state").value(newInfo.address.state)) + .andExpect(jsonPath("\$.address.postalCode").value(newInfo.address.postalCode)) + .andExpect(jsonPath("\$.address.country").value(newInfo.address.country)) + .andExpect(jsonPath("\$.contactEmail").value(newInfo.contactEmail)) + client.getDetailsOfGroup(groupId = groupId, jwt = jwtForDefaultUser) + .andExpect(status().isOk) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE)) + .andExpect(jsonPath("\$.group.groupName").value(newInfo.groupName)) + .andExpect(jsonPath("\$.group.institution").value(newInfo.institution)) + .andExpect(jsonPath("\$.group.address.line1").value(newInfo.address.line1)) + .andExpect(jsonPath("\$.group.address.line2").value(newInfo.address.line2)) + .andExpect(jsonPath("\$.group.address.city").value(newInfo.address.city)) + .andExpect(jsonPath("\$.group.address.state").value(newInfo.address.state)) + .andExpect(jsonPath("\$.group.address.postalCode").value(newInfo.address.postalCode)) + .andExpect(jsonPath("\$.group.address.country").value(newInfo.address.country)) + .andExpect(jsonPath("\$.group.contactEmail").value(newInfo.contactEmail)) + } + @Test fun `WHEN superuser queries groups of user THEN returns all groups`() { client.createNewGroup(group = DEFAULT_GROUP, jwt = jwtForDefaultUser)