Collection: new widget to add book to collection

This commit is contained in:
2026-04-04 23:15:44 +02:00
parent c7abbfe4d4
commit 2552ba8e94
11 changed files with 279 additions and 16 deletions

View 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
}

View File

@@ -3,6 +3,7 @@ package apitest
import (
"encoding/json"
"fmt"
"strconv"
"net/http"
"net/http/httptest"
@@ -15,14 +16,14 @@ import (
)
func TestSearchBook_MultipleBooks(t *testing.T) {
result := testSearchBook(t, "san", "", "")
result := testSearchBook(t, "san", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(2), result.Count)
assert.Equal(t, 2, len(result.Books))
}
func TestSearchBook_OneBookNotUserBook(t *testing.T) {
result := testSearchBook(t, "iliade", "", "")
result := testSearchBook(t, "iliade", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
@@ -38,7 +39,7 @@ func TestSearchBook_OneBookNotUserBook(t *testing.T) {
}
func TestSearchBook_OneBookRead(t *testing.T) {
result := testSearchBook(t, "dieux", "", "")
result := testSearchBook(t, "dieux", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
@@ -55,7 +56,7 @@ func TestSearchBook_OneBookRead(t *testing.T) {
}
func TestSearchBook_OneBookStartRead(t *testing.T) {
result := testSearchBook(t, "Recherches", "", "")
result := testSearchBook(t, "Recherches", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
@@ -72,7 +73,7 @@ func TestSearchBook_OneBookStartRead(t *testing.T) {
}
func TestSearchBook_ISBN(t *testing.T) {
result := testSearchBook(t, "9782070337903", "", "")
result := testSearchBook(t, "9782070337903", "", "", dto.NoInventaireSearch)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
@@ -88,7 +89,7 @@ func TestSearchBook_ISBN(t *testing.T) {
}
func TestSearchBook_ISBNInventaire(t *testing.T) {
result := testSearchBook(t, "9782253158400", "", "")
result := testSearchBook(t, "9782253158400", "", "", dto.InventaireIfNothingFound)
assert.Equal(t, int64(1), result.Count)
assert.Equal(t,
[]dto.BookItemGet{{
@@ -107,17 +108,17 @@ func TestSearchBook_ISBNInventaire(t *testing.T) {
}
func TestSearchBook_Limit(t *testing.T) {
result := testSearchBook(t, "a", "10", "")
result := testSearchBook(t, "a", "10", "", dto.NoInventaireSearch)
assert.Equal(t, 10, len(result.Books))
}
func TestSearchBook_Offset(t *testing.T) {
result := testSearchBook(t, "sa", "", "2")
result := testSearchBook(t, "sa", "", "2", dto.NoInventaireSearch)
assert.Equal(t, int64(5), result.Count)
assert.Equal(t, 3, len(result.Books))
}
func testSearchBook(t *testing.T, searchterm string, limit string, offset string) dto.BookItemsGet {
func testSearchBook(t *testing.T, searchterm string, limit string, offset string, inventaireSearchType dto.InventaireSearchType) dto.BookItemsGet {
router := testutils.TestSetup()
u, err := url.Parse("/ws/search/" + searchterm)
@@ -137,6 +138,7 @@ func testSearchBook(t *testing.T, searchterm string, limit string, offset string
q := u.Query()
q.Set("lang", "fr")
q.Set("inventaire", strconv.Itoa(int(inventaireSearchType)))
u.RawQuery = q.Encode()
token := testutils.ConnectDemoUser(router)