Commit d1cb79ff authored by Sonja Happ's avatar Sonja Happ
Browse files

WIP: scenario tests and IC tests independent of hard coded data #44

parent 616a1c74
......@@ -24,7 +24,6 @@ package helper
import (
"encoding/json"
"fmt"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/jinzhu/gorm/dialects/postgres"
"golang.org/x/crypto/bcrypt"
......@@ -63,10 +62,13 @@ var StrPasswordC = "guestpw"
// Hash passwords with bcrypt algorithm
var bcryptCost = 10
var pw0, _ = bcrypt.GenerateFromPassword([]byte(StrPassword0), bcryptCost)
var pwA, _ = bcrypt.GenerateFromPassword([]byte(StrPasswordA), bcryptCost)
var pwB, _ = bcrypt.GenerateFromPassword([]byte(StrPasswordB), bcryptCost)
var pwC, _ = bcrypt.GenerateFromPassword([]byte(StrPasswordC), bcryptCost)
var User0 = database.User{Username: "User_0", Password: string(pw0),
Role: "Admin", Mail: "User_0@example.com"}
var UserA = database.User{Username: "User_A", Password: string(pwA),
Role: "User", Mail: "User_A@example.com", Active: true}
var UserB = database.User{Username: "User_B", Password: string(pwB),
......@@ -83,27 +85,6 @@ type UserRequest struct {
Active string `json:"active,omitempty"`
}
var NewUserA = UserRequest{
Username: UserA.Username,
Password: StrPasswordA,
Role: UserA.Role,
Mail: UserA.Mail,
}
var NewUserB = UserRequest{
Username: UserB.Username,
Password: StrPasswordB,
Role: UserB.Role,
Mail: UserB.Mail,
}
var NewUserC = UserRequest{
Username: UserC.Username,
Password: StrPasswordC,
Role: UserC.Role,
Mail: UserC.Mail,
}
// Infrastructure components
var propertiesA = json.RawMessage(`{"prop1" : "a nice prop"}`)
......@@ -148,11 +129,6 @@ var ScenarioA = database.Scenario{
Running: true,
StartParameters: postgres.Jsonb{startParametersA},
}
var ScenarioB = database.Scenario{
Name: "Scenario_B",
Running: false,
StartParameters: postgres.Jsonb{startParametersB},
}
// Component Configuration
......@@ -184,27 +160,6 @@ var OutSignalB = database.Signal{
Unit: "V",
}
var OutSignalC = database.Signal{
Name: "outSignal_C",
Direction: "out",
Index: 3,
Unit: "---",
}
var OutSignalD = database.Signal{
Name: "outSignal_D",
Direction: "out",
Index: 4,
Unit: "---",
}
var OutSignalE = database.Signal{
Name: "outSignal_E",
Direction: "out",
Index: 5,
Unit: "---",
}
var InSignalA = database.Signal{
Name: "inSignal_A",
Direction: "in",
......@@ -231,11 +186,8 @@ var DashboardB = database.Dashboard{
}
// Widgets
var customPropertiesBox = json.RawMessage(`{"border_color" : "#4287f5", "border_color_opacity": 1, "background_color" : "#961520", "background_color_opacity" : 1}`)
var customPropertiesSlider = json.RawMessage(`{"default_value" : 0, "orientation" : 0, "rangeUseMinMax": false, "rangeMin" : 0, "rangeMax": 200, "rangeUseMinMax" : true, "showUnit": true, "continous_update": false, "value": "", "resizeLeftRightLock": false, "resizeTopBottomLock": true, "step": 0.1 }`)
var customPropertiesLabel = json.RawMessage(`{"textSize" : "20", "fontColor" : "#4287f5", "fontColor_opacity": 1}`)
var customPropertiesButton = json.RawMessage(`{"pressed": false, "toggle" : false, "on_value" : 1, "off_value" : 0, "background_color": "#961520", "font_color": "#4287f5", "border_color": "#4287f5", "background_color_opacity": 1}`)
var customPropertiesLamp = json.RawMessage(`{"signal" : 0, "on_color" : "#4287f5", "off_color": "#961520", "threshold" : 0.5, "on_color_opacity": 1, "off_color_opacity": 1}`)
var WidgetA = database.Widget{
Name: "Label",
......@@ -267,51 +219,6 @@ var WidgetB = database.Widget{
SignalIDs: []int64{},
}
var WidgetC = database.Widget{
Name: "Box",
Type: "Box",
Width: 200,
Height: 200,
MinHeight: 10,
MinWidth: 50,
X: 300,
Y: 10,
Z: 0,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesBox},
SignalIDs: []int64{},
}
var WidgetD = database.Widget{
Name: "Button",
Type: "Button",
Width: 100,
Height: 100,
MinHeight: 50,
MinWidth: 100,
X: 10,
Y: 50,
Z: 0,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesButton},
SignalIDs: []int64{},
}
var WidgetE = database.Widget{
Name: "Lamp",
Type: "Lamp",
Width: 200,
Height: 20,
MinHeight: 10,
MinWidth: 50,
X: 50,
Y: 300,
Z: 0,
IsLocked: false,
CustomProperties: postgres.Jsonb{customPropertiesLamp},
SignalIDs: []int64{},
}
func ReadTestDataFromJson(path string) error {
jsonFile, err := os.Open(path)
......@@ -334,40 +241,18 @@ func ReadTestDataFromJson(path string) error {
return nil
}
// add default admin user, normal users and a guest to the DB
// add test users defined above
func AddTestUsers() error {
err := ReadTestDataFromJson("../../testdata/testdata.json")
if err != nil {
return err
}
//create a copy of global test data
if len(GlobalTestData.Users) == 0 {
return fmt.Errorf("no users in test data")
}
testUsers := []database.User{User0, UserA, UserB, UserC}
database.DBpool.AutoMigrate(&database.User{})
err, _ = database.DBAddAdminUser(configuration.GlobalConfig)
if err != nil {
return err
}
for _, user := range GlobalTestData.Users {
if user.Role != "Admin" {
// add users to DB that are not admin users
var newUser database.User
newUser.Username = user.Username
newUser.Role = user.Role
newUser.Mail = user.Mail
pwEnc, _ := bcrypt.GenerateFromPassword([]byte(user.Password), 10)
newUser.Password = string(pwEnc)
err = database.DBpool.Create(&newUser).Error
if err != nil {
return err
}
for _, user := range testUsers {
err := database.DBpool.Create(&user).Error
if err != nil {
return err
}
}
return nil
......
......@@ -46,8 +46,8 @@ type Credentials struct {
}
var AdminCredentials = Credentials{
Username: "admin",
Password: "xyz789",
Username: "User_0",
Password: StrPassword0,
}
var UserACredentials = Credentials{
......
This diff is collapsed.
......@@ -22,6 +22,7 @@
package scenario
import (
"encoding/json"
"fmt"
"os"
"testing"
......@@ -51,6 +52,18 @@ type UserRequest struct {
Active string `json:"active,omitempty"`
}
var newScenario1 = ScenarioRequest{
Name: "Scenario1",
Running: true,
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
var newScenario2 = ScenarioRequest{
Name: "Scenario2",
Running: false,
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1B", "parameter2" : "testValue2B", "parameter3" : 55}`)},
}
func TestMain(m *testing.M) {
err := configuration.InitConfig()
if err != nil {
......@@ -82,12 +95,6 @@ func TestAddScenario(t *testing.T) {
database.MigrateModels()
assert.NoError(t, helper.AddTestUsers())
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
......@@ -102,12 +109,12 @@ func TestAddScenario(t *testing.T) {
// test POST scenarios/ $newScenario as normal user
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Compare POST's response with the newScenario
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario})
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
// Read newScenario's ID from the response
......@@ -121,7 +128,7 @@ func TestAddScenario(t *testing.T) {
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Compare GET's response with the newScenario
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario})
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
// try to POST a malformed scenario
......@@ -150,7 +157,7 @@ func TestAddScenario(t *testing.T) {
// try to add scenario as guest user
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
......@@ -190,18 +197,13 @@ func TestUpdateScenario(t *testing.T) {
assert.NoError(t, err)
// test POST scenarios/ $newScenario
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Compare POST's response with the newScenario
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario})
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
// Read newScenario's ID from the response
......@@ -269,14 +271,9 @@ func TestGetAllScenariosAsAdmin(t *testing.T) {
"/api/authenticate", "POST", helper.UserBCredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenarioB
newScenarioB := ScenarioRequest{
Name: helper.ScenarioB.Name,
Running: helper.ScenarioB.Running,
StartParameters: helper.ScenarioB.StartParameters,
}
// test POST scenarios/ $newScenario1
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenarioB})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -285,14 +282,9 @@ func TestGetAllScenariosAsAdmin(t *testing.T) {
"/api/authenticate", "POST", helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenarioA
newScenarioA := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
// test POST scenarios/ $newScenario2
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenarioA})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -325,15 +317,9 @@ func TestGetAllScenariosAsUser(t *testing.T) {
"/api/scenarios", "GET", nil)
assert.NoError(t, err)
// test POST scenarios/ $newScenarioB
newScenarioB := ScenarioRequest{
Name: helper.ScenarioB.Name,
Running: helper.ScenarioB.Running,
StartParameters: helper.ScenarioB.StartParameters,
}
// test POST scenarios/ $newScenario2
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenarioB})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -342,14 +328,9 @@ func TestGetAllScenariosAsUser(t *testing.T) {
"/api/authenticate", "POST", helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenarioA
newScenarioA := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
// test POST scenarios/ $newScenario1
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenarioA})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -378,13 +359,8 @@ func TestDeleteScenario(t *testing.T) {
assert.NoError(t, err)
// test POST scenarios/ $newScenario
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -427,7 +403,7 @@ func TestDeleteScenario(t *testing.T) {
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Compare DELETE's response with the newScenario
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario})
err = helper.CompareResponse(resp, helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
// Again count the number of all the scenarios returned
......@@ -450,13 +426,8 @@ func TestAddUserToScenario(t *testing.T) {
assert.NoError(t, err)
// test POST scenarios/ $newScenario
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -535,13 +506,8 @@ func TestGetAllUsersOfScenario(t *testing.T) {
assert.NoError(t, err)
// test POST scenarios/ $newScenario
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......@@ -620,13 +586,8 @@ func TestRemoveUserFromScenario(t *testing.T) {
assert.NoError(t, err)
// test POST scenarios/ $newScenario
newScenario := ScenarioRequest{
Name: helper.ScenarioA.Name,
Running: helper.ScenarioA.Running,
StartParameters: helper.ScenarioA.StartParameters,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment