Changed assets endpoint to multipart

This commit is contained in:
2022-02-06 16:53:53 +01:00
parent d4ac4409ad
commit 8bc01e0984
3 changed files with 102 additions and 26 deletions

View File

@@ -3,7 +3,9 @@ package api
import (
"TheAdversary/database"
"TheAdversary/schema"
"encoding/base64"
"bytes"
"fmt"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
@@ -31,6 +33,13 @@ func TestAssetsGet(t *testing.T) {
checkTestInformation(t, server.URL, []testInformation{
{
Method: http.MethodGet,
Code: http.StatusUnauthorized,
},
{
Method: http.MethodGet,
Cookie: map[string]string{
"session_id": initSession(),
},
Query: map[string]interface{}{
"q": "linux",
},
@@ -45,6 +54,9 @@ func TestAssetsGet(t *testing.T) {
},
{
Method: http.MethodGet,
Cookie: map[string]string{
"session_id": initSession(),
},
Query: map[string]interface{}{
"limit": 1,
},
@@ -59,6 +71,9 @@ func TestAssetsGet(t *testing.T) {
},
{
Method: http.MethodGet,
Cookie: map[string]string{
"session_id": initSession(),
},
ResultBody: []schema.Asset{
{
Id: 1,
@@ -81,27 +96,47 @@ func TestAssetsPost(t *testing.T) {
t.Fatal(err)
}
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
mw.SetBoundary("test")
formFile, _ := mw.CreateFormFile("file", "srfwsr")
formFile.Write([]byte("just a test file"))
mw.WriteField("name", "test")
mw.Close()
fmt.Println(buf.String())
server := httptest.NewServer(http.HandlerFunc(assetsPost))
checkTestInformation(t, server.URL, []testInformation{
{
Method: http.MethodPost,
Body: assetsPostPayload{
Name: "test",
Content: base64.StdEncoding.EncodeToString([]byte("test asset")),
Code: http.StatusUnauthorized,
},
{
Method: http.MethodPost,
Header: map[string]string{
"Content-Type": "multipart/form-data; boundary=test",
},
Cookie: map[string]string{
"session_id": initSession(),
},
Body: buf.Bytes(),
ResultBody: schema.Asset{
Id: 1,
Name: "test",
Link: "/assets/test",
},
Code: http.StatusOK,
Code: http.StatusCreated,
},
{
Method: http.MethodPost,
Body: assetsPostPayload{
Name: "test",
Content: base64.StdEncoding.EncodeToString([]byte("test asset")),
Header: map[string]string{
"Content-Type": "multipart/form-data; boundary=test",
},
Cookie: map[string]string{
"session_id": initSession(),
},
Body: buf.Bytes(),
Code: http.StatusConflict,
},
})
@@ -122,6 +157,13 @@ func TestAssetsDelete(t *testing.T) {
checkTestInformation(t, server.URL, []testInformation{
{
Method: http.MethodDelete,
Code: http.StatusUnauthorized,
},
{
Method: http.MethodDelete,
Cookie: map[string]string{
"session_id": initSession(),
},
Body: assetsDeletePayload{
Id: 1,
},
@@ -129,6 +171,9 @@ func TestAssetsDelete(t *testing.T) {
},
{
Method: http.MethodDelete,
Cookie: map[string]string{
"session_id": initSession(),
},
Body: assetsDeletePayload{
Id: 69,
},