namestring_test.go (3646B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package util_test 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/suite" 24 "github.com/superseriousbusiness/gotosocial/internal/util" 25 ) 26 27 type NamestringSuite struct { 28 suite.Suite 29 } 30 31 func (suite *NamestringSuite) TestExtractWebfingerParts1() { 32 webfinger := "acct:stonerkitty.monster@stonerkitty.monster" 33 username, host, err := util.ExtractWebfingerParts(webfinger) 34 suite.NoError(err) 35 36 suite.Equal("stonerkitty.monster", username) 37 suite.Equal("stonerkitty.monster", host) 38 } 39 40 func (suite *NamestringSuite) TestExtractWebfingerParts2() { 41 webfinger := "@stonerkitty.monster@stonerkitty.monster" 42 username, host, err := util.ExtractWebfingerParts(webfinger) 43 suite.NoError(err) 44 45 suite.Equal("stonerkitty.monster", username) 46 suite.Equal("stonerkitty.monster", host) 47 } 48 49 func (suite *NamestringSuite) TestExtractWebfingerParts3() { 50 webfinger := "acct:someone@somewhere" 51 username, host, err := util.ExtractWebfingerParts(webfinger) 52 suite.NoError(err) 53 54 suite.Equal("someone", username) 55 suite.Equal("somewhere", host) 56 } 57 58 func (suite *NamestringSuite) TestExtractWebfingerParts4() { 59 webfinger := "@stoner-kitty.monster@stonerkitty.monster" 60 username, host, err := util.ExtractWebfingerParts(webfinger) 61 suite.NoError(err) 62 63 suite.Equal("stoner-kitty.monster", username) 64 suite.Equal("stonerkitty.monster", host) 65 } 66 67 func (suite *NamestringSuite) TestExtractWebfingerParts5() { 68 webfinger := "@stonerkitty.monster" 69 username, host, err := util.ExtractWebfingerParts(webfinger) 70 suite.NoError(err) 71 72 suite.Equal("stonerkitty.monster", username) 73 suite.Empty(host) 74 } 75 76 func (suite *NamestringSuite) TestExtractWebfingerParts6() { 77 webfinger := "@@stonerkitty.monster" 78 _, _, err := util.ExtractWebfingerParts(webfinger) 79 suite.EqualError(err, "couldn't match mention @@stonerkitty.monster") 80 } 81 82 func (suite *NamestringSuite) TestExtractNamestringParts1() { 83 namestring := "@stonerkitty.monster@stonerkitty.monster" 84 username, host, err := util.ExtractNamestringParts(namestring) 85 suite.NoError(err) 86 87 suite.Equal("stonerkitty.monster", username) 88 suite.Equal("stonerkitty.monster", host) 89 } 90 91 func (suite *NamestringSuite) TestExtractNamestringParts2() { 92 namestring := "@stonerkitty.monster" 93 username, host, err := util.ExtractNamestringParts(namestring) 94 suite.NoError(err) 95 96 suite.Equal("stonerkitty.monster", username) 97 suite.Empty(host) 98 } 99 100 func (suite *NamestringSuite) TestExtractNamestringParts3() { 101 namestring := "@someone@somewhere" 102 username, host, err := util.ExtractWebfingerParts(namestring) 103 suite.NoError(err) 104 105 suite.Equal("someone", username) 106 suite.Equal("somewhere", host) 107 } 108 109 func (suite *NamestringSuite) TestExtractNamestringParts4() { 110 namestring := "" 111 _, _, err := util.ExtractNamestringParts(namestring) 112 suite.EqualError(err, "couldn't match mention ") 113 } 114 115 func TestNamestringSuite(t *testing.T) { 116 suite.Run(t, &NamestringSuite{}) 117 }