192 lines
6.8 KiB
Go
192 lines
6.8 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"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://dev:password@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)
|
|
|
|
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.Equal(t, "", cr.Text)
|
|
assert.True(t, cr.Deleted)
|
|
}
|
|
|
|
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://dev:password@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)
|
|
|
|
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://dev:password@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)
|
|
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"])
|
|
|
|
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, "", comments[0].Text)
|
|
assert.True(t, comments[0].Deleted)
|
|
|
|
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://dev:password@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)
|
|
}
|
|
|
|
func TestAdmin_Import(t *testing.T) {
|
|
srv, port := prep(t)
|
|
assert.NotNil(t, srv)
|
|
defer cleanup(srv)
|
|
|
|
// add 2 initial comments, will be deleted by import
|
|
c1 := store.Comment{Text: "test test #1",
|
|
Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blahX"}}
|
|
c2 := store.Comment{Text: "test test #2",
|
|
Locator: store.Locator{SiteID: "radio-t", URL: "https://radio-t.com/blahX"}}
|
|
addComment(t, c1, port)
|
|
addComment(t, c2, port)
|
|
|
|
r := strings.NewReader(`{"id":"2aa0478c-df1b-46b1-b561-03d507cf482c","pid":"","text":"<p>test test #1</p>","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah1"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.849053725-05:00"}
|
|
{"id":"83fd97fd-ff64-48d1-9fb7-ca7769c77037","pid":"p1","text":"<p>test test #2</p>","user":{"name":"developer one","id":"dev","picture":"/api/v1/avatar/remark.image","profile":"https://remark42.com","admin":true,"ip":"ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741"},"locator":{"site":"radio-t","url":"https://radio-t.com/blah2"},"score":0,"votes":{},"time":"2018-04-30T01:37:00.861387771-05:00"}`)
|
|
|
|
resp, err := http.Post(fmt.Sprintf("http://dev:password@127.0.0.1:%d/api/v1/admin/import?site=radio-t&provider=native",
|
|
port), "application/json", r)
|
|
assert.Nil(t, err)
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
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/blah2", Count: 1}, {URL: "https://radio-t.com/blah1", Count: 1}}, pi)
|
|
}
|