* Make backend tests wait on conditions instead of durations The backend workflow has a long tail of runs that fail once and pass on a rerun. Every one of them comes down to a test assuming an operation finishes within some duration rather than waiting for the state it needs. Three were reproducible and each was reproduced against the old code before being changed: TestServerAuthHooks minted a token that lived one second and never tested expiry, so a slow runner turned the first POST into a 401; TestServerApp_AnonMode saw "connection refused" because waitForHTTPServerStart returned silently after three seconds and left a later assertion to fail with something unrelated; TestFsStore_Cleanup slept 200ms against a 300ms ttl that Cleanup widens to 400ms with its commit grace, so roughly 100ms of stall collected an image meant to survive. Fixed sleeps before asserting on asynchronous work are replaced with polls on the condition itself, using require.Eventually and require.EventuallyWithT, and require.Never where the assertion is that something did not happen. Polling closures assert on the CollectT they are handed rather than on t, since testify runs them on another goroutine, and polls that issue HTTP requests stay under the rate limit on the routes they poll through. Where a test needs time to have passed, the clock input is pinned instead: staging ages are stamped with os.Chtimes on both sides of the cleanup boundary right before each call, which also makes the 100ms commit grace an exact case rather than something no assertion reaches, and the RSS tests set store.Comment.Timestamp explicitly rather than racing the wall clock into the first 100ms of a second so pubDate matches. chooseUnusedPort takes a port from the kernel's ephemeral range. Picking at random out of a fixed 10000-port window let two package binaries, which go test ./... runs concurrently, land on the same number between the probe closing and the server binding. The start helpers fail naming the port they waited on, and the SSL tests wait on the redirect port as well as the TLS one. Arbitrary budgets that nothing tests are gone: ten HTTP clients with a one-second timeout against bolt-backed import and export, the "should take about 100msec" assertions, and a one-second bound on noticing an already cancelled context. Shutdown stays bounded at ten seconds so a hang is still caught. Two assertions get stronger. TestServerAuthHooks accepted 403 or 401 from a blocked user, an alternative that existed only because the short token could expire mid-test; it is deterministically 403 now. TestAdmin_BlockedList asserted two users blocked while one carried the same 150ms ttl the next step waits to lapse, so the halves raced each other. goleak stops reporting the regexp2 clock goroutine, which chroma pulls in for syntax highlighting and which lives for up to a second after the last match with a timeout; it ends on its own but a binary finishing inside that window was reported as leaking, and this suite now finishes sooner. The ignore for net/http.(*Server).Shutdown goes the other way: it no longer matches anything, with both packages run fifteen times each under CPU oversubscription to confirm. Two gaps the change would otherwise have opened are covered directly rather than left to the side effects that used to cover them. The one-second token was the only thing exercising the authenticator's ClaimsUpd hook on refresh, so TestServerApp_ClaimsUpd now calls the hook itself and checks admin, blocked, email and restricted-name impersonation, including the two pass-through cases. Lifting the open-route limit removed the last incidental exercise of the rate limiter, so TestRateLimiter drives a burst past the allowance and checks the refusals and that the limit is per client. Both run without a wall clock, and both were confirmed to fail when the behaviour they cover is removed. Production code is untouched. The two sleeps outside test code, the 429 backoff in cmd/cleanup.go and the submit poll in store/image/image.go, are left alone: no CI failure implicates them. Test sleeps drop from 67 to 21, all of them either inside a testing/synctest bubble or a poll interval. The suite runs in about 22 seconds instead of 46, mostly because TestPublic_FindCommentsCtrl_ConsistentCount no longer paces a hundred subtests with an 80ms sleep each to stay under the open route limit. The 300s per-package budget now matches across both workflows, the race_test target and the documented command, and CLAUDE.md records the convention. with '#' will be ignored, and an empty message aborts the commit. # # Date: Sat Aug 22 01:12:31 2026 +0100 # # interactive rebase in progress; onto7c312da1# Last command done (1 command done): # reword deb6cbf1 # Make backend tests wait on conditions instead of durations # Next command to do (1 remaining command): # reword 262e6dc2 # Apply go fix under Go 1.27 # You are currently editing a commit while rebasing branch 'fix/backend-test-flakiness' on '7c312da1'. # # Changes to be committed: .github/workflows/release.yml # modified: CLAUDE.md # modified: Makefile modified: backend/_example/memory_store/server/rpc_test.go # modified: backend/app/cmd/import_test.go # modified: backend/app/cmd/server_test.go # modified: backend/app/main_test.go # modified: backend/app/rest/api/admin_test.go # modified: backend/app/rest/api/middleware_test.go # modified: backend/app/rest/api/migrator_test.go # modified: backend/app/rest/api/rest_private_test.go # modified: backend/app/rest/api/rest_public_test.go # modified: backend/app/rest/api/rest_test.go # modified: backend/app/rest/api/rss_test.go # modified: backend/app/rest/proxy/image_test.go # modified: backend/app/store/image/fs_store_test.go # modified: backend/app/store/service/service_test.go # modified: docs/backlog/api-tests-deadlock-on-macos.md # * Apply go fix under Go 1.27 Go 1.27 extends go fix with the modernizers, so `go fix ./...` now rewrites patterns the language has since replaced. Running it across all three modules produces this: legacy sync/atomic calls on plain integers become the atomic types (notify.Service.closed, image.Service.term and submitCount, and several test counters), reverse index loops become slices.Backward, a Split-then-index becomes strings.Cut, counted loops become range over an int, and interface{} becomes any in the e2e suite. The example module needed no changes. The e2e module is behind a build tag, so it only matches with `go fix -tags e2e ./...`. One knock-on: prealloc can see the bound of a loop once it is written as range over an int, so the slice it feeds is now preallocated. with '#' will be ignored, and an empty message aborts the commit. # # Date: Sat Aug 22 01:32:09 2026 +0100 # # interactive rebase in progress; onto7c312da1# Last commands done (2 commands done): # reword deb6cbf1 262e6dc2 # Apply go fix under Go 1.27 # No commands remaining. # You are currently editing a commit while rebasing branch 'fix/backend-test-flakiness' on '7c312da1'. # # Changes to be committed: backend/app/migrator/native.go # modified: backend/app/notify/notify.go backend/app/rest/api/rest_private_test.go # modified: backend/app/store/comment.go # modified: backend/app/store/image/image.go # modified: backend/app/store/service/service_test.go # modified: backend/app/store/service/title_test.go # modified: e2e/e2e_test.go # modified: e2e/widgets_test.go #
2043 lines
77 KiB
Go
2043 lines
77 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"testing/synctest"
|
|
"time"
|
|
|
|
"github.com/go-pkgz/lgr"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"github.com/umputun/remark42/backend/app/store"
|
|
"github.com/umputun/remark42/backend/app/store/admin"
|
|
"github.com/umputun/remark42/backend/app/store/engine"
|
|
"github.com/umputun/remark42/backend/app/store/image"
|
|
)
|
|
|
|
func TestService_CreateFromEmpty(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks}
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
beforeCreate := time.Now()
|
|
id, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
assert.True(t, id != "", id)
|
|
|
|
res, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, "text", res.Text)
|
|
assert.WithinRange(t, res.Timestamp, beforeCreate, time.Now(), "timestamp set during create")
|
|
assert.Equal(t, "user", res.User.ID)
|
|
assert.Equal(t, "name", res.User.Name)
|
|
assert.Equal(t, "23f97cf4d5c29ef788ca2bdd1c9e75656c0e4149", res.User.IP)
|
|
assert.Equal(t, map[string]bool(nil), res.Votes)
|
|
assert.Equal(t, map[string]store.VotedIPInfo(nil), res.VotedIPs)
|
|
}
|
|
|
|
func TestService_CreateSiteDisabled(t *testing.T) {
|
|
ks := admin.NewStaticStore("secret 123", []string{"xxx"}, nil, "email")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks}
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.EqualError(t, err, "failed to prepare comment: can't get secret for site radio-t: site radio-t disabled")
|
|
}
|
|
|
|
func TestService_CreateFromPartial(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks}
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
Timestamp: time.Date(2018, 3, 25, 16, 34, 33, 0, time.UTC),
|
|
Votes: map[string]bool{"u1": true, "u2": false},
|
|
VotedIPs: map[string]store.VotedIPInfo{"xxx": {Value: true, Timestamp: time.Now()},
|
|
"yyy": {Value: false, Timestamp: time.Now()}},
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
id, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
assert.True(t, id != "", id)
|
|
|
|
res, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, "text", res.Text)
|
|
assert.Equal(t, comment.Timestamp, res.Timestamp)
|
|
assert.Equal(t, "user", res.User.ID)
|
|
assert.Equal(t, "name", res.User.Name)
|
|
assert.Equal(t, "23f97cf4d5c29ef788ca2bdd1c9e75656c0e4149", res.User.IP)
|
|
assert.Equal(t, "", res.PostTitle)
|
|
assert.Equal(t, comment.Votes, res.Votes)
|
|
}
|
|
|
|
func TestService_CreateWithQuotesInTitle(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks}
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
PostTitle: "some example \"in quotes\"",
|
|
Timestamp: time.Date(2018, 3, 25, 16, 34, 33, 0, time.UTC),
|
|
Votes: map[string]bool{"u1": true, "u2": false},
|
|
VotedIPs: map[string]store.VotedIPInfo{"xxx": {Value: true, Timestamp: time.Now()},
|
|
"yyy": {Value: false, Timestamp: time.Now()}},
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
id, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
assert.True(t, id != "", id)
|
|
|
|
res, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, "text", res.Text)
|
|
assert.Equal(t, comment.Timestamp, res.Timestamp)
|
|
assert.Equal(t, "user", res.User.ID)
|
|
assert.Equal(t, "name", res.User.Name)
|
|
assert.Equal(t, "23f97cf4d5c29ef788ca2bdd1c9e75656c0e4149", res.User.IP)
|
|
assert.Equal(t, "some example \"in quotes\"", res.PostTitle)
|
|
}
|
|
|
|
func TestService_CreateFromPartialWithTitle(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks,
|
|
TitleExtractor: NewTitleExtractor(http.Client{Timeout: 5 * time.Second}, []string{"127.0.0.1"})}
|
|
defer b.Close()
|
|
|
|
postPath := "/post/42"
|
|
postTitle := "Post Title 42"
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.String() == postPath {
|
|
_, err := fmt.Fprintf(w, "<html><title>%s</title><body>...</body></html>", postTitle)
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
w.WriteHeader(404)
|
|
}))
|
|
defer ts.Close()
|
|
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
Timestamp: time.Date(2018, 3, 25, 16, 34, 33, 0, time.UTC),
|
|
Votes: map[string]bool{"u1": true, "u2": false},
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: ts.URL + postPath, SiteID: "radio-t"},
|
|
}
|
|
id, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
assert.True(t, id != "", id)
|
|
|
|
res, err := b.Engine.Get(getReq(store.Locator{URL: ts.URL + postPath, SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, postTitle, res.PostTitle)
|
|
|
|
comment.PostTitle = "post blah"
|
|
id, err = b.Create(comment)
|
|
assert.NoError(t, err)
|
|
res, err = b.Engine.Get(getReq(store.Locator{URL: ts.URL + postPath, SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, "post blah", res.PostTitle, "keep comment title")
|
|
}
|
|
|
|
func TestService_Put(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks}
|
|
|
|
comment := store.Comment{
|
|
ID: "c-1",
|
|
ParentID: "id-1",
|
|
Text: "test text",
|
|
User: store.User{ID: "user2", Name: "user name 2"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
}
|
|
_, err := b.Create(comment)
|
|
require.NoError(t, err)
|
|
|
|
// create new comment with everything different to replace the first one with fields below
|
|
updatedComment := store.Comment{
|
|
ID: "c-1",
|
|
ParentID: "id-new",
|
|
Text: "new text",
|
|
User: store.User{ID: "user3", Name: "user name 3"},
|
|
Locator: store.Locator{URL: "https://example.com", SiteID: "example"},
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
}
|
|
|
|
err = b.Put(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, updatedComment)
|
|
require.NoError(t, err)
|
|
|
|
// request with wrong user, should not affect the comment user
|
|
got, err := b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "c-1", store.User{ID: "user1", Name: "user name 1"})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "c-1", got.ID)
|
|
assert.Equal(t, "new text", got.Text)
|
|
assert.Equal(t, "id-1", got.ParentID, "should be unaltered")
|
|
assert.Equal(t, "user2", got.User.ID, "should be unaltered")
|
|
assert.Equal(t, "user name 2", got.User.Name, "should be unaltered")
|
|
assert.Equal(t, "https://radio-t.com", got.Locator.URL, "should be unaltered")
|
|
assert.Equal(t, "radio-t", got.Locator.SiteID, "should be unaltered")
|
|
assert.Equal(t, time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC), got.Timestamp, "should be unaltered")
|
|
|
|
}
|
|
|
|
func TestService_SetTitle(t *testing.T) {
|
|
var titleEnable atomic.Int32
|
|
tss := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if titleEnable.Load() == 0 {
|
|
w.WriteHeader(404)
|
|
}
|
|
if r.URL.String() == "/post1" {
|
|
_, err := w.Write([]byte("<html><title>post1 blah 123</title><body> 2222</body></html>"))
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
if r.URL.String() == "/post2" {
|
|
_, err := w.Write([]byte("<html><title>post2 blah 123</title><body> 2222</body></html>"))
|
|
assert.NoError(t, err)
|
|
return
|
|
}
|
|
w.WriteHeader(404)
|
|
}))
|
|
defer tss.Close()
|
|
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks,
|
|
TitleExtractor: NewTitleExtractor(http.Client{Timeout: 5 * time.Second}, []string{"127.0.0.1"})}
|
|
defer b.Close()
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
Timestamp: time.Date(2018, 3, 25, 16, 34, 33, 0, time.UTC),
|
|
Votes: map[string]bool{"u1": true, "u2": false},
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: tss.URL + "/post1", SiteID: "radio-t"},
|
|
}
|
|
|
|
id, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
assert.True(t, id != "", id)
|
|
|
|
res, err := b.Engine.Get(getReq(store.Locator{URL: tss.URL + "/post1", SiteID: "radio-t"}, id))
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res)
|
|
assert.Equal(t, "", res.PostTitle)
|
|
|
|
b.TitleExtractor.cache.Purge()
|
|
|
|
titleEnable.Store(1)
|
|
c, err := b.SetTitle(store.Locator{URL: tss.URL + "/post1", SiteID: "radio-t"}, id)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "post1 blah 123", c.PostTitle)
|
|
|
|
bErr := DataStore{Engine: eng, AdminStore: ks}
|
|
defer bErr.Close()
|
|
_, err = bErr.SetTitle(store.Locator{URL: tss.URL + "/post1", SiteID: "radio-t"}, id)
|
|
require.EqualError(t, err, "no title extractor")
|
|
}
|
|
|
|
func TestService_Vote(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: -1}
|
|
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 0, res[0].Score)
|
|
assert.Equal(t, 0, res[0].Vote)
|
|
assert.Equal(t, map[string]bool(nil), res[0].Votes, "no votes initially")
|
|
|
|
// vote +1 as user1
|
|
req := VoteReq{
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
CommentID: res[0].ID,
|
|
UserID: "user1",
|
|
UserIP: "123",
|
|
Val: true,
|
|
}
|
|
c, err := b.Vote(req)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score)
|
|
assert.Equal(t, 1, c.Vote)
|
|
assert.Equal(t, map[string]bool{"user1": true}, c.Votes, "user voted +")
|
|
// check result as user1
|
|
c, err = b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, store.User{ID: "user1"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score)
|
|
assert.Equal(t, 1, c.Vote, "can see own vote result")
|
|
assert.Nil(t, c.Votes)
|
|
assert.Nil(t, c.VotedIPs)
|
|
// check result as user2
|
|
c, err = b.Get(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, store.User{ID: "user2"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score)
|
|
assert.Equal(t, 0, c.Vote, "can't see other user vote result")
|
|
assert.Nil(t, c.Votes)
|
|
assert.Nil(t, c.VotedIPs)
|
|
|
|
req = VoteReq{
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
CommentID: res[0].ID,
|
|
UserID: "user",
|
|
UserIP: "123",
|
|
Val: true,
|
|
}
|
|
c, err = b.Vote(req)
|
|
assert.Error(t, err, "self-voting not allowed")
|
|
|
|
req = VoteReq{
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
CommentID: res[0].ID,
|
|
UserID: "user1",
|
|
UserIP: "123",
|
|
Val: true,
|
|
}
|
|
_, err = b.Vote(req)
|
|
assert.Error(t, err, "double-voting rejected")
|
|
assert.True(t, strings.HasPrefix(err.Error(), "user user1 already voted"))
|
|
|
|
// check in last as user1
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{ID: "user1"})
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res[0])
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 1, res[0].Score)
|
|
assert.Equal(t, 1, res[0].Vote)
|
|
assert.Equal(t, 0.0, res[0].Controversy)
|
|
|
|
// check in last as user2
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{ID: "user2"})
|
|
assert.NoError(t, err)
|
|
t.Logf("%+v", res[0])
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 1, res[0].Score)
|
|
assert.Equal(t, 0, res[0].Vote)
|
|
assert.Equal(t, 0.0, res[0].Controversy)
|
|
|
|
req = VoteReq{
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
CommentID: res[0].ID,
|
|
UserID: "user1",
|
|
UserIP: "123",
|
|
Val: false,
|
|
}
|
|
_, err = b.Vote(req)
|
|
assert.NoError(t, err, "vote reset")
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 0, res[0].Score)
|
|
assert.Equal(t, 0, res[0].Vote)
|
|
assert.Equal(t, map[string]bool(nil), res[0].Votes, "vote reset ok")
|
|
assert.Equal(t, map[string]store.VotedIPInfo(nil), res[0].VotedIPs, "vote reset ok")
|
|
}
|
|
|
|
func TestService_VoteLimit(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: 2}
|
|
|
|
_, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user2", Val: true})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user3", Val: true})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user4", Val: true})
|
|
assert.Error(t, err, "vote limit reached")
|
|
assert.True(t, strings.HasPrefix(err.Error(), "maximum number of votes exceeded for comment id-1"))
|
|
|
|
_, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user4", Val: true})
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestService_VotesDisabled(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: 0}
|
|
|
|
_, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user2", Val: true})
|
|
assert.EqualError(t, err, "maximum number of votes exceeded for comment id-1")
|
|
}
|
|
|
|
func TestService_VoteAggressive(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: -1}
|
|
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.NoError(t, err)
|
|
t.Logf("%+v", res[0])
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 0, res[0].Score)
|
|
assert.Equal(t, map[string]bool(nil), res[0].Votes, "no votes initially")
|
|
|
|
// add a vote as user2
|
|
_, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: res[0].ID,
|
|
UserID: "user2", Val: true})
|
|
require.NoError(t, err)
|
|
|
|
// crazy vote +1 as user1
|
|
var wg sync.WaitGroup
|
|
for range 1000 {
|
|
wg.Go(func() {
|
|
_, _ = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: res[0].ID,
|
|
UserID: "user1", Val: true})
|
|
})
|
|
}
|
|
wg.Wait()
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{ID: "user1"})
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("%+v", res[0])
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, 2, res[0].Score, "add single +1")
|
|
assert.Equal(t, 1, res[0].Vote, "user1 voted +1")
|
|
assert.Equal(t, 0, len(res[0].Votes), "votes hidden")
|
|
assert.Equal(t, 0, len(res[0].VotedIPs), "vote ips hidden")
|
|
|
|
// random +1/-1 result should be [0..2]
|
|
for range 100 {
|
|
wg.Go(func() {
|
|
val := rand.Intn(2) > 0
|
|
_, _ = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: res[0].ID,
|
|
UserID: "user1", Val: val})
|
|
})
|
|
}
|
|
wg.Wait()
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, 3, len(res))
|
|
t.Logf("%+v %d", res[0], res[0].Score)
|
|
assert.True(t, res[0].Score >= 0 && res[0].Score <= 2, "unexpected score %d", res[0].Score)
|
|
}
|
|
|
|
func TestService_VoteConcurrent(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: -1}
|
|
|
|
comment := store.Comment{
|
|
Text: "text",
|
|
User: store.User{IP: "192.168.1.1", ID: "user", Name: "name"},
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.NoError(t, err)
|
|
|
|
// concurrent vote +1 as multiple users for the same comment
|
|
var wg sync.WaitGroup
|
|
for i := range 100 {
|
|
wg.Go(func() {
|
|
_, _ = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: res[0].ID,
|
|
UserID: fmt.Sprintf("user1-%d", i), Val: true})
|
|
})
|
|
}
|
|
wg.Wait()
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 100, res[0].Score, "should have 100 score")
|
|
assert.Equal(t, 0, len(res[0].Votes), "should hide votes")
|
|
assert.Equal(t, 0, len(res[0].VotedIPs), "should hide vote ips")
|
|
assert.Equal(t, 0.0, res[0].Controversy, "should have 0 controversy")
|
|
}
|
|
|
|
func TestService_VotePositive(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"),
|
|
MaxVotes: -1, PositiveScore: true} // allow positive voting only
|
|
|
|
_, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user2", Val: false})
|
|
assert.EqualError(t, err, "minimal score reached for comment id-1")
|
|
|
|
c, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user3", Val: true})
|
|
assert.NoError(t, err, "minimal score doesn't affect positive vote")
|
|
assert.Equal(t, 1, c.Score)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user2", Val: true})
|
|
assert.NoError(t, err, "vote set to +2")
|
|
assert.Equal(t, 2, c.Score)
|
|
|
|
// check +, -, -
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user5", Val: true})
|
|
assert.NoError(t, err, "user5 +1, score 3")
|
|
assert.Equal(t, 3, c.Score)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user5", Val: false})
|
|
assert.NoError(t, err, "user5 -1, score reset to 2")
|
|
assert.Equal(t, 2, c.Score)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user5", Val: false})
|
|
assert.NoError(t, err, "user5 -1, score 1")
|
|
assert.Equal(t, 1, c.Score)
|
|
|
|
// allow negative voting
|
|
b.PositiveScore = false
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user2", Val: false})
|
|
assert.NoError(t, err, "minimal score ignored")
|
|
assert.Equal(t, 0, c.Score)
|
|
assert.Equal(t, 2.0, c.Controversy)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-1",
|
|
UserID: "user4", Val: false})
|
|
assert.NoError(t, err, "minimal score ignored")
|
|
assert.Equal(t, -1, c.Score)
|
|
}
|
|
|
|
func TestService_VoteControversy(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: -1}
|
|
|
|
c, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user2", Val: false})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, -1, c.Score, "should have -1 score")
|
|
assert.InDelta(t, 0.00, c.Controversy, 0.01)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user3", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, c.Score, "should have 0 score")
|
|
assert.InDelta(t, 2.00, c.Controversy, 0.01)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user4", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score, "should have 1 score")
|
|
assert.InDelta(t, 1.73, c.Controversy, 0.01)
|
|
|
|
// check if stored
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 1, res[0].Score, "should have 1 score")
|
|
assert.InDelta(t, 1.73, res[0].Controversy, 0.01)
|
|
}
|
|
|
|
func TestService_RestrictedWords(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
restictedWordLister := StaticRestrictedWordsLister{Words: []string{"restricted"}}
|
|
b := DataStore{Engine: eng, AdminStore: ks, RestrictedWordsMatcher: NewRestrictedWordsMatcher(restictedWordLister)}
|
|
|
|
// test creating a comment with restricted words which should fail with appropriate error
|
|
reply := store.Comment{
|
|
ID: "c-1",
|
|
ParentID: "id-1",
|
|
Text: "restricted word",
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name 2"},
|
|
}
|
|
id, err := b.Create(reply)
|
|
assert.EqualError(t, err, ErrRestrictedWordsFound.Error(), "should fail with RestrictedWordError")
|
|
assert.Empty(t, id)
|
|
}
|
|
|
|
func TestDataStore_AdminStoreErrors(t *testing.T) {
|
|
badKey := true
|
|
badEnabled := true
|
|
as := admin.StoreMock{
|
|
OnEventFunc: func(string, admin.EventType) error { return errors.New("err") },
|
|
KeyFunc: func(string) (string, error) {
|
|
if badKey {
|
|
return "", errors.New("mock key err")
|
|
}
|
|
return "secret", nil
|
|
},
|
|
EnabledFunc: func(string) (bool, error) {
|
|
if badEnabled {
|
|
return false, errors.New("mock enabled err")
|
|
}
|
|
return true, nil
|
|
},
|
|
AdminsFunc: func(string) ([]string, error) { return nil, errors.New("mock admins err") },
|
|
}
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: &as, MaxVotes: -1}
|
|
comment := store.Comment{
|
|
ID: "c-1",
|
|
ParentID: "id-1",
|
|
Text: "restricted word",
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name 2"},
|
|
}
|
|
|
|
// key call error
|
|
id, err := b.Create(comment)
|
|
assert.ErrorContainsf(t, err, "mock key err", "should fail with mock error")
|
|
assert.Empty(t, id)
|
|
assert.Equal(t, len(as.KeyCalls()), 1)
|
|
assert.Equal(t, len(as.EnabledCalls()), 0)
|
|
assert.Equal(t, len(as.OnEventCalls()), 0)
|
|
|
|
// enabled call error
|
|
badKey = false
|
|
id, err = b.Create(comment)
|
|
assert.ErrorContains(t, err, "mock enabled err", "should fail with mock error")
|
|
assert.Empty(t, id)
|
|
assert.Equal(t, len(as.KeyCalls()), 2)
|
|
assert.Equal(t, len(as.EnabledCalls()), 1)
|
|
assert.Equal(t, len(as.OnEventCalls()), 0)
|
|
|
|
// only OnEvent error
|
|
badEnabled = false
|
|
id, err = b.Create(comment)
|
|
assert.NoError(t, err, "OnEvent error should be just logged")
|
|
assert.Equal(t, id, "c-1")
|
|
assert.Equal(t, len(as.KeyCalls()), 3)
|
|
assert.Equal(t, len(as.EnabledCalls()), 2)
|
|
assert.Equal(t, len(as.OnEventCalls()), 1)
|
|
|
|
// OnEvent error on Vote call
|
|
_, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "c-1",
|
|
UserID: "user4", Val: true})
|
|
assert.NoError(t, err, "OnEvent error should be just logged")
|
|
assert.Equal(t, len(as.KeyCalls()), 4)
|
|
assert.Equal(t, len(as.EnabledCalls()), 3)
|
|
assert.Equal(t, len(as.OnEventCalls()), 2)
|
|
|
|
// admins error
|
|
isAdmin := b.IsAdmin("radio-t", "user2")
|
|
assert.False(t, isAdmin)
|
|
assert.Equal(t, len(as.AdminsCalls()), 1)
|
|
assert.Equal(t, len(as.OnEventCalls()), 2)
|
|
|
|
// OnEvent error on EditComment call
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "c-1", EditRequest{Text: "new text"})
|
|
assert.NoError(t, err, "OnEvent error should be just logged")
|
|
|
|
// OnEvent error on Delete call
|
|
err = b.Delete(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "c-1", store.SoftDelete)
|
|
assert.NoError(t, err, "OnEvent error should be just logged")
|
|
|
|
// OnEvent error on EditComment Delete call
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "c-1", EditRequest{Delete: true})
|
|
assert.NoError(t, err, "OnEvent error should be just logged")
|
|
}
|
|
|
|
func TestService_VoteSameIP(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"), MaxVotes: -1}
|
|
b.RestrictSameIPVotes.Enabled = true
|
|
|
|
c, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user2", UserIP: "123", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score, "should have 1 score")
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user3", UserIP: "123", Val: true})
|
|
assert.EqualError(t, err, "the same ip cce61be6e0a692420ae0de31dceca179123c3b8a already voted for id-2")
|
|
assert.Equal(t, 1, c.Score, "still have 1 score, rejected")
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user2", UserIP: "123", Val: false})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, c.Score, "reset to 0 score, opposite vote allowed")
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user2", UserIP: "123", Val: false})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, -1, c.Score, "set to -1 score, correction vote allowed")
|
|
}
|
|
|
|
func TestService_VoteSameIPWithDuration(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123"),
|
|
MaxVotes: -1}
|
|
b.RestrictSameIPVotes.Enabled = true
|
|
b.RestrictSameIPVotes.Duration = 500 * time.Millisecond
|
|
|
|
c, err := b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user2", UserIP: "123", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c.Score, "should have 1 score")
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user3", UserIP: "123", Val: true})
|
|
assert.EqualError(t, err, "the same ip cce61be6e0a692420ae0de31dceca179123c3b8a already voted for id-2")
|
|
assert.Equal(t, 1, c.Score, "still have 1 score")
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user4", UserIP: "12345", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 2, c.Score, "have 2 score")
|
|
|
|
time.Sleep(501 * time.Millisecond)
|
|
|
|
c, err = b.Vote(VoteReq{Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, CommentID: "id-2",
|
|
UserID: "user3", UserIP: "123", Val: true})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 3, c.Score, "have 3 score")
|
|
})
|
|
}
|
|
|
|
func TestService_Controversy(t *testing.T) {
|
|
tbl := []struct {
|
|
ups, downs int
|
|
res float64
|
|
}{
|
|
{0, 0, 0},
|
|
{10, 5, 3.87},
|
|
{20, 5, 2.24},
|
|
{20, 50, 5.47},
|
|
{20, 0, 0},
|
|
{1100, 500, 28.60},
|
|
{1100, 12100, 2.37},
|
|
{100, 100, 200},
|
|
{101, 101, 202},
|
|
}
|
|
|
|
b := DataStore{}
|
|
for i, tt := range tbl {
|
|
t.Run(fmt.Sprintf("check-%d-%d:%d", i, tt.ups, tt.downs), func(t *testing.T) {
|
|
assert.InDelta(t, tt.res, b.controversy(tt.ups, tt.downs), 0.01)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestService_Pin(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Equal(t, false, res[0].Pin)
|
|
|
|
err = b.SetPin(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, true)
|
|
assert.NoError(t, err)
|
|
|
|
c, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, true, c.Pin)
|
|
|
|
err = b.SetPin(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, false)
|
|
assert.NoError(t, err)
|
|
c, err = b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, false, c.Pin)
|
|
}
|
|
|
|
func TestService_EditComment(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
defer b.Close()
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Nil(t, res[0].Edit)
|
|
|
|
comment, err := b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "my edit", comment.Edit.Summary)
|
|
assert.Equal(t, "xxx", comment.Text)
|
|
assert.Equal(t, "yyy", comment.Orig)
|
|
|
|
c, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID))
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "my edit", c.Edit.Summary)
|
|
assert.Equal(t, "xxx", c.Text)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"})
|
|
assert.NoError(t, err, "allow second edit")
|
|
}
|
|
|
|
func TestService_DeleteComment(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
defer b.Close()
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Nil(t, res[0].Edit)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, EditRequest{Delete: true})
|
|
assert.NoError(t, err)
|
|
|
|
c, err := b.Engine.Get(getReq(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID))
|
|
assert.NoError(t, err)
|
|
assert.True(t, c.Deleted)
|
|
t.Logf("%+v", c)
|
|
}
|
|
|
|
func TestService_EditCommentDurationFailed(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Nil(t, res[0].Edit)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestService_EditCommentReplyFailed(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
defer b.Close()
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[1])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Nil(t, res[1].Edit)
|
|
|
|
reply := store.Comment{
|
|
ID: "123456",
|
|
ParentID: "id-1",
|
|
Text: "some text",
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name 2"},
|
|
}
|
|
_, err = b.Create(reply)
|
|
assert.NoError(t, err)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[1].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit"})
|
|
assert.EqualError(t, err, "parent comment with reply can't be edited, id-1")
|
|
}
|
|
|
|
func TestService_EditCommentAdmin(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123"), AdminEdits: true}
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
t.Logf("%+v", res[0])
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Nil(t, res[0].Edit)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit", Admin: true})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = b.EditComment(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID,
|
|
EditRequest{Orig: "yyy", Text: "xxx", Summary: "my edit", Admin: false})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestService_ValidateComment(t *testing.T) {
|
|
b := DataStore{MinCommentSize: 6, MaxCommentSize: 2000, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
longText := fmt.Sprintf("%4000s", "X")
|
|
|
|
tbl := []struct {
|
|
inp store.Comment
|
|
err string
|
|
}{
|
|
{inp: store.Comment{}, err: "empty comment text"},
|
|
{inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid", Name: "name"}}, err: ""},
|
|
{inp: store.Comment{Orig: "something blah", User: store.User{ID: "myid"}}, err: "empty user info"},
|
|
{inp: store.Comment{Orig: "short", User: store.User{ID: "myid", Name: "name"}}, err: "comment text is smaller than min allowed size 6 (5)"},
|
|
{inp: store.Comment{Orig: longText, User: store.User{ID: "myid", Name: "name"}}, err: "comment text exceeded max allowed size 2000 (4000)"},
|
|
{inp: store.Comment{Orig: "here is a link with relative URL: [google.com](url)", User: store.User{ID: "myid", Name: "name"}}, err: "links should start with mailto:, http:// or https://"},
|
|
{inp: store.Comment{Orig: "here is a link with relative URL: [google.com](url)", User: store.User{ID: "myid", Name: "name"}}, err: "links should start with mailto:, http:// or https://"},
|
|
{inp: store.Comment{Orig: "multiple links, one is bad: [test](http://test) [test2](bad_url) [test3](https://test3)", User: store.User{ID: "myid", Name: "name"}}, err: "links should start with mailto:, http:// or https://"},
|
|
}
|
|
|
|
for n, tt := range tbl {
|
|
err := b.ValidateComment(&tt.inp)
|
|
if tt.err == "" {
|
|
assert.NoError(t, err, "check #%d", n)
|
|
continue
|
|
}
|
|
require.Error(t, err)
|
|
assert.EqualError(t, err, tt.err, "check #%d", n)
|
|
}
|
|
}
|
|
|
|
func TestService_Counts(t *testing.T) {
|
|
b, teardown := prepStoreEngine(t) // two comments for https://radio-t.com
|
|
defer teardown()
|
|
|
|
// add one more for https://radio-t.com/2
|
|
comment := store.Comment{
|
|
ID: "123456",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
svc := DataStore{Engine: b}
|
|
res, err := svc.Counts("radio-t", []string{"https://radio-t.com/2"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []store.PostInfo{{URL: "https://radio-t.com/2", Count: 1}}, res)
|
|
|
|
res, err = svc.Counts("radio-t", []string{"https://radio-t.com", "https://radio-t.com/2", "blah"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []store.PostInfo{
|
|
{URL: "https://radio-t.com", Count: 2},
|
|
{URL: "https://radio-t.com/2", Count: 1},
|
|
{URL: "blah", Count: 0},
|
|
}, res)
|
|
}
|
|
|
|
func TestService_GetMetas(t *testing.T) {
|
|
// two comments for https://radio-t.com
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
|
|
um, pm, err := b.Metas("radio-t")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 0, len(um))
|
|
assert.Equal(t, 0, len(pm))
|
|
|
|
assert.NoError(t, b.SetVerified("radio-t", "user1", true))
|
|
assert.NoError(t, b.SetBlock("radio-t", "user1", true, time.Hour))
|
|
assert.NoError(t, b.SetBlock("radio-t", "user2", true, time.Hour))
|
|
assert.NoError(t, b.SetReadOnly(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, true))
|
|
|
|
// set email for one existing and one non-existing user
|
|
req := engine.UserDetailRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user2", Detail: engine.UserEmail, Update: "test@example.org"}
|
|
value, err := b.Engine.UserDetail(req)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []engine.UserDetailEntry{{UserID: "user2", Email: "test@example.org"}}, value)
|
|
req.UserID = "user3"
|
|
value, err = b.Engine.UserDetail(req)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []engine.UserDetailEntry{{UserID: "user3", Email: "test@example.org"}}, value)
|
|
|
|
um, pm, err = b.Metas("radio-t")
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 3, len(um))
|
|
assert.Equal(t, "user1", um[0].ID)
|
|
assert.Equal(t, true, um[0].Verified)
|
|
assert.Equal(t, engine.UserDetailEntry{Email: ""}, um[0].Details)
|
|
assert.Equal(t, true, um[0].Blocked.Status)
|
|
assert.Equal(t, false, um[1].Verified)
|
|
assert.Equal(t, true, um[1].Blocked.Status)
|
|
assert.Equal(t, "test@example.org", um[1].Details.Email)
|
|
assert.Equal(t, "user3", um[2].ID)
|
|
assert.Equal(t, "test@example.org", um[2].Details.Email)
|
|
|
|
require.Equal(t, 1, len(pm))
|
|
assert.Equal(t, "https://radio-t.com", pm[0].URL)
|
|
assert.Equal(t, true, pm[0].ReadOnly)
|
|
}
|
|
|
|
func TestService_SetMetas(t *testing.T) {
|
|
// two comments for https://radio-t.com
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
umetas := []UserMetaData{}
|
|
pmetas := []PostMetaData{}
|
|
err := b.SetMetas("radio-t", umetas, pmetas)
|
|
assert.NoError(t, err, "empty metas")
|
|
|
|
um1 := UserMetaData{ID: "user1", Verified: true, Details: engine.UserDetailEntry{Email: "test@example.org"}}
|
|
um2 := UserMetaData{ID: "user2"}
|
|
um2.Blocked.Status = true
|
|
um2.Blocked.Until = time.Now().AddDate(0, 1, 1)
|
|
|
|
pmetas = []PostMetaData{{URL: "https://radio-t.com", ReadOnly: true}}
|
|
err = b.SetMetas("radio-t", []UserMetaData{um1, um2}, pmetas)
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, b.IsReadOnly(store.Locator{SiteID: "radio-t", URL: "https://radio-t.com"}))
|
|
assert.True(t, b.IsVerified("radio-t", "user1"))
|
|
assert.True(t, b.IsBlocked("radio-t", "user2"))
|
|
val, err := b.Engine.UserDetail(engine.UserDetailRequest{Locator: store.Locator{SiteID: "radio-t"}, UserID: "user1", Detail: engine.UserEmail})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, []engine.UserDetailEntry{{UserID: "user1", Email: "test@example.org"}}, val)
|
|
}
|
|
|
|
func TestService_UserDetailsOperations(t *testing.T) {
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
|
|
// add single valid entry
|
|
result, err := b.SetUserEmail("radio-t", "u1", "test@example.com")
|
|
assert.NoError(t, err, "No error inserting entry expected")
|
|
assert.Equal(t, "test@example.com", result)
|
|
result, err = b.SetUserTelegram("radio-t", "u1", "test@example.com")
|
|
assert.NoError(t, err, "No error inserting entry expected")
|
|
assert.Equal(t, "test@example.com", result)
|
|
|
|
// read valid entry back
|
|
result, err = b.GetUserEmail("radio-t", "u1")
|
|
assert.NoError(t, err, "No error reading entry expected")
|
|
assert.Equal(t, "test@example.com", result)
|
|
result, err = b.GetUserTelegram("radio-t", "u1")
|
|
assert.NoError(t, err, "No error reading entry expected")
|
|
assert.Equal(t, "test@example.com", result)
|
|
|
|
// delete existing entry
|
|
err = b.DeleteUserDetail("radio-t", "u1", engine.UserEmail)
|
|
assert.NoError(t, err, "No error deleting entry expected")
|
|
err = b.DeleteUserDetail("radio-t", "u1", engine.UserTelegram)
|
|
assert.NoError(t, err, "No error deleting entry expected")
|
|
|
|
// read deleted entry
|
|
result, err = b.GetUserEmail("radio-t", "u1")
|
|
assert.NoError(t, err, "No error reading entry expected")
|
|
assert.Empty(t, result)
|
|
result, err = b.GetUserTelegram("radio-t", "u1")
|
|
assert.NoError(t, err, "No error reading entry expected")
|
|
assert.Empty(t, result)
|
|
|
|
// insert entry with invalid site_id
|
|
result, err = b.SetUserEmail("bad-site", "u3", "does_not_matter@example.com")
|
|
assert.Error(t, err, "Site not found")
|
|
assert.Empty(t, result)
|
|
result, err = b.SetUserTelegram("bad-site", "u3", "does_not_matter@example.com")
|
|
assert.Error(t, err, "Site not found")
|
|
assert.Empty(t, result)
|
|
|
|
// read entry with invalid site_id
|
|
result, err = b.GetUserEmail("bad-site", "u3")
|
|
assert.Error(t, err, "Site not found")
|
|
assert.Empty(t, result)
|
|
result, err = b.GetUserTelegram("bad-site", "u3")
|
|
assert.Error(t, err, "Site not found")
|
|
assert.Empty(t, result)
|
|
}
|
|
|
|
func TestService_IsAdmin(t *testing.T) {
|
|
// two comments for https://radio-t.com
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", []string{"radio-t"}, []string{"user2"}, "user@email.com")}
|
|
|
|
assert.False(t, b.IsAdmin("radio-t", "user1"))
|
|
assert.True(t, b.IsAdmin("radio-t", "user2"))
|
|
assert.False(t, b.IsAdmin("radio-t-bad", "user1"))
|
|
}
|
|
|
|
func TestService_HasReplies(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng,
|
|
AdminStore: admin.NewStaticStore("secret 123", []string{"radio-t"}, []string{"user2"}, "user@email.com")}
|
|
defer b.Close()
|
|
|
|
comment := store.Comment{
|
|
ID: "id-1",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
assert.False(t, b.HasReplies(comment))
|
|
|
|
reply := store.Comment{
|
|
ID: "c-1",
|
|
ParentID: "id-1",
|
|
Text: "some text",
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name 2"},
|
|
}
|
|
_, err := b.Create(reply)
|
|
assert.NoError(t, err)
|
|
_, found := b.repliesCache.Peek(comment.ID)
|
|
assert.False(t, found, "not yet checked")
|
|
assert.True(t, b.HasReplies(comment))
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.True(t, found, "checked and has replies")
|
|
|
|
// deletion of the parent comment shouldn't work as the comment has replies
|
|
_, err = b.EditComment(reply.Locator, comment.ID, EditRequest{Orig: comment.ID, Delete: true, Summary: "user deletes the comment"})
|
|
assert.EqualError(t, err, "parent comment with reply can't be edited, "+comment.ID)
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.True(t, found, "checked and has replies")
|
|
|
|
// should not report replies after deletion of the child
|
|
err = b.Delete(reply.Locator, reply.ID, store.HardDelete)
|
|
assert.NoError(t, err)
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.False(t, found, "cleaned up from cache by Delete call")
|
|
assert.False(t, b.HasReplies(comment))
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.False(t, found, "checked and has no replies")
|
|
|
|
// recreate reply with the new ID
|
|
reply.ID = "c-2"
|
|
_, err = b.Create(reply)
|
|
assert.NoError(t, err)
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.False(t, found, "not yet checked")
|
|
assert.True(t, b.HasReplies(comment))
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.True(t, found, "checked and has replies again")
|
|
|
|
// should not report replies after deletion of the child using Edit mechanism
|
|
_, err = b.EditComment(reply.Locator, reply.ID, EditRequest{Orig: reply.ID, Delete: true, Summary: "user deletes the comment"})
|
|
assert.NoError(t, err)
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.False(t, found, "cleaned up from cache by EditComment call")
|
|
assert.False(t, b.HasReplies(comment))
|
|
_, found = b.repliesCache.Peek(reply.ParentID)
|
|
assert.False(t, found, "checked and has no replies")
|
|
}
|
|
|
|
func TestService_UserReplies(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
c1 := store.Comment{
|
|
ID: "comment-id-1",
|
|
Text: "test 123",
|
|
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
|
|
User: store.User{ID: "u1", Name: "developer one u1"},
|
|
}
|
|
c2 := store.Comment{
|
|
ID: "comment-id-2",
|
|
ParentID: "comment-id-1",
|
|
Text: "xyz test",
|
|
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
|
|
User: store.User{ID: "u2", Name: "developer one u2"},
|
|
}
|
|
c3 := store.Comment{
|
|
ID: "comment-id-3",
|
|
ParentID: "comment-id-1",
|
|
Text: "xyz test",
|
|
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
|
|
User: store.User{ID: "u2", Name: "developer one u3"},
|
|
}
|
|
c4 := store.Comment{
|
|
ID: "comment-id-4",
|
|
ParentID: "",
|
|
Text: "xyz test",
|
|
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
|
|
User: store.User{ID: "u4", Name: "developer one u4"},
|
|
}
|
|
c5 := store.Comment{
|
|
ID: "comment-id-5",
|
|
ParentID: "comment-id-1",
|
|
Text: "xyz test",
|
|
Locator: store.Locator{URL: "https://radio-t.com/blah10", SiteID: "radio-t"},
|
|
User: store.User{ID: "u2", Name: "developer one u2"},
|
|
}
|
|
|
|
// small sleeps give each Create a unique nanosecond timestamp under synctest's fake clock,
|
|
// since Bolt keys the "last" bucket by comment timestamp
|
|
_, err := b.Create(c1)
|
|
require.NoError(t, err)
|
|
time.Sleep(time.Nanosecond)
|
|
_, err = b.Create(c2)
|
|
require.NoError(t, err)
|
|
time.Sleep(time.Nanosecond)
|
|
_, err = b.Create(c3)
|
|
require.NoError(t, err)
|
|
time.Sleep(time.Nanosecond)
|
|
_, err = b.Create(c4)
|
|
require.NoError(t, err)
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
_, err = b.Create(c5)
|
|
require.NoError(t, err)
|
|
|
|
cc, u, err := b.UserReplies("radio-t", "u1", 10, time.Hour)
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 3, len(cc), "3 replies to u1")
|
|
assert.Equal(t, "developer one u1", u)
|
|
|
|
// advance fake clock so c2 and c3 (created 100ms before c5) age past the 299ms window,
|
|
// leaving only c5 as a recent reply
|
|
time.Sleep(200 * time.Millisecond)
|
|
cc, u, err = b.UserReplies("radio-t", "u1", 10, time.Millisecond*299)
|
|
require.NoError(t, err)
|
|
require.Equal(t, "developer one u1", u)
|
|
require.Equal(t, 1, len(cc), "1 reply to u1 in the last 299ms")
|
|
|
|
cc, u, err = b.UserReplies("radio-t", "u2", 10, time.Hour)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, len(cc), "0 replies to u2")
|
|
assert.Equal(t, "developer one u2", u)
|
|
|
|
cc, u, err = b.UserReplies("radio-t", "uxxx", 10, time.Hour)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, len(cc), "0 replies to uxxx")
|
|
assert.Equal(t, "", u)
|
|
})
|
|
}
|
|
|
|
func TestService_Find(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
res, err := b.Find(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time", store.User{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 2, len(res))
|
|
|
|
// add one more for https://radio-t.com/2
|
|
comment := store.Comment{
|
|
ID: "123456",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
Score: 1,
|
|
Votes: map[string]bool{"id-1": true, "id-2": true, "123456": false},
|
|
PostTitle: `some title, <a href="http://radio-t.com">link</a>`,
|
|
}
|
|
_, err = b.Engine.Create(comment) // create directly with engine, doesn't set Controversy
|
|
assert.NoError(t, err)
|
|
|
|
// make sure Controversy altered
|
|
res, err = b.Find(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "-controversy", store.User{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, 3, len(res))
|
|
assert.Equal(t, "123456", res[0].ID)
|
|
assert.InDelta(t, 1.73, res[0].Controversy, 0.01)
|
|
assert.Equal(t, "id-1", res[1].ID)
|
|
assert.InDelta(t, 0, res[1].Controversy, 0.01)
|
|
|
|
// make sure title sanitized
|
|
assert.Equal(t, "some title, link", res[0].PostTitle)
|
|
}
|
|
|
|
func TestService_FindSince(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
res, err := b.FindSince(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time", store.User{}, time.Time{})
|
|
require.NoError(t, err)
|
|
require.Equal(t, 2, len(res))
|
|
assert.Equal(t, "id-1", res[0].ID)
|
|
|
|
res, err = b.FindSince(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "time", store.User{},
|
|
time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC))
|
|
require.NoError(t, err)
|
|
require.Equal(t, 1, len(res))
|
|
assert.Equal(t, "id-2", res[0].ID)
|
|
}
|
|
|
|
func TestService_Info(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more comment for another URL to test site-wide Info request
|
|
comment := store.Comment{
|
|
ID: "123456xyz",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com/another", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
require.NoError(t, err)
|
|
|
|
// get non-existing URL info
|
|
info, err := b.Info(store.Locator{URL: "bad", SiteID: "radio-t"}, 0)
|
|
assert.Error(t, err)
|
|
assert.Empty(t, info)
|
|
|
|
// get non-existing site info
|
|
info, err = b.Info(store.Locator{SiteID: "bad"}, 0)
|
|
assert.Error(t, err)
|
|
assert.Empty(t, info)
|
|
|
|
// test two initially created comments and store first comment FirstTS
|
|
info, err = b.Info(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, 0)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "https://radio-t.com", info.URL)
|
|
assert.Equal(t, 2, info.Count)
|
|
assert.False(t, info.ReadOnly)
|
|
assert.True(t, info.LastTS.After(info.FirstTS))
|
|
firstTS := info.FirstTS
|
|
|
|
info, err = b.Info(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "https://radio-t.com", info.URL)
|
|
assert.True(t, info.ReadOnly)
|
|
|
|
// get last created comment LastTS
|
|
info, err = b.Info(store.Locator{URL: "https://radio-t.com/another", SiteID: "radio-t"}, 0)
|
|
require.NoError(t, err)
|
|
lastTS := info.LastTS
|
|
|
|
// site-level request
|
|
info, err = b.Info(store.Locator{SiteID: "radio-t"}, 1)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, 3, info.Count)
|
|
assert.Empty(t, info.URL, "site-level request should not set URL")
|
|
assert.False(t, info.ReadOnly, "site-level request should not set ReadOnly")
|
|
assert.Equal(t, firstTS, info.FirstTS, "site-level request should have FirstTS from the first post")
|
|
assert.Equal(t, lastTS, info.LastTS, "site-level request should have LastTS from the last post")
|
|
}
|
|
|
|
func TestService_Delete(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.Equal(t, 2, len(res))
|
|
assert.NoError(t, err)
|
|
|
|
err = b.Delete(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, res[0].ID, store.SoftDelete)
|
|
assert.NoError(t, err)
|
|
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
assert.Equal(t, 1, len(res), "one left")
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestService_deleteImagesOnCommentDelete(t *testing.T) {
|
|
lgr.Setup(lgr.Debug, lgr.CallerFile, lgr.CallerFunc)
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
mockStore := image.StoreMock{
|
|
DeleteFunc: func(string) error { return nil },
|
|
CommitFunc: func(string) error { return nil },
|
|
ResetCleanupTimerFunc: func(string) error { return nil },
|
|
}
|
|
imgSvc := image.NewService(&mockStore,
|
|
image.ServiceParams{
|
|
EditDuration: 50 * time.Millisecond,
|
|
ImageAPI: "/images/dev/",
|
|
ProxyAPI: "/non_existent",
|
|
})
|
|
defer imgSvc.Close(context.TODO())
|
|
|
|
// two comments for https://radio-t.com
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 50 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123"), ImageService: imgSvc}
|
|
|
|
c := store.Comment{
|
|
ID: "id-22",
|
|
Text: `some text <img src="/images/dev/pic1.png"/> xx <img src="/images/dev/pic2.png"/>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err := b.Engine.Create(c) // create directly with engine, doesn't call submitImages
|
|
assert.NoError(t, err)
|
|
b.submitImages(c)
|
|
// reply to the first comment with one new image and one existing one
|
|
c = store.Comment{
|
|
ID: "id-23",
|
|
ParentID: "id-22",
|
|
Text: `some text <img src="/images/dev/pic2.png"/> xx <img src="/images/dev/pic3.png"/>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err = b.Engine.Create(c) // create directly with engine, doesn't call submitImages
|
|
assert.NoError(t, err)
|
|
b.submitImages(c)
|
|
|
|
// verify that images are in staging store
|
|
assert.Equal(t, 4, len(mockStore.ResetCleanupTimerCalls()))
|
|
assert.Equal(t, "dev/pic1.png", mockStore.ResetCleanupTimerCalls()[0].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.ResetCleanupTimerCalls()[1].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.ResetCleanupTimerCalls()[2].ID)
|
|
assert.Equal(t, "dev/pic3.png", mockStore.ResetCleanupTimerCalls()[3].ID)
|
|
time.Sleep(b.EditDuration + 100*time.Millisecond)
|
|
// verify that they got into the main store
|
|
assert.Equal(t, 4, len(mockStore.CommitCalls()))
|
|
assert.Equal(t, "dev/pic1.png", mockStore.CommitCalls()[0].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.CommitCalls()[1].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.CommitCalls()[2].ID)
|
|
assert.Equal(t, "dev/pic3.png", mockStore.CommitCalls()[3].ID)
|
|
|
|
// delete the first comment
|
|
err = b.Delete(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "id-22", store.SoftDelete)
|
|
assert.NoError(t, err)
|
|
// verify that images are deleted from the main store
|
|
assert.Equal(t, 1, len(mockStore.DeleteCalls()))
|
|
assert.Equal(t, "dev/pic1.png", mockStore.DeleteCalls()[0].ID)
|
|
|
|
// delete the second comment
|
|
err = b.Delete(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"}, "id-23", store.SoftDelete)
|
|
assert.NoError(t, err)
|
|
// verify that images are deleted from the main store
|
|
assert.Equal(t, 3, len(mockStore.DeleteCalls()))
|
|
assert.Equal(t, "dev/pic2.png", mockStore.DeleteCalls()[1].ID)
|
|
assert.Equal(t, "dev/pic3.png", mockStore.DeleteCalls()[2].ID)
|
|
})
|
|
}
|
|
|
|
// DeleteUser removes all comments from user
|
|
func TestService_DeleteUser(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "123456xyz",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
assert.Equal(t, 3, len(res), "3 comments initially, for 2 diff users and 2 posts")
|
|
assert.NoError(t, err)
|
|
|
|
err = b.DeleteUser("radio-t", "user1", store.HardDelete)
|
|
assert.NoError(t, err)
|
|
|
|
res, err = b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
require.Equal(t, 1, len(res), "only one comment left for user2")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "user2", res[0].User.ID)
|
|
}
|
|
|
|
func TestService_List(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "id-3",
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
res, err := b.List("radio-t", 0, 0)
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(res), "2 posts")
|
|
assert.Equal(t, "https://radio-t.com/2", res[0].URL)
|
|
assert.Equal(t, 1, res[0].Count)
|
|
assert.Equal(t, time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC), res[0].FirstTS)
|
|
assert.Equal(t, time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC), res[0].LastTS)
|
|
|
|
assert.Equal(t, "https://radio-t.com", res[1].URL)
|
|
assert.Equal(t, 2, res[1].Count)
|
|
assert.Equal(t, time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC), res[1].FirstTS)
|
|
assert.Equal(t, time.Date(2017, 12, 20, 15, 18, 23, 0, time.UTC), res[1].LastTS)
|
|
}
|
|
|
|
func TestService_Count(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "id-3",
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
c, err := b.Count(store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 2, c)
|
|
|
|
c, err = b.Count(store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c)
|
|
|
|
c, err = b.Count(store.Locator{URL: "https://radio-t.com/3", SiteID: "radio-t"})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, c)
|
|
}
|
|
|
|
func TestService_UserComments(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "id-3",
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
cc, err := b.User("radio-t", "user1", 0, 0, store.User{})
|
|
assert.NoError(t, err)
|
|
require.Equal(t, 2, len(cc), "two recs for user1")
|
|
assert.Equal(t, "id-2", cc[0].ID, "reverse sort")
|
|
assert.Equal(t, "id-1", cc[1].ID, "reverse sort")
|
|
}
|
|
|
|
func TestService_UserCount(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "id-3",
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
c, err := b.UserCount("radio-t", "user1")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 2, c)
|
|
|
|
c, err = b.UserCount("radio-t", "user2")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 1, c)
|
|
|
|
_, err = b.UserCount("radio-t", "userBad")
|
|
assert.EqualError(t, err, "no comments for user userBad in store for radio-t site")
|
|
}
|
|
|
|
func TestService_DeleteAll(t *testing.T) {
|
|
// two comments for https://radio-t.com, no reply
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 100 * time.Millisecond,
|
|
AdminStore: admin.NewStaticStore("secret 123", nil, []string{"user2"}, "user@email.com")}
|
|
|
|
// add one more for user2
|
|
comment := store.Comment{
|
|
ID: "id-3",
|
|
Timestamp: time.Date(2018, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Locator: store.Locator{URL: "https://radio-t.com/2", SiteID: "radio-t"},
|
|
User: store.User{ID: "user2", Name: "user name"},
|
|
}
|
|
_, err := b.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
err = b.DeleteAll("radio-t")
|
|
assert.NoError(t, err)
|
|
|
|
res, err := b.Last("radio-t", 0, time.Time{}, store.User{})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 0, len(res))
|
|
}
|
|
|
|
func TestService_submitImages(t *testing.T) {
|
|
lgr.Setup(lgr.Debug, lgr.CallerFile, lgr.CallerFunc)
|
|
|
|
synctest.Test(t, func(t *testing.T) {
|
|
mockStore := image.StoreMock{
|
|
CommitFunc: func(string) error { return nil },
|
|
ResetCleanupTimerFunc: func(string) error { return nil },
|
|
}
|
|
imgSvc := image.NewService(&mockStore,
|
|
image.ServiceParams{
|
|
EditDuration: 50 * time.Millisecond,
|
|
ImageAPI: "/images/dev/",
|
|
ProxyAPI: "/non_existent",
|
|
})
|
|
defer imgSvc.Close(context.TODO())
|
|
|
|
// two comments for https://radio-t.com
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 50 * time.Millisecond,
|
|
AdminStore: admin.NewStaticKeyStore("secret 123"), ImageService: imgSvc}
|
|
|
|
c := store.Comment{
|
|
ID: "id-22",
|
|
Text: `some text <img src="/images/dev/pic1.png"/> xx <img src="/images/dev/pic2.png"/>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err := b.Engine.Create(c) // create directly with engine, doesn't call submitImages
|
|
assert.NoError(t, err)
|
|
|
|
b.submitImages(c)
|
|
assert.Equal(t, 2, len(mockStore.ResetCleanupTimerCalls()))
|
|
assert.Equal(t, "dev/pic1.png", mockStore.ResetCleanupTimerCalls()[0].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.ResetCleanupTimerCalls()[1].ID)
|
|
time.Sleep(b.EditDuration + 100*time.Millisecond)
|
|
assert.Equal(t, 2, len(mockStore.CommitCalls()))
|
|
assert.Equal(t, "dev/pic1.png", mockStore.CommitCalls()[0].ID)
|
|
assert.Equal(t, "dev/pic2.png", mockStore.CommitCalls()[1].ID)
|
|
})
|
|
}
|
|
|
|
func TestService_ResubmitStagingImages(t *testing.T) {
|
|
synctest.Test(t, func(t *testing.T) {
|
|
mockStore := image.StoreMock{
|
|
InfoFunc: func() (image.StoreInfo, error) {
|
|
return image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil
|
|
},
|
|
CommitFunc: func(string) error {
|
|
return nil
|
|
},
|
|
ResetCleanupTimerFunc: func(string) error { return nil },
|
|
}
|
|
imgSvc := image.NewService(&mockStore,
|
|
image.ServiceParams{
|
|
EditDuration: 10 * time.Millisecond,
|
|
ImageAPI: "http://127.0.0.1:8080/api/v1/picture/",
|
|
ProxyAPI: "http://127.0.0.1:8080/api/v1/img",
|
|
})
|
|
defer imgSvc.Close(context.TODO())
|
|
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvc}
|
|
|
|
// create comment with three images without preparing it properly
|
|
comment := store.Comment{
|
|
ID: "id-0",
|
|
Text: `<img src="http://127.0.0.1:8080/api/v1/picture/dev_user/bqf122eq9r8ad657n3ng" alt="startrails_01.jpg"><br/>
|
|
<img src="http://127.0.0.1:8080/api/v1/picture/dev_user/bqf321eq9r8ad657n3ng" alt="cat.png"><br/>
|
|
<img src="http://127.0.0.1:8080/api/v1/img?src=aHR0cHM6Ly9ob21lcGFnZXMuY2FlLndpc2MuZWR1L35lY2U1MzMvaW1hZ2VzL2JvYXQucG5n" alt="cat.png"><br/>
|
|
<img src="https://homepages.cae.wisc.edu/~ece533/images/boat.png" alt="boat.png">`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err := b.Engine.Create(comment)
|
|
require.NoError(t, err)
|
|
|
|
// resubmit single comment with three images, of which two are in staging storage
|
|
err = b.ResubmitStagingImages([]string{"radio-t"})
|
|
assert.NoError(t, err)
|
|
|
|
// wait for Submit goroutine to commit image
|
|
time.Sleep(b.EditDuration + time.Millisecond*100)
|
|
|
|
assert.Equal(t, 1, len(mockStore.InfoCalls()))
|
|
assert.Equal(t, 3, len(mockStore.CommitCalls()))
|
|
|
|
// empty answer
|
|
mockStoreEmpty := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
|
|
return image.StoreInfo{FirstStagingImageTS: time.Time{}}, nil
|
|
}}
|
|
imgSvcEmpty := image.NewService(&mockStoreEmpty,
|
|
image.ServiceParams{
|
|
EditDuration: 10 * time.Millisecond,
|
|
ImageAPI: "http://127.0.0.1:8080/api/v1/picture/",
|
|
})
|
|
defer imgSvcEmpty.Close(context.TODO())
|
|
bEmpty := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvcEmpty}
|
|
|
|
// resubmit receive empty timestamp and should do nothing )
|
|
err = bEmpty.ResubmitStagingImages([]string{"radio-t", "non_existent"})
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(mockStore.InfoCalls()))
|
|
|
|
// error from image storage
|
|
mockStoreError := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
|
|
return image.StoreInfo{}, fmt.Errorf("mock_err")
|
|
}}
|
|
imgSvcError := image.NewService(&mockStoreError,
|
|
image.ServiceParams{
|
|
EditDuration: 10 * time.Millisecond,
|
|
ImageAPI: "http://127.0.0.1:8080/api/v1/picture/",
|
|
})
|
|
defer imgSvcError.Close(context.TODO())
|
|
bError := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvcError}
|
|
|
|
// resubmit will receive error from image storage and should return it
|
|
err = bError.ResubmitStagingImages([]string{"radio-t"})
|
|
assert.EqualError(t, err, "mock_err")
|
|
|
|
assert.Equal(t, 1, len(mockStore.InfoCalls()))
|
|
assert.Equal(t, 3, len(mockStore.ResetCleanupTimerCalls()))
|
|
assert.Equal(t, "dev_user/bqf122eq9r8ad657n3ng", mockStore.ResetCleanupTimerCalls()[0].ID)
|
|
assert.Equal(t, "dev_user/bqf321eq9r8ad657n3ng", mockStore.ResetCleanupTimerCalls()[1].ID)
|
|
assert.Equal(t, "cached_images/12318fbd4c55e9d177b8b5ae197bc89c5afd8e07-a41fcb00643f28d700504256ec81cbf2e1aac53e", mockStore.ResetCleanupTimerCalls()[2].ID)
|
|
})
|
|
}
|
|
|
|
func TestService_ResubmitStagingImages_EngineError(t *testing.T) {
|
|
mockStore := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
|
|
return image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil
|
|
}}
|
|
imgSvc := image.NewService(&mockStore,
|
|
image.ServiceParams{
|
|
EditDuration: 10 * time.Millisecond,
|
|
ImageAPI: "http://127.0.0.1:8080/api/v1/picture/",
|
|
})
|
|
defer imgSvc.Close(context.TODO())
|
|
|
|
first := true
|
|
engineMock := engine.InterfaceMock{
|
|
FindFunc: func(engine.FindRequest) ([]store.Comment, error) {
|
|
if first {
|
|
first = false
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("mockError")
|
|
},
|
|
}
|
|
site1Req := engine.FindRequest{Locator: store.Locator{SiteID: "site1", URL: ""}, Sort: "time", Since: time.Time{}.Add(time.Second)}
|
|
site2Req := engine.FindRequest{Locator: store.Locator{SiteID: "site2", URL: ""}, Sort: "time", Since: time.Time{}.Add(time.Second)}
|
|
b := DataStore{Engine: &engineMock, EditDuration: 10 * time.Millisecond, ImageService: imgSvc}
|
|
|
|
// one call without error and one with error
|
|
err := b.ResubmitStagingImages([]string{"site1", "site2"})
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "problem finding comments for site site2: mockError")
|
|
|
|
assert.Equal(t, 1, len(mockStore.InfoCalls()))
|
|
assert.Equal(t, 2, len(engineMock.FindCalls()))
|
|
assert.Equal(t, site1Req, engineMock.FindCalls()[0].Req)
|
|
assert.Equal(t, site2Req, engineMock.FindCalls()[1].Req)
|
|
}
|
|
|
|
func TestService_alterComment(t *testing.T) {
|
|
engineMock := engine.InterfaceMock{
|
|
FlagFunc: func(engine.FlagRequest) (bool, error) {
|
|
return false, nil
|
|
},
|
|
}
|
|
svc := DataStore{Engine: &engineMock}
|
|
|
|
r := svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"},
|
|
Locator: store.Locator{URL: "http://example.com?foo=bar"}},
|
|
store.User{Name: "dev", ID: "devid", Admin: false})
|
|
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "", ID: "devid"},
|
|
Locator: store.Locator{URL: "http://example.com?foo=bar"}}, r, "ip cleaned")
|
|
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"}},
|
|
store.User{Name: "dev", ID: "devid", Admin: true})
|
|
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"}}, r, "ip not cleaned")
|
|
assert.Equal(t, 4, len(engineMock.FlagCalls()))
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[1].Req)
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[2].Req)
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[3].Req)
|
|
|
|
first := true
|
|
engineMock = engine.InterfaceMock{
|
|
FlagFunc: func(engine.FlagRequest) (bool, error) {
|
|
if first {
|
|
first = false
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
},
|
|
}
|
|
svc = DataStore{Engine: &engineMock}
|
|
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid", Verified: true}},
|
|
store.User{Name: "dev", ID: "devid", Admin: false})
|
|
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "", ID: "devid", Verified: true}}, r, "verified set")
|
|
assert.Equal(t, 2, len(engineMock.FlagCalls()))
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[1].Req)
|
|
|
|
first = true
|
|
engineMock = engine.InterfaceMock{
|
|
FlagFunc: func(engine.FlagRequest) (bool, error) {
|
|
if first {
|
|
first = false
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
},
|
|
}
|
|
svc = DataStore{Engine: &engineMock}
|
|
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid", Verified: true},
|
|
Locator: store.Locator{URL: "javascript:alert('XSS1')"}},
|
|
store.User{Name: "dev", ID: "devid", Admin: false})
|
|
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "", Verified: true, Blocked: true, ID: "devid"},
|
|
Deleted: false}, r, "blocked")
|
|
assert.Equal(t, 1, len(engineMock.FlagCalls()))
|
|
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
|
|
}
|
|
|
|
func TestService_alterCommentsFlagCaching(t *testing.T) {
|
|
t.Run("repeated user looked up once", func(t *testing.T) {
|
|
engineMock := engine.InterfaceMock{
|
|
FlagFunc: func(engine.FlagRequest) (bool, error) { return false, nil },
|
|
}
|
|
svc := DataStore{Engine: &engineMock}
|
|
|
|
comments := make([]store.Comment, 0, 5)
|
|
for i := range 5 {
|
|
comments = append(comments, store.Comment{ID: fmt.Sprintf("c%d", i),
|
|
User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}})
|
|
}
|
|
svc.alterComments(comments, store.User{ID: "u1"})
|
|
|
|
// one Blocked + one Verified lookup for the single user, not two per comment
|
|
assert.Equal(t, 2, len(engineMock.FlagCalls()), "5 comments by one user -> 2 flag lookups")
|
|
})
|
|
|
|
t.Run("distinct users looked up per user", func(t *testing.T) {
|
|
engineMock := engine.InterfaceMock{
|
|
FlagFunc: func(req engine.FlagRequest) (bool, error) {
|
|
return req.Flag == engine.Blocked && req.UserID == "blocked", nil // "blocked" user is blocked
|
|
},
|
|
}
|
|
svc := DataStore{Engine: &engineMock}
|
|
|
|
comments := []store.Comment{
|
|
{ID: "c1", User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}},
|
|
{ID: "c2", User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}},
|
|
{ID: "c3", User: store.User{ID: "blocked"}, Locator: store.Locator{SiteID: "site1"}},
|
|
{ID: "c4", User: store.User{ID: "blocked"}, Locator: store.Locator{SiteID: "site1"}},
|
|
}
|
|
res := svc.alterComments(comments, store.User{ID: "admin", Admin: true})
|
|
|
|
// u1: Blocked+Verified (2); blocked user: Blocked only, Verified skipped (1) = 3 total
|
|
assert.Equal(t, 3, len(engineMock.FlagCalls()), "two distinct users -> 3 flag lookups")
|
|
assert.True(t, res[2].User.Blocked && res[3].User.Blocked, "blocked user marked blocked")
|
|
assert.False(t, res[0].User.Blocked, "u1 not blocked")
|
|
})
|
|
|
|
t.Run("flag read error is not cached", func(t *testing.T) {
|
|
var blockedCalls int
|
|
engineMock := engine.InterfaceMock{
|
|
FlagFunc: func(req engine.FlagRequest) (bool, error) {
|
|
if req.Flag == engine.Blocked {
|
|
blockedCalls++
|
|
if blockedCalls == 1 {
|
|
return false, fmt.Errorf("transient flag read error")
|
|
}
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
},
|
|
}
|
|
svc := DataStore{Engine: &engineMock}
|
|
|
|
comments := []store.Comment{
|
|
{ID: "c0", User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}},
|
|
{ID: "c1", User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}},
|
|
{ID: "c2", User: store.User{ID: "u1"}, Locator: store.Locator{SiteID: "site1"}},
|
|
}
|
|
res := svc.alterComments(comments, store.User{ID: "admin", Admin: true})
|
|
|
|
// the errored first lookup must not be cached, so the next comment retries and
|
|
// picks up the real blocked state; once it succeeds the result is cached
|
|
assert.False(t, res[0].User.Blocked, "errored lookup treated as not blocked")
|
|
assert.True(t, res[1].User.Blocked, "retry after error picks up blocked state")
|
|
assert.True(t, res[2].User.Blocked, "successful read is cached")
|
|
assert.Equal(t, 2, blockedCalls, "blocked retried once after the error, then cached")
|
|
})
|
|
}
|
|
|
|
func Benchmark_ServiceCreate(b *testing.B) {
|
|
dbFile := fmt.Sprintf("%s/test-remark42-%d.db", os.TempDir(), rand.Intn(9999999999))
|
|
defer func() { _ = os.Remove(dbFile) }()
|
|
|
|
boltStore, err := engine.NewBoltDB(bolt.Options{}, engine.BoltSite{FileName: dbFile, SiteID: "radio-t"})
|
|
svc := DataStore{Engine: boltStore, EditDuration: 50 * time.Millisecond, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
require.NoError(b, err)
|
|
defer func() { assert.NoError(b, svc.Close()) }()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
comment := store.Comment{
|
|
ID: "id-" + strconv.Itoa(i),
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err = svc.Create(comment)
|
|
require.NoError(b, err)
|
|
}
|
|
}
|
|
|
|
func TestService_DoubleClose_Bolt(t *testing.T) {
|
|
dbFile := fmt.Sprintf("%s/test-remark42-%d.db", os.TempDir(), rand.Intn(9999999999))
|
|
defer func() { _ = os.Remove(dbFile) }()
|
|
|
|
boltStore, err := engine.NewBoltDB(bolt.Options{}, engine.BoltSite{FileName: dbFile, SiteID: "radio-t"})
|
|
svc := DataStore{Engine: boltStore, EditDuration: 50 * time.Millisecond, AdminStore: admin.NewStaticKeyStore("secret 123")}
|
|
require.NoError(t, err)
|
|
assert.NoError(t, boltStore.Close())
|
|
assert.NoError(t, boltStore.Close(), "second call should not result in panic or errors")
|
|
assert.NoError(t, svc.Close())
|
|
assert.NoError(t, svc.Close(), "second call should not result in panic or errors")
|
|
}
|
|
|
|
func TestService_DoubleClose_Static(t *testing.T) {
|
|
ks := admin.NewStaticKeyStore("secret 123")
|
|
eng, teardown := prepStoreEngine(t)
|
|
defer teardown()
|
|
b := DataStore{Engine: eng, AdminStore: ks,
|
|
TitleExtractor: NewTitleExtractor(http.Client{Timeout: 5 * time.Second}, []string{})}
|
|
assert.NoError(t, b.Close())
|
|
// second call should not result in panic or errors
|
|
assert.NoError(t, b.Close())
|
|
}
|
|
|
|
// makes new boltdb, put two records
|
|
func prepStoreEngine(t *testing.T) (e engine.Interface, teardown func()) {
|
|
testDBLoc, err := os.MkdirTemp("", "test_image_r42")
|
|
require.NoError(t, err)
|
|
testDB := path.Join(testDBLoc, "test.db")
|
|
_ = os.Remove(testDB)
|
|
|
|
st := time.Now()
|
|
boltStore, err := engine.NewBoltDB(bolt.Options{}, engine.BoltSite{FileName: testDB, SiteID: "radio-t"})
|
|
assert.NoError(t, err)
|
|
|
|
comment := store.Comment{
|
|
ID: "id-1",
|
|
Text: `some text, <a href="http://radio-t.com">link</a>`,
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 22, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err = boltStore.Create(comment)
|
|
assert.NoError(t, err)
|
|
|
|
comment = store.Comment{
|
|
ID: "id-2",
|
|
Text: "some text2",
|
|
Timestamp: time.Date(2017, 12, 20, 15, 18, 23, 0, time.UTC),
|
|
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
|
|
User: store.User{ID: "user1", Name: "user name"},
|
|
}
|
|
_, err = boltStore.Create(comment)
|
|
assert.NoError(t, err)
|
|
t.Logf("prepared store engine in %v", time.Since(st))
|
|
return boltStore, func() {
|
|
assert.NoError(t, boltStore.Close())
|
|
_ = os.Remove(testDB)
|
|
}
|
|
}
|
|
|
|
func getReq(locator store.Locator, commentID string) engine.GetRequest {
|
|
return engine.GetRequest{
|
|
Locator: locator,
|
|
CommentID: commentID,
|
|
}
|
|
}
|