Create new collections from my collections view
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
@@ -88,27 +84,7 @@ func TestPostBookHandler_AuthorTooLong(t *testing.T) {
|
||||
}
|
||||
|
||||
func testPostBookHandler(t *testing.T, bookJson string, expectedCode int) uint {
|
||||
router := testutils.TestSetup()
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("POST", "/ws/book",
|
||||
strings.NewReader(string(bookJson)))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, expectedCode, w.Code)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
return 0
|
||||
}
|
||||
var parsed struct {
|
||||
ID uint
|
||||
}
|
||||
|
||||
err := json.Unmarshal(w.Body.Bytes(), &parsed)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
return parsed.ID
|
||||
status, id := testutils.TestPostCall(t, "/ws/book", bookJson)
|
||||
assert.Equal(t, expectedCode, status)
|
||||
return id
|
||||
}
|
||||
|
||||
31
internal/apitest/post_collection_test.go
Normal file
31
internal/apitest/post_collection_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPostCollectionHandler_Ok(t *testing.T) {
|
||||
collectionJson :=
|
||||
`{
|
||||
"name": "My collection"
|
||||
}`
|
||||
status, _ := testPostCollectionHandler(t, collectionJson)
|
||||
assert.Equal(t, http.StatusOK, status)
|
||||
}
|
||||
|
||||
func TestPostCollectionHandler_NameTooLong(t *testing.T) {
|
||||
collectionJson :=
|
||||
`{
|
||||
"name": "rsteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenrsteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchenrsteerdemenschderraumschiffgebrauchlichtalsseinursprungvonkraftgestartseinlangefahrthinzwischensternartigraumaufdersuchen"
|
||||
}`
|
||||
status, _ := testPostCollectionHandler(t, collectionJson)
|
||||
assert.Equal(t, http.StatusBadRequest, status)
|
||||
}
|
||||
|
||||
func testPostCollectionHandler(t *testing.T, collectionJson string) (int, uint) {
|
||||
return testutils.TestPostCall(t, "/ws/collection", collectionJson)
|
||||
}
|
||||
Reference in New Issue
Block a user