Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
fhennig authored and corneliusroemer committed Sep 27, 2024
1 parent 47b76d2 commit 76e193b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 76e193b

Please sign in to comment.