gotosocial/internal/ap/extractattachments_test.go
tobi 32738d83a8 [bugfix] Update GetURL to extract url from Link objects with href (#4249)
# Description

> If this is a code change, please include a summary of what you've coded, and link to the issue(s) it closes/implements.
>
> If this is a documentation change, please briefly describe what you've changed and why.

This pull request updates our parsing of the `url` property in incoming ActivityPub items to also include Link items, and not just bare URIs.

The first discovered url is still used as the *gtsmodel.Account or *gtsmodel.Status `url` property, so this change only really affects our dereference URL anti-spoof check thingy.

~~Should fix https://codeberg.org/superseriousbusiness/gotosocial/issues/4248 but I need to run it and test it myself first to be sure.~~

Fixes https://codeberg.org/superseriousbusiness/gotosocial/issues/4248

## Checklist

Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]`

If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want).

- [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md).
- [x] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat.
- [x] I/we have not leveraged AI to create the proposed changes.
- [x] I/we have performed a self-review of added code.
- [x] I/we have written code that is legible and maintainable by others.
- [x] I/we have commented the added code, particularly in hard-to-understand areas.
- [ ] I/we have made any necessary changes to documentation.
- [x] I/we have added tests that cover new code.
- [x] I/we have run tests and they pass locally with the changes.
- [x] I/we have run `go fmt ./...` and `golangci-lint run`.

Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4249
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Co-committed-by: tobi <tobi.smethurst@protonmail.com>
2025-06-09 12:32:00 +02:00

81 lines
2.5 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 (
"encoding/json"
"testing"
"code.superseriousbusiness.org/activity/streams"
"code.superseriousbusiness.org/gotosocial/internal/ap"
"github.com/stretchr/testify/suite"
)
type ExtractAttachmentsTestSuite struct {
APTestSuite
}
func (suite *ExtractAttachmentsTestSuite) TestExtractAttachmentMissingURL() {
d1 := suite.document1
d1.SetActivityStreamsUrl(streams.NewActivityStreamsUrlProperty())
attachment, err := ap.ExtractAttachment(d1)
suite.EqualError(err, "ExtractAttachment: empty attachment URL")
suite.Nil(attachment)
}
func (suite *ExtractAttachmentsTestSuite) TestExtractDescription() {
// Note: normally a single attachment on a Note or
// similar wouldn't have the `@context` field set,
// but we set it here because we're parsing it as
// a discrete/standalone AP Object for this test.
attachmentableJSON := `{
"@context": "https://www.w3.org/ns/activitystreams",
"mediaType": "image/jpeg",
"name": "z64KTcw2h2bZ8s67k2.jpg",
"summary": "A very large panel that is entirely twist switches",
"type": "Document",
"url": "https://example.org/d/XzKw4M2Sc1pBxj3hY4.jpg"
}`
raw := make(map[string]interface{})
if err := json.Unmarshal([]byte(attachmentableJSON), &raw); err != nil {
suite.FailNow(err.Error())
}
t, err := streams.ToType(suite.T().Context(), raw)
if err != nil {
suite.FailNow(err.Error())
}
attachmentable, ok := t.(ap.Attachmentable)
if !ok {
suite.FailNow("type was not Attachmentable")
}
attachment, err := ap.ExtractAttachment(attachmentable)
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("A very large panel that is entirely twist switches", attachment.Description)
}
func TestExtractAttachmentsTestSuite(t *testing.T) {
suite.Run(t, &ExtractAttachmentsTestSuite{})
}