GoToSocial/internal/ap/collections_test.go
kim 1d51e3c8d6
[bugfix] 2643 bug search for account url doesnt always work when redirected (#2673)
* update activity library so dereferencer returns full response and checks *final* link to allow for redirects

* temporarily add bodged fixed library

* remove unused code

* update getAccountFeatured() to use dereferenceCollectionPage()

* make sure to release map

* perform a 2nd decode to ensure reader is empty after primary decode

* add comment explaining choice of using Decode() instead of Unmarshal()

* update embedded activity library to latest matching https://github.com/superseriousbusiness/activity/pull/21

* add checks to look for changed URI and re-check database if redirected

* update max iteration count to 512, add checks during dereferenceAncestors() for indirect URLs

* remove doubled-up code

* fix use of status instead of current

* use URIs for checking equality for security

* use the latest known URI for boost_of_uri in case original was an indirect

* add dereferenceCollection() function for dereferenceAccountFeatured()

* pull in latest github.com/superseriousbusiness/activity version (and remove the bodge!!)

* fix typo in code comments

* update decodeType() to accept a readcloser and handle body closing

* switch to checking using BoostOfID and add note why not using BoostOfURI

* ensure InReplyTo gets unset when deleting status parent in case currently stubbed

* add tests for Collection and CollectionPage iterators
2024-02-23 16:24:40 +01:00

149 lines
4.8 KiB
Go

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ap_test
import (
"net/url"
"slices"
"testing"
"github.com/superseriousbusiness/activity/pub"
"github.com/superseriousbusiness/activity/streams"
"github.com/superseriousbusiness/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/ap"
)
var testIteratorIRIs = [][]string{
{
"https://google.com",
"https://mastodon.social",
"http://naughty.naughty.website/heres/the/porn",
"https://god.monarchies.suck?yes=they&really=do",
},
{
// zero length
},
{
"https://superseriousbusiness.org",
"http://gotosocial.tv/@slothsgonewild",
},
}
func TestToCollectionIterator(t *testing.T) {
for _, iris := range testIteratorIRIs {
testToCollectionIterator(t, toCollection(iris), "", iris)
testToCollectionIterator(t, toOrderedCollection(iris), "", iris)
}
testToCollectionIterator(t, streams.NewActivityStreamsAdd(), "*typeadd.ActivityStreamsAdd(Add) was not Collection-like", nil)
testToCollectionIterator(t, streams.NewActivityStreamsBlock(), "*typeblock.ActivityStreamsBlock(Block) was not Collection-like", nil)
}
func TestToCollectionPageIterator(t *testing.T) {
for _, iris := range testIteratorIRIs {
testToCollectionPageIterator(t, toCollectionPage(iris), "", iris)
testToCollectionPageIterator(t, toOrderedCollectionPage(iris), "", iris)
}
testToCollectionPageIterator(t, streams.NewActivityStreamsAdd(), "*typeadd.ActivityStreamsAdd(Add) was not CollectionPage-like", nil)
testToCollectionPageIterator(t, streams.NewActivityStreamsBlock(), "*typeblock.ActivityStreamsBlock(Block) was not CollectionPage-like", nil)
}
func testToCollectionIterator(t *testing.T, in vocab.Type, expectErr string, expectIRIs []string) {
collect, err := ap.ToCollectionIterator(in)
if !errCheck(err, expectErr) {
t.Fatalf("did not return expected error: expect=%v receive=%v", expectErr, err)
}
iris := gatherFromIterator(collect)
if !slices.Equal(iris, expectIRIs) {
t.Fatalf("did not return expected iris: expect=%v receive=%v", expectIRIs, iris)
}
}
func testToCollectionPageIterator(t *testing.T, in vocab.Type, expectErr string, expectIRIs []string) {
page, err := ap.ToCollectionPageIterator(in)
if !errCheck(err, expectErr) {
t.Fatalf("did not return expected error: expect=%v receive=%v", expectErr, err)
}
iris := gatherFromIterator(page)
if !slices.Equal(iris, expectIRIs) {
t.Fatalf("did not return expected iris: expect=%v receive=%v", expectIRIs, iris)
}
}
func toCollection(iris []string) vocab.ActivityStreamsCollection {
collect := streams.NewActivityStreamsCollection()
collect.SetActivityStreamsItems(toItems(iris))
return collect
}
func toOrderedCollection(iris []string) vocab.ActivityStreamsOrderedCollection {
collect := streams.NewActivityStreamsOrderedCollection()
collect.SetActivityStreamsOrderedItems(toOrderedItems(iris))
return collect
}
func toCollectionPage(iris []string) vocab.ActivityStreamsCollectionPage {
page := streams.NewActivityStreamsCollectionPage()
page.SetActivityStreamsItems(toItems(iris))
return page
}
func toOrderedCollectionPage(iris []string) vocab.ActivityStreamsOrderedCollectionPage {
page := streams.NewActivityStreamsOrderedCollectionPage()
page.SetActivityStreamsOrderedItems(toOrderedItems(iris))
return page
}
func toItems(iris []string) vocab.ActivityStreamsItemsProperty {
items := streams.NewActivityStreamsItemsProperty()
for _, iri := range iris {
u, _ := url.Parse(iri)
items.AppendIRI(u)
}
return items
}
func toOrderedItems(iris []string) vocab.ActivityStreamsOrderedItemsProperty {
items := streams.NewActivityStreamsOrderedItemsProperty()
for _, iri := range iris {
u, _ := url.Parse(iri)
items.AppendIRI(u)
}
return items
}
func gatherFromIterator(iter ap.CollectionIterator) []string {
var iris []string
if iter == nil {
return nil
}
for item := iter.NextItem(); item != nil; item = iter.NextItem() {
id, _ := pub.ToId(item)
if id != nil {
iris = append(iris, id.String())
}
}
return iris
}
func errCheck(err error, str string) bool {
if err == nil {
return str == ""
}
return err.Error() == str
}