mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
const UserIDCtx = "user_id"
|
|
const UserRoleCtx = "user_role"
|
|
|
|
func ProvideErrorResponse(c *gin.Context, err error) bool {
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
errormsg := "Record not Found in DB: " + err.Error()
|
|
c.JSON(http.StatusNotFound, gin.H{
|
|
"error": errormsg,
|
|
})
|
|
} else {
|
|
errormsg := "Error on DB Query or transaction: " + err.Error()
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"error": errormsg,
|
|
})
|
|
}
|
|
return true // Error
|
|
}
|
|
return false // No error
|
|
}
|
|
|
|
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)
|
|
}
|