From f3a157dedbb7d4112b8f5e3bf9801fab12827f8e Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 28 May 2018 18:29:42 -0500 Subject: [PATCH] set RO flag on find --- app/rest/api/rest.go | 6 +++++- app/rest/api/rest_test.go | 42 +++++++++++++++++++++++++++++++++++++++ app/rest/tree.go | 2 +- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index 8fe9461f..e2db5bac 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -347,7 +347,11 @@ func (s *Rest) findCommentsCtrl(w http.ResponseWriter, r *http.Request) { var b []byte switch r.URL.Query().Get("format") { case "tree": - b, e = encodeJSONWithHTML(rest.MakeTree(maskedComments, sort, s.ReadOnlyAge)) + tree := rest.MakeTree(maskedComments, sort, s.ReadOnlyAge) + if s.DataService.IsReadOnly(locator) { + tree.Info.ReadOnly = true + } + b, e = encodeJSONWithHTML(tree) default: b, e = encodeJSONWithHTML(maskedComments) } diff --git a/app/rest/api/rest_test.go b/app/rest/api/rest_test.go index 16187524..499e0425 100644 --- a/app/rest/api/rest_test.go +++ b/app/rest/api/rest_test.go @@ -269,6 +269,48 @@ func TestRest_FindAge(t *testing.T) { assert.True(t, tree.Info.ReadOnly, "post is old") } +func TestRest_FindReadOnly(t *testing.T) { + srv, ts := prep(t) + assert.NotNil(t, srv) + defer cleanup(ts) + + c1 := store.Comment{Text: "test test #1", ParentID: "", Timestamp: time.Now().AddDate(0, 0, -1), + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah1"}, User: store.User{ID: "u1"}} + _, err := srv.DataService.Create(c1) + + require.Nil(t, err) + + c2 := store.Comment{Text: "test test #2", ParentID: "", Timestamp: time.Now().AddDate(0, 0, -2), + Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blah2"}, User: store.User{ID: "u1"}} + _, err = srv.DataService.Create(c2) + require.Nil(t, err) + + // set post to read-only + client := http.Client{} + req, err := http.NewRequest(http.MethodPut, + fmt.Sprintf("%s/api/v1/admin/readonly?site=radio-t&url=https://radio-t.com/blah1&ro=1", ts.URL), nil) + assert.Nil(t, err) + withBasicAuth(req, "dev", "password") + _, err = client.Do(req) + require.Nil(t, err) + + tree := rest.Tree{} + res, code := get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah1&format=tree") + assert.Equal(t, 200, code) + err = json.Unmarshal([]byte(res), &tree) + require.Nil(t, err) + assert.Equal(t, "https://radio-t.com/blah1", tree.Info.URL) + assert.True(t, tree.Info.ReadOnly, "post is ro") + + tree = rest.Tree{} + res, code = get(t, ts.URL+"/api/v1/find?site=radio-t&url=https://radio-t.com/blah2&format=tree") + assert.Equal(t, 200, code) + err = json.Unmarshal([]byte(res), &tree) + require.Nil(t, err) + assert.Equal(t, "https://radio-t.com/blah2", tree.Info.URL) + assert.False(t, tree.Info.ReadOnly, "post is writable") +} + func TestRest_Update(t *testing.T) { srv, ts := prep(t) assert.NotNil(t, srv) diff --git a/app/rest/tree.go b/app/rest/tree.go index 453ec9f5..0b3aee32 100644 --- a/app/rest/tree.go +++ b/app/rest/tree.go @@ -22,7 +22,7 @@ type Node struct { tsCreated time.Time } -// recurData wraps all fileds used in recursive processing as intermediate results +// recurData wraps all fields used in recursive processing as intermediate results type recurData struct { tsModified time.Time tsCreated time.Time