7
7
"net/http"
8
8
"net/http/httptest"
9
9
"os"
10
+ "strconv"
10
11
"strings"
11
12
"testing"
12
13
"time"
@@ -210,7 +211,7 @@ func TestRest_Find(t *testing.T) {
210
211
assert .Equal (t , id2 , comments .Comments [0 ].ID )
211
212
212
213
// get in tree mode
213
- tree := service. Tree {}
214
+ tree := treeWithInfo {}
214
215
res , code = get (t , ts .URL + "/api/v1/find?site=remark42&url=https://radio-t.com/blah1&format=tree" )
215
216
assert .Equal (t , http .StatusOK , code )
216
217
err = json .Unmarshal ([]byte (res ), & tree )
@@ -236,7 +237,7 @@ func TestRest_FindAge(t *testing.T) {
236
237
_ , err = srv .DataService .Create (c2 )
237
238
require .NoError (t , err )
238
239
239
- tree := service. Tree {}
240
+ tree := treeWithInfo {}
240
241
241
242
res , code := get (t , ts .URL + "/api/v1/find?site=remark42&url=https://radio-t.com/blah1&format=tree" )
242
243
assert .Equal (t , http .StatusOK , code )
@@ -279,15 +280,15 @@ func TestRest_FindReadOnly(t *testing.T) {
279
280
require .NoError (t , err )
280
281
require .NoError (t , resp .Body .Close ())
281
282
282
- tree := service. Tree {}
283
+ tree := treeWithInfo {}
283
284
res , code := get (t , ts .URL + "/api/v1/find?site=remark42&url=https://radio-t.com/blah1&format=tree" )
284
285
assert .Equal (t , http .StatusOK , code )
285
286
err = json .Unmarshal ([]byte (res ), & tree )
286
287
require .NoError (t , err )
287
288
assert .Equal (t , "https://radio-t.com/blah1" , tree .Info .URL )
288
289
assert .True (t , tree .Info .ReadOnly , "post is ro" )
289
290
290
- tree = service. Tree {}
291
+ tree = treeWithInfo {}
291
292
res , code = get (t , ts .URL + "/api/v1/find?site=remark42&url=https://radio-t.com/blah2&format=tree" )
292
293
assert .Equal (t , http .StatusOK , code )
293
294
err = json .Unmarshal ([]byte (res ), & tree )
@@ -515,6 +516,83 @@ func TestRest_FindUserComments_CWE_918(t *testing.T) {
515
516
assert .Equal (t , arbitraryServer .URL , resp .Comments [0 ].Locator .URL , "arbitrary URL provided by the request" )
516
517
}
517
518
519
+ func TestPublic_FindCommentsCtrl_ConsistentCount (t * testing.T ) {
520
+ // test that comment counting is consistent between tree and plain formats
521
+ ts , srv , teardown := startupT (t )
522
+ defer teardown ()
523
+
524
+ commentLocator := store.Locator {URL : "test-url" , SiteID : "remark42" }
525
+
526
+ // Adding initial comments and voting
527
+ c1 := store.Comment {Text : "top-level comment 1" , Locator : commentLocator }
528
+ id1 := addComment (t , c1 , ts )
529
+
530
+ c2 := store.Comment {Text : "top-level comment 2" , Locator : commentLocator }
531
+ id2 := addComment (t , c2 , ts )
532
+
533
+ c3 := store.Comment {Text : "second-level comment 1" , ParentID : id1 , Locator : commentLocator }
534
+ id3 := addComment (t , c3 , ts )
535
+
536
+ c4 := store.Comment {Text : "third-level comment 1" , ParentID : id3 , Locator : commentLocator }
537
+ _ = addComment (t , c4 , ts )
538
+
539
+ c5 := store.Comment {Text : "second-level comment 2" , ParentID : id2 , Locator : commentLocator }
540
+ id5 := addComment (t , c5 , ts )
541
+
542
+ c6 := store.Comment {Text : "third-level comment 2" , ParentID : id5 , Locator : commentLocator }
543
+ id6 := addComment (t , c6 , ts )
544
+
545
+ c7 := store.Comment {Text : "top-level comment 3" , Locator : commentLocator }
546
+ id7 := addComment (t , c7 , ts )
547
+
548
+ c8 := store.Comment {Text : "second-level comment 3" , ParentID : id7 , Locator : commentLocator }
549
+ id8 := addComment (t , c8 , ts )
550
+
551
+ err := srv .DataService .Delete (commentLocator , id8 , store .SoftDelete )
552
+ assert .NoError (t , err )
553
+ err = srv .DataService .Delete (commentLocator , id6 , store .HardDelete )
554
+ assert .NoError (t , err )
555
+ srv .Cache .Flush (cache.FlusherRequest {})
556
+
557
+ currentTimestamp := time .Now ().Unix () * 1000 // For the "since" parameter.
558
+
559
+ testCases := []struct {
560
+ params string
561
+ expectedBody string
562
+ }{
563
+ {"" , `"info":{"url":"test-url","count":6,` },
564
+ {"format=plain" , `"info":{"url":"test-url","count":6,` },
565
+ {"since=" + strconv .FormatInt (currentTimestamp - 10 , 10 ), `"info":{"url":"test-url","count":6,` },
566
+ {"format=plain&since=" + strconv .FormatInt (currentTimestamp - 10 , 10 ), `"info":{"url":"test-url","count":6,` },
567
+ {"format=plain&since=" + strconv .FormatInt (currentTimestamp - 10 , 10 ), `"info":{"url":"test-url","count":6,` },
568
+ {"format=tree" , `"info":{"url":"test-url","count":6` },
569
+ {"format=tree&since=" + strconv .FormatInt (currentTimestamp - 10 , 10 ), `"info":{"url":"test-url","count":6` },
570
+ {"format=tree&sort=+time" , `"info":{"url":"test-url","count":6` },
571
+ {"format=tree&sort=-score" , `"info":{"url":"test-url","count":6` },
572
+ {"sort=+time" , `"info":{"url":"test-url","count":6` },
573
+ {"sort=-time" , `"info":{"url":"test-url","count":6` },
574
+ {"sort=+score" , `"info":{"url":"test-url","count":6` },
575
+ {"sort=-score" , `"info":{"url":"test-url","count":6` },
576
+ {"sort=+controversy" , `"info":{"url":"test-url","count":6` },
577
+ {"sort=-controversy" , `"info":{"url":"test-url","count":6` },
578
+ {"format=tree&sort=+time&since=" + strconv .FormatInt (currentTimestamp - 10 , 10 ), `"info":{"url":"test-url","count":6` },
579
+ {"format=tree&sort=-score&since=" + strconv .FormatInt (currentTimestamp - 100 , 10 ), `"info":{"url":"test-url","count":6` },
580
+ {"format=tree&sort=+controversy&since=" + strconv .FormatInt (currentTimestamp - 1000 , 10 ), `"info":{"url":"test-url","count":6` },
581
+ }
582
+
583
+ for _ , tc := range testCases {
584
+ t .Run (tc .params , func (t * testing.T ) {
585
+ url := fmt .Sprintf (ts .URL + "/api/v1/find?site=remark42&%s" , tc .params )
586
+ body , code := get (t , url )
587
+ assert .Equal (t , http .StatusOK , code )
588
+ assert .Contains (t , body , tc .expectedBody )
589
+ t .Log (body )
590
+ // prevent hit limiter from engaging
591
+ time .Sleep (30 * time .Millisecond )
592
+ })
593
+ }
594
+ }
595
+
518
596
func TestRest_UserInfo (t * testing.T ) {
519
597
ts , _ , teardown := startupT (t )
520
598
defer teardown ()
0 commit comments