52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package apitest
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.artlef.fr/bibliomane/internal/testutils"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDeleteCollectionHandler_EmptyCollection(t *testing.T) {
|
|
collectionId := "8"
|
|
status := testDeleteCollectionHandler(collectionId)
|
|
assert.Equal(t, http.StatusOK, status)
|
|
getStatus, _ := testGetCollection(t, collectionId, "10", "0")
|
|
assert.Equal(t, http.StatusNotFound, getStatus)
|
|
}
|
|
|
|
func TestDeleteCollectionHandler_NotEmptyCollection(t *testing.T) {
|
|
collectionId := "7"
|
|
status := testDeleteCollectionHandler(collectionId)
|
|
assert.Equal(t, http.StatusOK, status)
|
|
getStatus, _ := testGetCollection(t, collectionId, "10", "0")
|
|
assert.Equal(t, http.StatusNotFound, getStatus)
|
|
}
|
|
|
|
func TestDeleteCollectionHandler_NonExistingCollection(t *testing.T) {
|
|
collectionId := "425"
|
|
status := testDeleteCollectionHandler(collectionId)
|
|
assert.Equal(t, http.StatusNotFound, status)
|
|
}
|
|
|
|
func TestDeleteCollectionHandler_ForbiddenCollection(t *testing.T) {
|
|
collectionId := "3"
|
|
status := testDeleteCollectionHandler(collectionId)
|
|
assert.Equal(t, http.StatusUnauthorized, status)
|
|
}
|
|
|
|
func testDeleteCollectionHandler(id string) int {
|
|
router := testutils.TestSetup()
|
|
w := httptest.NewRecorder()
|
|
|
|
token := testutils.ConnectDemoUser(router)
|
|
req, _ := http.NewRequest("DELETE", "/ws/collection/"+id, nil)
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
router.ServeHTTP(w, req)
|
|
|
|
return w.Code
|
|
}
|