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

better test

and one simple bugfix
parent 8faaf5f0
...@@ -97,7 +97,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -97,7 +97,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil { if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 } return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
} }
attID, err := uuid.Parse(vars["id"]) attID, err := uuid.Parse(vars["att"])
if err != nil { if err != nil {
return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 } return &HTTPError{ errors.New("invalid URL"), "invalid URL param", 400 }
} }
...@@ -117,7 +117,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -117,7 +117,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
} }
var share Share var share Share
err = db.Where("id = ?", att.ShareID).First(&share).Error err = db.Where("id = ?", att.ShareID.String()).First(&share).Error
if errors.Is(err, gorm.ErrRecordNotFound) { if errors.Is(err, gorm.ErrRecordNotFound) {
return &HTTPError{err, "Record not found", 404} return &HTTPError{err, "Record not found", 404}
} }
......
...@@ -72,8 +72,8 @@ func Reset() { ...@@ -72,8 +72,8 @@ func Reset() {
} }
os.MkdirAll(filepath.Join(config.mediaDir, "data"), os.ModePerm) os.MkdirAll(filepath.Join(config.mediaDir, "data"), os.ModePerm)
os.MkdirAll(filepath.Join(config.mediaDir, "temp"), os.ModePerm) os.MkdirAll(filepath.Join(config.mediaDir, "temp"), os.ModePerm)
// testfile // testfiles
ioutil.WriteFile(filepath.Join(config.mediaDir, "poggers.txt"), []byte("Hallo welt"), os.ModePerm) ioutil.WriteFile(filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()), []byte("KEKW KEKW KEKW"), os.ModePerm)
} }
///////////////////////////////////// /////////////////////////////////////
...@@ -105,9 +105,9 @@ func TestGetShare(t *testing.T) { ...@@ -105,9 +105,9 @@ func TestGetShare(t *testing.T) {
t.Run("happy path", func(t *testing.T) { t.Run("happy path", func(t *testing.T) {
header := map[string][]string{ header := map[string][]string{
"Authorization": { "Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM=" }, "Authorization": { "Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM=" }, // pw: test123
} }
req, _ := http.NewRequest("GET", ts.URL + "/share/5713d228-a042-446d-a5e4-183b19fa832a", nil) req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[0].ID.String()), nil)
req.Header = header req.Header = header
res, _ := http.DefaultClient.Do(req) res, _ := http.DefaultClient.Do(req)
...@@ -124,19 +124,19 @@ func TestGetShare(t *testing.T) { ...@@ -124,19 +124,19 @@ func TestGetShare(t *testing.T) {
}) })
t.Run("unauthorized", func(t *testing.T) { t.Run("unauthorized", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL + "/share/5713d228-a042-446d-a5e4-183b19fa832a", nil) req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[0].ID.String()), nil)
res, _ := http.DefaultClient.Do(req) res, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusUnauthorized, res.StatusCode) assert.Equal(t, http.StatusUnauthorized, res.StatusCode)
}) })
t.Run("not found", func(t *testing.T) { t.Run("not found", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL + "/share/4015a76b-09d0-402b-814f-bd9fa48ce8e1", nil) req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/4015a76b-09d0-402b-814f-bd9fa48ce8e1", ts.URL), nil)
res, _ := http.DefaultClient.Do(req) res, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusNotFound, res.StatusCode) assert.Equal(t, http.StatusNotFound, res.StatusCode)
}) })
t.Run("not ready", func(t *testing.T) { t.Run("not ready", func(t *testing.T) {
req, _ := http.NewRequest("GET", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791", nil) req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[2].ID.String()), nil)
res, _ := http.DefaultClient.Do(req) res, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusForbidden, res.StatusCode) assert.Equal(t, http.StatusForbidden, res.StatusCode)
}) })
...@@ -146,11 +146,26 @@ func TestDownloadFile(t *testing.T) { ...@@ -146,11 +146,26 @@ func TestDownloadFile(t *testing.T) {
Reset() Reset()
router := mux.NewRouter() router := mux.NewRouter()
ts := httptest.NewServer(router) ts := httptest.NewServer(router)
router.Handle("/share/{id}", endpointREST(GetShare)).Methods("GET") router.Handle("/share/{id}/attachment/{att}", endpointREST(DownloadFile)).Methods("GET")
defer ts.Close() defer ts.Close()
t.Run("happy path", func(t *testing.T) { t.Run("happy path", func(t *testing.T) {
//assert req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s/attachment/%s", ts.URL, shares[0].ID.String(), shares[0].Attachments[0].ID.String()), nil)
req.Header = map[string][]string{
"Authorization": { "Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM=" }, // pw: test123
}
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
assert.FileExists(t, filepath.Join("data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
expected, _ := ioutil.ReadFile(filepath.Join("data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
assert.EqualValues(t, expected ,body)
assert.EqualValues(t, http.StatusOK, res.StatusCode)
})
t.Run("not found", func(t *testing.T) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s/attachment/%s", ts.URL, shares[0].ID.String(), "dfvjdfiogbj"), nil)
res, _ := http.DefaultClient.Do(req)
assert.EqualValues(t, http.StatusNotFound, res.StatusCode)
}) })
} }
...@@ -242,9 +257,7 @@ func TestUploadAttachment(t *testing.T) { ...@@ -242,9 +257,7 @@ func TestUploadAttachment(t *testing.T) {
body := &bytes.Buffer{} body := &bytes.Buffer{}
writer := multipart.NewWriter(body) writer := multipart.NewWriter(body)
fw, _ := writer.CreateFormFile("files", "poggers.txt") fw, _ := writer.CreateFormFile("files", "poggers.txt")
file, _ := os.Open(filepath.Join(config.mediaDir, "poggers.txt")) io.Copy(fw, strings.NewReader("POG POG POG POG"))
io.Copy(fw, file)
file.Close()
writer.Close() writer.Close()
req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", bytes.NewReader(body.Bytes())) req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791/attachments", bytes.NewReader(body.Bytes()))
......
...@@ -42,7 +42,12 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error { ...@@ -42,7 +42,12 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error {
sh.ID = uid sh.ID = uid
} }
// create temporary dir // create temporary dir
return os.MkdirAll(filepath.Join(config.mediaDir, "temp", sh.ID.String()), os.ModePerm) if sh.IsTemporary == true {
return os.MkdirAll(filepath.Join(config.mediaDir, "temp", sh.ID.String()), os.ModePerm)
} else {
return os.MkdirAll(filepath.Join(config.mediaDir, "data", sh.ID.String()), os.ModePerm)
}
} }
func (att *Attachment) BeforeCreate(scope *gorm.DB) error { func (att *Attachment) BeforeCreate(scope *gorm.DB) error {
......
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