Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
ChiefSend2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Chiefs
ChiefSend2
Commits
ef3cb24c
Commit
ef3cb24c
authored
4 years ago
by
Lukas Böhm
Browse files
Options
Downloads
Patches
Plain Diff
better test
and one simple bugfix
parent
8faaf5f0
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/api.go
+2
-2
2 additions, 2 deletions
api/api.go
api/api_test.go
+25
-12
25 additions, 12 deletions
api/api_test.go
api/database.go
+6
-1
6 additions, 1 deletion
api/database.go
with
33 additions
and
15 deletions
api/api.go
+
2
−
2
View file @
ef3cb24c
...
...
@@ -97,7 +97,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
if
err
!=
nil
{
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
{
return
&
HTTPError
{
errors
.
New
(
"invalid URL"
),
"invalid URL param"
,
400
}
}
...
...
@@ -117,7 +117,7 @@ func DownloadFile(w http.ResponseWriter, r *http.Request) *HTTPError {
}
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
)
{
return
&
HTTPError
{
err
,
"Record not found"
,
404
}
}
...
...
This diff is collapsed.
Click to expand it.
api/api_test.go
+
25
−
12
View file @
ef3cb24c
...
...
@@ -72,8 +72,8 @@ func Reset() {
}
os
.
MkdirAll
(
filepath
.
Join
(
config
.
mediaDir
,
"data"
),
os
.
ModePerm
)
os
.
MkdirAll
(
filepath
.
Join
(
config
.
mediaDir
,
"temp"
),
os
.
ModePerm
)
// testfile
ioutil
.
WriteFile
(
filepath
.
Join
(
config
.
mediaDir
,
"
poggers.txt"
),
[]
byte
(
"Hallo welt
"
),
os
.
ModePerm
)
// testfile
s
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) {
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
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
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
...
...
@@ -124,19 +124,19 @@ func TestGetShare(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
)
assert
.
Equal
(
t
,
http
.
StatusUnauthorized
,
res
.
StatusCode
)
})
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
)
assert
.
Equal
(
t
,
http
.
StatusNotFound
,
res
.
StatusCode
)
})
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
)
assert
.
Equal
(
t
,
http
.
StatusForbidden
,
res
.
StatusCode
)
})
...
...
@@ -146,11 +146,26 @@ func TestDownloadFile(t *testing.T) {
Reset
()
router
:=
mux
.
NewRouter
()
ts
:=
httptest
.
NewServer
(
router
)
router
.
Handle
(
"/share/{id}"
,
endpointREST
(
GetShar
e
))
.
Methods
(
"GET"
)
router
.
Handle
(
"/share/{id}
/attachment/{att}
"
,
endpointREST
(
DownloadFil
e
))
.
Methods
(
"GET"
)
defer
ts
.
Close
()
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) {
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
()
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
()))
...
...
This diff is collapsed.
Click to expand it.
api/database.go
+
6
−
1
View file @
ef3cb24c
...
...
@@ -42,7 +42,12 @@ func (sh *Share) BeforeCreate(scope *gorm.DB) error {
sh
.
ID
=
uid
}
// create temporary dir
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment