-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): create
POST /groups/{groupName}
endpoint to create a…
… new group
- Loading branch information
1 parent
21492a0
commit 4b95fe5
Showing
4 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
backend/src/main/kotlin/org/pathoplexus/backend/controller/GroupManagementController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.pathoplexus.backend.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.Parameter | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement | ||
import org.pathoplexus.backend.service.GroupManagementDatabaseService | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.validation.annotation.Validated | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.ResponseStatus | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@Validated | ||
@SecurityRequirement(name = "bearerAuth") | ||
class GroupManagementController( | ||
private val groupManagementDatabaseService: GroupManagementDatabaseService, | ||
) { | ||
|
||
@Operation(description = "Create a new Group. The user creating the group will be added to the group.") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@PostMapping("/groups/{groupName}", produces = [MediaType.APPLICATION_JSON_VALUE]) | ||
fun createNewGroup( | ||
@UsernameFromJwt username: String, | ||
@Parameter( | ||
description = "A new group name", | ||
) @PathVariable groupName: String, | ||
) = groupManagementDatabaseService.createNewGroup(groupName, username) | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/main/kotlin/org/pathoplexus/backend/service/GroupManagementDatabaseService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.pathoplexus.backend.service | ||
|
||
import org.jetbrains.exposed.sql.insert | ||
import org.jetbrains.exposed.sql.select | ||
import org.pathoplexus.backend.controller.BadRequestException | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional | ||
class GroupManagementDatabaseService { | ||
|
||
fun createNewGroup(groupName: String, username: String) { | ||
val groupEntity = GroupsTable.select { GroupsTable.groupNameColumn eq groupName } | ||
.singleOrNull() | ||
|
||
if (groupEntity != null) { | ||
throw BadRequestException("Group already exists") | ||
} | ||
|
||
GroupsTable.insert { | ||
it[GroupsTable.groupNameColumn] = groupName | ||
} | ||
|
||
UserGroupsTable.insert { | ||
it[UserGroupsTable.userNameColumn] = username | ||
it[UserGroupsTable.groupNameColumn] = groupName | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
backend/src/test/kotlin/org/pathoplexus/backend/controller/GroupManagementControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.pathoplexus.backend.controller | ||
|
||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
|
||
@EndpointTest | ||
class GroupManagementControllerTest(@Autowired private val mockMvc: MockMvc) { | ||
|
||
@Test | ||
fun `GIVEN invalid authorization token THEN returns 401 Unauthorized`() { | ||
expectUnauthorizedResponse(isModifyingRequest = true) { | ||
createNewGroup("testGroup", jwt = it) | ||
} | ||
} | ||
|
||
// TODO(668: verify that the group has one user after creation | ||
@Test | ||
fun `GIVEN WHEN creating a group THEN return success with 204`() { | ||
createNewGroup("testGroup").andExpect(status().isNoContent()) | ||
} | ||
|
||
@Test | ||
fun `GIVEN an existing group WHEN creating a group with same name THEN return 400`() { | ||
createNewGroup("testGroup") | ||
|
||
createNewGroup("testGroup").andExpect(status().isBadRequest()) | ||
} | ||
|
||
private fun createNewGroup(groupName: String, jwt: String? = jwtForDefaultUser) = mockMvc.perform( | ||
post("/groups/$groupName") | ||
.withAuth(jwt), | ||
) | ||
} |