summaryrefslogtreecommitdiff
path: root/internal/ap/extractmentions_test.go
blob: e49347e5cb5e8c37a8131e548884a5e608af10d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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 (
	"testing"

	"github.com/stretchr/testify/suite"
	"github.com/superseriousbusiness/activity/streams"
	"github.com/superseriousbusiness/gotosocial/internal/ap"
	"github.com/superseriousbusiness/gotosocial/testrig"
)

type ExtractMentionsTestSuite struct {
	APTestSuite
}

func (suite *ExtractMentionsTestSuite) TestExtractMentionsFromNote() {
	note := suite.noteWithMentions1

	mentions, err := ap.ExtractMentions(note)
	suite.NoError(err)
	suite.Len(mentions, 2)

	m1 := mentions[0]
	suite.Equal("@dumpsterqueer@superseriousbusiness.org", m1.NameString)
	suite.Equal("https://gts.superseriousbusiness.org/users/dumpsterqueer", m1.TargetAccountURI)

	m2 := mentions[1]
	suite.Equal("@f0x@superseriousbusiness.org", m2.NameString)
	suite.Equal("https://gts.superseriousbusiness.org/users/f0x", m2.TargetAccountURI)
}

func (suite *ExtractMentionsTestSuite) TestExtractMentions() {
	newMention := func(nameString string, href string) ap.Mentionable {
		mention := streams.NewActivityStreamsMention()

		if nameString != "" {
			nameProp := streams.NewActivityStreamsNameProperty()
			nameProp.AppendXMLSchemaString(nameString)
			mention.SetActivityStreamsName(nameProp)
		}

		if href != "" {
			hrefProp := streams.NewActivityStreamsHrefProperty()
			hrefProp.SetIRI(testrig.URLMustParse(href))
			mention.SetActivityStreamsHref(hrefProp)
		}

		return mention
	}

	type test struct {
		nameString         string
		href               string
		expectedNameString string
		expectedHref       string
		expectedErr        string
	}

	for i, t := range []test{
		{
			// Mention with both Name and Href set, should be fine.
			nameString:         "@someone@example.org",
			href:               "https://example.org/@someone",
			expectedNameString: "@someone@example.org",
			expectedHref:       "https://example.org/@someone",
			expectedErr:        "",
		},
		{
			// Mention with just Href set, should be fine.
			nameString:         "",
			href:               "https://example.org/@someone",
			expectedNameString: "",
			expectedHref:       "https://example.org/@someone",
			expectedErr:        "",
		},
		{
			// Mention with just Name set, should be fine.
			nameString:         "@someone@example.org",
			href:               "",
			expectedNameString: "@someone@example.org",
			expectedHref:       "",
			expectedErr:        "",
		},
		{
			// Mention with nothing set, not fine!
			nameString:         "",
			href:               "",
			expectedNameString: "",
			expectedHref:       "",
			expectedErr:        "ExtractMention: neither Name nor Href were set",
		},
	} {
		apMention := newMention(t.nameString, t.href)
		mention, err := ap.ExtractMention(apMention)

		if err != nil {
			if errString := err.Error(); errString != t.expectedErr {
				suite.Fail("",
					"test %d expected error %s, got %s",
					i+1, t.expectedErr, errString,
				)
			}
			continue
		} else if t.expectedErr != "" {
			suite.Fail("",
				"test %d expected error %s, got no error",
				i+1, t.expectedErr,
			)
		}

		if mention.NameString != t.expectedNameString {
			suite.Fail("",
				"test %d expected nameString %s, got %s",
				i+1, t.expectedNameString, mention.NameString,
			)
		}

		if mention.TargetAccountURI != t.expectedHref {
			suite.Fail("",
				"test %d expected href %s, got %s",
				i+1, t.expectedHref, mention.TargetAccountURI,
			)
		}
	}
}

func TestExtractMentionsTestSuite(t *testing.T) {
	suite.Run(t, &ExtractMentionsTestSuite{})
}