77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package apitest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.artlef.fr/bibliomane/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPostCollectionBookHandler_AddOk(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"bookId": 9
|
|
}`
|
|
collectionId := "1"
|
|
status := testPostCollectionBookHandler(collectionId, payload)
|
|
assert.Equal(t, http.StatusOK, status)
|
|
_, collection := testGetCollection(t, collectionId, "10", "0")
|
|
assert.Equal(t, int64(7), collection.Count)
|
|
}
|
|
|
|
func TestPostCollectionBookHandler_BookOK(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"bookId": 7
|
|
}`
|
|
collectionId := "2"
|
|
status := testPostCollectionBookHandler(collectionId, payload)
|
|
assert.Equal(t, http.StatusOK, status)
|
|
_, collection := testGetCollection(t, collectionId, "10", "0")
|
|
assert.Equal(t, uint(7), collection.Items[0].Book.ID)
|
|
}
|
|
|
|
func TestPostCollectionBookHandler_CollectionNotFound(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"bookId": 9
|
|
}`
|
|
status := testPostCollectionBookHandler("12", payload)
|
|
assert.Equal(t, http.StatusNotFound, status)
|
|
}
|
|
|
|
func TestPostCollectionBookHandler_BookNotFound(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"bookId": 14654
|
|
}`
|
|
status := testPostCollectionBookHandler("1", payload)
|
|
assert.Equal(t, http.StatusNotFound, status)
|
|
}
|
|
|
|
func TestPostCollectionBookHandler_Unauthorized(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"bookId": 9
|
|
}`
|
|
status := testPostCollectionBookHandler("3", payload)
|
|
assert.Equal(t, http.StatusUnauthorized, status)
|
|
}
|
|
|
|
func testPostCollectionBookHandler(collectionId string, payload string) int {
|
|
router := testutils.TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("POST", "/ws/collection/"+collectionId+"/addbook",
|
|
strings.NewReader(payload))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
|
|
return w.Code
|
|
}
|