Backend: Add tests to internal/api

This commit is contained in:
Theresa Gresch 2020-05-04 14:40:58 +02:00
parent a2310fb6c6
commit 73e0d294ff
2 changed files with 36 additions and 14 deletions

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"github.com/tidwall/gjson"
"net/http" "net/http"
"testing" "testing"
@ -8,20 +9,20 @@ import (
) )
func TestGetDownload(t *testing.T) { func TestGetDownload(t *testing.T) {
t.Run("could not find original", func(t *testing.T) { t.Run("download not existing file", func(t *testing.T) {
app, router, conf := NewApiTest() app, router, conf := NewApiTest()
GetDownload(router, conf) GetDownload(router, conf)
result := PerformRequest(app, "GET", "/api/v1/download/123xxx") r := PerformRequest(app, "GET", "/api/v1/download/123xxx")
assert.Equal(t, http.StatusNotFound, result.Code) val := gjson.Get(r.Body.String(), "error")
assert.Equal(t, "record not found", val.String())
assert.Equal(t, http.StatusNotFound, r.Code)
}) })
t.Run("download existing not existing file", func(t *testing.T) { t.Run("could not find original", func(t *testing.T) {
app, router, conf := NewApiTest() app, router, conf := NewApiTest()
GetDownload(router, conf)
DownloadAlbum(router, conf) r := PerformRequest(app, "GET", "/api/v1/download/3cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
assert.Equal(t, http.StatusNotFound, r.Code)
result := PerformRequest(app, "GET", "/api/v1/download/555")
assert.Equal(t, http.StatusNotFound, result.Code)
}) })
} }

View file

@ -12,16 +12,37 @@ func TestGetFile(t *testing.T) {
t.Run("search for existing file", func(t *testing.T) { t.Run("search for existing file", func(t *testing.T) {
app, router, ctx := NewApiTest() app, router, ctx := NewApiTest()
GetFile(router, ctx) GetFile(router, ctx)
result := PerformRequest(app, "GET", "/api/v1/files/2cad9168fa6acc5c5c2965ddf6ec465ca42fd818") r := PerformRequest(app, "GET", "/api/v1/files/2cad9168fa6acc5c5c2965ddf6ec465ca42fd818")
assert.Equal(t, http.StatusOK, result.Code) assert.Equal(t, http.StatusOK, r.Code)
val := gjson.Get(result.Body.String(), "FileName") val := gjson.Get(r.Body.String(), "FileName")
assert.Equal(t, "exampleFileName.jpg", val.String()) assert.Equal(t, "exampleFileName.jpg", val.String())
}) })
t.Run("search for not existing file", func(t *testing.T) { t.Run("search for not existing file", func(t *testing.T) {
app, router, ctx := NewApiTest() app, router, ctx := NewApiTest()
GetFile(router, ctx) GetFile(router, ctx)
result := PerformRequest(app, "GET", "/api/v1/files/111") r := PerformRequest(app, "GET", "/api/v1/files/111")
assert.Equal(t, http.StatusNotFound, result.Code) assert.Equal(t, http.StatusNotFound, r.Code)
}) })
} }
func TestLinkFile(t *testing.T) {
t.Run("album not found", func(t *testing.T) {
app, router, ctx := NewApiTest()
LinkFile(router, ctx)
r := PerformRequest(app, "POST", "/api/v1/files/3cad9168fa6acc5c5c2965ddf6ec465ca42fd818/link")
t.Log(r.Body.String())
assert.Equal(t, http.StatusNotFound, r.Code)
val := gjson.Get(r.Body.String(), "error")
assert.Equal(t, "Album not found", val.String())
})
t.Run("invalid request", func(t *testing.T) {
app, router, ctx := NewApiTest()
LinkFile(router, ctx)
r := PerformRequest(app, "POST", "/api/v1/files/ft9es39w45bnlqdw/link")
assert.Equal(t, http.StatusBadRequest, r.Code)
val := gjson.Get(r.Body.String(), "error")
assert.Equal(t, "Invalid request", val.String())
})
}