mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
mode testEndpoint and authenticateForTest functions to common package
This commit is contained in:
parent
6081fab080
commit
b6c2d9fc6a
2 changed files with 65 additions and 64 deletions
|
@ -1,12 +1,17 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
const UserIDCtx = "user_id"
|
const UserIDCtx = "user_id"
|
||||||
|
@ -75,3 +80,48 @@ func GetFileID(c *gin.Context) (int, error) {
|
||||||
return fileID, err
|
return fileID, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEndpoint(t *testing.T, router *gin.Engine, token string, url string, method string, body []byte, expected_code int, expected_response string) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
if body != nil {
|
||||||
|
req, _ := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Add("Authorization", "Bearer "+token)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
} else {
|
||||||
|
req, _ := http.NewRequest(method, url, nil)
|
||||||
|
req.Header.Add("Authorization", "Bearer "+token)
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, expected_code, w.Code)
|
||||||
|
fmt.Println(w.Body.String())
|
||||||
|
assert.Equal(t, expected_response, w.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func AuthenticateForTest(t *testing.T, router *gin.Engine, url string, method string, body []byte, expected_code int) string {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
req, _ := http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
|
assert.Equal(t, expected_code, w.Code)
|
||||||
|
|
||||||
|
var body_data map[string]interface{}
|
||||||
|
|
||||||
|
err := json.Unmarshal([]byte(w.Body.String()), &body_data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
success := body_data["success"].(bool)
|
||||||
|
if !success {
|
||||||
|
panic(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(w.Body.String())
|
||||||
|
|
||||||
|
return body_data["token"].(string)
|
||||||
|
}
|
||||||
|
|
|
@ -1,16 +1,12 @@
|
||||||
package simulation
|
package simulation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/routes/user"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
|
|
||||||
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
"git.rwth-aachen.de/acs/public/villas/villasweb-backend-go/common"
|
||||||
)
|
)
|
||||||
|
@ -148,80 +144,35 @@ func TestSimulationEndpoints(t *testing.T) {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
authenticate(t, router, "/api/authenticate", "POST", credjson, 200)
|
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||||
|
|
||||||
// test GET simulations/
|
// test GET simulations/
|
||||||
testEndpoint(t, router, "/api/simulations/", "GET", nil, 200, string(msgSimulationsjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/", "GET", nil, 200, string(msgSimulationsjson))
|
||||||
|
|
||||||
// test POST simulations/
|
// test POST simulations/
|
||||||
testEndpoint(t, router, "/api/simulations/", "POST", simulationCjson, 200, string(msgOKjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/", "POST", simulationCjson, 200, string(msgOKjson))
|
||||||
|
|
||||||
// test GET simulations/:SimulationID
|
// test GET simulations/:SimulationID
|
||||||
testEndpoint(t, router, "/api/simulations/3", "GET", nil, 200, string(msgSimulationjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/3", "GET", nil, 200, string(msgSimulationjson))
|
||||||
|
|
||||||
// test DELETE simulations/:SimulationID
|
// test DELETE simulations/:SimulationID
|
||||||
testEndpoint(t, router, "/api/simulations/3", "DELETE", nil, 200, string(msgOKjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/3", "DELETE", nil, 200, string(msgOKjson))
|
||||||
testEndpoint(t, router, "/api/simulations/", "GET", nil, 200, string(msgSimulationsjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/", "GET", nil, 200, string(msgSimulationsjson))
|
||||||
|
|
||||||
// test GET simulations/:SimulationID/users
|
// test GET simulations/:SimulationID/users
|
||||||
testEndpoint(t, router, "/api/simulations/1/users", "GET", nil, 200, string(msgUsersjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/users", "GET", nil, 200, string(msgUsersjson))
|
||||||
|
|
||||||
// test DELETE simulations/:SimulationID/user
|
// test DELETE simulations/:SimulationID/user
|
||||||
testEndpoint(t, router, "/api/simulations/1/user?username=User_B", "DELETE", nil, 200, string(msgOKjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/user?username=User_B", "DELETE", nil, 200, string(msgOKjson))
|
||||||
testEndpoint(t, router, "/api/simulations/1/users", "GET", nil, 200, string(msgUserAjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/users", "GET", nil, 200, string(msgUserAjson))
|
||||||
|
|
||||||
// test PUT simulations/:SimulationID/user
|
// test PUT simulations/:SimulationID/user
|
||||||
testEndpoint(t, router, "/api/simulations/1/user?username=User_B", "PUT", nil, 200, string(msgOKjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/user?username=User_B", "PUT", nil, 200, string(msgOKjson))
|
||||||
testEndpoint(t, router, "/api/simulations/1/users", "GET", nil, 200, string(msgUsersjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/users", "GET", nil, 200, string(msgUsersjson))
|
||||||
|
|
||||||
// test DELETE simulations/:SimulationID/user for logged in user User_A
|
// test DELETE simulations/:SimulationID/user for logged in user User_A
|
||||||
testEndpoint(t, router, "/api/simulations/1/user?username=User_A", "DELETE", nil, 200, string(msgOKjson))
|
common.TestEndpoint(t, router, token, "/api/simulations/1/user?username=User_A", "DELETE", nil, 200, string(msgOKjson))
|
||||||
testEndpoint(t, router, "/api/simulations/1/users", "GET", nil, 422, "\"Access denied (for simulation ID).\"")
|
common.TestEndpoint(t, router, token, "/api/simulations/1/users", "GET", nil, 422, "\"Access denied (for simulation ID).\"")
|
||||||
|
|
||||||
// TODO add tests for other return codes
|
// TODO add tests for other return codes
|
||||||
}
|
}
|
||||||
|
|
||||||
func testEndpoint(t *testing.T, router *gin.Engine, url string, method string, body []byte, expected_code int, expected_response string) {
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
if body != nil {
|
|
||||||
req, _ := http.NewRequest(method, url, bytes.NewBuffer(body))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
req.Header.Add("Authorization", "Bearer "+token)
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
} else {
|
|
||||||
req, _ := http.NewRequest(method, url, nil)
|
|
||||||
req.Header.Add("Authorization", "Bearer "+token)
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, expected_code, w.Code)
|
|
||||||
fmt.Println(w.Body.String())
|
|
||||||
assert.Equal(t, expected_response, w.Body.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func authenticate(t *testing.T, router *gin.Engine, url string, method string, body []byte, expected_code int) {
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
req, _ := http.NewRequest(method, url, bytes.NewBuffer(body))
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
assert.Equal(t, expected_code, w.Code)
|
|
||||||
|
|
||||||
var body_data map[string]interface{}
|
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(w.Body.String()), &body_data)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
success := body_data["success"].(bool)
|
|
||||||
if !success {
|
|
||||||
panic(-1)
|
|
||||||
}
|
|
||||||
// save token to global variable
|
|
||||||
token = body_data["token"].(string)
|
|
||||||
|
|
||||||
fmt.Println(w.Body.String())
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue