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
fd6ede75
Commit
fd6ede75
authored
Mar 26, 2021
by
Lukas Böhm
🎱
Browse files
fix database and tests
parent
908d0d70
Changes
3
Hide whitespace changes
Inline
Side-by-side
api/api_test.go
View file @
fd6ede75
...
...
@@ -76,8 +76,7 @@ func TestAllShares(t *testing.T) {
defer
ts
.
Close
()
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
req
,
_
:=
http
.
NewRequest
(
"GET"
,
ts
.
URL
+
"/shares"
,
nil
)
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
res
,
_
:=
http
.
Get
(
ts
.
URL
+
"/shares"
)
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
var
erg
[]
Share
json
.
Unmarshal
(
body
,
&
erg
)
...
...
@@ -154,7 +153,7 @@ func TestOpenShare(t *testing.T) {
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
b
,
_
:=
json
.
Marshal
(
Share
{
ID
:
uuid
.
MustParse
(
"e5134044-2704-4864-85be-318fb158009f"
),
Name
:
"T
opKek
"
,
Name
:
"T
estOpenShare
"
,
})
req
,
_
:=
http
.
NewRequest
(
"POST"
,
ts
.
URL
+
"/shares"
,
strings
.
NewReader
(
string
(
b
)))
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
...
...
@@ -168,18 +167,28 @@ func TestOpenShare(t *testing.T) {
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
fmt
.
Println
(
string
(
body
))
assert
.
Equal
(
t
,
http
.
StatusBadRequest
,
res
.
StatusCode
)
})
}
func
TestCloseShare
(
t
*
testing
.
T
)
{
t
.
Run
(
"happy path"
,
func
(
t
*
testing
.
T
)
{
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
)
res
,
_
:=
http
.
DefaultClient
.
Do
(
req
)
body
,
_
:=
ioutil
.
ReadAll
(
res
.
Body
)
fmt
.
Println
(
string
(
body
))
assert
.
Equal
(
t
,
http
.
StatusOK
,
res
.
StatusCode
)
})
t
.
Run
(
"not found"
,
func
(
t
*
testing
.
T
)
{
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
)
})
t
.
Run
(
"bad request"
,
func
(
t
*
testing
.
T
)
{
...
...
api/database.go
View file @
fd6ede75
...
...
@@ -6,6 +6,7 @@ import (
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"os"
"path/filepath"
"time"
)
...
...
@@ -32,28 +33,26 @@ type Attachment struct {
ShareID
uuid
.
UUID
`json:"-" gorm:"not null"`
}
func
(
att
*
Attachment
)
BeforeCreate
(
scope
*
gorm
.
DB
)
error
{
if
att
.
ID
.
String
()
==
nuid
{
return
nil
}
uid
,
err
:=
uuid
.
NewRandom
()
if
err
!=
nil
{
return
err
func
(
sh
*
Share
)
BeforeCreate
(
scope
*
gorm
.
DB
)
error
{
if
sh
.
ID
.
String
()
==
"00000000-0000-0000-0000-000000000000"
{
uid
,
err
:=
uuid
.
NewRandom
()
if
err
!=
nil
{
return
err
}
sh
.
ID
=
uid
}
att
.
ID
=
uid
return
nil
// create temporary dir
return
os
.
MkdirAll
(
filepath
.
Join
(
config
.
mediaDir
,
"temp"
,
sh
.
ID
.
String
()),
os
.
ModePerm
)
}
func
(
sh
*
Share
)
BeforeCreate
(
scope
*
gorm
.
DB
)
error
{
if
sh
.
ID
.
String
()
==
nuid
{
return
nil
}
uid
,
err
:=
uuid
.
NewRandom
()
if
err
!=
nil
{
return
err
func
(
att
*
Attachment
)
BeforeCreate
(
scope
*
gorm
.
DB
)
error
{
if
att
.
ID
.
String
()
==
"00000000-0000-0000-0000-000000000000"
{
uid
,
err
:=
uuid
.
NewRandom
()
if
err
!=
nil
{
return
err
}
att
.
ID
=
uid
}
sh
.
ID
=
uid
return
nil
}
...
...
api/main.go
View file @
fd6ede75
...
...
@@ -37,5 +37,5 @@ func PrettyPrint(i interface{}) {
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
fmt
.
Print
(
string
(
b
))
fmt
.
Print
ln
(
string
(
b
))
}
Write
Preview
Supports
Markdown
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