move tests to dedicated package

This commit is contained in:
2025-10-27 18:08:47 +01:00
parent cae46614ba
commit d407b41c9f
7 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,43 @@
package apitest
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
"github.com/stretchr/testify/assert"
)
func TestPostBookReadHandler_Ok(t *testing.T) {
userBookJson :=
`{
"bookId": 6
}`
testPostBookReadHandler(t, userBookJson, http.StatusOK)
}
func TestPostBookReadHandler_IDDoesNotExist(t *testing.T) {
userBookJson :=
`{
"bookId": 46546
}`
testPostBookReadHandler(t, userBookJson, http.StatusNotFound)
}
func testPostBookReadHandler(t *testing.T, userBookJson string, expectedCode int) {
router := testutils.TestSetup()
w := httptest.NewRecorder()
token := testutils.ConnectDemo2User(router)
req, _ := http.NewRequest("POST", "/book/read",
strings.NewReader(string(userBookJson)))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
router.ServeHTTP(w, req)
log.Printf("%s\n", w.Body.String())
assert.Equal(t, expectedCode, w.Code)
}