Backend: Add unit tests for internal/api

This commit is contained in:
Theresa Gresch 2020-07-14 17:02:39 +02:00
parent 2db97ef168
commit c027e31304
4 changed files with 85 additions and 10 deletions

View File

@ -21,22 +21,23 @@ func TestGetSettings(t *testing.T) {
}
func TestSaveSettings(t *testing.T) {
/* t.Run("successful request", func(t *testing.T) {
app, router, conf := NewApiTest()
GetSettings(router, conf)
t.Run("successful request", func(t *testing.T) {
app, router, _ := NewApiTest()
GetSettings(router)
r := PerformRequest(app, "GET", "/api/v1/settings")
val := gjson.Get(r.Body.String(), "language")
assert.Equal(t, "de", val.String())
assert.Equal(t, "en", val.String())
assert.Equal(t, http.StatusOK, r.Code)
SaveSettings(router, conf)
r2 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "en"}`)
val2 := gjson.Get(r2.Body.String(), "language")
assert.Equal(t, "en", val2.String())
SaveSettings(router)
r2 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "de"}`)
assert.Equal(t, http.StatusOK, r2.Code)
r3 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "de"}`)
r4 := PerformRequest(app, "GET", "/api/v1/settings")
val2 := gjson.Get(r4.Body.String(), "language")
assert.Equal(t, "de", val2.String())
r3 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "en"}`)
assert.Equal(t, http.StatusOK, r3.Code)
}) */
})
t.Run("bad request", func(t *testing.T) {
app, router, _ := NewApiTest()
SaveSettings(router)

View File

@ -14,4 +14,10 @@ func TestGetPreview(t *testing.T) {
r := PerformRequest(app, "GET", "/a/1jxf3jfn2k/st9lxuqxpogaaba7/preview")
assert.Equal(t, http.StatusNotFound, r.Code)
})
t.Run("invalid token", func(t *testing.T) {
app, router, _ := NewApiTest()
SharePreview(router)
r := PerformRequest(app, "GET", "/a/xxx/st9lxuqxpogaaba7/preview")
assert.Equal(t, http.StatusNotFound, r.Code)
})
}

View File

@ -0,0 +1,36 @@
package api
import (
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
func TestGetShares(t *testing.T) {
t.Run("invalid token or share", func(t *testing.T) {
app, router, _ := NewApiTest()
Shares(router)
r := PerformRequest(app, "GET", "/api/v1/1jxf3jfn2k/st9lxuqxpogaaba7")
assert.Equal(t, http.StatusTemporaryRedirect, r.Code)
})
//TODO Why does it panic?
/*t.Run("valid token and share", func(t *testing.T) {
app, router, _ := NewApiTest()
Shares(router)
r := PerformRequest(app, "GET", "/api/v1/4jxf3jfn2k/at9lxuqxpogaaba7")
assert.Equal(t, http.StatusTemporaryRedirect, r.Code)
})*/
t.Run("invalid token", func(t *testing.T) {
app, router, _ := NewApiTest()
Shares(router)
r := PerformRequest(app, "GET", "/api/v1/xxx")
assert.Equal(t, http.StatusTemporaryRedirect, r.Code)
})
//TODO Why does it panic?
/*t.Run("valid token", func(t *testing.T) {
app, router, _ := NewApiTest()
Shares(router)
r := PerformRequest(app, "GET", "/api/v1/4jxf3jfn2k")
assert.Equal(t, http.StatusTemporaryRedirect, r.Code)
})*/
}

View File

@ -15,6 +15,30 @@ func TestGetSvg(t *testing.T) {
assert.Equal(t, photoIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("raw", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)
GetSvg(router)
r := PerformRequest(app, "GET", "/api/v1/svg/raw")
assert.Equal(t, rawIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("file", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)
GetSvg(router)
r := PerformRequest(app, "GET", "/api/v1/svg/file")
assert.Equal(t, fileIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("video", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)
GetSvg(router)
r := PerformRequest(app, "GET", "/api/v1/svg/video")
assert.Equal(t, videoIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("label", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)
@ -31,6 +55,14 @@ func TestGetSvg(t *testing.T) {
assert.Equal(t, albumIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("folder", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)
GetSvg(router)
r := PerformRequest(app, "GET", "/api/v1/svg/folder")
assert.Equal(t, albumIconSvg, r.Body.Bytes())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("broken", func(t *testing.T) {
app, router, conf := NewApiTest()
t.Log(conf)