2019-07-17 15:16:33 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2019-12-11 13:10:20 +00:00
|
|
|
|
2020-05-27 17:38:40 +00:00
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
2019-12-11 13:10:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-17 15:16:33 +00:00
|
|
|
)
|
|
|
|
|
2020-05-04 14:02:49 +00:00
|
|
|
func TestUpdateLabel(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateLabel(router)
|
2020-05-23 18:58:58 +00:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/lt9k3pw1wowuy3c7", `{"Name": "Updated01", "Priority": 2}`)
|
|
|
|
val := gjson.Get(r.Body.String(), "Name")
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, "Updated01", val.String())
|
|
|
|
val2 := gjson.Get(r.Body.String(), "CustomSlug")
|
|
|
|
assert.Equal(t, "updated01", val2.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2019-07-17 15:16:33 +00:00
|
|
|
})
|
2020-05-04 14:02:49 +00:00
|
|
|
|
2019-07-17 15:16:33 +00:00
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateLabel(router)
|
2020-05-23 18:58:58 +00:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/lt9k3pw1wowuy3c7", `{"Name": 123, "Priority": 4, "Uncertainty": 80}`)
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
2019-07-17 15:16:33 +00:00
|
|
|
|
2020-05-04 14:02:49 +00:00
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdateLabel(router)
|
2020-05-26 07:02:19 +00:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/labels/xxx", `{"Name": "Updated01", "Priority": 4, "Uncertainty": 80}`)
|
2020-05-04 14:02:49 +00:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Label not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2019-07-17 15:16:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLikeLabel(t *testing.T) {
|
2020-02-02 18:39:49 +00:00
|
|
|
t.Run("like not existing label", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
LikeLabel(router)
|
2020-05-04 14:02:49 +00:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/labels/8775789/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-02 18:39:49 +00:00
|
|
|
})
|
|
|
|
t.Run("like existing label", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
2021-04-19 10:24:14 +00:00
|
|
|
|
|
|
|
// Register routes.
|
2021-09-18 13:32:39 +00:00
|
|
|
SearchLabels(router)
|
2021-04-19 10:24:14 +00:00
|
|
|
LikeLabel(router)
|
|
|
|
|
2021-07-22 16:31:56 +00:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/labels?count=3&q=like-label")
|
2020-05-14 12:18:28 +00:00
|
|
|
t.Log(r2.Body.String())
|
2021-07-22 16:31:56 +00:00
|
|
|
val := gjson.Get(r2.Body.String(), `#(Slug=="like-label").Favorite`)
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, "false", val.String())
|
2021-04-19 10:24:14 +00:00
|
|
|
|
2020-05-14 12:18:28 +00:00
|
|
|
r := PerformRequest(app, "POST", "/api/v1/labels/lt9k3pw1wowuy3c9/like")
|
|
|
|
t.Log(r.Body.String())
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-07-14 09:24:00 +00:00
|
|
|
|
2021-07-22 16:31:56 +00:00
|
|
|
r3 := PerformRequest(app, "GET", "/api/v1/labels?count=3&q=like-label")
|
2020-05-14 12:18:28 +00:00
|
|
|
t.Log(r3.Body.String())
|
2021-07-22 16:31:56 +00:00
|
|
|
val2 := gjson.Get(r3.Body.String(), `#(Slug=="like-label").Favorite`)
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, "true", val2.String())
|
2019-07-17 15:16:33 +00:00
|
|
|
})
|
2020-07-14 09:24:00 +00:00
|
|
|
t.Run("like existing label with prio < 0", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
LikeLabel(router)
|
|
|
|
r := PerformRequest(app, "POST", "/api/v1/labels/lt9k3pw1wowuy311/like")
|
|
|
|
t.Log(r.Body.String())
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2019-07-17 15:16:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDislikeLabel(t *testing.T) {
|
2020-02-02 18:39:49 +00:00
|
|
|
t.Run("dislike not existing label", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
2019-07-17 15:16:33 +00:00
|
|
|
|
2020-06-25 12:54:04 +00:00
|
|
|
DislikeLabel(router)
|
2019-07-17 15:16:33 +00:00
|
|
|
|
2020-05-04 14:02:49 +00:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/labels/5678/like")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2019-07-17 15:16:33 +00:00
|
|
|
})
|
2020-02-02 18:39:49 +00:00
|
|
|
t.Run("dislike existing label", func(t *testing.T) {
|
2020-06-25 12:54:04 +00:00
|
|
|
app, router, _ := NewApiTest()
|
2021-04-19 10:24:14 +00:00
|
|
|
|
|
|
|
// Register routes.
|
2021-09-18 13:32:39 +00:00
|
|
|
SearchLabels(router)
|
2021-04-19 10:24:14 +00:00
|
|
|
DislikeLabel(router)
|
|
|
|
|
2020-05-30 12:52:47 +00:00
|
|
|
r2 := PerformRequest(app, "GET", "/api/v1/labels?count=3&q=landscape")
|
|
|
|
t.Logf("HTTP BODY: %s", r2.Body.String())
|
2020-05-23 18:58:58 +00:00
|
|
|
val := gjson.Get(r2.Body.String(), `#(Slug=="landscape").Favorite`)
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, "true", val.String())
|
2020-02-02 18:39:49 +00:00
|
|
|
|
2020-05-04 14:02:49 +00:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/labels/lt9k3pw1wowuy3c2/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
|
2020-05-30 12:52:47 +00:00
|
|
|
r3 := PerformRequest(app, "GET", "/api/v1/labels?count=3&q=landscape")
|
2020-05-23 18:58:58 +00:00
|
|
|
val2 := gjson.Get(r3.Body.String(), `#(Slug=="landscape").Favorite`)
|
2020-05-04 14:02:49 +00:00
|
|
|
assert.Equal(t, "false", val2.String())
|
2020-02-02 18:39:49 +00:00
|
|
|
})
|
2020-07-14 09:24:00 +00:00
|
|
|
|
|
|
|
t.Run("dislike existing label with prio < 0", func(t *testing.T) {
|
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
DislikeLabel(router)
|
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/labels/lt9k3pw1wowuy312/like")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
})
|
2019-07-17 15:16:33 +00:00
|
|
|
}
|