Skip to content

Commit

Permalink
Added scenario ID existence checks on usergroup add
Browse files Browse the repository at this point in the history
Signed-off-by: SystemsPurge <[email protected]>
  • Loading branch information
SystemsPurge committed Oct 16, 2024
1 parent c4d632f commit 8da1b1f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions routes/usergroup/usergroup_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package usergroup

import (
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -27,6 +28,7 @@ import (
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/user"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
)

func RegisterUserGroupEndpoints(r *gin.RouterGroup) {
Expand Down Expand Up @@ -74,6 +76,15 @@ func addUserGroup(c *gin.Context) {

// Create the new user group from the request
newUserGroup := req.createUserGroup()
db := database.GetDB()
for _, sm := range newUserGroup.ScenarioMappings {
var sc database.Scenario
if err := db.Find(&sc, "ID = ?", sm.ScenarioID).Error; errors.Is(err, gorm.ErrRecordNotFound) {
helper.NotFoundError(c,
"Scenario mappings referencing inexistent scenario ID: "+strconv.Itoa(int(sm.ScenarioID)))
return
}
}

// Save the new user group to the database
err := newUserGroup.save()
Expand Down
8 changes: 8 additions & 0 deletions routes/usergroup/usergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ func TestAddUserGroup(t *testing.T) {
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)

//Test with inexitent scenario
code, resp, err = helper.TestEndpoint(router, token, "/api/v2/usergroups",
"POST", helper.KeyModels{"usergroup": newUserGroupOneMapping})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)

// Test with valid user group with one scenario mapping
helper.TestEndpoint(router, token, "/api/v2/scenarios", "POST", helper.KeyModels{"scenario": database.Scenario{Name: "scenario1"}})
helper.TestEndpoint(router, token, "/api/v2/scenarios", "POST", helper.KeyModels{"scenario": database.Scenario{Name: "scenario2"}})
code, resp, err = helper.TestEndpoint(router, token, "/api/v2/usergroups",
"POST", helper.KeyModels{"usergroup": newUserGroupOneMapping})
assert.NoError(t, err)
Expand Down

0 comments on commit 8da1b1f

Please sign in to comment.