2023-03-12 15:00:57 +00:00
|
|
|
// 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/>.
|
2022-06-11 10:01:34 +01:00
|
|
|
|
|
|
|
package util_test
|
|
|
|
|
|
|
|
import (
|
2023-09-11 17:38:31 +01:00
|
|
|
"net/url"
|
2022-06-11 10:01:34 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamestringSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
2023-09-11 17:38:31 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-11 10:01:34 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:38:31 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-11 10:01:34 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 17:38:31 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-11 10:01:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNamestringSuite(t *testing.T) {
|
|
|
|
suite.Run(t, &NamestringSuite{})
|
|
|
|
}
|