From 53e239db019052f3f3498d2364b7ff1fb701c87d Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 11 Jan 2018 15:18:49 -0600 Subject: [PATCH] admin tests --- app/migrator/remark.go | 1 - app/rest/admin_test.go | 141 ++++++++++++++++++++++++++++++++++++++++ app/rest/server.go | 20 ------ app/rest/server_test.go | 71 +------------------- 4 files changed, 143 insertions(+), 90 deletions(-) create mode 100644 app/rest/admin_test.go diff --git a/app/migrator/remark.go b/app/migrator/remark.go index 9718ca02..64e80dae 100644 --- a/app/migrator/remark.go +++ b/app/migrator/remark.go @@ -42,7 +42,6 @@ func (r *Remark) Export(w io.Writer, siteID string) error { return errors.Wrapf(err, "can't marshal %v", comments) } data := buf.Bytes() - data = append(data, '\n') if _, err := w.Write(data); err != nil { return errors.Wrap(err, "can't write comment data") } diff --git a/app/rest/admin_test.go b/app/rest/admin_test.go new file mode 100644 index 00000000..002180a6 --- /dev/null +++ b/app/rest/admin_test.go @@ -0,0 +1,141 @@ +package rest + +import ( + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + + "io/ioutil" + + "strings" + + "github.com/stretchr/testify/require" + "github.com/umputun/remark/app/store" +) + +func TestAdmin_Delete(t *testing.T) { + srv, port := prep(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"}} + c2 := store.Comment{Text: "test test #2", ParentID: "p1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}} + + id1 := addComment(t, c1, port) + addComment(t, c2, port) + + client := http.Client{} + req, err := http.NewRequest(http.MethodDelete, + fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/comment/%s?site=radio-t&url=https://radio-t.com/blah", port, id1), nil) + assert.Nil(t, err) + resp, err := client.Do(req) + assert.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + + _, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) + assert.Equal(t, 400, code) +} + +func TestAdmin_Pin(t *testing.T) { + srv, port := prep(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"}} + c2 := store.Comment{Text: "test test #2", ParentID: "p1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}} + + id1 := addComment(t, c1, port) + addComment(t, c2, port) + + pin := func(val int) int { + client := http.Client{} + req, err := http.NewRequest(http.MethodPut, + fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/pin/%s?site=radio-t&url=https://radio-t.com/blah&pin=%d", port, id1, val), + nil) + assert.Nil(t, err) + resp, err := client.Do(req) + assert.Nil(t, err) + return resp.StatusCode + } + + code := pin(1) + assert.Equal(t, 200, code) + + body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) + assert.Equal(t, 200, code) + cr := store.Comment{} + err := json.Unmarshal([]byte(body), &cr) + assert.Nil(t, err) + assert.True(t, cr.Pin) + + code = pin(-1) + assert.Equal(t, 200, code) + body, code = get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) + assert.Equal(t, 200, code) + cr = store.Comment{} + err = json.Unmarshal([]byte(body), &cr) + assert.Nil(t, err) + assert.False(t, cr.Pin) +} + +func TestAdmin_Block(t *testing.T) { + srv, port := prep(t) + assert.NotNil(t, srv) + defer cleanup(srv) + + 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", + port, "user1", val), nil) + assert.Nil(t, err) + resp, err := client.Do(req) + require.Nil(t, err) + body, err = ioutil.ReadAll(resp.Body) + assert.Nil(t, err) + resp.Body.Close() + return resp.StatusCode, body + } + + code, body := block(1) + require.Equal(t, 200, code) + j := JSON{} + 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"]) + + code, body = block(-1) + require.Equal(t, 200, code) + err = json.Unmarshal(body, &j) + assert.Nil(t, err) + assert.Equal(t, false, j["block"]) +} + +func TestAdmin_Export(t *testing.T) { + srv, port := prep(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/blah1"}} + c2 := store.Comment{Text: "test test #2", ParentID: "p1", + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah2"}} + + addComment(t, c1, port) + addComment(t, c2, port) + + body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/export?site=radio-t&mode=stream", port)) + assert.Equal(t, 200, code) + assert.Equal(t, 2, strings.Count(body, "\n")) + assert.Equal(t, 2, strings.Count(body, "\"text\"")) + t.Logf("%s", body) +} diff --git a/app/rest/server.go b/app/rest/server.go index ee3647d7..e5d46738 100644 --- a/app/rest/server.go +++ b/app/rest/server.go @@ -91,7 +91,6 @@ func (s *Server) Run(port int) { rapi.With(auth.Auth(s.SessionStore, s.Admins, maybeDevMode(auth.Full))).Group(func(rauth chi.Router) { rauth.Post("/comment", s.createCommentCtrl) rauth.Put("/comment/{id}", s.updateCommentCtrl) - rauth.Delete("/comment/{id}", s.deleteCommentCtrl) rauth.Get("/user", s.userInfoCtrl) rauth.Put("/vote/{id}", s.voteCtrl) @@ -203,25 +202,6 @@ func (s *Server) updateCommentCtrl(w http.ResponseWriter, r *http.Request) { render.JSON(w, r, res) } -// DELETE /comment/{id}?site=siteID&url=post-url -func (s *Server) deleteCommentCtrl(w http.ResponseWriter, r *http.Request) { - - id := chi.URLParam(r, "id") - log.Printf("[DEBUG] delete comment %s", id) - - locator := store.Locator{SiteID: r.URL.Query().Get("site"), URL: r.URL.Query().Get("url")} - err := s.DataService.Delete(locator, id) - if err != nil { - common.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't delete comment") - return - } - - s.respCache.flush() - - render.Status(r, http.StatusOK) - render.JSON(w, r, JSON{"id": id, "loc": locator}) -} - // GET /find?site=siteID&url=post-url&format=[tree|plain]&sort=[+/-time|+/-score] // find comments for given post. Returns in tree or plain formats, sorted func (s *Server) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { diff --git a/app/rest/server_test.go b/app/rest/server_test.go index 0aa54f7b..23b88004 100644 --- a/app/rest/server_test.go +++ b/app/rest/server_test.go @@ -16,6 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/umputun/remark/app/migrator" "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/store" ) @@ -216,31 +217,6 @@ func TestServer_FindUserComments(t *testing.T) { assert.Equal(t, 3, len(comments), "should have 3 comments") } -func TestServer_Delete(t *testing.T) { - srv, port := prep(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"}} - c2 := store.Comment{Text: "test test #2", ParentID: "p1", - Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}} - - id1 := addComment(t, c1, port) - addComment(t, c2, port) - - client := http.Client{} - req, err := http.NewRequest(http.MethodDelete, - fmt.Sprintf("http://127.0.0.1:%d/api/v1/comment/%s?site=radio-t&url=https://radio-t.com/blah", port, id1), nil) - assert.Nil(t, err) - resp, err := client.Do(req) - assert.Nil(t, err) - assert.Equal(t, 200, resp.StatusCode) - - _, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) - assert.Equal(t, 400, code) -} - func TestServer_UserInfo(t *testing.T) { srv, port := prep(t) assert.NotNil(t, srv) @@ -355,50 +331,6 @@ func TestServer_List(t *testing.T) { assert.Equal(t, []store.PostInfo{{URL: "https://radio-t.com/blah1", Count: 3}, {URL: "https://radio-t.com/blah2", Count: 2}}, pi) } -func TestServer_Pin(t *testing.T) { - srv, port := prep(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"}} - c2 := store.Comment{Text: "test test #2", ParentID: "p1", - Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}} - - id1 := addComment(t, c1, port) - addComment(t, c2, port) - - pin := func(val int) int { - client := http.Client{} - req, err := http.NewRequest(http.MethodPut, - fmt.Sprintf("http://127.0.0.1:%d/api/v1/admin/pin/%s?site=radio-t&url=https://radio-t.com/blah&pin=%d", port, id1, val), - nil) - assert.Nil(t, err) - resp, err := client.Do(req) - assert.Nil(t, err) - return resp.StatusCode - } - - code := pin(1) - assert.Equal(t, 200, code) - - body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) - assert.Equal(t, 200, code) - cr := store.Comment{} - err := json.Unmarshal([]byte(body), &cr) - assert.Nil(t, err) - assert.True(t, cr.Pin) - - code = pin(-1) - assert.Equal(t, 200, code) - body, code = get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/id/%s?site=radio-t&url=https://radio-t.com/blah", port, id1)) - assert.Equal(t, 200, code) - cr = store.Comment{} - err = json.Unmarshal([]byte(body), &cr) - assert.Nil(t, err) - assert.False(t, cr.Pin) -} - func prep(t *testing.T) (srv *Server, port int) { dataStore, err := store.NewBoltDB(store.BoltSite{FileName: testDb, SiteID: "radio-t"}) require.Nil(t, err) @@ -408,6 +340,7 @@ func prep(t *testing.T) (srv *Server, port int) { AuthFacebook: &auth.Provider{}, AuthGithub: &auth.Provider{}, AuthGoogle: &auth.Provider{}, + Exporter: &migrator.Remark{DataStore: dataStore}, } go func() { port = rand.Intn(50000) + 1025