From a06d90d08569df7ef9e80d470d7df206c59f5df7 Mon Sep 17 00:00:00 2001 From: Sonja Happ Date: Thu, 25 Jul 2019 11:38:28 +0200 Subject: [PATCH] revise dashboard testing --- common/database.go | 17 ++-- common/models.go | 3 +- common/responses.go | 52 +++++----- common/testdata.go | 11 +++ routes/dashboard/dashboard_endpoints.go | 18 ++-- routes/dashboard/dashboard_methods.go | 2 +- routes/dashboard/dashboard_test.go | 125 +++--------------------- 7 files changed, 74 insertions(+), 154 deletions(-) diff --git a/common/database.go b/common/database.go index e3c4456..336eb8d 100644 --- a/common/database.go +++ b/common/database.go @@ -121,6 +121,10 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Create(&SimulationModelA).Error) checkErr(test_db.Create(&SimulationModelB).Error) + // Dashboards + checkErr(test_db.Create(&DashboardA).Error) + checkErr(test_db.Create(&DashboardB).Error) + file_A := File{Name: "File_A"} file_B := File{Name: "File_B"} file_C := File{Name: "File_C"} @@ -130,11 +134,6 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Create(&file_C).Error) checkErr(test_db.Create(&file_D).Error) - dab_A := Dashboard{Name: "Dashboard_A"} - dab_B := Dashboard{Name: "Dashboard_B"} - checkErr(test_db.Create(&dab_A).Error) - checkErr(test_db.Create(&dab_B).Error) - widg_A := Widget{Name: "Widget_A"} widg_B := Widget{Name: "Widget_B"} checkErr(test_db.Create(&widg_A).Error) @@ -155,12 +154,12 @@ func DummyPopulateDB(test_db *gorm.DB) { checkErr(test_db.Model(&ScenarioA).Association("SimulationModels").Append(&SimulationModelB).Error) // Scenario HM Dashboards - checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_A).Error) - checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&dab_B).Error) + checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&DashboardA).Error) + checkErr(test_db.Model(&ScenarioA).Association("Dashboards").Append(&DashboardB).Error) // Dashboard HM Widget - checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_A).Error) - checkErr(test_db.Model(&dab_A).Association("Widgets").Append(&widg_B).Error) + checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_A).Error) + checkErr(test_db.Model(&DashboardA).Association("Widgets").Append(&widg_B).Error) // SimulationModel HM Signals checkErr(test_db.Model(&SimulationModelA).Association("InputMapping").Append(&InSignalA).Error) diff --git a/common/models.go b/common/models.go index aa3c618..4fa1b07 100644 --- a/common/models.go +++ b/common/models.go @@ -139,7 +139,7 @@ type Widget struct { // Locked state of widget IsLocked bool `gorm:"default:false"` // Custom properties of widget as JSON string - CustomProperties string + CustomProperties postgres.Jsonb // ID of dashboard to which widget belongs DashboardID uint // Files that belong to widget (for example images) @@ -170,6 +170,7 @@ type File struct { FileData []byte `gorm:"column:FileData"` } +// Credentials type (not for DB) type credentials struct { Username string `json:"username"` Password string `json:"password"` diff --git a/common/responses.go b/common/responses.go index 42cc0ea..19ff0f4 100644 --- a/common/responses.go +++ b/common/responses.go @@ -39,38 +39,38 @@ type SimulatorResponse struct { } type DashboardResponse struct { - ID uint `json:"ID"` - Name string `json:"Name"` - Grid int `json:"Grid"` - ScenarioID uint `json:"ScenarioID"` + ID uint `json:"id"` + Name string `json:"name"` + Grid int `json:"grid"` + ScenarioID uint `json:"scenarioID"` } type WidgetResponse struct { - ID uint `json:"ID"` - Name string `json:"Name"` - Type string `json:"Type"` - Width uint `json:"Width"` - Height uint `json:"Height"` - MinWidth uint `json:"MinWidth"` - MinHeight uint `json:"MinHeight"` - X int `json:"X"` - Y int `json:"Y"` - Z int `json:"Z"` - DashboardID uint `json:"DashboardID"` - IsLocked bool `json:"IsLocked"` - CustomProperties string `json:"CustomProperties"` + ID uint `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Width uint `json:"width"` + Height uint `json:"height"` + MinWidth uint `json:"minWidth"` + MinHeight uint `json:"minHeight"` + X int `json:"x"` + Y int `json:"y"` + Z int `json:"z"` + DashboardID uint `json:"dashboardID"` + IsLocked bool `json:"isLocked"` + CustomProperties postgres.Jsonb `json:"customProperties"` } type FileResponse struct { - Name string `json:"Name"` - ID uint `json:"ID"` - Type string `json:"Type"` - Size uint `json:"Size"` - H uint `json:"ImageHeight"` - W uint `json:"ImageWidth"` - Date string `json:"Date"` - WidgetID uint `json:"WidgetID"` - SimulationModelID uint `json:"SimulationModelID"` + Name string `json:"name"` + ID uint `json:"id"` + Type string `json:"type"` + Size uint `json:"size"` + H uint `json:"imageHeight"` + W uint `json:"imageWidth"` + Date string `json:"date"` + WidgetID uint `json:"widgetID"` + SimulationModelID uint `json:"simulationModelID"` } type SignalResponse struct { diff --git a/common/testdata.go b/common/testdata.go index 4c4edc8..265bdae 100644 --- a/common/testdata.go +++ b/common/testdata.go @@ -338,3 +338,14 @@ var InSignalCUpdated_response = SignalResponse{ Unit: InSignalCUpdated.Unit, SimulationModelID: InSignalCUpdated.SimulationModelID, } + +// Dashboards + +var DashboardA = Dashboard{ID: 1, Name: "Dashboard_A", Grid: 15, ScenarioID: 1} +var DashboardA_response = DashboardResponse{ID: DashboardA.ID, Name: DashboardA.Name, Grid: DashboardA.Grid, ScenarioID: DashboardA.ScenarioID} +var DashboardB = Dashboard{ID: 2, Name: "Dashboard_B", Grid: 10, ScenarioID: 1} +var DashboardB_response = DashboardResponse{ID: DashboardB.ID, Name: DashboardB.Name, Grid: DashboardB.Grid, ScenarioID: DashboardB.ScenarioID} +var DashboardC = Dashboard{ID: 3, Name: "Dashboard_C", Grid: 25, ScenarioID: 1} +var DashboardC_response = DashboardResponse{ID: DashboardC.ID, Name: DashboardC.Name, Grid: DashboardC.Grid, ScenarioID: DashboardC.ScenarioID} +var DashboardCUpdated = Dashboard{ID: DashboardC.ID, Name: "Dashboard_Cupdated", Grid: 24, ScenarioID: DashboardC.ScenarioID} +var DashboardCUpdated_response = DashboardResponse{ID: DashboardCUpdated.ID, Name: DashboardCUpdated.Name, Grid: DashboardCUpdated.Grid, ScenarioID: DashboardCUpdated.ScenarioID} diff --git a/routes/dashboard/dashboard_endpoints.go b/routes/dashboard/dashboard_endpoints.go index b8a6e5d..be2f1ee 100644 --- a/routes/dashboard/dashboard_endpoints.go +++ b/routes/dashboard/dashboard_endpoints.go @@ -56,7 +56,7 @@ func getDashboards(c *gin.Context) { // @Accept json // @Produce json // @Tags dashboards -// @Param inputDab body common.DashboardResponse true "Dashboard to be added incl. ID of Scenario" +// @Param inputDab body common.ResponseMsgDashboard true "Dashboard to be added incl. ID of Scenario" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -65,8 +65,8 @@ func getDashboards(c *gin.Context) { // @Router /dashboards [post] func addDashboard(c *gin.Context) { - var newDab Dashboard - err := c.BindJSON(&newDab) + var newDabData common.ResponseMsgDashboard + err := c.BindJSON(&newDabData) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() c.JSON(http.StatusBadRequest, gin.H{ @@ -75,6 +75,12 @@ func addDashboard(c *gin.Context) { return } + var newDab Dashboard + newDab.ID = newDabData.Dashboard.ID + newDab.Grid = newDabData.Dashboard.Grid + newDab.ScenarioID = newDabData.Dashboard.ScenarioID + newDab.Name = newDabData.Dashboard.Name + ok, _ := scenario.CheckPermissions(c, common.Create, "body", int(newDab.ScenarioID)) if !ok { return @@ -96,7 +102,7 @@ func addDashboard(c *gin.Context) { // @Tags dashboards // @Accept json // @Produce json -// @Param inputDab body common.DashboardResponse true "Dashboard to be updated" +// @Param inputDab body common.ResponseMsgDashboard true "Dashboard to be updated" // @Success 200 "OK." // @Failure 401 "Unauthorized Access" // @Failure 403 "Access forbidden." @@ -111,7 +117,7 @@ func updateDashboard(c *gin.Context) { return } - var modifiedDab Dashboard + var modifiedDab common.ResponseMsgDashboard err := c.BindJSON(&modifiedDab) if err != nil { errormsg := "Bad request. Error binding form data to JSON: " + err.Error() @@ -121,7 +127,7 @@ func updateDashboard(c *gin.Context) { return } - err = d.update(modifiedDab) + err = d.update(modifiedDab.Dashboard) if common.ProvideErrorResponse(c, err) == false { c.JSON(http.StatusOK, gin.H{ "message": "OK.", diff --git a/routes/dashboard/dashboard_methods.go b/routes/dashboard/dashboard_methods.go index 33c94bf..a534b67 100644 --- a/routes/dashboard/dashboard_methods.go +++ b/routes/dashboard/dashboard_methods.go @@ -46,7 +46,7 @@ func (d *Dashboard) addToScenario() error { return err } -func (d *Dashboard) update(modifiedDab Dashboard) error { +func (d *Dashboard) update(modifiedDab common.DashboardResponse) error { db := common.GetDB() diff --git a/routes/dashboard/dashboard_test.go b/routes/dashboard/dashboard_test.go index 2cc0237..b844f59 100644 --- a/routes/dashboard/dashboard_test.go +++ b/routes/dashboard/dashboard_test.go @@ -10,84 +10,16 @@ import ( "git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user" ) -var token string - -type credentials struct { - Username string `json:"username"` - Password string `json:"password"` -} - -var cred = credentials{ - Username: "User_A", - Password: "abc123", -} - -var msgOK = common.ResponseMsg{ - Message: "OK.", -} - -var dabA = common.DashboardResponse{ - ID: 1, - Name: "Dashboard_A", - Grid: 15, - ScenarioID: 1, -} - -var dabB = common.DashboardResponse{ - ID: 2, - Name: "Dashboard_B", - Grid: 15, - ScenarioID: 1, -} - -var dabC = common.Dashboard{ - ID: 3, - Name: "Dashboard_C", - Grid: 99, - ScenarioID: 1, -} - -var dabCupdated = common.Dashboard{ - ID: dabC.ID, - Name: "Dashboard_CUpdated", - ScenarioID: dabC.ScenarioID, - Grid: dabC.Grid, -} - -var dabC_response = common.DashboardResponse{ - ID: dabC.ID, - Name: dabC.Name, - Grid: dabC.Grid, - ScenarioID: dabC.ScenarioID, -} - -var dabC_responseUpdated = common.DashboardResponse{ - ID: dabCupdated.ID, - Name: dabCupdated.Name, - Grid: dabCupdated.Grid, - ScenarioID: dabCupdated.ScenarioID, -} - -var myDashboards = []common.DashboardResponse{ - dabA, - dabB, -} - -var msgDashboards = common.ResponseMsgDashboards{ - Dashboards: myDashboards, -} - -var msgDab = common.ResponseMsgDashboard{ - Dashboard: dabC_response, -} - -var msgDabupdated = common.ResponseMsgDashboard{ - Dashboard: dabC_responseUpdated, -} - // Test /dashboards endpoints func TestEndpoints(t *testing.T) { + var token string + + var myDashboards = []common.DashboardResponse{common.DashboardA_response, common.DashboardB_response} + var msgDashboards = common.ResponseMsgDashboards{Dashboards: myDashboards} + var msgDab = common.ResponseMsgDashboard{Dashboard: common.DashboardC_response} + var msgDabupdated = common.ResponseMsgDashboard{Dashboard: common.DashboardCUpdated_response} + db := common.DummyInitDB() defer db.Close() common.DummyPopulateDB(db) @@ -103,40 +35,11 @@ func TestEndpoints(t *testing.T) { RegisterDashboardEndpoints(api.Group("/dashboards")) - credjson, err := json.Marshal(cred) - if err != nil { - panic(err) - } - - msgOKjson, err := json.Marshal(msgOK) - if err != nil { - panic(err) - } - - msgDashboardsjson, err := json.Marshal(msgDashboards) - if err != nil { - panic(err) - } - - msgDabjson, err := json.Marshal(msgDab) - if err != nil { - panic(err) - } - - msgDabupdatedjson, err := json.Marshal(msgDabupdated) - if err != nil { - panic(err) - } - - dabCjson, err := json.Marshal(dabC) - if err != nil { - panic(err) - } - - dabCupdatedjson, err := json.Marshal(dabCupdated) - if err != nil { - panic(err) - } + credjson, _ := json.Marshal(common.CredUser) + msgOKjson, _ := json.Marshal(common.MsgOK) + msgDashboardsjson, _ := json.Marshal(msgDashboards) + msgDabjson, _ := json.Marshal(msgDab) + msgDabupdatedjson, _ := json.Marshal(msgDabupdated) token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200) @@ -144,13 +47,13 @@ func TestEndpoints(t *testing.T) { common.TestEndpoint(t, router, token, "/api/dashboards?scenarioID=1", "GET", nil, 200, msgDashboardsjson) // test POST dashboards - common.TestEndpoint(t, router, token, "/api/dashboards", "POST", dabCjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/dashboards", "POST", msgDabjson, 200, msgOKjson) // test GET dashboards/:dashboardID to check if previous POST worked correctly common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabjson) // test PUT dashboards/:dashboardID - common.TestEndpoint(t, router, token, "/api/dashboards/3", "PUT", dabCupdatedjson, 200, msgOKjson) + common.TestEndpoint(t, router, token, "/api/dashboards/3", "PUT", msgDabupdatedjson, 200, msgOKjson) common.TestEndpoint(t, router, token, "/api/dashboards/3", "GET", nil, 200, msgDabupdatedjson) // test DELETE dashboards/:dashboardID