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

fix database and tests

parent 908d0d70
......@@ -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: "TopKek",
Name: "TestOpenShare",
})
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) {
......
......@@ -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
}
......
......@@ -37,5 +37,5 @@ func PrettyPrint(i interface{}) {
if err != nil {
fmt.Println(err)
}
fmt.Print(string(b))
fmt.Println(string(b))
}
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