2020-02-03 15:49:32 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-17 21:20:38 +02:00
|
|
|
"github.com/tidwall/gjson"
|
2020-02-03 15:49:32 +01:00
|
|
|
)
|
|
|
|
|
2020-05-05 09:28:52 +02:00
|
|
|
func TestAddPhotoLabel(t *testing.T) {
|
|
|
|
t.Run("add new label", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh8/label", `{"Name": "testAddLabel", "Uncertainty": 95, "Priority": 2}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
assert.Contains(t, r.Body.String(), "TestAddLabel")
|
|
|
|
})
|
|
|
|
t.Run("add existing label", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh8/label", `{"Name": "Flower", "Uncertainty": 10, "Priority": 2}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
val := gjson.Get(r.Body.String(), "Labels.#(LabelID==1000001).Uncertainty")
|
|
|
|
assert.Equal(t, "10", val.String())
|
|
|
|
})
|
|
|
|
t.Run("not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/xxx/label", `{"Name": "Flower", "Uncertainty": 10, "Priority": 2}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Photo not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("invalid request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
AddPhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "POST", "/api/v1/photos/pt9jtdre2lvl0yh8/label", `{"Name": 123, "Uncertainty": 10, "Priority": 2}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:49:32 +01:00
|
|
|
func TestRemovePhotoLabel(t *testing.T) {
|
|
|
|
t.Run("photo with label", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/pt9jtdre2lvl0yh7/label/1000001")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
|
|
|
val := gjson.Get(r.Body.String(), "Labels.#(LabelID==1000001).Uncertainty")
|
2020-04-17 21:20:38 +02:00
|
|
|
assert.Equal(t, "100", val.String())
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Contains(t, r.Body.String(), "cake")
|
|
|
|
|
|
|
|
})
|
|
|
|
t.Run("remove manually added label", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/pt9jtdre2lvl0yh7/label/1000002")
|
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-08 14:31:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Labels")
|
|
|
|
assert.NotContains(t, val.String(), "cake")
|
2020-05-05 09:28:52 +02:00
|
|
|
})
|
|
|
|
t.Run("photo not found", func(t *testing.T) {
|
2020-06-26 14:26:36 +02:00
|
|
|
app, router, _ := NewApiTest()
|
2020-06-25 14:54:04 +02:00
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/xxx/label/10000001")
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Photo not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("label not existing", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/pt9jtdre2lvl0yh7/label/xxx")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:49:32 +01:00
|
|
|
})
|
|
|
|
t.Run("try to remove wrong label", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/pt9jtdre2lvl0yh7/label/1000000")
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Record not found", val.String())
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
2020-02-03 15:49:32 +01:00
|
|
|
})
|
|
|
|
t.Run("not existing photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
RemovePhotoLabel(router)
|
2020-05-05 09:28:52 +02:00
|
|
|
r := PerformRequest(app, "DELETE", "/api/v1/photos/xx/label/")
|
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePhotoLabel(t *testing.T) {
|
|
|
|
t.Run("successful request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0yh0/label/1000006", `{"Label": {"Name": "NewLabelName"}}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Code)
|
2020-05-23 20:58:58 +02:00
|
|
|
val := gjson.Get(r.Body.String(), "Title")
|
2020-05-11 17:11:04 +02:00
|
|
|
assert.Contains(t, val.String(), "NewLabelName")
|
2020-05-05 09:28:52 +02:00
|
|
|
})
|
|
|
|
t.Run("photo not found", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/xxx/label/1000006", `{"Label": {"Name": "NewLabelName"}}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
val := gjson.Get(r.Body.String(), "error")
|
|
|
|
assert.Equal(t, "Photo not found", val.String())
|
|
|
|
})
|
|
|
|
t.Run("label not existing", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0yh0/label/9000006", `{"Label": {"Name": "NewLabelName"}}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("label not linked to photo", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0yh0/label/1000005", `{"Label": {"Name": "NewLabelName"}}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, r.Code)
|
|
|
|
})
|
|
|
|
t.Run("bad request", func(t *testing.T) {
|
2020-06-25 14:54:04 +02:00
|
|
|
app, router, _ := NewApiTest()
|
|
|
|
UpdatePhotoLabel(router)
|
2020-05-23 20:58:58 +02:00
|
|
|
r := PerformRequestWithBody(app, "PUT", "/api/v1/photos/pt9jtdre2lvl0yh0/label/1000006", `{"Label": {"Name": 123}}`)
|
2020-05-05 09:28:52 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, r.Code)
|
2020-02-03 15:49:32 +01:00
|
|
|
})
|
|
|
|
}
|