diff --git a/common/utilities.go b/common/utilities.go index 1a3b91a..fc2f56b 100644 --- a/common/utilities.go +++ b/common/utilities.go @@ -93,6 +93,40 @@ func TestEndpoint(t *testing.T, router *gin.Engine, token string, url string, me } +func NewAuthenticateForTest(router *gin.Engine, url string, + method string, body []byte, expected_code int) (string, error) { + + w := httptest.NewRecorder() + + req, _ := http.NewRequest(method, url, bytes.NewBuffer(body)) + req.Header.Set("Content-Type", "application/json") + 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) + } + + var body_data map[string]interface{} + + // Get the response + err := json.Unmarshal([]byte(w.Body.String()), &body_data) + if err != nil { + return "", err + } + + // Check the response + success := body_data["success"].(bool) + if !success { + fmt.Println("Authentication not successful: ", body_data["message"]) + return "", fmt.Errorf("Authentication unsuccessful!") + } + + // Return the token and nil error + return body_data["token"].(string), nil +} + func AuthenticateForTest(t *testing.T, router *gin.Engine, url string, method string, body []byte, expected_code int) string { w := httptest.NewRecorder() diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 4ee01d6..b39ac2e 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -29,11 +29,12 @@ func TestUserEndpoints(t *testing.T) { credjson, _ := json.Marshal(common.CredAdmin) msgUsersjson, _ := json.Marshal(msgUsers) - token := common.AuthenticateForTest(t, router, "/api/authenticate", - "POST", credjson, 200) + token, err := common.NewAuthenticateForTest(router, + "/api/authenticate", "POST", credjson, 200) + assert.NoError(t, err) // test GET user/ - err := common.NewTestEndpoint(router, token, "/api/users", "GET", + err = common.NewTestEndpoint(router, token, "/api/users", "GET", nil, 200, msgUsersjson) assert.NoError(t, err) }