191 lines
4.3 KiB
Go
191 lines
4.3 KiB
Go
package apitest
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.artlef.fr/bibliomane/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPutRatingUserBooksHandler_UpdateRating(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 5
|
|
}`
|
|
bookId := "17"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, 5, book.Rating)
|
|
assert.Equal(t, true, book.Read)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RateNewBookMakeItRead(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 7
|
|
}`
|
|
bookId := "18"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, 7, book.Rating)
|
|
assert.Equal(t, true, book.Read)
|
|
assert.Equal(t, false, book.WantRead)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RateWantedBook(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 6
|
|
}`
|
|
bookId := "2"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, 6, book.Rating)
|
|
assert.Equal(t, true, book.Read)
|
|
assert.Equal(t, false, book.WantRead)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RatingTypeWrong(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": "bad"
|
|
}`
|
|
bookId := "18"
|
|
testPutUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RatingMin(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": -3
|
|
}`
|
|
bookId := "18"
|
|
testPutUserBooks(t, payload, bookId, http.StatusBadRequest)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RatingMax(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 15
|
|
}`
|
|
bookId := "18"
|
|
testPutUserBooks(t, payload, bookId, http.StatusBadRequest)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_BadBookId(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 15
|
|
}`
|
|
bookId := "18574"
|
|
testPutUserBooks(t, payload, bookId, http.StatusNotFound)
|
|
}
|
|
|
|
func TestPutReadUserBooks_NewReadOk(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"read": true
|
|
}`
|
|
bookId := "21"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, true, book.Read)
|
|
assert.Equal(t, false, book.WantRead)
|
|
}
|
|
|
|
func TestPutReadUserBooks_NewReadDateOk(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"read": true,
|
|
"endDate": "2025-10-20"
|
|
}`
|
|
bookId := "9"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, true, book.Read)
|
|
assert.Equal(t, "2025-10-20", book.EndReadDate)
|
|
}
|
|
|
|
func TestPutReadUserBooks_UnsetEndDate(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"read": true,
|
|
"endDate": "null"
|
|
}`
|
|
bookId := "9"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, true, book.Read)
|
|
assert.Equal(t, "", book.EndReadDate)
|
|
}
|
|
|
|
func TestPutReadUserBooks_UnsetReadOk(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"read": false
|
|
}`
|
|
bookId := "9"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, false, book.Read)
|
|
assert.Equal(t, "", book.EndReadDate)
|
|
}
|
|
|
|
func TestPutStartReadUserBooks_WrongDateFormat(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"startDate": "19/11/2025"
|
|
}`
|
|
bookId := "6"
|
|
testPutUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
|
}
|
|
|
|
func TestPutStartReadUserBooks_NewReadOk(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"startDate": "2025-11-19"
|
|
}`
|
|
bookId := "6"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, "2025-11-19", book.StartReadDate)
|
|
}
|
|
|
|
func TestPutStartReadUserBooks_Unset(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"startDate": "null"
|
|
}`
|
|
bookId := "6"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, "", book.StartReadDate)
|
|
}
|
|
|
|
func TestPutWantRead_SetTrue(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"wantread": true
|
|
}`
|
|
bookId := "17"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, true, book.WantRead)
|
|
}
|
|
|
|
func TestPutWantRead_SetFalse(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"wantread": false
|
|
}`
|
|
bookId := "2"
|
|
testPutUserBooks(t, payload, bookId, http.StatusOK)
|
|
book := testGetBook(t, bookId, http.StatusOK)
|
|
assert.Equal(t, false, book.WantRead)
|
|
}
|
|
|
|
func testPutUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
|
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId)
|
|
}
|