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

fix tests

parent f4978a93
...@@ -30,7 +30,7 @@ func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) { ...@@ -30,7 +30,7 @@ func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if e.Code == 401 { if e.Code == 401 {
w.Header().Set("WWW-Authenticate", `Basic realm="Please enter the password"`) w.Header().Set("WWW-Authenticate", `Basic realm="Please enter the password"`)
} }
http.Error(w, e.Message + " - " + e.Error.Error(), e.Code) http.Error(w, fmt.Sprintf("%s - %s", (*e).Message, (*e).Error.Error()), (*e).Code)
} }
} }
...@@ -74,17 +74,17 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -74,17 +74,17 @@ func GetShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't fetch data", 500} return &HTTPError{err, "Can't fetch data", 500}
} }
if share.IsTemporary == true { if share.IsTemporary == true {
return &HTTPError{err, "Share is not finalized", 403} return &HTTPError{errors.New("share is not finalized"), "Share is not finalized", 403}
} }
// auth // auth
if share.Password != "" { if share.Password != "" {
sid, pass, _ := r.BasicAuth() sid, pass, _ := r.BasicAuth()
if sid != share.ID.String() { if sid != share.ID.String() {
return &HTTPError{err, "wrong username", 401} return &HTTPError{errors.New("Unauthorized"), "wrong username", 401}
} }
if pass != share.Password { if pass != share.Password {
return &HTTPError{err, "wrong password", 401} return &HTTPError{errors.New("Unauthorized"), "wrong password", 401}
} }
} }
...@@ -132,10 +132,10 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -132,10 +132,10 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
if share.Password != "" { if share.Password != "" {
sid, pass, _ := r.BasicAuth() sid, pass, _ := r.BasicAuth()
if sid != share.ID.String() { if sid != share.ID.String() {
return &HTTPError{err, "wrong username", 401} return &HTTPError{errors.New("Unauthorized"), "wrong username", 401}
} }
if pass != share.Password { if pass != share.Password {
return &HTTPError{err, "wrong password", 401} return &HTTPError{errors.New("Unauthorized"), "wrong password", 401}
} }
} }
...@@ -257,7 +257,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError { ...@@ -257,7 +257,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't fetch data", 500} return &HTTPError{err, "Can't fetch data", 500}
} }
if share.IsTemporary != true { if share.IsTemporary != true {
return &HTTPError{ err, "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 // Parse file from body
...@@ -379,7 +379,7 @@ func RetrieveShare(shareID uuid.UUID, withAtt bool) (*Share, *HTTPError) { ...@@ -379,7 +379,7 @@ func RetrieveShare(shareID uuid.UUID, withAtt bool) (*Share, *HTTPError) {
return nil, &HTTPError{err, "Can't fetch data", 500} return nil, &HTTPError{err, "Can't fetch data", 500}
} }
if share.IsTemporary == true { if share.IsTemporary == true {
return nil, &HTTPError{err, "Share is not finalized", 403} return nil, &HTTPError{errors.New("share is not finalized"), "Share is not finalized", 403}
} }
return &share, nil return &share, nil
} }
......
...@@ -28,6 +28,7 @@ var shares = []Share { ...@@ -28,6 +28,7 @@ var shares = []Share {
IsPublic: false, IsPublic: false,
IsTemporary: false, IsTemporary: false,
Password: "test123", Password: "test123",
Emails:[]string{""},
Attachments: []Attachment { Attachments: []Attachment {
{ {
...@@ -44,6 +45,7 @@ var shares = []Share { ...@@ -44,6 +45,7 @@ var shares = []Share {
Name: "TestFinalPublic", Name: "TestFinalPublic",
IsPublic: true, IsPublic: true,
IsTemporary: false, IsTemporary: false,
Emails:[]string{""},
}, },
{ {
ID: uuid.MustParse("a558aca3-fb40-400b-8dc6-ae49c705c791"), ID: uuid.MustParse("a558aca3-fb40-400b-8dc6-ae49c705c791"),
...@@ -51,6 +53,7 @@ var shares = []Share { ...@@ -51,6 +53,7 @@ var shares = []Share {
DownloadLimit: 300, DownloadLimit: 300,
IsPublic: true, IsPublic: true,
IsTemporary: true, IsTemporary: true,
Emails:[]string{""},
}, },
} }
...@@ -62,8 +65,8 @@ func Reset() { ...@@ -62,8 +65,8 @@ func Reset() {
db.AutoMigrate(&Attachment{}) db.AutoMigrate(&Attachment{})
db.AutoMigrate(&Share{}) db.AutoMigrate(&Share{})
// delete everything // delete everything
db.Where("1 = 1").Delete(Share{}) db.Where("1 = 1").Delete(&Share{})
db.Where("1 = 1").Delete(Attachment{}) db.Where("1 = 1").Delete(&Attachment{})
os.RemoveAll(filepath.Join(config.mediaDir, "data")) os.RemoveAll(filepath.Join(config.mediaDir, "data"))
os.RemoveAll(filepath.Join(config.mediaDir, "temp")) os.RemoveAll(filepath.Join(config.mediaDir, "temp"))
// create everything // create everything
...@@ -270,8 +273,6 @@ func TestUploadAttachment(t *testing.T) { ...@@ -270,8 +273,6 @@ func TestUploadAttachment(t *testing.T) {
req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Content-Type", writer.FormDataContentType())
res, _ := http.DefaultClient.Do(req) res, _ := http.DefaultClient.Do(req)
//resBody, _ :=ioutil.ReadAll(res.Body)
//fmt.Println(string(resBody))
assert.EqualValues(t, http.StatusOK, res.StatusCode) assert.EqualValues(t, http.StatusOK, res.StatusCode)
}) })
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
// Share has many Attachments, ShareID is the foreign key // Share has many Attachments, ShareID is the foreign key
type Share struct { type Share struct {
ID uuid.UUID `json:"id" gorm:"type:uuid; primary_key"` ID uuid.UUID `json:"id" gorm:"primary_key"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Expires *time.Time `json:"expires,omitempty"` Expires *time.Time `json:"expires,omitempty"`
DownloadLimit uint `json:"download_limit,omitempty"` DownloadLimit uint `json:"download_limit,omitempty"`
...@@ -28,7 +28,7 @@ type Share struct { ...@@ -28,7 +28,7 @@ type Share struct {
} }
type Attachment struct { type Attachment struct {
ID uuid.UUID `json:"id" gorm:"type:uuid; primary_key"` ID uuid.UUID `json:"id" gorm:"primary_key"`
Filename string `json:"filename" gorm:"not null"` Filename string `json:"filename" gorm:"not null"`
Filesize int64 `json:"filesize" gorm:"not null; default:0"` Filesize int64 `json:"filesize" gorm:"not null; default:0"`
IsEncrypted bool `json:"-" gorm:"not null; default:false"` IsEncrypted bool `json:"-" gorm:"not null; default:false"`
...@@ -54,13 +54,17 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error { ...@@ -54,13 +54,17 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error {
} }
// create temporary dir // create temporary dir
if sh.IsTemporary == true { if sh.IsTemporary == true {
if err := os.MkdirAll(filepath.Join(config.mediaDir, "temp", sh.ID.String()), os.ModePerm); err != nil { return nil } if err := os.MkdirAll(filepath.Join(config.mediaDir, "temp", sh.ID.String()), os.ModePerm); err != nil {
return nil
}
} else { } else {
if err:= os.MkdirAll(filepath.Join(config.mediaDir, "data", sh.ID.String()), os.ModePerm); err != nil { return nil } if err := os.MkdirAll(filepath.Join(config.mediaDir, "data", sh.ID.String()), os.ModePerm); err != nil {
return nil
}
} }
//convert email adresses //convert email adresses
sh.EMailsDB = strings.Join(sh.Emails,";") sh.EMailsDB = strings.Join(sh.Emails, ";")
return nil return 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