mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-12-23 02:32:12 +00:00
2cac5a4613
* [feature] Support Actor URIs for webfinger queries It's now possible to pass an Actor URI as the resource to query for when doing a webfinger query. The code now extracts the username and domain from the URI. The URI needs to be fully qualified, including having a scheme of http or https to be recognised as such. The acct scheme is handled as we used to, including dealing with an erroneous leading @ on the username. We retain the ability to handle resources without a scheme by parsing them again with the acct scheme if the original parse failed. This can happen due to parsing ambiguities when dealing with a string like user@domain.tld:port. * [bugfix] Remove debugging changes * [chore] Make TestExtractNamestring table-driven * [chore] Unnest Trim and Split for readability
132 lines
5.8 KiB
Go
132 lines
5.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 util_test
|
|
|
|
import (
|
|
"net/url"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
)
|
|
|
|
type NamestringSuite struct {
|
|
suite.Suite
|
|
}
|
|
|
|
func (suite *NamestringSuite) TestExtractWebfingerParts() {
|
|
tests := []struct {
|
|
in, username, domain, err string
|
|
}{
|
|
{in: "acct:stonerkitty.monster@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "acct:stonerkitty.monster@stonerkitty.monster:8080", username: "stonerkitty.monster", domain: "stonerkitty.monster:8080"},
|
|
{in: "acct:@stonerkitty.monster@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "stonerkitty.monster@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "stonerkitty.monster@stonerkitty.monster:8080", username: "stonerkitty.monster", domain: "stonerkitty.monster:8080"},
|
|
{in: "@stonerkitty.monster@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "acct:@@stonerkitty.monster@stonerkitty.monster", err: "failed to extract user and domain from: acct:@@stonerkitty.monster@stonerkitty.monster"},
|
|
{in: "acct:@stonerkitty.monster@@stonerkitty.monster", err: "failed to extract user and domain from: acct:@stonerkitty.monster@@stonerkitty.monster"},
|
|
{in: "@@stonerkitty.monster@stonerkitty.monster", err: "failed to extract user and domain from: @@stonerkitty.monster@stonerkitty.monster"},
|
|
{in: "@stonerkitty.monster@@stonerkitty.monster", err: "failed to extract user and domain from: @stonerkitty.monster@@stonerkitty.monster"},
|
|
{in: "s3:stonerkitty.monster@stonerkitty.monster", err: "unsupported scheme: s3 for resource: s3:stonerkitty.monster@stonerkitty.monster"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
suite.Run(tt.in, func() {
|
|
suite.T().Parallel()
|
|
username, domain, err := util.ExtractWebfingerParts(tt.in)
|
|
if tt.err == "" {
|
|
suite.NoError(err)
|
|
suite.Equal(tt.username, username)
|
|
suite.Equal(tt.domain, domain)
|
|
} else {
|
|
suite.EqualError(err, tt.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *NamestringSuite) TestExtractWebfingerPartsFromURI() {
|
|
tests := []struct {
|
|
in, username, domain, err string
|
|
}{
|
|
{in: "https://stonerkitty.monster/users/stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster/users/@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster/@stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster/@@stonerkitty.monster", username: "@stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster:8080/users/stonerkitty.monster", username: "stonerkitty.monster", domain: "stonerkitty.monster:8080"},
|
|
{in: "https://stonerkitty.monster/users/stonerkitty.monster/evil", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster/@stonerkitty.monster/evil", username: "stonerkitty.monster", domain: "stonerkitty.monster"},
|
|
{in: "/@stonerkitty.monster", err: "failed to extract domain from: /@stonerkitty.monster"},
|
|
{in: "/users/stonerkitty.monster", err: "failed to extract domain from: /users/stonerkitty.monster"},
|
|
{in: "@stonerkitty.monster", err: "failed to extract domain from: @stonerkitty.monster"},
|
|
{in: "users/stonerkitty.monster", err: "failed to extract domain from: users/stonerkitty.monster"},
|
|
{in: "https://stonerkitty.monster/users/", err: "failed to extract username from: https://stonerkitty.monster/users/"},
|
|
{in: "https://stonerkitty.monster/users/@", err: "failed to extract username from: https://stonerkitty.monster/users/@"},
|
|
{in: "https://stonerkitty.monster/@", err: "failed to extract username from: https://stonerkitty.monster/@"},
|
|
{in: "https://stonerkitty.monster/", err: "failed to extract username from: https://stonerkitty.monster/"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
suite.Run(tt.in, func() {
|
|
suite.T().Parallel()
|
|
uri, _ := url.Parse(tt.in)
|
|
username, domain, err := util.ExtractWebfingerPartsFromURI(uri)
|
|
if tt.err == "" {
|
|
suite.NoError(err)
|
|
suite.Equal(tt.username, username)
|
|
suite.Equal(tt.domain, domain)
|
|
} else {
|
|
suite.EqualError(err, tt.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (suite *NamestringSuite) TestExtractNamestring() {
|
|
tests := []struct {
|
|
in, username, host, err string
|
|
}{
|
|
{in: "@stonerkitty.monster@stonerkitty.monster", username: "stonerkitty.monster", host: "stonerkitty.monster"},
|
|
{in: "@stonerkitty.monster", username: "stonerkitty.monster"},
|
|
{in: "@someone@somewhere", username: "someone", host: "somewhere"},
|
|
{in: "", err: "couldn't match mention "},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
suite.Run(tt.in, func() {
|
|
suite.T().Parallel()
|
|
username, host, err := util.ExtractNamestringParts(tt.in)
|
|
if tt.err != "" {
|
|
suite.EqualError(err, tt.err)
|
|
} else {
|
|
suite.NoError(err)
|
|
suite.Equal(tt.username, username)
|
|
suite.Equal(tt.host, host)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNamestringSuite(t *testing.T) {
|
|
suite.Run(t, &NamestringSuite{})
|
|
}
|