73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package apitest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type hasId struct {
|
|
ID uint `json:"id"`
|
|
}
|
|
|
|
func TestPostImportBookHandler_Ok(t *testing.T) {
|
|
id := testPostImportBookHandler(t, "isbn:9782253004752", http.StatusOK)
|
|
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
|
assert.Equal(t, "les Hauts de Hurle-Vent", book.Title)
|
|
assert.Equal(t, "Emily Brontë", book.Author)
|
|
assert.Equal(t, "isbn:9782253004752", book.InventaireID)
|
|
assert.Equal(t, "/bookcover/44abbcbdc1092212c2bae66f5165019dac1e2a7b.webp", book.CoverPath)
|
|
}
|
|
|
|
func TestPostImportBookHandler_OkAuthorKey(t *testing.T) {
|
|
id := testPostImportBookHandler(t, "isbn:9782290033630", http.StatusOK)
|
|
book := testGetBook(t, strconv.FormatUint(uint64(id), 10), 200)
|
|
assert.Equal(t, "Dr Bloodmoney", book.Title)
|
|
assert.Equal(t, "Philip K. Dick", book.Author)
|
|
assert.Equal(t, "isbn:9782290033630", book.InventaireID)
|
|
assert.Equal(t, "/bookcover/1d1493159d031224a42b37c4417fcbb8c76b00bd.webp", book.CoverPath)
|
|
}
|
|
|
|
func TestPostImportBookHandler_NoOLID(t *testing.T) {
|
|
testPostImportBookHandler(t, "", http.StatusBadRequest)
|
|
}
|
|
|
|
func testPostImportBookHandler(t *testing.T, openlibraryid string, expectedCode int) uint {
|
|
router := testutils.TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
queryJson := `{
|
|
"inventaireid":"%s",
|
|
"lang":"fr"
|
|
}`
|
|
queryJson = fmt.Sprintf(queryJson, openlibraryid)
|
|
req, _ := http.NewRequest("POST", "/importbook",
|
|
strings.NewReader(string(queryJson)))
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, expectedCode, w.Code)
|
|
|
|
if w.Code == 200 {
|
|
var parsedId hasId
|
|
err := json.Unmarshal(w.Body.Bytes(), &parsedId)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
return parsedId.ID
|
|
} else if expectedCode == 200 {
|
|
var stringerr = w.Body.String()
|
|
t.Error(stringerr)
|
|
return 0
|
|
}
|
|
return 0
|
|
}
|