145 lines
3.3 KiB
Go
145 lines
3.3 KiB
Go
package apitest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type bookSearchGet struct {
|
|
Id uint `json:"id"`
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
Rating int `json:"rating"`
|
|
Read bool `json:"read"`
|
|
WantRead bool `json:"wantread"`
|
|
CoverPath string `json:"coverPath"`
|
|
}
|
|
|
|
func TestSearchBook_MultipleBooks(t *testing.T) {
|
|
books := testSearchBook(t, "san", "", "")
|
|
|
|
assert.Equal(t, 2, len(books))
|
|
}
|
|
|
|
func TestSearchBook_OneBookNotUserBook(t *testing.T) {
|
|
books := testSearchBook(t, "iliade", "", "")
|
|
assert.Equal(t, 1, len(books))
|
|
assert.Equal(t,
|
|
[]bookSearchGet{{
|
|
Title: "Iliade",
|
|
Author: "Homère",
|
|
Id: 29,
|
|
Rating: 0,
|
|
Read: false,
|
|
WantRead: false,
|
|
CoverPath: "/bookcover/iliade.jpeg",
|
|
}},
|
|
books)
|
|
}
|
|
|
|
func TestSearchBook_OneBookRead(t *testing.T) {
|
|
books := testSearchBook(t, "dieux", "", "")
|
|
assert.Equal(t, 1, len(books))
|
|
assert.Equal(t,
|
|
[]bookSearchGet{{
|
|
Title: "Les dieux ont soif",
|
|
Author: "Anatole France",
|
|
Id: 4,
|
|
Rating: 7,
|
|
Read: true,
|
|
WantRead: false,
|
|
CoverPath: "/bookcover/lesdieuxontsoif.jpg",
|
|
}},
|
|
books)
|
|
}
|
|
|
|
func TestSearchBook_Limit(t *testing.T) {
|
|
books := testSearchBook(t, "a", "10", "")
|
|
assert.Equal(t, 10, len(books))
|
|
}
|
|
|
|
func TestSearchBook_Offset(t *testing.T) {
|
|
books := testSearchBook(t, "sa", "", "2")
|
|
assert.Equal(t, 3, len(books))
|
|
}
|
|
|
|
func testSearchBook(t *testing.T, searchterm string, limit string, offset string) []bookSearchGet {
|
|
router := testutils.TestSetup()
|
|
|
|
u, err := url.Parse("/search/" + searchterm)
|
|
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()
|
|
}
|
|
|
|
token := testutils.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 books []bookSearchGet
|
|
s := w.Body.String()
|
|
err = json.Unmarshal([]byte(s), &books)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
assert.Equal(t, 200, w.Code)
|
|
return books
|
|
}
|
|
|
|
func TestSearchBookCount_OK(t *testing.T) {
|
|
router := testutils.TestSetup()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("GET", "/search/sa/count", nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
var count countResponse
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
err := json.Unmarshal(w.Body.Bytes(), &count)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
assert.Equal(t, 5, count.Count)
|
|
}
|
|
|
|
func TestSearchBookCount_Zero(t *testing.T) {
|
|
router := testutils.TestSetup()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("GET", "/search/dsfsfdsdfsfd/count", nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
var count countResponse
|
|
|
|
assert.Equal(t, 200, w.Code)
|
|
err := json.Unmarshal(w.Body.Bytes(), &count)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
assert.Equal(t, 0, count.Count)
|
|
}
|