diff --git a/app/rest/server_test.go b/app/rest/server_test.go index bb0df721..0aa54f7b 100644 --- a/app/rest/server_test.go +++ b/app/rest/server_test.go @@ -14,8 +14,8 @@ import ( "time" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "github.com/umputun/remark/app/rest/auth" "github.com/umputun/remark/app/store" ) @@ -231,8 +231,7 @@ func TestServer_Delete(t *testing.T) { 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) + 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) @@ -302,6 +301,104 @@ func TestServer_Vote(t *testing.T) { } +func TestServer_Count(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, c1, port) + addComment(t, c1, port) + addComment(t, c2, port) + addComment(t, c2, port) + + body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/count?site=radio-t&url=https://radio-t.com/blah1", port)) + assert.Equal(t, 200, code) + j := JSON{} + err := json.Unmarshal([]byte(body), &j) + assert.Nil(t, err) + assert.Equal(t, 3.0, j["count"]) + + body, code = get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/count?site=radio-t&url=https://radio-t.com/blah2", port)) + assert.Equal(t, 200, code) + err = json.Unmarshal([]byte(body), &j) + assert.Nil(t, err) + assert.Equal(t, 2.0, j["count"]) +} + +func TestServer_List(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, c1, port) + addComment(t, c1, port) + addComment(t, c2, port) + addComment(t, c2, port) + + body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/list?site=radio-t", port)) + assert.Equal(t, 200, code) + pi := []store.PostInfo{} + err := json.Unmarshal([]byte(body), &pi) + assert.Nil(t, err) + 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)