test for masked comments

This commit is contained in:
Umputun
2018-02-04 16:51:27 -06:00
parent 8925da510a
commit 7fca267e8b
3 changed files with 24 additions and 6 deletions
+1 -1
View File
@@ -50,7 +50,7 @@ func (a *admin) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) {
}
a.cache.Flush()
render.Status(r, http.StatusOK)
render.JSON(w, r, JSON{"id": id, "loc": locator})
render.JSON(w, r, JSON{"id": id, "locator": locator})
}
// PUT /user/{userid}?site=side-id&block=1 - block or unblock user
+20 -2
View File
@@ -88,10 +88,20 @@ func TestAdmin_Block(t *testing.T) {
assert.NotNil(t, srv)
defer cleanup(srv)
c1 := store.Comment{Text: "test test #1",
Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}, User: store.User{Name: "user1 name", ID: "user1"}}
c2 := store.Comment{Text: "test test #2", ParentID: "p1",
Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}, User: store.User{Name: "user2", ID: "user2"}}
_, err := srv.DataService.Create(c1)
assert.Nil(t, err)
_, err = srv.DataService.Create(c2)
assert.Nil(t, err)
block := func(val int) (code int, body []byte) {
client := http.Client{}
req, err := http.NewRequest(http.MethodPut,
fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/user/%s?site=radio-t&url=https://radio-t.com/blah&block=%d",
fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/user/%s?site=radio-t&block=%d",
port, "user1", val), nil)
assert.Nil(t, err)
resp, err := client.Do(req)
@@ -105,12 +115,20 @@ func TestAdmin_Block(t *testing.T) {
code, body := block(1)
require.Equal(t, 200, code)
j := JSON{}
err := json.Unmarshal(body, &j)
err = json.Unmarshal(body, &j)
assert.Nil(t, err)
assert.Equal(t, "user1", j["user_id"])
assert.Equal(t, true, j["block"])
assert.Equal(t, "radio-t", j["site_id"])
res, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/find?site=radio-t&url=https://radio-t.com/blah&sort=+time", port))
assert.Equal(t, 200, code)
comments := []store.Comment{}
err = json.Unmarshal([]byte(res), &comments)
assert.Nil(t, err)
assert.Equal(t, 2, len(comments), "should have 2 comments")
assert.Equal(t, "this comment was deleted", comments[0].Text)
code, body = block(-1)
require.Equal(t, 200, code)
err = json.Unmarshal(body, &j)
+3 -3
View File
@@ -222,13 +222,13 @@ func (s *Server) findCommentsCtrl(w http.ResponseWriter, r *http.Request) {
if e != nil {
return nil, e
}
comments = s.mod.maskBlockedUsers(comments)
maskedComments := s.mod.maskBlockedUsers(comments)
var b []byte
switch r.URL.Query().Get("format") {
case "tree":
b, e = encodeJSONWithHTML(format.MakeTree(comments, r.URL.Query().Get("sort")))
b, e = encodeJSONWithHTML(format.MakeTree(maskedComments, r.URL.Query().Get("sort")))
default:
b, e = encodeJSONWithHTML(comments)
b, e = encodeJSONWithHTML(maskedComments)
}
return b, e
})