88 lines
2.0 KiB
Go
88 lines
2.0 KiB
Go
package apitest
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestPutRatingUserBooksHandler_UpdateRating(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 5
|
|
}`
|
|
bookId := "17"
|
|
testPutRateUserBooks(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"
|
|
testPutRateUserBooks(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"
|
|
testPutRateUserBooks(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"
|
|
testPutRateUserBooks(t, payload, bookId, http.StatusInternalServerError)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RatingMin(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": -3
|
|
}`
|
|
bookId := "18"
|
|
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_RatingMax(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 15
|
|
}`
|
|
bookId := "18"
|
|
testPutRateUserBooks(t, payload, bookId, http.StatusBadRequest)
|
|
}
|
|
|
|
func TestPutRatingUserBooksHandler_BadBookId(t *testing.T) {
|
|
payload :=
|
|
`{
|
|
"rating": 15
|
|
}`
|
|
bookId := "18574"
|
|
testPutRateUserBooks(t, payload, bookId, http.StatusNotFound)
|
|
}
|
|
|
|
func testPutRateUserBooks(t *testing.T, payload string, bookId string, expectedCode int) {
|
|
testutils.TestBookPutCallWithDemoPayload(t, payload, bookId, expectedCode, "/ws/book/"+bookId+"/rate")
|
|
}
|