mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
Adds new function for testing endpoints WIP:
- NewTestEndpoint() should replace TestEndpoint() since it is returning an error in case that the code or the response is not matching the expected one. The assertion __must__ be executed in the body of the actual test (of the corresponding package) so the printed error message can include the right number of line and the file where the assertion failed. - The function is used now only in package scenario tests.
This commit is contained in:
parent
cf69e59a5f
commit
4c0d74a4a8
2 changed files with 60 additions and 13 deletions
|
@ -36,6 +36,40 @@ func ProvideErrorResponse(c *gin.Context, err error) bool {
|
|||
return false // No error
|
||||
}
|
||||
|
||||
func NewTestEndpoint(router *gin.Engine, token string, url string,
|
||||
method string, body []byte, expected_code int,
|
||||
expected_response []byte) error {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Check the return HTTP Code
|
||||
if w.Code != expected_code {
|
||||
return fmt.Errorf("HTTP Code: Expected \"%v\". Got \"%v\".",
|
||||
expected_code, w.Code)
|
||||
}
|
||||
|
||||
// Check the response
|
||||
opts := jsondiff.DefaultConsoleOptions()
|
||||
diff, _ := jsondiff.Compare(w.Body.Bytes(), expected_response, &opts)
|
||||
if diff.String() != "FullMatch" {
|
||||
return fmt.Errorf("Response: Expected \"%v\". Got \"%v\".",
|
||||
"FullMatch", diff.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestEndpoint(t *testing.T, router *gin.Engine, token string, url string, method string, body []byte, expected_code int, expected_response []byte) {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
|
|
@ -54,35 +54,48 @@ func TestScenarioEndpoints(t *testing.T) {
|
|||
token = common.AuthenticateForTest(t, router, "/api/authenticate", "POST", credjson, 200)
|
||||
|
||||
// test GET scenarios/
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||
err := common.NewTestEndpoint(router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test POST scenarios/
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios", "POST", msgScenariojson, 200, msgOKjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios", "POST", msgScenariojson, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test GET scenarios/:ScenarioID
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "GET", nil, 200, msgScenariojson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/3", "GET", nil, 200, msgScenariojson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test PUT scenarios/:ScenarioID
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "PUT", msgScenarioUpdatedjson, 200, msgOKjson)
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "GET", nil, 200, msgScenarioUpdatedjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/3", "PUT", msgScenarioUpdatedjson, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/3", "GET", nil, 200, msgScenarioUpdatedjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test DELETE scenarios/:ScenarioID
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/3", "DELETE", nil, 200, msgOKjson)
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/3", "DELETE", nil, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios", "GET", nil, 200, msgScenariosjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test GET scenarios/:ScenarioID/users
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUsersjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUsersjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test DELETE scenarios/:ScenarioID/user
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/user?username=User_B", "DELETE", nil, 200, msgOKjson)
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUserAjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/user?username=User_B", "DELETE", nil, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUserAjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test PUT scenarios/:ScenarioID/user
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/user?username=User_B", "PUT", nil, 200, msgOKjson)
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUsersjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/user?username=User_B", "PUT", nil, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/users", "GET", nil, 200, msgUsersjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test DELETE scenarios/:ScenarioID/user for logged in user User_A
|
||||
common.TestEndpoint(t, router, token, "/api/scenarios/1/user?username=User_A", "DELETE", nil, 200, msgOKjson)
|
||||
err = common.NewTestEndpoint(router, token, "/api/scenarios/1/user?username=User_A", "DELETE", nil, 200, msgOKjson)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// test if deletion of user from scenario has worked
|
||||
w2 := httptest.NewRecorder()
|
||||
|
|
Loading…
Add table
Reference in a new issue