148 lines
3.2 KiB
Go
148 lines
3.2 KiB
Go
package testutils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.artlef.fr/bibliomane/internal/config"
|
|
"git.artlef.fr/bibliomane/internal/setup"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestSetup() *gin.Engine {
|
|
c := config.LoadConfigFromFile("config_test/test.toml")
|
|
return setup.Setup(&c)
|
|
}
|
|
|
|
type loginResponse struct {
|
|
Message string `json:"message"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func ConnectDemoUser(router *gin.Engine) string {
|
|
loginJson :=
|
|
`{
|
|
"username": "demo",
|
|
"password":"demopw"
|
|
}`
|
|
return connectUser(router, loginJson)
|
|
}
|
|
|
|
func ConnectDemo2User(router *gin.Engine) string {
|
|
loginJson :=
|
|
`{
|
|
"username": "demo2",
|
|
"password":"demopw"
|
|
}`
|
|
return connectUser(router, loginJson)
|
|
}
|
|
|
|
func connectUser(router *gin.Engine, loginJson string) string {
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("POST", "/ws/auth/login", strings.NewReader(loginJson))
|
|
router.ServeHTTP(w, req)
|
|
var parsedResponse loginResponse
|
|
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return parsedResponse.Token
|
|
}
|
|
|
|
func TestBookPutCallWithDemoPayload(t *testing.T, payload string, bookId string, expectedCode int, url string) {
|
|
router := TestSetup()
|
|
token := ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("PUT", url, strings.NewReader(payload))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
if w.Code != expectedCode {
|
|
t.Errorf("%s", w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestFetchOneModel[T any](t *testing.T, urlpath string, id string) (int, T) {
|
|
router := TestSetup()
|
|
|
|
token := ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("GET", urlpath+"/"+id, nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
var result T
|
|
err := json.Unmarshal(w.Body.Bytes(), &result)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
return w.Code, result
|
|
}
|
|
|
|
func TestFetchModel[T any](t *testing.T, urlpath string, limit string, offset string) (int, T) {
|
|
router := TestSetup()
|
|
|
|
u, err := url.Parse(urlpath)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if limit != "" {
|
|
q := u.Query()
|
|
q.Set("limit", limit)
|
|
u.RawQuery = q.Encode()
|
|
}
|
|
if offset != "" {
|
|
q := u.Query()
|
|
q.Set("offset", offset)
|
|
u.RawQuery = q.Encode()
|
|
}
|
|
|
|
q := u.Query()
|
|
q.Set("lang", "fr")
|
|
u.RawQuery = q.Encode()
|
|
|
|
token := ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("GET", u.String(), nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
var result T
|
|
s := w.Body.String()
|
|
err = json.Unmarshal([]byte(s), &result)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
return w.Code, result
|
|
}
|
|
|
|
func TestPostCall(t *testing.T, urlpath string, payload string) (int, uint) {
|
|
router := TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("POST", urlpath,
|
|
strings.NewReader(payload))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
return w.Code, 0
|
|
}
|
|
var parsed struct {
|
|
ID uint
|
|
}
|
|
|
|
err := json.Unmarshal(w.Body.Bytes(), &parsed)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
return w.Code, parsed.ID
|
|
}
|