Backend: Add tests to internal/api
This commit is contained in:
parent
1479a3ec5b
commit
ff505579ec
7 changed files with 226 additions and 10 deletions
|
@ -11,22 +11,21 @@ func TestGetThumbnail(t *testing.T) {
|
|||
t.Run("invalid type", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx")
|
||||
r := PerformRequest(app, "GET", "/api/v1/thumbnails/1/xxx")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("invalid hash", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500")
|
||||
r := PerformRequest(app, "GET", "/api/v1/thumbnails/1/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("could not find original", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetThumbnail(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/thumbnails/123xxx/tile_500")
|
||||
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/thumbnails/2cad9168fa6acc5c5c2965ddf6ec465ca42fd818/tile_500")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
)
|
||||
|
||||
func TestGetPreview(t *testing.T) {
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, ctx := NewApiTest()
|
||||
GetPreview(router, ctx)
|
||||
result := PerformRequest(app, "GET", "/api/v1/preview")
|
||||
assert.Equal(t, http.StatusOK, result.Code)
|
||||
r := PerformRequest(app, "GET", "/api/v1/preview")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
|
47
internal/api/session_test.go
Normal file
47
internal/api/session_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateSession(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateSession(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "photoprism"}`)
|
||||
val2 := gjson.Get(r.Body.String(), "user.Email")
|
||||
assert.Equal(t, "photoprism@localhost", val2.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("bad request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateSession(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": 123, "password": "xxx"}`)
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
t.Run("invalid password", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateSession(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "xxx"}`)
|
||||
val := gjson.Get(r.Body.String(), "error")
|
||||
assert.Equal(t, "Invalid password", val.String())
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeleteSession(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateSession(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/session", `{"email": "photoprism", "password": "photoprism"}`)
|
||||
token := gjson.Get(r.Body.String(), "token")
|
||||
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
DeleteSession(router, conf)
|
||||
r := PerformRequest(app, "DELETE", "/api/v1/session/"+token.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
46
internal/api/settings_test.go
Normal file
46
internal/api/settings_test.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetSettings(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
GetSettings(router, conf)
|
||||
r := PerformRequest(app, "GET", "/api/v1/settings")
|
||||
val := gjson.Get(r.Body.String(), "theme")
|
||||
assert.NotEmpty(t, val.String())
|
||||
val2 := gjson.Get(r.Body.String(), "language")
|
||||
assert.NotEmpty(t, val2.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSaveSettings(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
GetSettings(router, conf)
|
||||
r := PerformRequest(app, "GET", "/api/v1/settings")
|
||||
val := gjson.Get(r.Body.String(), "language")
|
||||
assert.Equal(t, "de", 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())
|
||||
assert.Equal(t, http.StatusOK, r2.Code)
|
||||
r3 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "de"}`)
|
||||
assert.Equal(t, http.StatusOK, r3.Code)
|
||||
})
|
||||
t.Run("bad request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
SaveSettings(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": 123}`)
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
}
|
19
internal/api/status_test.go
Normal file
19
internal/api/status_test.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetStatus(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
GetStatus(router, conf)
|
||||
r := PerformRequest(app, "GET", "/api/v1/status")
|
||||
val := gjson.Get(r.Body.String(), "status")
|
||||
assert.Equal(t, "operational", val.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
50
internal/api/svg_test.go
Normal file
50
internal/api/svg_test.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetSvg(t *testing.T) {
|
||||
t.Run("photo", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
t.Log(conf)
|
||||
GetSvg(router)
|
||||
r := PerformRequest(app, "GET", "/api/v1/svg/photo")
|
||||
assert.Equal(t, photoIconSvg, r.Body.Bytes())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("label", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
t.Log(conf)
|
||||
GetSvg(router)
|
||||
r := PerformRequest(app, "GET", "/api/v1/svg/label")
|
||||
assert.Equal(t, labelIconSvg, r.Body.Bytes())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("album", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
t.Log(conf)
|
||||
GetSvg(router)
|
||||
r := PerformRequest(app, "GET", "/api/v1/svg/album")
|
||||
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)
|
||||
GetSvg(router)
|
||||
r := PerformRequest(app, "GET", "/api/v1/svg/broken")
|
||||
assert.Equal(t, brokenIconSvg, r.Body.Bytes())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("uncached", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
t.Log(conf)
|
||||
GetSvg(router)
|
||||
r := PerformRequest(app, "GET", "/api/v1/svg/uncached")
|
||||
assert.Equal(t, uncachedIconSvg, r.Body.Bytes())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
}
|
55
internal/api/zip_test.go
Normal file
55
internal/api/zip_test.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateZip(t *testing.T) {
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateZip(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`)
|
||||
val := gjson.Get(r.Body.String(), "message")
|
||||
assert.Contains(t, val.String(), "zip created")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
t.Run("no photos selected", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateZip(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": []}`)
|
||||
val := gjson.Get(r.Body.String(), "error")
|
||||
assert.Equal(t, "No photos selected", val.String())
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
t.Run("invalid request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateZip(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": [123, "pt9jtdre2lvl0yxx"]}`)
|
||||
assert.Equal(t, http.StatusBadRequest, r.Code)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDownloadZip(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
CreateZip(router, conf)
|
||||
r := PerformRequestWithBody(app, "POST", "/api/v1/zip", `{"photos": ["pt9jtdre2lvl0y12", "pt9jtdre2lvl0y11"]}`)
|
||||
filename := gjson.Get(r.Body.String(), "filename")
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
|
||||
t.Run("successful request", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
DownloadZip(router, conf)
|
||||
r := PerformRequest(app, "GET", "/api/v1/zip/"+filename.String())
|
||||
assert.Equal(t, http.StatusOK, r.Code)
|
||||
})
|
||||
|
||||
t.Run("zip not existing", func(t *testing.T) {
|
||||
app, router, conf := NewApiTest()
|
||||
DownloadZip(router, conf)
|
||||
r := PerformRequest(app, "GET", "/api/v1/zip/xxx")
|
||||
assert.Equal(t, http.StatusNotFound, r.Code)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue