remove basePath and basePath settings

This commit is contained in:
Steffen Vogel 2021-02-05 19:26:15 +01:00
parent b24387a22a
commit e4d200e6fd
19 changed files with 792 additions and 625 deletions

View file

@ -50,8 +50,6 @@ func InitConfig() error {
configFile = flag.String("config", "", "Path to YAML configuration file")
mode = flag.String("mode", "release", "Select debug/release/test mode (default is release)")
port = flag.String("port", "4000", "Port of the backend (default is 4000)")
baseHost = flag.String("base-host", "localhost:4000", "The host at which the backend is hosted (default: localhost)")
basePath = flag.String("base-path", "/api/v2", "The path at which the API routes are located (default /api/v2)")
adminUser = flag.String("admin-user", "", "Initial admin username")
adminPass = flag.String("admin-pass", "", "Initial admin password")
adminMail = flag.String("admin-mail", "", "Initial admin mail address")
@ -85,8 +83,6 @@ func InitConfig() error {
"amqp.pass": *amqpPass,
"mode": *mode,
"port": *port,
"base.host": *baseHost,
"base.path": *basePath,
"admin.user": *adminUser,
"admin.pass": *adminPass,
"admin.mail": *adminMail,
@ -165,39 +161,29 @@ func InitConfig() error {
return nil
}
func ConfigureBackend() (string, string, string, string, string, string, string, error) {
func ConfigureBackend() (string, string, string, string, string, error) {
err := InitConfig()
if err != nil {
log.Printf("Error during initialization of global configuration: %v, aborting.", err.Error())
return "", "", "", "", "", "", "", err
return "", "", "", "", "", err
}
mode, err := GlobalConfig.String("mode")
if err != nil {
log.Printf("Error reading mode from global configuration: %v, aborting.", err.Error())
return "", "", "", "", "", "", "", err
return "", "", "", "", "", err
}
baseHost, err := GlobalConfig.String("base.host")
if err != nil {
log.Printf("Error reading base.host from global configuration: %v, aborting.", err.Error())
return "", "", "", "", "", "", "", err
}
basePath, err := GlobalConfig.String("base.path")
if err != nil {
log.Printf("Error reading base.path from global configuration: %v, aborting.", err.Error())
return "", "", "", "", "", "", "", err
}
port, err := GlobalConfig.String("port")
if err != nil {
log.Printf("Error reading port from global configuration: %v, aborting.", err.Error())
return "", "", "", "", "", "", "", err
return "", "", "", "", "", err
}
AMQPhost, _ := GlobalConfig.String("amqp.host")
AMQPuser, _ := GlobalConfig.String("amqp.user")
AMQPpass, _ := GlobalConfig.String("amqp.pass")
return mode, baseHost, basePath, port, AMQPhost, AMQPuser, AMQPpass, nil
return mode, port, AMQPhost, AMQPuser, AMQPpass, nil
}

View file

@ -32,6 +32,41 @@ var doc = `{
"basePath": "{{.BasePath}}",
"paths": {
"/authenticate": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authentication"
],
"summary": "Check if user is authenticated and provide details on how the user can authenticate",
"operationId": "authenticated",
"responses": {
"200": {
"description": "JSON web token, success status, message and authenticated user object",
"schema": {
"$ref": "#/definitions/api.ResponseAuthenticate"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/api.ResponseError"
}
},
"500": {
"description": "Internal server error.",
"schema": {
"$ref": "#/definitions/api.ResponseError"
}
}
}
}
},
"/authenticate/{mechanism}": {
"post": {
"consumes": [
"application/json"
@ -53,6 +88,17 @@ var doc = `{
"schema": {
"$ref": "#/definitions/user.loginRequest"
}
},
{
"enum": [
"internal",
"external"
],
"type": "string",
"description": "Login mechanism",
"name": "mechanism",
"in": "path",
"required": true
}
],
"responses": {
@ -77,6 +123,26 @@ var doc = `{
}
}
},
"/config": {
"get": {
"produces": [
"application/json"
],
"tags": [
"config"
],
"summary": "Get config VILLASweb to be used by frontend",
"operationId": "config",
"responses": {
"200": {
"description": "The configuration",
"schema": {
"$ref": "#/definitions/config.Config"
}
}
}
}
},
"/configs": {
"get": {
"security": [
@ -3346,6 +3412,62 @@ var doc = `{
}
}
},
"config.Config": {
"type": "object",
"properties": {
"authentication": {
"$ref": "#/definitions/config.ConfigAuthentication"
},
"contact": {
"$ref": "#/definitions/config.ConfigContact"
},
"mode": {
"type": "string"
},
"sub_title": {
"type": "string"
},
"title": {
"type": "string"
}
}
},
"config.ConfigAuthentication": {
"type": "object",
"properties": {
"external": {
"$ref": "#/definitions/config.ConfigAuthenticationExternal"
},
"logout_url": {
"type": "string"
}
}
},
"config.ConfigAuthenticationExternal": {
"type": "object",
"properties": {
"authorize_url": {
"type": "string"
},
"enabled": {
"type": "boolean"
},
"provider_name": {
"type": "string"
}
}
},
"config.ConfigContact": {
"type": "object",
"properties": {
"mail": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"dashboard.addDashboardRequest": {
"type": "object",
"properties": {

View file

@ -16,6 +16,41 @@
"basePath": "/api/v2",
"paths": {
"/authenticate": {
"get": {
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"authentication"
],
"summary": "Check if user is authenticated and provide details on how the user can authenticate",
"operationId": "authenticated",
"responses": {
"200": {
"description": "JSON web token, success status, message and authenticated user object",
"schema": {
"$ref": "#/definitions/api.ResponseAuthenticate"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"$ref": "#/definitions/api.ResponseError"
}
},
"500": {
"description": "Internal server error.",
"schema": {
"$ref": "#/definitions/api.ResponseError"
}
}
}
}
},
"/authenticate/{mechanism}": {
"post": {
"consumes": [
"application/json"
@ -37,6 +72,17 @@
"schema": {
"$ref": "#/definitions/user.loginRequest"
}
},
{
"enum": [
"internal",
"external"
],
"type": "string",
"description": "Login mechanism",
"name": "mechanism",
"in": "path",
"required": true
}
],
"responses": {
@ -61,6 +107,26 @@
}
}
},
"/config": {
"get": {
"produces": [
"application/json"
],
"tags": [
"config"
],
"summary": "Get config VILLASweb to be used by frontend",
"operationId": "config",
"responses": {
"200": {
"description": "The configuration",
"schema": {
"$ref": "#/definitions/config.Config"
}
}
}
}
},
"/configs": {
"get": {
"security": [
@ -3330,6 +3396,62 @@
}
}
},
"config.Config": {
"type": "object",
"properties": {
"authentication": {
"$ref": "#/definitions/config.ConfigAuthentication"
},
"contact": {
"$ref": "#/definitions/config.ConfigContact"
},
"mode": {
"type": "string"
},
"sub_title": {
"type": "string"
},
"title": {
"type": "string"
}
}
},
"config.ConfigAuthentication": {
"type": "object",
"properties": {
"external": {
"$ref": "#/definitions/config.ConfigAuthenticationExternal"
},
"logout_url": {
"type": "string"
}
}
},
"config.ConfigAuthenticationExternal": {
"type": "object",
"properties": {
"authorize_url": {
"type": "string"
},
"enabled": {
"type": "boolean"
},
"provider_name": {
"type": "string"
}
}
},
"config.ConfigContact": {
"type": "object",
"properties": {
"mail": {
"type": "string"
},
"name": {
"type": "string"
}
}
},
"dashboard.addDashboardRequest": {
"type": "object",
"properties": {

View file

@ -83,6 +83,42 @@ definitions:
StartParameters:
$ref: '#/definitions/postgres.Jsonb'
type: object
config.Config:
properties:
authentication:
$ref: '#/definitions/config.ConfigAuthentication'
contact:
$ref: '#/definitions/config.ConfigContact'
mode:
type: string
sub_title:
type: string
title:
type: string
type: object
config.ConfigAuthentication:
properties:
external:
$ref: '#/definitions/config.ConfigAuthenticationExternal'
logout_url:
type: string
type: object
config.ConfigAuthenticationExternal:
properties:
authorize_url:
type: string
enabled:
type: boolean
provider_name:
type: string
type: object
config.ConfigContact:
properties:
mail:
type: string
name:
type: string
type: object
dashboard.addDashboardRequest:
properties:
dashboard:
@ -446,6 +482,31 @@ info:
version: "2.0"
paths:
/authenticate:
get:
consumes:
- application/json
operationId: authenticated
produces:
- application/json
responses:
"200":
description: JSON web token, success status, message and authenticated user
object
schema:
$ref: '#/definitions/api.ResponseAuthenticate'
"401":
description: Unauthorized
schema:
$ref: '#/definitions/api.ResponseError'
"500":
description: Internal server error.
schema:
$ref: '#/definitions/api.ResponseError'
summary: Check if user is authenticated and provide details on how the user
can authenticate
tags:
- authentication
/authenticate/{mechanism}:
post:
consumes:
- application/json
@ -457,11 +518,20 @@ paths:
required: true
schema:
$ref: '#/definitions/user.loginRequest'
- description: Login mechanism
enum:
- internal
- external
in: path
name: mechanism
required: true
type: string
produces:
- application/json
responses:
"200":
description: JSON web token, success status, message and authenticated user object
description: JSON web token, success status, message and authenticated user
object
schema:
$ref: '#/definitions/api.ResponseAuthenticate'
"401":
@ -475,6 +545,19 @@ paths:
summary: Authentication for user
tags:
- authentication
/config:
get:
operationId: config
produces:
- application/json
responses:
"200":
description: The configuration
schema:
$ref: '#/definitions/config.Config'
summary: Get config VILLASweb to be used by frontend
tags:
- config
/configs:
get:
operationId: getConfigs
@ -513,7 +596,8 @@ paths:
- application/json
operationId: addConfig
parameters:
- description: component configuration to be added incl. IDs of scenario and IC
- description: component configuration to be added incl. IDs of scenario and
IC
in: body
name: inputConfig
required: true
@ -1078,7 +1162,8 @@ paths:
- application/json
responses:
"204":
description: Backend is healthy, database and AMQP broker connections are alive
description: Backend is healthy, database and AMQP broker connections are
alive
schema:
type: string
"500":
@ -1310,7 +1395,8 @@ paths:
$ref: '#/definitions/api.ResponseError'
security:
- Bearer: []
summary: Send an action to IC (only available if backend server is started with -amqp parameter)
summary: Send an action to IC (only available if backend server is started with
-amqp parameter)
tags:
- infrastructure-components
/ic/{ICID}/configs:
@ -1986,7 +2072,8 @@ paths:
- application/json
operationId: AddSignal
parameters:
- description: A signal to be added to the component configuration incl. direction and config ID to which signal shall be added
- description: A signal to be added to the component configuration incl. direction
and config ID to which signal shall be added
in: body
name: inputSignal
required: true
@ -2271,7 +2358,8 @@ paths:
- application/json
operationId: UpdateUser
parameters:
- description: User to be updated (anything except for ID can be changed, role can only be change by admin)
- description: User to be updated (anything except for ID can be changed, role
can only be change by admin)
in: body
name: inputUser
required: true

View file

@ -266,8 +266,7 @@ func CompareResponse(resp *bytes.Buffer, expected interface{}) error {
}
// Authenticate a user for testing purposes
func AuthenticateForTest(router *gin.Engine, url string,
method string, credentials interface{}) (string, error) {
func AuthenticateForTest(router *gin.Engine, credentials interface{}) (string, error) {
w := httptest.NewRecorder()
@ -277,7 +276,7 @@ func AuthenticateForTest(router *gin.Engine, url string,
return "", fmt.Errorf("Failed to marshal credentials: %v", err)
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
req, err := http.NewRequest("POST", "/api/v2/authenticate/internal", bytes.NewBuffer(body))
if err != nil {
return "", fmt.Errorf("Failed to create new request: %v", err)
}

View file

@ -39,8 +39,8 @@ import (
)
var router *gin.Engine
var base_api_configs = "/api/configs"
var base_api_auth = "/api/authenticate"
var base_api_configs = "/api/v2/configs"
var base_api_auth = "/api/v2/authenticate"
type ConfigRequest struct {
Name string `json:"name,omitempty"`
@ -83,8 +83,7 @@ func newFalse() *bool {
func addScenarioAndIC() (scenarioID uint, ICID uint) {
// authenticate as admin
token, _ := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.AdminCredentials)
token, _ := helper.AuthenticateForTest(router, helper.AdminCredentials)
// POST $newICA
newICA := ICRequest{
@ -101,7 +100,7 @@ func addScenarioAndIC() (scenarioID uint, ICID uint) {
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newICA})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newICA})
if code != 200 || err != nil {
fmt.Println("Adding IC returned code", code, err, resp)
}
@ -111,14 +110,13 @@ func addScenarioAndIC() (scenarioID uint, ICID uint) {
// POST a second IC to change to that IC during testing
code, resp, err = helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newICA})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newICA})
if code != 200 || err != nil {
fmt.Println("Adding IC returned code", code, err, resp)
}
// authenticate as normal user
token, _ = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, _ = helper.AuthenticateForTest(router, helper.UserACredentials)
// POST $newScenario
newScenario := ScenarioRequest{
@ -127,7 +125,7 @@ func addScenarioAndIC() (scenarioID uint, ICID uint) {
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
if code != 200 || err != nil {
fmt.Println("Adding Scenario returned code", code, err, resp)
}
@ -137,7 +135,7 @@ func addScenarioAndIC() (scenarioID uint, ICID uint) {
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID), uint(newICID)
}
@ -155,7 +153,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -183,8 +181,7 @@ func TestAddConfig(t *testing.T) {
newConfig1.ScenarioID = scenarioID
newConfig1.ICID = ICID
// authenticate as normal userB who has no access to new scenario
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err := helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to POST with no access
@ -195,8 +192,7 @@ func TestAddConfig(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST non JSON body
@ -206,8 +202,7 @@ func TestAddConfig(t *testing.T) {
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newConfig
@ -246,8 +241,7 @@ func TestAddConfig(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// Try to GET the newConfig with no access
@ -271,8 +265,7 @@ func TestUpdateConfig(t *testing.T) {
scenarioID, ICID := addScenarioAndIC()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newConfig
@ -293,8 +286,7 @@ func TestUpdateConfig(t *testing.T) {
}
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to PUT with no access
@ -305,8 +297,7 @@ func TestUpdateConfig(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as guest user who has access to component config
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to PUT as guest
@ -317,8 +308,7 @@ func TestUpdateConfig(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to PUT a non JSON body
@ -380,8 +370,7 @@ func TestDeleteConfig(t *testing.T) {
newConfig1.ICID = ICID
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newConfig
@ -395,8 +384,7 @@ func TestDeleteConfig(t *testing.T) {
assert.NoError(t, err)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to DELETE with no access
@ -407,8 +395,7 @@ func TestDeleteConfig(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the component config returned for scenario
@ -447,8 +434,7 @@ func TestGetAllConfigsOfScenario(t *testing.T) {
newConfig1.ICID = ICID
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newConfig
@ -465,8 +451,7 @@ func TestGetAllConfigsOfScenario(t *testing.T) {
assert.Equal(t, 1, NumberOfConfigs)
// authenticate as normal userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get configs without access

View file

@ -51,8 +51,6 @@ type Config struct {
Title string `json:"title"`
SubTitle string `json:"sub_title"`
Mode string `json:"mode"`
BaseHost string `json:"base_host"`
BasePath string `json:"base_path"`
Contact ConfigContact `json:"contact"`
Authentication ConfigAuthentication `json:"authentication"`
}
@ -71,8 +69,6 @@ func getConfig(c *gin.Context) {
resp := &Config{}
resp.Mode, _ = cfg.String("mode")
resp.BaseHost, _ = cfg.String("base.host")
resp.BasePath, _ = cfg.String("base.path")
resp.Authentication.LogoutURL, _ = cfg.String("auth.logout-url")
resp.Authentication.External.Enabled, _ = cfg.Bool("auth.external")
resp.Authentication.External.AuthorizeURL, _ = cfg.String("auth.external.authorize-url")

View file

@ -67,7 +67,7 @@ func addScenario(token string) (scenarioID uint) {
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
_, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
if err != nil {
log.Panic("The following error happend on POSTing a scenario: ", err.Error())
}
@ -77,7 +77,7 @@ func addScenario(token string) (scenarioID uint) {
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID)
}
@ -94,7 +94,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -112,8 +112,7 @@ func TestAddDashboard(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
scenarioID := addScenario(token)
@ -121,7 +120,7 @@ func TestAddDashboard(t *testing.T) {
// test POST dashboards/ $newDashboad
newDashboard.ScenarioID = scenarioID
code, resp, err := helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -135,7 +134,7 @@ func TestAddDashboard(t *testing.T) {
// Get the newDashboard
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -150,13 +149,13 @@ func TestAddDashboard(t *testing.T) {
}
// this should NOT work and return a unprocessable entity 442 status code
code, resp, err = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": malformedNewDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": malformedNewDashboard})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// this should NOT work and return a bad request 400 status code
code, resp, err = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", "This is a test using plain text as body")
"/api/v2/dashboards", "POST", "This is a test using plain text as body")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -164,18 +163,17 @@ func TestAddDashboard(t *testing.T) {
// should return not found error
newDashboard.ScenarioID = scenarioID + 1
code, resp, err = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// try to get dashboard as a user that is not in the scenario (userB)
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// this should fail with unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -183,7 +181,7 @@ func TestAddDashboard(t *testing.T) {
// this should give an unprocessable entity error
newDashboard.ScenarioID = scenarioID
code, resp, err = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
}
@ -194,8 +192,7 @@ func TestUpdateDashboard(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
scenarioID := addScenario(token)
@ -203,7 +200,7 @@ func TestUpdateDashboard(t *testing.T) {
// test POST dashboards/ $newDashboard
newDashboard.ScenarioID = scenarioID
code, resp, err := helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -217,24 +214,22 @@ func TestUpdateDashboard(t *testing.T) {
}
// authenticate as guest user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to update a dashboard as guest
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -244,7 +239,7 @@ func TestUpdateDashboard(t *testing.T) {
// Get the updatedDashboard
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -254,13 +249,13 @@ func TestUpdateDashboard(t *testing.T) {
// try to update a dashboard that does not exist (should return not found 404 status code)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID+1), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID+1), "PUT", helper.KeyModels{"dashboard": updatedDashboard})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// try to update with a malformed body, should return a bad request error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "PUT", "This is the body of a malformed update request.")
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "PUT", "This is the body of a malformed update request.")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
}
@ -271,8 +266,7 @@ func TestDeleteDashboard(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
scenarioID := addScenario(token)
@ -280,7 +274,7 @@ func TestDeleteDashboard(t *testing.T) {
// test POST dashboards/ $newDashboard
newDashboard.ScenarioID = scenarioID
code, resp, err := helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -289,35 +283,33 @@ func TestDeleteDashboard(t *testing.T) {
assert.NoError(t, err)
// try to delete a dashboard from a scenario to which the user has no access
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// this should fail with unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "DELETE", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to delete a dashboard that does not exist; should return a not found error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID+1), "DELETE", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID+1), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// Count the number of all the dashboards returned for scenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
// Delete the added newDashboard
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards/%v", newDashboardID), "DELETE", nil)
fmt.Sprintf("/api/v2/dashboards/%v", newDashboardID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -327,7 +319,7 @@ func TestDeleteDashboard(t *testing.T) {
// Again count the number of all the dashboards returned for scenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber-1)
@ -340,21 +332,20 @@ func TestGetAllDashboardsOfScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
scenarioID := addScenario(token)
// Count the number of all the dashboards returned for scenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
// test POST dashboards/ $newDashboard
newDashboard.ScenarioID = scenarioID
code, resp, err := helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -365,31 +356,30 @@ func TestGetAllDashboardsOfScenario(t *testing.T) {
ScenarioID: scenarioID,
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboardB})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboardB})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Count again the number of all the dashboards returned for scenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber+2, finalNumber)
// try to get all dashboards of a scenario that does not exist (should fail with not found)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID+1), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID+1), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// try to get all dashboards as a user that does not belong to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// this should fail with unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/dashboards?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/dashboards?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)

View file

@ -54,12 +54,10 @@ type ScenarioRequest struct {
func addScenario() (scenarioID uint) {
// authenticate as admin
token, _ := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, _ := helper.AuthenticateForTest(router, helper.AdminCredentials)
// authenticate as normal user
token, _ = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, _ = helper.AuthenticateForTest(router, helper.UserACredentials)
// POST $newScenario
newScenario := ScenarioRequest{
@ -68,14 +66,14 @@ func addScenario() (scenarioID uint) {
StartParameters: postgres.Jsonb{RawMessage: json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
_, resp, _ := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
// Read newScenario's ID from the response
newScenarioID, _ := helper.GetResponseID(resp)
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID)
}
@ -92,7 +90,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -114,8 +112,7 @@ func TestAddFile(t *testing.T) {
scenarioID := addScenario()
// authenticate as userB who has no access to the elements in the DB
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err := helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
emptyBuf := &bytes.Buffer{}
@ -123,26 +120,25 @@ func TestAddFile(t *testing.T) {
// try to POST to a scenario to which UserB has no access
// should return a 422 unprocessable entity error
code, resp, err := helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "POST", emptyBuf)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "POST", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal userA
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST without a scenario ID
// should return a bad request error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files"), "POST", emptyBuf)
fmt.Sprintf("/api/v2/files"), "POST", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// try to POST an invalid file
// should return a bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "POST", emptyBuf)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "POST", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -171,7 +167,7 @@ func TestAddFile(t *testing.T) {
// Create the request
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf)
req, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf)
assert.NoError(t, err, "create request")
req.Header.Set("Content-Type", contentType)
@ -185,20 +181,19 @@ func TestAddFile(t *testing.T) {
// Get the new file
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "GET", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
assert.Equalf(t, string(c1), resp.String(), "Response body: \n%v\n", resp)
// authenticate as userB who has no access to the elements in the DB
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get a file to which user has no access
// should return unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "GET", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -215,8 +210,7 @@ func TestUpdateFile(t *testing.T) {
scenarioID := addScenario()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// create a testfile.txt in local folder
@ -243,7 +237,7 @@ func TestUpdateFile(t *testing.T) {
// Create the POST request
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf)
req, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf)
assert.NoError(t, err, "create request")
req.Header.Set("Content-Type", contentType)
@ -256,8 +250,7 @@ func TestUpdateFile(t *testing.T) {
assert.NoError(t, err)
// authenticate as userB who has no access to the elements in the DB
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
emptyBuf := &bytes.Buffer{}
@ -265,32 +258,30 @@ func TestUpdateFile(t *testing.T) {
// try to PUT to a file to which UserB has no access
// should return a 422 unprocessable entity error
code, resp, err := helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "PUT", emptyBuf)
fmt.Sprintf("/api/v2/files/%v", newFileID), "PUT", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as guest user C
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to PUT as guest
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "PUT", emptyBuf)
fmt.Sprintf("/api/v2/files/%v", newFileID), "PUT", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// Prepare update
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to PUT with empty body
// should return bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "PUT", emptyBuf)
fmt.Sprintf("/api/v2/files/%v", newFileID), "PUT", emptyBuf)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -318,7 +309,7 @@ func TestUpdateFile(t *testing.T) {
// Create the PUT request
w_updated := httptest.NewRecorder()
req, err = http.NewRequest("PUT", fmt.Sprintf("/api/files/%v", newFileID), bodyBufUpdated)
req, err = http.NewRequest("PUT", fmt.Sprintf("/api/v2/files/%v", newFileID), bodyBufUpdated)
assert.NoError(t, err, "create request")
req.Header.Set("Content-Type", contentType)
@ -332,7 +323,7 @@ func TestUpdateFile(t *testing.T) {
// Get the updated file
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileIDUpdated), "GET", nil)
fmt.Sprintf("/api/v2/files/%v", newFileIDUpdated), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
assert.Equalf(t, string(c2), resp.String(), "Response body: \n%v\n", resp)
@ -348,8 +339,7 @@ func TestDeleteFile(t *testing.T) {
scenarioID := addScenario()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// create a testfile.txt in local folder
@ -375,7 +365,7 @@ func TestDeleteFile(t *testing.T) {
// Create the request
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf)
req, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf)
assert.NoError(t, err, "create request")
req.Header.Set("Content-Type", contentType)
req.Header.Add("Authorization", "Bearer "+token)
@ -398,7 +388,7 @@ func TestDeleteFile(t *testing.T) {
// Create the request
w2 := httptest.NewRecorder()
req2, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf2)
req2, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf2)
assert.NoError(t, err, "create request")
req2.Header.Set("Content-Type", contentType2)
req2.Header.Add("Authorization", "Bearer "+token)
@ -409,66 +399,62 @@ func TestDeleteFile(t *testing.T) {
assert.NoError(t, err)
// authenticate as userB who has no access to the elements in the DB
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to DELETE file from scenario to which userB has no access
// should return an unprocessable entity error
code, resp, err := helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "DELETE", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all files returned for scenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
// try to DELETE non-existing fileID
// should return not found
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/5"), "DELETE", nil)
fmt.Sprintf("/api/v2/files/5"), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// authenticate as guest user C
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to DELETE file of scenario as guest
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "DELETE", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Delete the added file 1
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID), "DELETE", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Delete the added file 2
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files/%v", newFileID2), "DELETE", nil)
fmt.Sprintf("/api/v2/files/%v", newFileID2), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Again count the number of all the files returned for scenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber-2, finalNumber)
@ -485,31 +471,29 @@ func TestGetAllFilesOfScenario(t *testing.T) {
scenarioID := addScenario()
// authenticate as userB who has no access to the elements in the DB
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err := helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get all files for scenario to which userB has not access
// should return unprocessable entity error
code, resp, err := helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal userA
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
//try to get all files with missing scenario ID; should return a bad request error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/files"), "GET", nil)
fmt.Sprintf("/api/v2/files"), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// Count the number of all files returned for scenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
// create a testfile.txt in local folder
@ -535,7 +519,7 @@ func TestGetAllFilesOfScenario(t *testing.T) {
// Create the request
w := httptest.NewRecorder()
req, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf1)
req, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf1)
assert.NoError(t, err, "create request")
req.Header.Set("Content-Type", contentType1)
req.Header.Add("Authorization", "Bearer "+token)
@ -561,7 +545,7 @@ func TestGetAllFilesOfScenario(t *testing.T) {
bodyWriter2.Close()
w2 := httptest.NewRecorder()
req2, err := http.NewRequest("POST", fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), bodyBuf2)
req2, err := http.NewRequest("POST", fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), bodyBuf2)
assert.NoError(t, err, "create request")
req2.Header.Set("Content-Type", contentType2)
req2.Header.Add("Authorization", "Bearer "+token)
@ -570,7 +554,7 @@ func TestGetAllFilesOfScenario(t *testing.T) {
// Again count the number of all the files returned for scenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/files?scenarioID=%v", scenarioID), "GET", nil)
fmt.Sprintf("/api/v2/files?scenarioID=%v", scenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber+2, finalNumber)
}

View file

@ -24,12 +24,13 @@ package infrastructure_component
import (
"encoding/json"
"fmt"
"github.com/streadway/amqp"
"log"
"os"
"testing"
"time"
"github.com/streadway/amqp"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/helper"
component_configuration "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/component-configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/scenario"
@ -131,7 +132,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api = router.Group("/api")
api = router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -172,14 +173,13 @@ func TestAddICAsAdmin(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// try to POST with non JSON body
// should result in bad request
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", "This is no JSON")
"/api/v2/ic", "POST", "This is no JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -189,13 +189,13 @@ func TestAddICAsAdmin(t *testing.T) {
UUID: newIC2.UUID,
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newMalformedIC})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newMalformedIC})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// test POST ic/ $newIC
code, resp, err = helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -209,7 +209,7 @@ func TestAddICAsAdmin(t *testing.T) {
// Get the newIC
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v", newICID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -221,13 +221,13 @@ func TestAddICAsAdmin(t *testing.T) {
// Try to GET a IC that does not exist
// should result in not found
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID+1), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v", newICID+1), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// test creation of external IC (should lead to emission of AMQP message to VILLAS)
code, resp, err = helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC2})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC2})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -245,8 +245,7 @@ func TestAddICAsUser(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST ic/ $newIC
@ -254,7 +253,7 @@ func TestAddICAsUser(t *testing.T) {
// Normal users are not allowed to add ICs
newIC1.ManagedExternally = newFalse()
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
}
@ -265,13 +264,12 @@ func TestUpdateICAsAdmin(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newIC
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -286,14 +284,14 @@ func TestUpdateICAsAdmin(t *testing.T) {
// try to PUT with non JSON body
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "PUT", "This is no JSON")
fmt.Sprintf("/api/v2/ic/%v", newICID), "PUT", "This is no JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// Test PUT IC
newIC1.WebsocketURL = "ThisIsMyNewURL"
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "PUT", helper.KeyModels{"ic": newIC1})
fmt.Sprintf("/api/v2/ic/%v", newICID), "PUT", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -303,7 +301,7 @@ func TestUpdateICAsAdmin(t *testing.T) {
// Get the updated newIC
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v", newICID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -355,7 +353,7 @@ func TestUpdateICAsAdmin(t *testing.T) {
// Should result in forbidden return code 403
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", 2), "PUT", helper.KeyModels{"ic": updatedIC})
fmt.Sprintf("/api/v2/ic/%v", 2), "PUT", helper.KeyModels{"ic": updatedIC})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
}
@ -366,13 +364,12 @@ func TestUpdateICAsUser(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newIC
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -381,15 +378,14 @@ func TestUpdateICAsUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Test PUT IC
// This should fail with unprocessable entity status code 422
newIC1.WebsocketURL = "ThisIsMyNewURL2"
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "PUT", helper.KeyModels{"ic": newIC1})
fmt.Sprintf("/api/v2/ic/%v", newICID), "PUT", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -401,13 +397,12 @@ func TestDeleteICAsAdmin(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newIC
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -417,12 +412,12 @@ func TestDeleteICAsAdmin(t *testing.T) {
// Count the number of all the ICs returned for admin
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
// Delete the added newIC
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "DELETE", nil)
fmt.Sprintf("/api/v2/ic/%v", newICID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -432,7 +427,7 @@ func TestDeleteICAsAdmin(t *testing.T) {
// Again count the number of all the ICs returned
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber-1)
@ -476,7 +471,7 @@ func TestDeleteICAsAdmin(t *testing.T) {
// Delete the added external IC (triggers an AMQP message, but should not remove the IC from the DB)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", 2), "DELETE", nil)
fmt.Sprintf("/api/v2/ic/%v", 2), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -485,7 +480,7 @@ func TestDeleteICAsAdmin(t *testing.T) {
// Again count the number of all the ICs returned
finalNumberAfterExtneralDelete, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber+1, finalNumberAfterExtneralDelete)
@ -498,13 +493,12 @@ func TestDeleteICAsUser(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newIC
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -513,15 +507,14 @@ func TestDeleteICAsUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Test DELETE ICs
// This should fail with unprocessable entity status code 422
newIC1.WebsocketURL = "ThisIsMyNewURL3"
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v", newICID), "DELETE", nil)
fmt.Sprintf("/api/v2/ic/%v", newICID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
}
@ -532,43 +525,41 @@ func TestGetAllICs(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// get the length of the GET all ICs response for user
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
// test POST ic/ $newICA
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// test POST ic/ $newICB
code, resp, err = helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// get the length of the GET all ICs response again
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+2)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// get the length of the GET all ICs response again
finalNumber2, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber2, initialNumber+2)
@ -580,13 +571,12 @@ func TestGetConfigsOfIC(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newICA
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -596,20 +586,19 @@ func TestGetConfigsOfIC(t *testing.T) {
// test GET ic/ID/confis
numberOfConfigs, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/ic/%v/configs", newICID), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v/configs", newICID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
assert.Equal(t, 0, numberOfConfigs)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test GET ic/ID/configs
numberOfConfigs, err = helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/ic/%v/configs", newICID), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v/configs", newICID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -618,7 +607,7 @@ func TestGetConfigsOfIC(t *testing.T) {
// Try to get configs of IC that does not exist
// should result in not found
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v/configs", newICID+1), "GET", nil)
fmt.Sprintf("/api/v2/ic/%v/configs", newICID+1), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
}
@ -629,13 +618,12 @@ func TestSendActionToIC(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// test POST ic/ $newICA
code, resp, err := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newIC1})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newIC1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -654,13 +642,13 @@ func TestSendActionToIC(t *testing.T) {
// Send action to IC
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v/action", newICID), "POST", actions)
fmt.Sprintf("/api/v2/ic/%v/action", newICID), "POST", actions)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Send malformed actions array to IC (should yield bad request)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/ic/%v/action", newICID), "POST", action1)
fmt.Sprintf("/api/v2/ic/%v/action", newICID), "POST", action1)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
}
@ -672,8 +660,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// fake an IC update message
@ -711,7 +698,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
// get the length of the GET all ICs response for user
number, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 0, number)
@ -753,7 +740,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
// get the length of the GET all ICs response for user
number, err = helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 1, number)
@ -782,7 +769,7 @@ func TestCreateUpdateViaAMQPRecv(t *testing.T) {
time.Sleep(waitingTime * time.Second)
// get the length of the GET all ICs response for user
number, err = helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 1, number)
@ -795,8 +782,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// fake an IC update message
@ -842,7 +828,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
// get the length of the GET all ICs response for user
number, err := helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 1, number)
@ -854,7 +840,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -876,7 +862,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/configs", "POST", helper.KeyModels{"config": newConfig})
"/api/v2/configs", "POST", helper.KeyModels{"config": newConfig})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -915,13 +901,13 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
// get the length of the GET all ICs response for user
number, err = helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 1, number)
// Delete component config from earlier
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/configs/%v", newConfigID), "DELETE", nil)
fmt.Sprintf("/api/v2/configs/%v", newConfigID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -931,7 +917,7 @@ func TestDeleteICViaAMQPRecv(t *testing.T) {
// get the length of the GET all ICs response for user
number, err = helper.LengthOfResponse(router, token,
"/api/ic", "GET", nil)
"/api/v2/ic", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 0, number)
}

View file

@ -125,13 +125,6 @@ func ReadTestDataFromJson(path string) error {
// Uses API endpoints to add test data to the backend; All endpoints have to be registered before invoking this function.
func AddTestData(cfg *config.Config, router *gin.Engine) (*bytes.Buffer, error) {
basePath, err := cfg.String("base.path")
if err != nil {
fmt.Println("error: testdata could not be added to DB: no base path specified")
return nil, err
}
database.MigrateModels()
// add Admin user (defaults to User_0, xyz789)
@ -147,7 +140,7 @@ func AddTestData(cfg *config.Config, router *gin.Engine) (*bytes.Buffer, error)
}
// authenticate as admin
token, err := helper.AuthenticateForTest(router, basePath+"/authenticate", "POST", Admin)
token, err := helper.AuthenticateForTest(router, Admin)
if err != nil {
return nil, err
}

View file

@ -23,10 +23,11 @@
package routes
import (
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"os"
"testing"
infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration"
"git.rwth-aachen.de/acs/public/villas/web-backend-go/database"
"github.com/gin-gonic/gin"
@ -50,8 +51,7 @@ func TestMain(m *testing.M) {
router = gin.Default()
basePath, _ := configuration.GlobalConfig.String("base.path")
api = router.Group(basePath)
api = router.Group("/api/v2")
os.Exit(m.Run())
}

View file

@ -46,8 +46,8 @@ import (
)
var router *gin.Engine
var base_api_results = "/api/results"
var base_api_auth = "/api/authenticate"
var base_api_results = "/api/v2/results"
var base_api_auth = "/api/v2/authenticate"
type ScenarioRequest struct {
Name string `json:"name,omitempty"`
@ -72,12 +72,10 @@ var newResult = ResultRequest{
func addScenario() (scenarioID uint) {
// authenticate as admin
token, _ := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, _ := helper.AuthenticateForTest(router, helper.AdminCredentials)
// authenticate as normal user
token, _ = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, _ = helper.AuthenticateForTest(router, helper.UserACredentials)
// POST $newScenario
newScenario := ScenarioRequest{
@ -86,14 +84,14 @@ func addScenario() (scenarioID uint) {
StartParameters: postgres.Jsonb{RawMessage: json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
_, resp, _ := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
// Read newScenario's ID from the response
newScenarioID, _ := helper.GetResponseID(resp)
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID)
}
@ -110,7 +108,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -135,8 +133,7 @@ func TestGetAllResultsOfScenario(t *testing.T) {
scenarioID := addScenario()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newResult
@ -158,8 +155,7 @@ func TestGetAllResultsOfScenario(t *testing.T) {
assert.Equal(t, 1, NumberOfConfigs)
// authenticate as normal userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get results without access
@ -185,8 +181,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
newResult.ScenarioID = scenarioID
newResult.ConfigSnapshots = confSnapshots
// authenticate as normal userB who has no access to new scenario
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err := helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to POST with no access
@ -197,8 +192,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST non JSON body
@ -243,8 +237,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// Try to GET the newResult with no access
@ -269,8 +262,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as guest user who has access to result
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to PUT as guest
@ -281,8 +273,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to PUT a non JSON body
@ -312,8 +303,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
newResult.Description = updatedResult.Description
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to DELETE with no access
@ -324,8 +314,7 @@ func TestAddGetUpdateDeleteResult(t *testing.T) {
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the results returned for scenario
@ -366,8 +355,7 @@ func TestAddDeleteResultFile(t *testing.T) {
newResult.ScenarioID = scenarioID
newResult.ConfigSnapshots = confSnapshots
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
base_api_auth, "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST newResult

View file

@ -77,7 +77,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -96,20 +96,19 @@ func TestAddScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST with non JSON body
// should return a bad request error
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", "this is not a JSON")
"/api/v2/scenarios", "POST", "this is not a JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// test POST scenarios/ $newScenario as normal user
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -123,7 +122,7 @@ func TestAddScenario(t *testing.T) {
// Get the newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -138,49 +137,46 @@ func TestAddScenario(t *testing.T) {
}
// this should NOT work and return a unprocessable entity 442 status code
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": malformedNewScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": malformedNewScenario})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// try to GET a non-existing scenario
// should return a not found 404
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID+1), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID+1), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// authenticate as guest user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// 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": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as userB who has no access to the added scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to GET a scenario to which user B has no access
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as admin user who has no access to everything
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err = helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// try to GET a scenario that is not created by admin user; should work anyway
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
}
@ -192,13 +188,12 @@ func TestUpdateScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -219,12 +214,12 @@ func TestUpdateScenario(t *testing.T) {
// try to update with non JSON body
// should return a bad request error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "PUT", "This is not a JSON body")
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "PUT", "This is not a JSON body")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "PUT", helper.KeyModels{"scenario": updatedScenario})
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "PUT", helper.KeyModels{"scenario": updatedScenario})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -234,7 +229,7 @@ func TestUpdateScenario(t *testing.T) {
// Get the updatedScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -244,7 +239,7 @@ func TestUpdateScenario(t *testing.T) {
// try to update a scenario that does not exist (should return not found 404 status code)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID+1), "PUT", helper.KeyModels{"scenario": updatedScenario})
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID+1), "PUT", helper.KeyModels{"scenario": updatedScenario})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
@ -257,45 +252,41 @@ func TestGetAllScenariosAsAdmin(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err := helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// get the length of the GET all scenarios response for admin
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
// authenticate as normal userB
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario1
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as normal userA
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario2
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as admin
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err = helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// get the length of the GET all scenarios response again
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+2)
@ -308,40 +299,37 @@ func TestGetAllScenariosAsUser(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal userB
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err := helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// get the length of the GET all scenarios response for userB
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
// test POST scenarios/ $newScenario2
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario2})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as normal userA
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario1
code, resp, err = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as normal userB
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// get the length of the GET all scenarios response again
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+1)
@ -354,13 +342,12 @@ func TestDeleteScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -370,35 +357,33 @@ func TestDeleteScenario(t *testing.T) {
// add guest user to new scenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as guest user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to delete scenario as guest
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the scenarios returned for userA
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
// Delete the added newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -408,7 +393,7 @@ func TestDeleteScenario(t *testing.T) {
// Again count the number of all the scenarios returned
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/scenarios", "GET", nil)
"/api/v2/scenarios", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber-1)
@ -421,13 +406,12 @@ func TestAddUserToScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -436,38 +420,36 @@ func TestAddUserToScenario(t *testing.T) {
assert.NoError(t, err)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to add new user User_C to scenario as userB
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// try to add new user UserB to scenario as userB
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the users returned for newScenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber, 1)
// add userB to newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -482,13 +464,13 @@ func TestAddUserToScenario(t *testing.T) {
// Count AGAIN the number of all the users returned for newScenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+1)
// try to add a non-existing user to newScenario, should return a not found 404
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_D", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_D", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
@ -501,13 +483,12 @@ func TestGetAllUsersOfScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -516,60 +497,56 @@ func TestGetAllUsersOfScenario(t *testing.T) {
assert.NoError(t, err)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get all users of new scenario with userB
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the users returned for newScenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber, 1)
// add userB to newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_B", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Count AGAIN the number of all the users returned for newScenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+1)
// authenticate as admin
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, err = helper.AuthenticateForTest(router, helper.AdminCredentials)
assert.NoError(t, err)
// set userB as inactive
modUserB := UserRequest{Active: "no"}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", 3), "PUT", helper.KeyModels{"user": modUserB})
fmt.Sprintf("/api/v2/users/%v", 3), "PUT", helper.KeyModels{"user": modUserB})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count AGAIN the number of all the users returned for newScenario
finalNumber2, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber2, initialNumber)
}
@ -581,13 +558,12 @@ func TestRemoveUserFromScenario(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST scenarios/ $newScenario
code, resp, err := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -597,36 +573,34 @@ func TestRemoveUserFromScenario(t *testing.T) {
// add userC to newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to delete userC from new scenario
// should return an unprocessable entity error
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the users returned for newScenario
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, 2, initialNumber)
// remove userC from newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -641,28 +615,28 @@ func TestRemoveUserFromScenario(t *testing.T) {
// Count AGAIN the number of all the users returned for newScenario
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/scenarios/%v/users", newScenarioID), "GET", nil)
fmt.Sprintf("/api/v2/scenarios/%v/users", newScenarioID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber-1, finalNumber)
// Try to remove userA from new scenario
// This should fail since User_A is the last user of newScenario
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_A", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_A", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 500, code, "Response body: \n%v\n", resp)
// Try to remove a user that does not exist in DB
// This should fail with not found 404 status code
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_D", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_D", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// Try to remove an admin user that is not explicitly a user of the scenario
// This should fail with not found 404 status code
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_0", newScenarioID), "DELETE", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_0", newScenarioID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)

View file

@ -91,8 +91,7 @@ func newFalse() *bool {
func addScenarioAndICAndConfig() (scenarioID uint, ICID uint, configID uint) {
// authenticate as admin
token, _ := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.AdminCredentials)
token, _ := helper.AuthenticateForTest(router, helper.AdminCredentials)
// POST $newICA
newICA := ICRequest{
@ -108,14 +107,13 @@ func addScenarioAndICAndConfig() (scenarioID uint, ICID uint, configID uint) {
ManagedExternally: newFalse(),
}
_, resp, _ := helper.TestEndpoint(router, token,
"/api/ic", "POST", helper.KeyModels{"ic": newICA})
"/api/v2/ic", "POST", helper.KeyModels{"ic": newICA})
// Read newIC's ID from the response
newICID, _ := helper.GetResponseID(resp)
// authenticate as normal user
token, _ = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, _ = helper.AuthenticateForTest(router, helper.UserACredentials)
// POST $newScenario
newScenario := ScenarioRequest{
@ -124,7 +122,7 @@ func addScenarioAndICAndConfig() (scenarioID uint, ICID uint, configID uint) {
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
_, resp, _ = helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
// Read newScenario's ID from the response
newScenarioID, _ := helper.GetResponseID(resp)
@ -137,14 +135,14 @@ func addScenarioAndICAndConfig() (scenarioID uint, ICID uint, configID uint) {
ICID: uint(newICID),
}
_, resp, _ = helper.TestEndpoint(router, token,
"/api/configs", "POST", helper.KeyModels{"config": newConfig})
"/api/v2/configs", "POST", helper.KeyModels{"config": newConfig})
// Read newConfig's ID from the response
newConfigID, _ := helper.GetResponseID(resp)
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID), uint(newICID), uint(newConfigID)
}
@ -162,7 +160,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -191,38 +189,35 @@ func TestAddSignal(t *testing.T) {
_, _, configID := addScenarioAndICAndConfig()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
newSignal1.ConfigID = configID
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to POST to component config without access
// should result in unprocessable entity
code, resp, err := helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal1})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST a signal with non JSON body
// should result in a bad request
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", "this is not a JSON")
"/api/v2/signals", "POST", "this is not a JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// test POST signals/ $newSignal
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal1})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -236,7 +231,7 @@ func TestAddSignal(t *testing.T) {
// Get the newSignal
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "GET", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -251,19 +246,18 @@ func TestAddSignal(t *testing.T) {
}
// this should NOT work and return a unprocessable entity 442 status code
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": malformedNewSignal})
"/api/v2/signals", "POST", helper.KeyModels{"signal": malformedNewSignal})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// Try to Get the newSignal as user B
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "GET", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
}
@ -279,14 +273,13 @@ func TestUpdateSignal(t *testing.T) {
_, _, configID := addScenarioAndICAndConfig()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST signals/ $newSignal
newSignal1.ConfigID = configID
code, resp, err := helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal1})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -301,44 +294,41 @@ func TestUpdateSignal(t *testing.T) {
}
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to PUT signal without access
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as guest user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to update signal as guest who has access to scenario
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to PUT with non JSON body
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "PUT", "This is not JSON")
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "PUT", "This is not JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// test PUT
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "PUT", helper.KeyModels{"signal": updatedSignal})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -348,7 +338,7 @@ func TestUpdateSignal(t *testing.T) {
// Get the updatedSignal
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "GET", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -358,7 +348,7 @@ func TestUpdateSignal(t *testing.T) {
// try to update a signal that does not exist (should return not found 404 status code)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID+1), "PUT", helper.KeyModels{"signal": updatedSignal})
fmt.Sprintf("/api/v2/signals/%v", newSignalID+1), "PUT", helper.KeyModels{"signal": updatedSignal})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
@ -375,8 +365,7 @@ func TestDeleteSignal(t *testing.T) {
_, _, configID := addScenarioAndICAndConfig()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// test POST signals/ $newSignal
@ -388,7 +377,7 @@ func TestDeleteSignal(t *testing.T) {
ConfigID: configID,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -397,31 +386,29 @@ func TestDeleteSignal(t *testing.T) {
assert.NoError(t, err)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// Try to DELETE signal with no access
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "DELETE", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the input signals returned for component config
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=in", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=in", configID), "GET", nil)
assert.NoError(t, err)
// add an output signal to make sure that counting of input signals works
newSignal1.ConfigID = configID
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal1})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -431,7 +418,7 @@ func TestDeleteSignal(t *testing.T) {
// Delete the added newSignal
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignalID), "DELETE", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignalID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -441,14 +428,14 @@ func TestDeleteSignal(t *testing.T) {
// Again count the number of all the input signals returned for component config
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=in", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=in", configID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber-1, finalNumber)
// Delete the output signal
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals/%v", newSignaloutID), "DELETE", nil)
fmt.Sprintf("/api/v2/signals/%v", newSignaloutID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
}
@ -464,13 +451,12 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) {
_, _, configID := addScenarioAndICAndConfig()
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the input signals returned for component config
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=in", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=in", configID), "GET", nil)
assert.NoError(t, err)
// test POST signals/ $newSignal
@ -482,7 +468,7 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) {
ConfigID: configID,
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignalA})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignalA})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -495,14 +481,14 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) {
ConfigID: configID,
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignalB})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignalB})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// add an output signal
newSignal1.ConfigID = configID
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignal1})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignal1})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -515,39 +501,38 @@ func TestGetAllInputSignalsOfConfig(t *testing.T) {
ConfigID: configID,
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/signals", "POST", helper.KeyModels{"signal": newSignalBout})
"/api/v2/signals", "POST", helper.KeyModels{"signal": newSignalBout})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Again count the number of all the input signals returned for component config
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=in", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=in", configID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber+2, finalNumber)
// Get the number of output signals
outputNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=out", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=out", configID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber+2, outputNumber)
// Try to get all signals for non-existing direction
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=thisiswrong", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=thisiswrong", configID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// authenticate as normal userB who has no access to new scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to get all input signals
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/signals?configID=%v&direction=in", configID), "GET", nil)
fmt.Sprintf("/api/v2/signals?configID=%v&direction=in", configID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)

View file

@ -46,13 +46,13 @@ func RegisterAuthenticate(r *gin.RouterGroup) {
// authenticated godoc
// @Summary Check if user is authenticated and provide details on how the user can authenticate
// @ID authenticate
// @ID authenticated
// @Accept json
// @Produce json
// @Tags authentication
// @Success 200 {object} docs.ResponseAuthenticate "JSON web token, success status, message and authenticated user object"
// @Failure 401 {object} docs.ResponseError "Unauthorized"
// @Failure 500 {object} docs.ResponseError "Internal server error."
// @Success 200 {object} api.ResponseAuthenticate "JSON web token, success status, message and authenticated user object"
// @Failure 401 {object} api.ResponseError "Unauthorized"
// @Failure 500 {object} api.ResponseError "Internal server error."
// @Router /authenticate [get]
func authenticated(c *gin.Context) {
ok, err := isAuthenticated(c)
@ -109,7 +109,7 @@ func authenticated(c *gin.Context) {
// @Success 200 {object} api.ResponseAuthenticate "JSON web token, success status, message and authenticated user object"
// @Failure 401 {object} api.ResponseError "Unauthorized"
// @Failure 500 {object} api.ResponseError "Internal server error."
// @Router /authenticate{mechanism} [post]
// @Router /authenticate/{mechanism} [post]
func authenticate(c *gin.Context) {
var user *User = nil

View file

@ -25,7 +25,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"os"
@ -63,7 +62,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
RegisterAuthenticate(api.Group("/authenticate"))
api.Use(Authentication())
@ -82,7 +81,7 @@ func TestAuthenticate(t *testing.T) {
// should result in unauthorized
w1 := httptest.NewRecorder()
body, _ := json.Marshal("This is no JSON")
req, err := http.NewRequest("POST", "/api/authenticate", bytes.NewBuffer(body))
req, err := http.NewRequest("POST", "/api/v2/authenticate", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w1, req)
@ -95,7 +94,7 @@ func TestAuthenticate(t *testing.T) {
// should result in unauthorized
w2 := httptest.NewRecorder()
body, _ = json.Marshal(malformedCredentials)
req, err = http.NewRequest("POST", "/api/authenticate", bytes.NewBuffer(body))
req, err = http.NewRequest("POST", "/api/v2/authenticate", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w2, req)
@ -107,7 +106,7 @@ func TestAuthenticate(t *testing.T) {
malformedCredentials.Password = "blablabla"
w3 := httptest.NewRecorder()
body, _ = json.Marshal(malformedCredentials)
req, err = http.NewRequest("POST", "/api/authenticate", bytes.NewBuffer(body))
req, err = http.NewRequest("POST", "/api/v2/authenticate", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w3, req)
@ -119,16 +118,14 @@ func TestAuthenticate(t *testing.T) {
malformedCredentials.Password = "wrong password"
w4 := httptest.NewRecorder()
body, _ = json.Marshal(malformedCredentials)
req, err = http.NewRequest("POST", "/api/authenticate", bytes.NewBuffer(body))
req, err = http.NewRequest("POST", "/api/v2/authenticate", bytes.NewBuffer(body))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w4, req)
assert.Equal(t, 401, w4.Code, w4.Body)
// authenticate as admin
log.Println(helper.AdminCredentials)
_, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
_, err = helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
}
@ -141,14 +138,13 @@ func TestAuthenticateQueryToken(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
w := httptest.NewRecorder()
// Create the request
req, err := http.NewRequest("GET", "/api/users?token="+token, nil)
req, err := http.NewRequest("GET", "/api/v2/users?token="+token, nil)
assert.NoError(t, err)
router.ServeHTTP(w, req)
@ -163,14 +159,13 @@ func TestAddGetUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// try to POST with non JSON body
// should result in bad request
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", "This is not JSON")
"/api/v2/users", "POST", "This is not JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -183,7 +178,7 @@ func TestAddGetUser(t *testing.T) {
// try POST with too short username
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -192,7 +187,7 @@ func TestAddGetUser(t *testing.T) {
wrongUser.Username = "Longenoughusername"
wrongUser.Password = "short"
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -201,7 +196,7 @@ func TestAddGetUser(t *testing.T) {
wrongUser.Password = "longenough"
wrongUser.Role = "ThisIsNotARole"
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -210,7 +205,7 @@ func TestAddGetUser(t *testing.T) {
wrongUser.Mail = "noemailaddress"
wrongUser.Role = "Guest"
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -220,7 +215,7 @@ func TestAddGetUser(t *testing.T) {
wrongUser.Role = ""
wrongUser.Password = ""
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -231,7 +226,7 @@ func TestAddGetUser(t *testing.T) {
wrongUser.Password = "blablabla"
wrongUser.Username = "admin"
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": wrongUser})
"/api/v2/users", "POST", helper.KeyModels{"user": wrongUser})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -244,7 +239,7 @@ func TestAddGetUser(t *testing.T) {
// test POST user/ $newUser
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -264,7 +259,7 @@ func TestAddGetUser(t *testing.T) {
// Get the newUser
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "GET", nil)
fmt.Sprintf("/api/v2/users/%v", newUserID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -275,7 +270,7 @@ func TestAddGetUser(t *testing.T) {
// try to GET user with invalid user ID
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/bla"), "GET", nil)
fmt.Sprintf("/api/v2/users/bla"), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
}
@ -288,8 +283,7 @@ func TestUsersNotAllowedActions(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// Add a user
@ -300,7 +294,7 @@ func TestUsersNotAllowedActions(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -308,34 +302,33 @@ func TestUsersNotAllowedActions(t *testing.T) {
assert.NoError(t, err)
// Authenticate as the added user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", UserRequest{
Username: newUser.Username,
Password: newUser.Password,
})
token, err = helper.AuthenticateForTest(router, UserRequest{
Username: newUser.Username,
Password: newUser.Password,
})
assert.NoError(t, err)
// Try to get all the users (NOT ALLOWED)
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// Try to read another user than self (eg. Admin) (NOT ALLOWED)
code, resp, err = helper.TestEndpoint(router, token,
"/api/users/0", "GET", nil)
"/api/v2/users/0", "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
// Try to delete another user (eg. Admin) (NOT ALLOWED)
code, resp, err = helper.TestEndpoint(router, token,
"/api/users/0", "DELETE", nil)
"/api/v2/users/0", "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// Try to delete self (NOT ALLOWED)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "DELETE", nil)
fmt.Sprintf("/api/v2/users/%v", newUserID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -349,13 +342,12 @@ func TestGetAllUsers(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// get the length of the GET all users response
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/users", "GET", nil)
"/api/v2/users", "GET", nil)
assert.NoError(t, err)
// Add a user
@ -366,13 +358,13 @@ func TestGetAllUsers(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// get the length of the GET all users response again
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/users", "GET", nil)
"/api/v2/users", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber+1)
@ -383,14 +375,13 @@ func TestGetAllUsers(t *testing.T) {
}
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", newUserCredentials)
token, err = helper.AuthenticateForTest(router, newUserCredentials)
assert.NoError(t, err)
// try to get all users as normal user
// should result in unprocessable entity eror
code, resp, err = helper.TestEndpoint(router, token,
"/api/users", "GET", nil)
"/api/v2/users", "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
@ -404,8 +395,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// Add a user that will modify itself
@ -416,7 +406,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -424,16 +414,15 @@ func TestModifyAddedUserAsUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as the new user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", UserRequest{
Username: newUser.Username,
Password: newUser.Password,
})
token, err = helper.AuthenticateForTest(router, UserRequest{
Username: newUser.Username,
Password: newUser.Password,
})
assert.NoError(t, err)
// Try PUT with invalid user ID in path
// Should return a bad request
code, resp, err = helper.TestEndpoint(router, token, fmt.Sprintf("/api/users/blabla"), "PUT",
code, resp, err = helper.TestEndpoint(router, token, fmt.Sprintf("/api/v2/users/blabla"), "PUT",
helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -441,7 +430,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
// Try to PUT a non JSON body
// Should return a bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT", "This is no JSON")
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT", "This is no JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -455,7 +444,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
// should result in forbidden
modActiveState := UserRequest{Active: "no"}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT", helper.KeyModels{"user": modActiveState})
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT", helper.KeyModels{"user": modActiveState})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
@ -463,7 +452,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
modRequest := UserRequest{Username: "myNewName"}
newUser.Username = modRequest.Username
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -474,7 +463,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
modRequest = UserRequest{Username: "myNewName"}
newUser.Username = modRequest.Username
code, resp, err = helper.TestEndpoint(router, token,
"/api/users/1", "PUT", helper.KeyModels{"user": modRequest})
"/api/v2/users/1", "PUT", helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
@ -482,7 +471,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
modRequest = UserRequest{Mail: "my@new.email"}
newUser.Mail = modRequest.Mail
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -493,7 +482,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
modRequest = UserRequest{Mail: "my@new.email"}
newUser.Mail = modRequest.Mail
code, resp, err = helper.TestEndpoint(router, token,
"/api/users/1", "PUT", helper.KeyModels{"user": modRequest})
"/api/v2/users/1", "PUT", helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
@ -501,7 +490,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
modRequest = UserRequest{Role: "Admin"}
newUser.Role = modRequest.Role
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
@ -511,7 +500,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
Password: "5tr0ng_pw!",
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -522,7 +511,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
OldPassword: "wrongoldpassword",
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
@ -533,23 +522,22 @@ func TestModifyAddedUserAsUser(t *testing.T) {
OldPassword: "mod_My5elf",
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// try to login as newUser with the modified username and password
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", UserRequest{
Username: newUser.Username,
Password: modRequest.Password,
})
token, err = helper.AuthenticateForTest(router, UserRequest{
Username: newUser.Username,
Password: modRequest.Password,
})
assert.NoError(t, err)
// modify Admin's password (ILLEGAL)
modRequest = UserRequest{Password: "4dm1ns_pw!"}
code, resp, err = helper.TestEndpoint(router, token,
"/api/users/1", "PUT", helper.KeyModels{"user": modRequest})
"/api/v2/users/1", "PUT", helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 403, code, "Response body: \n%v\n", resp)
}
@ -562,8 +550,7 @@ func TestInvalidUserUpdate(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// Add a user
@ -574,7 +561,7 @@ func TestInvalidUserUpdate(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -587,7 +574,7 @@ func TestInvalidUserUpdate(t *testing.T) {
Password: "longenough",
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID+1), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID+1), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
@ -597,7 +584,7 @@ func TestInvalidUserUpdate(t *testing.T) {
modRequest.Password = ""
modRequest.Username = "admin"
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -605,7 +592,7 @@ func TestInvalidUserUpdate(t *testing.T) {
// modify newUser's password with INVALID password
modRequest.Password = "short"
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -613,7 +600,7 @@ func TestInvalidUserUpdate(t *testing.T) {
// modify newUser's email with INVALID email
modRequest = UserRequest{Mail: "notEmail"}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -621,7 +608,7 @@ func TestInvalidUserUpdate(t *testing.T) {
// modify newUser's role with INVALID role
modRequest = UserRequest{Role: "noRole"}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -636,8 +623,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// Add a user
@ -648,7 +634,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -665,7 +651,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
modRequest := UserRequest{Username: "NewUsername"}
newUser.Username = modRequest.Username
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -676,7 +662,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
modRequest = UserRequest{Mail: "new@e.mail"}
newUser.Mail = modRequest.Mail
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -687,7 +673,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
modRequest = UserRequest{Role: "Admin"}
newUser.Role = modRequest.Role
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -699,7 +685,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
Password: "4_g00d_pw!",
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
@ -710,28 +696,26 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
OldPassword: adminpw,
}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// try to login as newUser with the modified username and password
_, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", UserRequest{
Username: newUser.Username,
Password: modRequest.Password,
})
_, err = helper.AuthenticateForTest(router, UserRequest{
Username: newUser.Username,
Password: modRequest.Password,
})
assert.NoError(t, err)
// authenticate as admin
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err = helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// modify newUser's Active status
modRequest = UserRequest{Active: "no"}
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "PUT",
fmt.Sprintf("/api/v2/users/%v", newUserID), "PUT",
helper.KeyModels{"user": modRequest})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -739,7 +723,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
// try to login as newUser with the modified active status
// should NOT work anymore!
code, resp, err = helper.TestEndpoint(router, token,
"/api/authenticate", "POST",
"/api/v2/authenticate", "POST",
UserRequest{
Username: newUser.Username,
Password: "4_g00d_pw!",
@ -756,8 +740,7 @@ func TestDeleteUser(t *testing.T) {
assert.NoError(t, err)
// authenticate as admin
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.Credentials{Username: "admin", Password: adminpw})
token, err := helper.AuthenticateForTest(router, helper.Credentials{Username: "admin", Password: adminpw})
assert.NoError(t, err)
// Add a user
@ -768,7 +751,7 @@ func TestDeleteUser(t *testing.T) {
Role: "User",
}
code, resp, err := helper.TestEndpoint(router, token,
"/api/users", "POST", helper.KeyModels{"user": newUser})
"/api/v2/users", "POST", helper.KeyModels{"user": newUser})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -778,31 +761,31 @@ func TestDeleteUser(t *testing.T) {
// try to DELETE with invalid ID
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/bla"), "DELETE", nil)
fmt.Sprintf("/api/v2/users/bla"), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// try to DELETE with ID that does not exist
// should result in not found
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID+1), "DELETE", nil)
fmt.Sprintf("/api/v2/users/%v", newUserID+1), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
// Count the number of all the users returned
initialNumber, err := helper.LengthOfResponse(router, token,
"/api/users", "GET", nil)
"/api/v2/users", "GET", nil)
assert.NoError(t, err)
// Delete the added newUser
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/users/%v", newUserID), "DELETE", nil)
fmt.Sprintf("/api/v2/users/%v", newUserID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Again count the number of all the users returned
finalNumber, err := helper.LengthOfResponse(router, token,
"/api/users", "GET", nil)
"/api/v2/users", "GET", nil)
assert.NoError(t, err)
assert.Equal(t, finalNumber, initialNumber-1)

View file

@ -92,7 +92,7 @@ func addScenarioAndDashboard(token string) (scenarioID uint, dashboardID uint) {
StartParameters: postgres.Jsonb{json.RawMessage(`{"parameter1" : "testValue1A", "parameter2" : "testValue2A", "parameter3" : 42}`)},
}
_, resp, _ := helper.TestEndpoint(router, token,
"/api/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
"/api/v2/scenarios", "POST", helper.KeyModels{"scenario": newScenario})
// Read newScenario's ID from the response
newScenarioID, _ := helper.GetResponseID(resp)
@ -104,14 +104,14 @@ func addScenarioAndDashboard(token string) (scenarioID uint, dashboardID uint) {
ScenarioID: uint(newScenarioID),
}
_, resp, _ = helper.TestEndpoint(router, token,
"/api/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
"/api/v2/dashboards", "POST", helper.KeyModels{"dashboard": newDashboard})
// Read newDashboard's ID from the response
newDashboardID, _ := helper.GetResponseID(resp)
// add the guest user to the new scenario
_, resp, _ = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
fmt.Sprintf("/api/v2/scenarios/%v/user?username=User_C", newScenarioID), "PUT", nil)
return uint(newScenarioID), uint(newDashboardID)
}
@ -129,7 +129,7 @@ func TestMain(m *testing.M) {
defer database.DBpool.Close()
router = gin.Default()
api := router.Group("/api")
api := router.Group("/api/v2")
user.RegisterAuthenticate(api.Group("/authenticate"))
api.Use(user.Authentication())
@ -150,40 +150,37 @@ func TestAddWidget(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
_, dashboardID := addScenarioAndDashboard(token)
newWidget.DashboardID = dashboardID
// authenticate as userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to POST the newWidget with no access to the scenario
// should result in unprocessable entity
code, resp, err := helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidget})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to POST non JSON body
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
"/api/widgets", "POST", "This is no JSON")
"/api/v2/widgets", "POST", "This is no JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// test POST widgets/ $newWidget
code, resp, err = helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidget})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -197,7 +194,7 @@ func TestAddWidget(t *testing.T) {
// Get the newWidget
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "GET", nil)
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -212,19 +209,18 @@ func TestAddWidget(t *testing.T) {
}
// this should NOT work and return a unprocessable entity 442 status code
code, resp, err = helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": malformedNewWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": malformedNewWidget})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to GET the newWidget with no access to the scenario
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "GET", nil)
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
}
@ -235,8 +231,7 @@ func TestUpdateWidget(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
_, dashboardID := addScenarioAndDashboard(token)
@ -244,7 +239,7 @@ func TestUpdateWidget(t *testing.T) {
// test POST widgets/ $newWidget
newWidget.DashboardID = dashboardID
code, resp, err := helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidget})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -264,44 +259,41 @@ func TestUpdateWidget(t *testing.T) {
}
// authenticate as userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to PUT the updatedWidget with no access to the scenario
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as guest user who has access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.GuestCredentials)
token, err = helper.AuthenticateForTest(router, helper.GuestCredentials)
assert.NoError(t, err)
// try to PUT as guest
// should NOT work and result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// try to PUT non JSON body
// should result in bad request
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "PUT", "This is no JSON")
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "PUT", "This is no JSON")
assert.NoError(t, err)
assert.Equalf(t, 400, code, "Response body: \n%v\n", resp)
// test PUT
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "PUT", helper.KeyModels{"widget": updatedWidget})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -311,7 +303,7 @@ func TestUpdateWidget(t *testing.T) {
// Get the updatedWidget
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "GET", nil)
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -321,7 +313,7 @@ func TestUpdateWidget(t *testing.T) {
// try to update a widget that does not exist (should return not found 404 status code)
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID+1), "PUT", helper.KeyModels{"widget": updatedWidget})
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID+1), "PUT", helper.KeyModels{"widget": updatedWidget})
assert.NoError(t, err)
assert.Equalf(t, 404, code, "Response body: \n%v\n", resp)
@ -333,8 +325,7 @@ func TestDeleteWidget(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
_, dashboardID := addScenarioAndDashboard(token)
@ -342,7 +333,7 @@ func TestDeleteWidget(t *testing.T) {
// test POST widgets/ $newWidget
newWidget.DashboardID = dashboardID
code, resp, err := helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidget})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -351,30 +342,28 @@ func TestDeleteWidget(t *testing.T) {
assert.NoError(t, err)
// authenticate as userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to DELETE the newWidget with no access to the scenario
// should result in unprocessable entity
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "DELETE", nil)
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the widgets returned for dashboard
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/widgets?dashboardID=%v", dashboardID), "GET", nil)
fmt.Sprintf("/api/v2/widgets?dashboardID=%v", dashboardID), "GET", nil)
assert.NoError(t, err)
// Delete the added newWidget
code, resp, err = helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets/%v", newWidgetID), "DELETE", nil)
fmt.Sprintf("/api/v2/widgets/%v", newWidgetID), "DELETE", nil)
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -384,7 +373,7 @@ func TestDeleteWidget(t *testing.T) {
// Again count the number of all the widgets returned for dashboard
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/widgets?dashboardID=%v", dashboardID), "GET", nil)
fmt.Sprintf("/api/v2/widgets?dashboardID=%v", dashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber-1, finalNumber)
@ -396,38 +385,35 @@ func TestGetAllWidgetsOfDashboard(t *testing.T) {
assert.NoError(t, helper.AddTestUsers())
// authenticate as normal user
token, err := helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err := helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
_, dashboardID := addScenarioAndDashboard(token)
// authenticate as userB who has no access to scenario
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserBCredentials)
token, err = helper.AuthenticateForTest(router, helper.UserBCredentials)
assert.NoError(t, err)
// try to GET all widgets of dashboard
// should result in unprocessable entity
code, resp, err := helper.TestEndpoint(router, token,
fmt.Sprintf("/api/widgets?dashboardID=%v", dashboardID), "GET", nil)
fmt.Sprintf("/api/v2/widgets?dashboardID=%v", dashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equalf(t, 422, code, "Response body: \n%v\n", resp)
// authenticate as normal user
token, err = helper.AuthenticateForTest(router,
"/api/authenticate", "POST", helper.UserACredentials)
token, err = helper.AuthenticateForTest(router, helper.UserACredentials)
assert.NoError(t, err)
// Count the number of all the widgets returned for dashboard
initialNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/widgets?dashboardID=%v", dashboardID), "GET", nil)
fmt.Sprintf("/api/v2/widgets?dashboardID=%v", dashboardID), "GET", nil)
assert.NoError(t, err)
// test POST widgets/ $newWidget
newWidget.DashboardID = dashboardID
code, resp, err = helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidget})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidget})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
@ -447,13 +433,13 @@ func TestGetAllWidgetsOfDashboard(t *testing.T) {
SignalIDs: []int64{},
}
code, resp, err = helper.TestEndpoint(router, token,
"/api/widgets", "POST", helper.KeyModels{"widget": newWidgetB})
"/api/v2/widgets", "POST", helper.KeyModels{"widget": newWidgetB})
assert.NoError(t, err)
assert.Equalf(t, 200, code, "Response body: \n%v\n", resp)
// Again count the number of all the widgets returned for dashboard
finalNumber, err := helper.LengthOfResponse(router, token,
fmt.Sprintf("/api/widgets?dashboardID=%v", dashboardID), "GET", nil)
fmt.Sprintf("/api/v2/widgets?dashboardID=%v", dashboardID), "GET", nil)
assert.NoError(t, err)
assert.Equal(t, initialNumber+2, finalNumber)

View file

@ -82,7 +82,7 @@ func addData(router *gin.Engine, cfg *config.Config) error {
func main() {
log.Println("Starting VILLASweb-backend-go")
mode, _, basePath, port, amqphost, amqpuser, amqppass, err := configuration.ConfigureBackend()
mode, port, amqphost, amqpuser, amqppass, err := configuration.ConfigureBackend()
if err != nil {
panic(err)
}
@ -100,7 +100,7 @@ func main() {
gin.SetMode(gin.ReleaseMode)
}
r := gin.Default()
api := r.Group(basePath)
api := r.Group("/api/v2")
routes.RegisterEndpoints(r, api)
//Start AMQP client