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("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
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(), "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) {
|
2020-07-14 17:02:39 +02:00
|
|
|
t.Run("successful request", 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(), "language")
|
2020-07-14 17:02:39 +02:00
|
|
|
assert.Equal(t, "en", val.String())
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
|
2020-07-14 17:02:39 +02:00
|
|
|
SaveSettings(router)
|
|
|
|
r2 := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": "de"}`)
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r2.Code)
|
2020-07-14 17:02:39 +02:00
|
|
|
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"}`)
|
2020-05-06 12:56:13 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r3.Code)
|
2020-07-14 17:02:39 +02:00
|
|
|
})
|
2020-05-06 12:56:13 +02:00
|
|
|
t.Run("bad request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
SaveSettings(router)
|
2020-05-06 12:56:13 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/settings", `{"language": 123}`)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
}
|