-
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.
- Loading branch information
1 parent
3591f46
commit c02f3f2
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
package usergroup | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
|
@@ -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{ | ||
|
@@ -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) | ||
} | ||
} | ||
} |