Skip to content

Commit

Permalink
Rewrote add user to usergroup test
Browse files Browse the repository at this point in the history
  • Loading branch information
SystemsPurge committed Oct 15, 2024
1 parent 3591f46 commit c02f3f2
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions routes/usergroup/usergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package usergroup

import (
"encoding/json"
"fmt"
"os"
"testing"

Expand All @@ -41,6 +43,15 @@ type UserGroupRequest struct {
ScenarioMappings []ScenarioMappingRequest `json:"scenarioMappings"`
}

type UserRequest struct {
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
OldPassword string `json:"oldPassword,omitempty"`
Mail string `json:"mail,omitempty"`
Role string `json:"role,omitempty"`
Active string `json:"active,omitempty"`
}

var newUserGroupOneMapping = UserGroupRequest{
Name: "UserGroup1",
ScenarioMappings: []ScenarioMappingRequest{
Expand Down Expand Up @@ -123,3 +134,57 @@ func TestAddUserGroup(t *testing.T) {
// Test with invalid user group and one mapping
// Test with invalid user group and multiple mappings
}

func TestAddUserToGroup(t *testing.T) {
// Prep DB
database.DropTables()
database.MigrateModels()
adminpw, _ := database.AddAdminUser(configuration.GlobalConfig)

//Auth
token, _ := helper.AuthenticateForTest(router, database.Credentials{Username: "admin", Password: adminpw})

//Post necessities
usr := UserRequest{Username: "adrienmarie", Password: "Legendre", Role: "User", Mail: "[email protected]"}
helper.TestEndpoint(router, token, "/api/v2/users", "POST", helper.KeyModels{"user": usr})
helper.TestEndpoint(router, token, "/api/v2/scenarios", "POST", helper.KeyModels{"scenario": database.Scenario{Name: "scenarioNoDups"}})
helper.TestEndpoint(router, token, "/api/v2/scenarios", "POST", helper.KeyModels{"scenario": database.Scenario{Name: "scenarioDups"}})
helper.TestEndpoint(router, token, "/api/v2/usergroups", "POST", helper.KeyModels{"usergroup": newUserGroupTwoMappings})

//Add user
code, _, err := helper.TestEndpoint(router, token, "/api/v2/usergroups/1/user?username=adrienmarie", "PUT", struct{}{})
assert.Equal(t, 200, code)
assert.NoError(t, err)

//get scenarios
_, res, _ := helper.TestEndpoint(router, token, "/api/v2/scenarios", "GET", struct{}{})
var scenariosMap map[string]([]database.Scenario)
json.Unmarshal(res.Bytes(), &scenariosMap)
scenarios := scenariosMap["scenarios"]

//Actual checks
assert.Equal(t, 3, len(scenarios))
for _, v := range scenarios {
path := fmt.Sprintf("/api/v2/scenarios/%d/users", v.ID)
var usersMap map[string]([]database.User)
_, res, _ = helper.TestEndpoint(router, token, path, "GET", struct{}{})
json.Unmarshal(res.Bytes(), &usersMap)
users := usersMap["users"]
switch v.ID {
case 1: //no dups
assert.Equal(t, "scenarioNoDups", v.Name)
assert.Equal(t, 2, len(users))
case 2: // with dups
assert.Equal(t, "scenarioDups", v.Name)
assert.Equal(t, 1, len(users))
assert.Equal(t, "admin", users[0].Username)
case 3: // duped scenario
assert.Equal(t, "scenarioDups adrienmarie", v.Name)
assert.Equal(t, 1, len(users))
assert.Equal(t, "adrienmarie", users[0].Username)
default:
// should not happen, fail manually
assert.Equal(t, 0, 1)
}
}
}

0 comments on commit c02f3f2

Please sign in to comment.