From 6250f2e52e9b8ed0a24507056bf6886436caedbe Mon Sep 17 00:00:00 2001 From: Umputun Date: Fri, 25 Jan 2019 18:24:58 -0600 Subject: [PATCH] allow empty list of comments on find for fresh post without anything #262 --- backend/app/rest/api/admin_test.go | 29 +++++++++++++++++++++++- backend/app/rest/api/rest_public.go | 2 +- backend/app/rest/api/rest_public_test.go | 14 ++++++++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/backend/app/rest/api/admin_test.go b/backend/app/rest/api/admin_test.go index c67b74bc..ad9da54e 100644 --- a/backend/app/rest/api/admin_test.go +++ b/backend/app/rest/api/admin_test.go @@ -17,12 +17,12 @@ import ( "github.com/go-pkgz/auth/token" R "github.com/go-pkgz/rest" "github.com/go-pkgz/rest/cache" - "github.com/umputun/remark/backend/app/store/service" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/umputun/remark/backend/app/store" + "github.com/umputun/remark/backend/app/store/service" ) func TestAdmin_Delete(t *testing.T) { @@ -444,6 +444,33 @@ func TestAdmin_ReadOnly(t *testing.T) { assert.Equal(t, http.StatusCreated, resp.StatusCode) } +func TestAdmin_ReadOnlyNoComments(t *testing.T) { + ts, srv, teardown := startupT(t) + defer teardown() + + client := http.Client{} + + // set post to read-only + req, err := http.NewRequest(http.MethodPut, + fmt.Sprintf("%s/api/v1/admin/readonly?site=radio-t&url=https://radio-t.com/blah&ro=1", ts.URL), nil) + assert.Nil(t, err) + req.SetBasicAuth("admin", "password") + resp, err := client.Do(req) + require.Nil(t, err) + assert.Equal(t, 200, resp.StatusCode) + _, err = srv.DataService.Info(store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah"}, 0) + assert.NotNil(t, err) + + res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah&format=tree") + assert.Equal(t, 200, code) + comments := commentsWithInfo{} + err = json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 0, len(comments.Comments), "should have 0 comments") + assert.True(t, comments.Info.ReadOnly) + t.Logf("%+v", comments) +} + func TestAdmin_ReadOnlyWithAge(t *testing.T) { ts, srv, teardown := startupT(t) defer teardown() diff --git a/backend/app/rest/api/rest_public.go b/backend/app/rest/api/rest_public.go index 4f49f91d..11573c5e 100644 --- a/backend/app/rest/api/rest_public.go +++ b/backend/app/rest/api/rest_public.go @@ -31,7 +31,7 @@ func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { data, err := s.Cache.Get(key, func() ([]byte, error) { comments, e := s.DataService.Find(locator, sort) if e != nil { - return nil, e + comments = []store.Comment{} // error should clear comments and continue for post info } maskedComments := s.adminService.alterComments(comments, r) var b []byte diff --git a/backend/app/rest/api/rest_public_test.go b/backend/app/rest/api/rest_public_test.go index 7d8b2d97..46eb106d 100644 --- a/backend/app/rest/api/rest_public_test.go +++ b/backend/app/rest/api/rest_public_test.go @@ -70,8 +70,12 @@ func TestRest_Find(t *testing.T) { ts, _, teardown := startupT(t) defer teardown() - _, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1") - assert.Equal(t, 400, code, "nothing in") + res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1") + assert.Equal(t, 200, code) + comments := commentsWithInfo{} + err := json.Unmarshal([]byte(res), &comments) + assert.Nil(t, err) + assert.Equal(t, 0, len(comments.Comments), "should have 0 comments") c1 := store.Comment{Text: "test test #1", ParentID: "", Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}} @@ -84,10 +88,10 @@ func TestRest_Find(t *testing.T) { assert.NotEqual(t, id1, id2) // get sorted by +time - res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time") + res, code = get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&sort=+time") assert.Equal(t, 200, code) - comments := commentsWithInfo{} - err := json.Unmarshal([]byte(res), &comments) + comments = commentsWithInfo{} + err = json.Unmarshal([]byte(res), &comments) assert.Nil(t, err) assert.Equal(t, 2, len(comments.Comments), "should have 2 comments") assert.Equal(t, id1, comments.Comments[0].ID)