photoprism/internal/api/subjects_test.go

123 lines
3.8 KiB
Go
Raw Permalink Normal View History

2021-09-02 14:12:31 +00:00
package api
import (
"net/http"
"testing"
"github.com/tidwall/gjson"
"github.com/stretchr/testify/assert"
)
func TestGetSubject(t *testing.T) {
t.Run("Ok", func(t *testing.T) {
2021-09-02 14:12:31 +00:00
app, router, _ := NewApiTest()
GetSubject(router)
r := PerformRequest(app, "GET", "/api/v1/subjects/jqy1y111h1njaaaa")
val := gjson.Get(r.Body.String(), "Slug")
assert.Equal(t, "dangling-subject", val.String())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("InvalidRequest", func(t *testing.T) {
2021-09-02 14:12:31 +00:00
app, router, _ := NewApiTest()
GetSubject(router)
r := PerformRequest(app, "GET", "/api/v1/subjects/xxx1y111h1njaaaa")
val := gjson.Get(r.Body.String(), "error")
assert.Equal(t, "Subject not found", val.String())
assert.Equal(t, http.StatusNotFound, r.Code)
})
}
2021-09-17 15:23:42 +00:00
func TestLikeSubject(t *testing.T) {
t.Run("InvalidSubject", func(t *testing.T) {
2021-09-17 15:23:42 +00:00
app, router, _ := NewApiTest()
LikeSubject(router)
r := PerformRequest(app, "POST", "/api/v1/subjects/8775789/like")
assert.Equal(t, http.StatusNotFound, r.Code)
})
t.Run("ExistingSubject", func(t *testing.T) {
2021-09-17 15:23:42 +00:00
app, router, _ := NewApiTest()
// Register routes.
GetSubject(router)
LikeSubject(router)
r := PerformRequest(app, "GET", "/api/v1/subjects/jqy3y652h8njw0sx")
t.Log(r.Body.String())
val := gjson.Get(r.Body.String(), "Slug")
assert.Equal(t, "joe-biden", val.String())
val2 := gjson.Get(r.Body.String(), "Favorite")
assert.Equal(t, "false", val2.String())
r2 := PerformRequest(app, "POST", "/api/v1/subjects/jqy3y652h8njw0sx/like")
t.Log(r2.Body.String())
assert.Equal(t, http.StatusOK, r2.Code)
r3 := PerformRequest(app, "GET", "/api/v1/subjects/jqy3y652h8njw0sx")
t.Log(r3.Body.String())
val3 := gjson.Get(r3.Body.String(), "Slug")
assert.Equal(t, "joe-biden", val3.String())
val4 := gjson.Get(r3.Body.String(), "Favorite")
assert.Equal(t, "true", val4.String())
})
}
2021-09-17 15:23:42 +00:00
func TestDislikeSubject(t *testing.T) {
t.Run("InvalidSubject", func(t *testing.T) {
2021-09-17 15:23:42 +00:00
app, router, _ := NewApiTest()
DislikeSubject(router)
r := PerformRequest(app, "DELETE", "/api/v1/subjects/8775789/like")
assert.Equal(t, http.StatusNotFound, r.Code)
})
t.Run("ExistingSubject", func(t *testing.T) {
2021-09-17 15:23:42 +00:00
app, router, _ := NewApiTest()
// Register routes.
GetSubject(router)
DislikeSubject(router)
r := PerformRequest(app, "GET", "/api/v1/subjects/jqu0xs11qekk9jx8")
t.Log(r.Body.String())
val := gjson.Get(r.Body.String(), "Slug")
assert.Equal(t, "john-doe", val.String())
val2 := gjson.Get(r.Body.String(), "Favorite")
assert.Equal(t, "true", val2.String())
r2 := PerformRequest(app, "DELETE", "/api/v1/subjects/jqu0xs11qekk9jx8/like")
t.Log(r2.Body.String())
assert.Equal(t, http.StatusOK, r2.Code)
r3 := PerformRequest(app, "GET", "/api/v1/subjects/jqu0xs11qekk9jx8")
t.Log(r3.Body.String())
val3 := gjson.Get(r3.Body.String(), "Slug")
assert.Equal(t, "john-doe", val3.String())
val4 := gjson.Get(r3.Body.String(), "Favorite")
assert.Equal(t, "false", val4.String())
})
}
2021-09-20 07:31:30 +00:00
func TestUpdateSubject(t *testing.T) {
t.Run("successful request person", func(t *testing.T) {
app, router, _ := NewApiTest()
UpdateSubject(router)
r := PerformRequestWithBody(app, "PUT", "/api/v1/subjects/jqu0xs11qekk9jx8", `{"Name": "Updated Name"}`)
val := gjson.Get(r.Body.String(), "Name")
assert.Equal(t, "Updated Name", val.String())
assert.Equal(t, http.StatusOK, r.Code)
})
t.Run("invalid request", func(t *testing.T) {
app, router, _ := NewApiTest()
UpdateSubject(router)
r := PerformRequestWithBody(app, "PUT", "/api/v1/subjects/jqu0xs11qekk9jx8", `{"Name": 123}`)
assert.Equal(t, http.StatusBadRequest, r.Code)
})
t.Run("not found", func(t *testing.T) {
app, router, _ := NewApiTest()
UpdateSubject(router)
r := PerformRequestWithBody(app, "PUT", "/api/v1/subjectss/xxx", `{"Name": "Updated Name"}`)
assert.Equal(t, http.StatusNotFound, r.Code)
})
}