Commit 18c17f0f authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

testUploadAttachment

parent c144bf1b
......@@ -238,7 +238,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't fetch data", 500}
}
if share.IsTemporary != true {
return &HTTPError{ errors.New("Cant upload to finalized shares"), "Can't upload to finalized Shares.", 403 }
return &HTTPError{ errors.New("Can't upload to finalized shares"), "Can't upload to finalized Shares.", 403 }
}
// Parse file from body
......
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
......@@ -63,6 +68,8 @@ func Reset() {
for _, sh := range shares {
db.Create(&sh)
}
// testfile
ioutil.WriteFile(filepath.Join(config.mediaDir, "kekw.txt"), []byte("Hallo welt"), os.ModePerm)
}
/////////////////////////////////////
......@@ -80,8 +87,8 @@ func TestAllShares(t *testing.T) {
body, _ := ioutil.ReadAll(res.Body)
var erg []Share
json.Unmarshal(body, &erg)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, shares[1], erg[0])
assert.EqualValues(t, http.StatusOK, res.StatusCode)
assert.EqualValues(t, shares[1], erg[0])
})
}
......@@ -139,7 +146,7 @@ func TestDownloadFile(t *testing.T) {
defer ts.Close()
t.Run("happy path", func(t *testing.T) {
//assert
})
}
......@@ -151,35 +158,58 @@ func TestOpenShare(t *testing.T) {
defer ts.Close()
t.Run("happy path", func(t *testing.T) {
b, _ := json.Marshal(Share {
ID: uuid.MustParse("e5134044-2704-4864-85be-318fb158009f"),
Name: "TestOpenShare",
})
var newShare = Share{
ID: uuid.MustParse("e5134044-2704-4864-85be-318fb158009f"),
Name: "TestOpenShare",
Expires: nil,
DownloadLimit: 69,
IsPublic: false,
Attachments: []Attachment {
{
ID: uuid.MustParse("2b524827-9c3c-47e0-9277-8b51fd45b4bd"),
Filename: "kekw.txt",
Filesize: 123456,
IsEncrypted: false,
ShareID: uuid.MustParse("e5134044-2704-4864-85be-318fb158009f"),
},
},
}
b, _ := json.Marshal(newShare)
req, _ := http.NewRequest("POST", ts.URL + "/shares", strings.NewReader(string(b)))
res, _ := http.DefaultClient.Do(req)
//body, _ := ioutil.ReadAll(res.Body)
body, _ := ioutil.ReadAll(res.Body)
var actual Share
json.Unmarshal(body, &actual)
// checks
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.DirExists(t, filepath.Join(config.mediaDir, "temp", newShare.ID.String()))
assert.NoDirExists(t, filepath.Join(config.mediaDir, "data", newShare.ID.String()))
assert.Equal(t, newShare.ID, actual.ID)
//assert.(t, newShare, actual)
})
t.Run("bad request", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/shares", nil)
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
//body, _ := ioutil.ReadAll(res.Body)
//fmt.Println(string(body))
// checks
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
})
}
func TestCloseShare(t *testing.T) {
Reset()
router := mux.NewRouter()
ts := httptest.NewServer(router)
router.Handle("/share/{id}", endpointREST(CloseShare)).Methods("POST")
defer ts.Close()
t.Run("happy path", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/share/e5134044-2704-4864-85be-318fb158009f", nil)
req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791", nil)
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
assert.Equal(t, http.StatusOK, res.StatusCode)
......@@ -197,8 +227,28 @@ func TestCloseShare(t *testing.T) {
}
func TestUploadAttachment(t *testing.T) {
Reset()
router := mux.NewRouter()
ts := httptest.NewServer(router)
router.Handle("/share/{id}/attachments", endpointREST(UploadAttachment)).Methods("POST")
defer ts.Close()
t.Run("happy path", func(t *testing.T) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fw, _ := writer.CreateFormFile("files", "poggers.txt")
file, _ := os.Open(filepath.Join(config.mediaDir, "poggers.txt"))
io.Copy(fw, file)
file.Close()
writer.Close()
req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", bytes.NewReader(body.Bytes()))
req.Header.Set("Content-Type", writer.FormDataContentType())
res, _ := http.DefaultClient.Do(req)
//resBody, _ :=ioutil.ReadAll(res.Body)
//fmt.Println(string(resBody))
assert.EqualValues(t, http.StatusOK, res.StatusCode)
})
t.Run("not found", func(t *testing.T) {
......@@ -206,7 +256,12 @@ func TestUploadAttachment(t *testing.T) {
})
t.Run("bad request", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", nil)
res, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
})
t.Run("forbidden", func(t *testing.T) {
})
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment