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

clean up

parent 6c9459e6
......@@ -51,13 +51,12 @@ func AllShares(w http.ResponseWriter, _ *http.Request) *HTTPError {
return SendJSON(w, shares)
}
func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("Get Share")
vars := mux.Vars(r)
shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
db, err := GetDatabase()
......@@ -91,18 +90,17 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return SendJSON(w, share)
}
func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("Download file")
vars := mux.Vars(r)
shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
attID, err := uuid.Parse(vars["att"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
db, err := GetDatabase()
......@@ -144,7 +142,6 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
return nil
}
func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("OpenShare")
......@@ -175,14 +172,13 @@ func OpenShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return SendJSON(w, newShare)
}
func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("CloseShare")
vars := mux.Vars(r)
shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
db, err := GetDatabase()
......@@ -224,7 +220,7 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
// "ShareID": share.ID,
//})
if err != nil {
return &HTTPError{ err, "Error creating background job", 500 }
return &HTTPError{err, "Error creating background job", 500}
}
PrettyPrint(job)
}
......@@ -232,14 +228,13 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return SendJSON(w, share)
}
func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
fmt.Println("UploadTest")
vars := mux.Vars(r)
shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
db, err := GetDatabase()
......@@ -257,7 +252,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("share is not finalized"), "Can't upload to finalized Shares.", 403 }
return &HTTPError{errors.New("share is not finalized"), "Can't upload to finalized Shares.", 403}
}
// Parse file from body
......@@ -277,7 +272,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
db.Begin()
sid, err := uuid.Parse(shareID.String())
if err != nil {
return &HTTPError{ err, "foreign key shareID not valid", 500 }
return &HTTPError{err, "foreign key shareID not valid", 500}
}
att.ShareID = sid
att.Filename = handler.Filename
......@@ -287,12 +282,12 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
// save file
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
return &HTTPError{ err, "cant read file", 500 }
return &HTTPError{err, "cant read file", 500}
}
err = ioutil.WriteFile(filepath.Join(config.mediaDir, "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm)
if err != nil {
db.Rollback()
return &HTTPError{ err, "cant save file", 500 }
return &HTTPError{err, "cant save file", 500}
}
db.Commit()
}
......@@ -306,7 +301,7 @@ func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError {
vars := mux.Vars(r)
shareID, err := uuid.Parse(vars["id"])
if err != nil {
return &HTTPError{ err, "invalid URL param", 400 }
return &HTTPError{err, "invalid URL param", 400}
}
share, er := RetrieveShare(shareID, true)
......@@ -320,17 +315,17 @@ func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError {
fileToZip, err := os.Open(filePath)
if err != nil {
return &HTTPError{ err, "error opening file", 500}
return &HTTPError{err, "error opening file", 500}
}
info, err := fileToZip.Stat()
if err != nil {
return &HTTPError{ err, "error getting file info", 500 }
return &HTTPError{err, "error getting file info", 500}
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return &HTTPError{ err, "error creating file header", 500 }
return &HTTPError{err, "error creating file header", 500}
}
header.Name = file.Filename
......@@ -338,19 +333,19 @@ func DownloadZip(w http.ResponseWriter, r *http.Request) *HTTPError {
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return &HTTPError{ err, "error creating header", 500 }
return &HTTPError{err, "error creating header", 500}
}
if _, err := io.Copy(writer, fileToZip); err != nil{
return &HTTPError{ err, "error copying file into zip archive", 500 }
if _, err := io.Copy(writer, fileToZip); err != nil {
return &HTTPError{err, "error copying file into zip archive", 500}
}
if err := fileToZip.Close(); err != nil {
return &HTTPError{ err, "error closing zip archive", 500 }
return &HTTPError{err, "error closing zip archive", 500}
}
}
err = zipWriter.Close()
if err != nil {
return &HTTPError{ err, "error when closing zip", 500 }
return &HTTPError{err, "error when closing zip", 500}
}
return nil
......@@ -368,7 +363,7 @@ func RetrieveShare(shareID uuid.UUID, withAtt bool) (*Share, *HTTPError) {
var share Share
if withAtt == true {
err = db.Preload("Attachments").Where("ID = ?", shareID).First(&share).Error
}else {
} else {
err = db.Where("ID = ?", shareID).First(&share).Error
}
......
......@@ -20,7 +20,7 @@ import (
)
// DATA
var shares = []Share {
var shares = []Share{
{
ID: uuid.MustParse("5713d228-a042-446d-a5e4-183b19fa832a"),
Name: "TestFinalPrivate",
......@@ -28,9 +28,9 @@ var shares = []Share {
IsPublic: false,
IsTemporary: false,
Password: "test123",
Emails:[]string{""},
Emails: []string{""},
Attachments: []Attachment {
Attachments: []Attachment{
{
ID: uuid.MustParse("913134c0-894f-4c4d-b545-92ec373168b1"),
Filename: "kekw.txt",
......@@ -45,7 +45,7 @@ var shares = []Share {
Name: "TestFinalPublic",
IsPublic: true,
IsTemporary: false,
Emails:[]string{""},
Emails: []string{""},
},
{
ID: uuid.MustParse("a558aca3-fb40-400b-8dc6-ae49c705c791"),
......@@ -53,7 +53,7 @@ var shares = []Share {
DownloadLimit: 300,
IsPublic: true,
IsTemporary: true,
Emails:[]string{""},
Emails: []string{""},
},
}
......@@ -108,7 +108,7 @@ func TestGetShare(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
header := map[string][]string{
"Authorization": { "Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM=" }, // pw: test123
"Authorization": {"Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM="}, // pw: test123
}
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/share/%s", ts.URL, shares[0].ID.String()), nil)
req.Header = header
......@@ -155,13 +155,13 @@ func TestDownloadFile(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
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
"Authorization": {"Basic NTcxM2QyMjgtYTA0Mi00NDZkLWE1ZTQtMTgzYjE5ZmE4MzJhOnRlc3QxMjM="}, // pw: test123
}
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
assert.FileExists(t, filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
expected, _ := ioutil.ReadFile(filepath.Join(config.mediaDir, "data", shares[0].ID.String(), shares[0].Attachments[0].ID.String()))
assert.EqualValues(t, expected ,body)
assert.EqualValues(t, expected, body)
assert.EqualValues(t, http.StatusOK, res.StatusCode)
})
......@@ -192,7 +192,7 @@ func TestOpenShare(t *testing.T) {
Expires: nil,
DownloadLimit: 69,
IsPublic: false,
Attachments: []Attachment {
Attachments: []Attachment{
{
ID: uuid.MustParse("2b524827-9c3c-47e0-9277-8b51fd45b4bd"),
Filename: "hallo.txt",
......@@ -203,7 +203,7 @@ func TestOpenShare(t *testing.T) {
},
}
b, _ := json.Marshal(newShare)
req, _ := http.NewRequest("POST", ts.URL + "/shares", strings.NewReader(string(b)))
req, _ := http.NewRequest("POST", ts.URL+"/shares", strings.NewReader(string(b)))
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
var actual Share
......@@ -218,7 +218,7 @@ func TestOpenShare(t *testing.T) {
})
t.Run("bad request", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/shares", nil)
req, _ := http.NewRequest("POST", ts.URL+"/shares", nil)
res, _ := http.DefaultClient.Do(req)
//body, _ := ioutil.ReadAll(res.Body)
//fmt.Println(string(body))
......@@ -235,7 +235,7 @@ func TestCloseShare(t *testing.T) {
defer ts.Close()
t.Run("happy path", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/share/a558aca3-fb40-400b-8dc6-ae49c705c791", nil)
req, _ := http.NewRequest("POST", ts.URL+"/share/a558aca3-fb40-400b-8dc6-ae49c705c791", nil)
res, _ := http.DefaultClient.Do(req)
body, _ := ioutil.ReadAll(res.Body)
......@@ -245,7 +245,7 @@ func TestCloseShare(t *testing.T) {
})
t.Run("not found", func(t *testing.T) {
req, _ := http.NewRequest("POST", ts.URL + "/share/e3334044-eeee-4864-85be-555fb158009f", nil)
req, _ := http.NewRequest("POST", ts.URL+"/share/e3334044-eeee-4864-85be-555fb158009f", nil)
res, _ := http.DefaultClient.Do(req)
assert.Equal(t, http.StatusNotFound, res.StatusCode)
})
......@@ -269,7 +269,7 @@ func TestUploadAttachment(t *testing.T) {
io.Copy(fw, strings.NewReader("POG POG POG POG"))
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()))
req.Header.Set("Content-Type", writer.FormDataContentType())
res, _ := http.DefaultClient.Do(req)
......@@ -281,7 +281,7 @@ 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)
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)
})
......
......@@ -20,7 +20,7 @@ var redisPool = &redis.Pool{
// Make an enqueuer with a particular namespace
var enqueuer = work.NewEnqueuer("ChiefSend", redisPool)
type Context struct{
type Context struct {
ShareID uuid.UUID
}
......
......@@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"log"
"os"
)
......@@ -19,20 +18,6 @@ var config = struct {
}
func main() {
SendMail(Share{
ID: uuid.MustParse("5713d228-a042-446d-a5e4-183b19fa832a"),
Name: "MailTest",
DownloadLimit: 100,
IsPublic: false,
IsTemporary: false,
Password: "test123",
Attachments: []Attachment{
{
Filename: "kekw.txt",
},
},
})
// test database connection
db, err := GetDatabase()
if err != nil {
......
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