Collection: new widget to add book to collection
This commit is contained in:
64
internal/apitest/post_collection_addbook_test.go
Normal file
64
internal/apitest/post_collection_addbook_test.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPostCollectionBookHandler_Ok(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"bookId": 9
|
||||
}`
|
||||
collectionId := "1"
|
||||
status := testPostCollectionBookHandler(t, collectionId, payload)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
_, collection := testGetCollection(t, collectionId, "10", "0")
|
||||
assert.Equal(t, int64(7), collection.Count)
|
||||
}
|
||||
|
||||
func TestPostCollectionBookHandler_CollectionNotFound(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"bookId": 9
|
||||
}`
|
||||
status := testPostCollectionBookHandler(t, "12", payload)
|
||||
assert.Equal(t, http.StatusNotFound, status)
|
||||
}
|
||||
|
||||
func TestPostCollectionBookHandler_BookNotFound(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"bookId": 14654
|
||||
}`
|
||||
status := testPostCollectionBookHandler(t, "1", payload)
|
||||
assert.Equal(t, http.StatusNotFound, status)
|
||||
}
|
||||
|
||||
func TestPostCollectionBookHandler_Unauthorized(t *testing.T) {
|
||||
payload :=
|
||||
`{
|
||||
"bookId": 9
|
||||
}`
|
||||
status := testPostCollectionBookHandler(t, "3", payload)
|
||||
assert.Equal(t, http.StatusUnauthorized, status)
|
||||
}
|
||||
|
||||
func testPostCollectionBookHandler(t *testing.T, 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
|
||||
}
|
||||
Reference in New Issue
Block a user