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
18c17f0f
Commit
18c17f0f
authored
4 years ago
by
Lukas Böhm
Browse files
Options
Downloads
Patches
Plain Diff
testUploadAttachment
parent
c144bf1b
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
api/api.go
+1
-1
1 addition, 1 deletion
api/api.go
api/api_test.go
+68
-13
68 additions, 13 deletions
api/api_test.go
with
69 additions
and
14 deletions
api/api.go
+
1
−
1
View file @
18c17f0f
...
...
@@ -238,7 +238,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
(
"Cant upload to finalized shares"
),
"Can't upload to finalized Shares."
,
403
}
return
&
HTTPError
{
errors
.
New
(
"Can
'
t upload to finalized shares"
),
"Can't upload to finalized Shares."
,
403
}
}
// Parse file from body
...
...
This diff is collapsed.
Click to expand it.
api/api_test.go
+
68
−
13
View file @
18c17f0f
package
main
import
(
"bytes"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
...
...
@@ -63,6 +68,8 @@ func Reset() {
for
_
,
sh
:=
range
shares
{
db
.
Create
(
&
sh
)
}
// testfile
ioutil
.
WriteFile
(
filepath
.
Join
(
config
.
mediaDir
,
"kekw.txt"
),
[]
byte
(
"Hallo welt"
),
os
.
ModePerm
)
}
/////////////////////////////////////
...
...
@@ -80,8 +87,8 @@ func TestAllShares(t *testing.T) {
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
var
erg
[]
Share
json
.
Unmarshal
(
body
,
&
erg
)
assert
.
Equal
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
assert
.
Equal
(
t
,
shares
[
1
],
erg
[
0
])
assert
.
Equal
Values
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
assert
.
Equal
Values
(
t
,
shares
[
1
],
erg
[
0
])
})
}
...
...
@@ -139,7 +146,7 @@ func TestDownloadFile(t *testing.T) {
defer
ts
.
Close
()
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
//assert
})
}
...
...
@@ -151,35 +158,58 @@ func TestOpenShare(t *testing.T) {
defer
ts
.
Close
()
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
b
,
_
:=
json
.
Marshal
(
Share
{
var
newShare
=
Share
{
ID
:
uuid
.
MustParse
(
"e5134044-2704-4864-85be-318fb158009f"
),
Name
:
"TestOpenShare"
,
})
Expires
:
nil
,
DownloadLimit
:
69
,
IsPublic
:
false
,
Attachments
:
[]
Attachment
{
{
ID
:
uuid
.
MustParse
(
"2b524827-9c3c-47e0-9277-8b51fd45b4bd"
),
Filename
:
"kekw.txt"
,
Filesize
:
123456
,
IsEncrypted
:
false
,
ShareID
:
uuid
.
MustParse
(
"e5134044-2704-4864-85be-318fb158009f"
),
},
},
}
b
,
_
:=
json
.
Marshal
(
newShare
)
req
,
_
:=
http
.
NewRequest
(
"POST"
,
ts
.
URL
+
"/shares"
,
strings
.
NewReader
(
string
(
b
)))
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
//body, _ := ioutil.ReadAll(res.Body)
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
var
actual
Share
json
.
Unmarshal
(
body
,
&
actual
)
// checks
assert
.
Equal
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
assert
.
DirExists
(
t
,
filepath
.
Join
(
config
.
mediaDir
,
"temp"
,
newShare
.
ID
.
String
()))
assert
.
NoDirExists
(
t
,
filepath
.
Join
(
config
.
mediaDir
,
"data"
,
newShare
.
ID
.
String
()))
assert
.
Equal
(
t
,
newShare
.
ID
,
actual
.
ID
)
//assert.(t, newShare, actual)
})
t
.
Run
(
"bad request"
,
func
(
t
*
testing
.
T
)
{
req
,
_
:=
http
.
NewRequest
(
"POST"
,
ts
.
URL
+
"/shares"
,
nil
)
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
body
,
_
:=
ioutil
.
ReadAll
(
res
.
B
ody
)
fmt
.
Println
(
string
(
body
))
//body, _ := ioutil.ReadAll(res.Body)
//fmt.Println(string(b
ody)
)
// checks
assert
.
Equal
(
t
,
http
.
StatusBadRequest
,
res
.
StatusCode
)
})
}
func
TestCloseShare
(
t
*
testing
.
T
)
{
Reset
()
router
:=
mux
.
NewRouter
()
ts
:=
httptest
.
NewServer
(
router
)
router
.
Handle
(
"/share/{id}"
,
endpointREST
(
CloseShare
))
.
Methods
(
"POST"
)
defer
ts
.
Close
()
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
req
,
_
:=
http
.
NewRequest
(
"POST"
,
ts
.
URL
+
"/share/
e5134044-2704-4864-85be-318fb158009f
"
,
nil
)
req
,
_
:=
http
.
NewRequest
(
"POST"
,
ts
.
URL
+
"/share/
a558aca3-fb40-400b-8dc6-ae49c705c791
"
,
nil
)
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
fmt
.
Println
(
string
(
body
))
assert
.
Equal
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
...
...
@@ -197,8 +227,28 @@ func TestCloseShare(t *testing.T) {
}
func
TestUploadAttachment
(
t
*
testing
.
T
)
{
Reset
()
router
:=
mux
.
NewRouter
()
ts
:=
httptest
.
NewServer
(
router
)
router
.
Handle
(
"/share/{id}/attachments"
,
endpointREST
(
UploadAttachment
))
.
Methods
(
"POST"
)
defer
ts
.
Close
()
t
.
Run
(
"happy path"
,
func
(
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
()
writer
.
Close
()
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
)
//resBody, _ :=ioutil.ReadAll(res.Body)
//fmt.Println(string(resBody))
assert
.
EqualValues
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
})
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
...
...
@@ -206,7 +256,12 @@ 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
)
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
assert
.
Equal
(
t
,
http
.
StatusBadRequest
,
res
.
StatusCode
)
})
t
.
Run
(
"forbidden"
,
func
(
t
*
testing
.
T
)
{
})
}
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