mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
add testing for widget endpoints
This commit is contained in:
parent
687e3863e4
commit
7c9318de8f
7 changed files with 233 additions and 38 deletions
|
@ -95,6 +95,8 @@ test:backend:endpoints:
|
|||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
- cd ../visualization
|
||||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
- cd ../widget
|
||||
- go test -v -args -dbhost=/var/run/postgresql
|
||||
dependencies:
|
||||
- build:backend
|
||||
|
||||
|
|
|
@ -143,7 +143,7 @@ type Widget struct {
|
|||
// Custom properties of widget as JSON string
|
||||
CustomProperties string
|
||||
// ID of visualization to which widget belongs
|
||||
VisualizationID uint `gorm:"not null"`
|
||||
VisualizationID uint
|
||||
// Files that belong to widget (for example images)
|
||||
Files []File `gorm:"foreignkey:WidgetID"`
|
||||
}
|
||||
|
|
|
@ -121,3 +121,11 @@ type ResponseMsgVisualizations struct {
|
|||
type ResponseMsgVisualization struct {
|
||||
Visualization VisualizationResponse `json:"visualization"`
|
||||
}
|
||||
|
||||
type ResponseMsgWidgets struct {
|
||||
Widgets []WidgetResponse `json:"widgets"`
|
||||
}
|
||||
|
||||
type ResponseMsgWidget struct {
|
||||
Widget WidgetResponse `json:"widget"`
|
||||
}
|
||||
|
|
|
@ -35,37 +35,6 @@ func ProvideErrorResponse(c *gin.Context, err error) bool {
|
|||
return false // No error
|
||||
}
|
||||
|
||||
func GetVisualizationID(c *gin.Context) (int, error) {
|
||||
|
||||
simID, err := strconv.Atoi(c.Param("visualizationID"))
|
||||
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("Bad request. No or incorrect format of visualization ID")
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": errormsg,
|
||||
})
|
||||
return -1, err
|
||||
} else {
|
||||
return simID, err
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func GetWidgetID(c *gin.Context) (int, error) {
|
||||
|
||||
widgetID, err := strconv.Atoi(c.Param("widgetID"))
|
||||
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("Bad request. No or incorrect format of widget ID")
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": errormsg,
|
||||
})
|
||||
return -1, err
|
||||
} else {
|
||||
return widgetID, err
|
||||
}
|
||||
}
|
||||
|
||||
func GetFileID(c *gin.Context) (int, error) {
|
||||
|
||||
fileID, err := strconv.Atoi(c.Param("fileID"))
|
||||
|
|
|
@ -122,7 +122,7 @@ func updateWidget(c *gin.Context) {
|
|||
err = w.update(modifiedWidget)
|
||||
if common.ProvideErrorResponse(c, err) == false {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "OK",
|
||||
"message": "OK.",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,11 +21,6 @@ func CheckPermissions(c *gin.Context, operation common.CRUD) (bool, Widget) {
|
|||
return false, w
|
||||
}
|
||||
|
||||
ok, _ := visualization.CheckPermissions(c, operation, "query", -1)
|
||||
if !ok {
|
||||
return false, w
|
||||
}
|
||||
|
||||
widgetID, err := strconv.Atoi(c.Param("widgetID"))
|
||||
if err != nil {
|
||||
errormsg := fmt.Sprintf("Bad request. No or incorrect format of widgetID path parameter")
|
||||
|
@ -40,5 +35,10 @@ func CheckPermissions(c *gin.Context, operation common.CRUD) (bool, Widget) {
|
|||
return false, w
|
||||
}
|
||||
|
||||
ok, _ := visualization.CheckPermissions(c, operation, "body", int(w.VisualizationID))
|
||||
if !ok {
|
||||
return false, w
|
||||
}
|
||||
|
||||
return true, w
|
||||
}
|
||||
|
|
216
routes/widget/widget_test.go
Normal file
216
routes/widget/widget_test.go
Normal file
|
@ -0,0 +1,216 @@
|
|||
package widget
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
||||
"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 wdgA = common.WidgetResponse{
|
||||
ID: 1,
|
||||
Name: "Widget_A",
|
||||
Type: "",
|
||||
Height: 0,
|
||||
Width: 0,
|
||||
MinHeight: 0,
|
||||
MinWidth: 0,
|
||||
X: 0,
|
||||
Y: 0,
|
||||
Z: 0,
|
||||
IsLocked: false,
|
||||
CustomProperties: "",
|
||||
VisualizationID: 1,
|
||||
}
|
||||
|
||||
var wdgB = common.WidgetResponse{
|
||||
ID: 2,
|
||||
Name: "Widget_B",
|
||||
Type: "",
|
||||
Height: 0,
|
||||
Width: 0,
|
||||
MinHeight: 0,
|
||||
MinWidth: 0,
|
||||
X: 0,
|
||||
Y: 0,
|
||||
Z: 0,
|
||||
IsLocked: false,
|
||||
CustomProperties: "",
|
||||
VisualizationID: 1,
|
||||
}
|
||||
|
||||
var wdgC = common.Widget{
|
||||
ID: 3,
|
||||
Name: "Widget_C",
|
||||
Type: "",
|
||||
Height: 30,
|
||||
Width: 100,
|
||||
MinHeight: 20,
|
||||
MinWidth: 50,
|
||||
X: 11,
|
||||
Y: 12,
|
||||
Z: 13,
|
||||
IsLocked: false,
|
||||
CustomProperties: "",
|
||||
VisualizationID: 1,
|
||||
}
|
||||
|
||||
var wdgCupdated = common.Widget{
|
||||
ID: wdgC.ID,
|
||||
Name: "Widget_CUpdated",
|
||||
Type: wdgC.Type,
|
||||
Height: wdgC.Height,
|
||||
Width: wdgC.Width,
|
||||
MinHeight: wdgC.MinHeight,
|
||||
MinWidth: wdgC.MinWidth,
|
||||
X: wdgC.X,
|
||||
Y: wdgC.Y,
|
||||
Z: wdgC.Z,
|
||||
IsLocked: wdgC.IsLocked,
|
||||
CustomProperties: wdgC.CustomProperties,
|
||||
VisualizationID: wdgC.VisualizationID,
|
||||
}
|
||||
|
||||
var wdgC_response = common.WidgetResponse{
|
||||
ID: wdgC.ID,
|
||||
Name: wdgC.Name,
|
||||
Type: wdgC.Type,
|
||||
Height: wdgC.Height,
|
||||
Width: wdgC.Width,
|
||||
MinHeight: wdgC.MinHeight,
|
||||
MinWidth: wdgC.MinWidth,
|
||||
X: wdgC.X,
|
||||
Y: wdgC.Y,
|
||||
Z: wdgC.Z,
|
||||
IsLocked: wdgC.IsLocked,
|
||||
CustomProperties: wdgC.CustomProperties,
|
||||
VisualizationID: wdgC.VisualizationID,
|
||||
}
|
||||
|
||||
var wdgC_responseUpdated = common.WidgetResponse{
|
||||
ID: wdgC.ID,
|
||||
Name: "Widget_CUpdated",
|
||||
Type: wdgC.Type,
|
||||
Height: wdgC.Height,
|
||||
Width: wdgC.Width,
|
||||
MinHeight: wdgC.MinHeight,
|
||||
MinWidth: wdgC.MinWidth,
|
||||
X: wdgC.X,
|
||||
Y: wdgC.Y,
|
||||
Z: wdgC.Z,
|
||||
IsLocked: wdgC.IsLocked,
|
||||
CustomProperties: wdgC.CustomProperties,
|
||||
VisualizationID: wdgC.VisualizationID,
|
||||
}
|
||||
|
||||
var myWidgets = []common.WidgetResponse{
|
||||
wdgA,
|
||||
wdgB,
|
||||
}
|
||||
|
||||
var msgWidgets = common.ResponseMsgWidgets{
|
||||
Widgets: myWidgets,
|
||||
}
|
||||
|
||||
var msgWdg = common.ResponseMsgWidget{
|
||||
Widget: wdgC_response,
|
||||
}
|
||||
|
||||
var msgWdgupdated = common.ResponseMsgWidget{
|
||||
Widget: wdgC_responseUpdated,
|
||||
}
|
||||
|
||||
// Test /models endpoints
|
||||
func TestSimulationModelEndpoints(t *testing.T) {
|
||||
|
||||
db := common.DummyInitDB()
|
||||
defer db.Close()
|
||||
common.DummyPopulateDB(db)
|
||||
|
||||
router := gin.Default()
|
||||
api := router.Group("/api")
|
||||
|
||||
// All endpoints require authentication except when someone wants to
|
||||
// login (POST /authenticate)
|
||||
user.VisitorAuthenticate(api.Group("/authenticate"))
|
||||
|
||||
api.Use(user.Authentication(true))
|
||||
|
||||
RegisterWidgetEndpoints(api.Group("/widgets"))
|
||||
|
||||
credjson, err := json.Marshal(cred)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgOKjson, err := json.Marshal(msgOK)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgWidgetsjson, err := json.Marshal(msgWidgets)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgWdgjson, err := json.Marshal(msgWdg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
msgWdgupdatedjson, err := json.Marshal(msgWdgupdated)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wdgCjson, err := json.Marshal(wdgC)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wdgCupdatedjson, err := json.Marshal(wdgCupdated)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||
|
||||
// test GET models
|
||||
common.TestEndpoint(t, router, token, "/api/widgets?visualizationID=1", "GET", nil, 200, string(msgWidgetsjson))
|
||||
|
||||
// test POST models
|
||||
common.TestEndpoint(t, router, token, "/api/widgets", "POST", wdgCjson, 200, string(msgOKjson))
|
||||
|
||||
// test GET models/:ModelID to check if previous POST worked correctly
|
||||
common.TestEndpoint(t, router, token, "/api/widgets/3", "GET", nil, 200, string(msgWdgjson))
|
||||
|
||||
// test PUT models/:ModelID
|
||||
common.TestEndpoint(t, router, token, "/api/widgets/3", "PUT", wdgCupdatedjson, 200, string(msgOKjson))
|
||||
common.TestEndpoint(t, router, token, "/api/widgets/3", "GET", nil, 200, string(msgWdgupdatedjson))
|
||||
|
||||
// test DELETE models/:ModelID
|
||||
common.TestEndpoint(t, router, token, "/api/widgets/3", "DELETE", nil, 200, string(msgOKjson))
|
||||
common.TestEndpoint(t, router, token, "/api/widgets?visualizationID=1", "GET", nil, 200, string(msgWidgetsjson))
|
||||
|
||||
// TODO add testing for other return codes
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue