Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Chiefs
ChiefSend2
Commits
7a810963
Commit
7a810963
authored
Mar 28, 2021
by
Lukas Böhm
🎱
Browse files
fix tests
parent
f4978a93
Changes
3
Hide whitespace changes
Inline
Side-by-side
api/api.go
View file @
7a810963
...
...
@@ -30,7 +30,7 @@ func (fn endpointREST) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if
e
.
Code
==
401
{
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 {
return
&
HTTPError
{
err
,
"Can't fetch data"
,
500
}
}
if
share
.
IsTemporary
==
true
{
return
&
HTTPError
{
err
,
"Share is not finalized"
,
403
}
return
&
HTTPError
{
err
ors
.
New
(
"share is not finalized"
)
,
"Share is not finalized"
,
403
}
}
// auth
if
share
.
Password
!=
""
{
sid
,
pass
,
_
:=
r
.
BasicAuth
()
if
sid
!=
share
.
ID
.
String
()
{
return
&
HTTPError
{
err
,
"wrong username"
,
401
}
return
&
HTTPError
{
err
ors
.
New
(
"Unauthorized"
)
,
"wrong username"
,
401
}
}
if
pass
!=
share
.
Password
{
return
&
HTTPError
{
err
,
"wrong password"
,
401
}
return
&
HTTPError
{
err
ors
.
New
(
"Unauthorized"
)
,
"wrong password"
,
401
}
}
}
...
...
@@ -132,10 +132,10 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
if
share
.
Password
!=
""
{
sid
,
pass
,
_
:=
r
.
BasicAuth
()
if
sid
!=
share
.
ID
.
String
()
{
return
&
HTTPError
{
err
,
"wrong username"
,
401
}
return
&
HTTPError
{
err
ors
.
New
(
"Unauthorized"
)
,
"wrong username"
,
401
}
}
if
pass
!=
share
.
Password
{
return
&
HTTPError
{
err
,
"wrong password"
,
401
}
return
&
HTTPError
{
err
ors
.
New
(
"Unauthorized"
)
,
"wrong password"
,
401
}
}
}
...
...
@@ -257,7 +257,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
return
&
HTTPError
{
err
,
"Can't fetch data"
,
500
}
}
if
share
.
IsTemporary
!=
true
{
return
&
HTTPError
{
err
,
"Can't upload to finalized Shares."
,
403
}
return
&
HTTPError
{
err
ors
.
New
(
"share is not finalized"
)
,
"Can't upload to finalized Shares."
,
403
}
}
// Parse file from body
...
...
@@ -379,7 +379,7 @@ func RetrieveShare(shareID uuid.UUID, withAtt bool) (*Share, *HTTPError) {
return
nil
,
&
HTTPError
{
err
,
"Can't fetch data"
,
500
}
}
if
share
.
IsTemporary
==
true
{
return
nil
,
&
HTTPError
{
err
,
"Share is not finalized"
,
403
}
return
nil
,
&
HTTPError
{
err
ors
.
New
(
"share is not finalized"
)
,
"Share is not finalized"
,
403
}
}
return
&
share
,
nil
}
...
...
api/api_test.go
View file @
7a810963
...
...
@@ -28,6 +28,7 @@ var shares = []Share {
IsPublic
:
false
,
IsTemporary
:
false
,
Password
:
"test123"
,
Emails
:
[]
string
{
""
},
Attachments
:
[]
Attachment
{
{
...
...
@@ -44,6 +45,7 @@ var shares = []Share {
Name
:
"TestFinalPublic"
,
IsPublic
:
true
,
IsTemporary
:
false
,
Emails
:
[]
string
{
""
},
},
{
ID
:
uuid
.
MustParse
(
"a558aca3-fb40-400b-8dc6-ae49c705c791"
),
...
...
@@ -51,6 +53,7 @@ var shares = []Share {
DownloadLimit
:
300
,
IsPublic
:
true
,
IsTemporary
:
true
,
Emails
:
[]
string
{
""
},
},
}
...
...
@@ -62,8 +65,8 @@ func Reset() {
db
.
AutoMigrate
(
&
Attachment
{})
db
.
AutoMigrate
(
&
Share
{})
// delete everything
db
.
Where
(
"1 = 1"
)
.
Delete
(
Share
{})
db
.
Where
(
"1 = 1"
)
.
Delete
(
Attachment
{})
db
.
Where
(
"1 = 1"
)
.
Delete
(
&
Share
{})
db
.
Where
(
"1 = 1"
)
.
Delete
(
&
Attachment
{})
os
.
RemoveAll
(
filepath
.
Join
(
config
.
mediaDir
,
"data"
))
os
.
RemoveAll
(
filepath
.
Join
(
config
.
mediaDir
,
"temp"
))
// create everything
...
...
@@ -270,8 +273,6 @@ func TestUploadAttachment(t *testing.T) {
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
)
})
...
...
api/database.go
View file @
7a810963
...
...
@@ -14,7 +14,7 @@ import (
// Share has many Attachments, ShareID is the foreign key
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"`
Expires
*
time
.
Time
`json:"expires,omitempty"`
DownloadLimit
uint
`json:"download_limit,omitempty"`
...
...
@@ -28,7 +28,7 @@ type Share 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"`
Filesize
int64
`json:"filesize" gorm:"not null; default:0"`
IsEncrypted
bool
`json:"-" gorm:"not null; default:false"`
...
...
@@ -54,13 +54,17 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error {
}
// create temporary dir
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
{
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
sh
.
EMailsDB
=
strings
.
Join
(
sh
.
Emails
,
";"
)
sh
.
EMailsDB
=
strings
.
Join
(
sh
.
Emails
,
";"
)
return
nil
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment