photoprism/internal/api/config_settings_test.go

48 lines
1.5 KiB
Go
Raw Normal View History

2020-05-06 12:56:13 +02:00
package api
import (
"net/http"
"testing"
2020-11-21 18:08:41 +01:00
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
2020-05-06 12:56:13 +02:00
)
func TestGetSettings(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
app, router, _ := NewApiTest()
GetSettings(router)
2020-05-06 12:56:13 +02:00
r := PerformRequest(app, "GET", "/api/v1/settings")
val := gjson.Get(r.Body.String(), "ui.theme")
2020-05-06 12:56:13 +02:00
assert.NotEmpty(t, val.String())
val2 := gjson.Get(r.Body.String(), "ui.language")
2020-05-06 12:56:13 +02:00
assert.NotEmpty(t, val2.String())
assert.Equal(t, http.StatusOK, r.Code)
})
}
func TestSaveSettings(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
app, router, _ := NewApiTest()
GetSettings(router)
2020-05-06 12:56:13 +02:00
r := PerformRequest(app, "GET", "/api/v1/settings")
val := gjson.Get(r.Body.String(), "ui.language")
assert.Equal(t, "en", val.String())
2020-05-06 12:56:13 +02:00
assert.Equal(t, http.StatusOK, r.Code)
SaveSettings(router)
r2 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"ui":{"language": "de"}}`)
2020-05-06 12:56:13 +02:00
assert.Equal(t, http.StatusOK, r2.Code)
r4 := PerformRequest(app, "GET", "/api/v1/settings")
val2 := gjson.Get(r4.Body.String(), "ui.language")
assert.Equal(t, "de", val2.String())
r3 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"ui":{"language": "en"}}`)
2020-05-06 12:56:13 +02:00
assert.Equal(t, http.StatusOK, r3.Code)
})
t.Run("BadRequest", func(t *testing.T) {
app, router, _ := NewApiTest()
SaveSettings(router)
r := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"ui":{"language":123}}`)
2020-05-06 12:56:13 +02:00
assert.Equal(t, http.StatusBadRequest, r.Code)
})
}