account_test.go (7296B)
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 dereferencing_test 19 20 import ( 21 "context" 22 "testing" 23 "time" 24 25 "github.com/stretchr/testify/suite" 26 "github.com/superseriousbusiness/gotosocial/internal/ap" 27 "github.com/superseriousbusiness/gotosocial/internal/config" 28 "github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing" 29 "github.com/superseriousbusiness/gotosocial/testrig" 30 ) 31 32 type AccountTestSuite struct { 33 DereferencerStandardTestSuite 34 } 35 36 func (suite *AccountTestSuite) TestDereferenceGroup() { 37 fetchingAccount := suite.testAccounts["local_account_1"] 38 39 groupURL := testrig.URLMustParse("https://unknown-instance.com/groups/some_group") 40 group, _, err := suite.dereferencer.GetAccountByURI( 41 context.Background(), 42 fetchingAccount.Username, 43 groupURL, 44 ) 45 suite.NoError(err) 46 suite.NotNil(group) 47 48 // group values should be set 49 suite.Equal("https://unknown-instance.com/groups/some_group", group.URI) 50 suite.Equal("https://unknown-instance.com/@some_group", group.URL) 51 suite.WithinDuration(time.Now(), group.FetchedAt, 5*time.Second) 52 53 // group should be in the database 54 dbGroup, err := suite.db.GetAccountByURI(context.Background(), group.URI) 55 suite.NoError(err) 56 suite.Equal(group.ID, dbGroup.ID) 57 suite.Equal(ap.ActorGroup, dbGroup.ActorType) 58 } 59 60 func (suite *AccountTestSuite) TestDereferenceService() { 61 fetchingAccount := suite.testAccounts["local_account_1"] 62 63 serviceURL := testrig.URLMustParse("https://owncast.example.org/federation/user/rgh") 64 service, _, err := suite.dereferencer.GetAccountByURI( 65 context.Background(), 66 fetchingAccount.Username, 67 serviceURL, 68 ) 69 suite.NoError(err) 70 suite.NotNil(service) 71 72 // service values should be set 73 suite.Equal("https://owncast.example.org/federation/user/rgh", service.URI) 74 suite.Equal("https://owncast.example.org/federation/user/rgh", service.URL) 75 suite.WithinDuration(time.Now(), service.FetchedAt, 5*time.Second) 76 77 // service should be in the database 78 dbService, err := suite.db.GetAccountByURI(context.Background(), service.URI) 79 suite.NoError(err) 80 suite.Equal(service.ID, dbService.ID) 81 suite.Equal(ap.ActorService, dbService.ActorType) 82 suite.Equal("example.org", dbService.Domain) 83 } 84 85 /* 86 We shouldn't try webfingering or making http calls to dereference local accounts 87 that might be passed into GetRemoteAccount for whatever reason, so these tests are 88 here to make sure that such cases are (basically) short-circuit evaluated and given 89 back as-is without trying to make any calls to one's own instance. 90 */ 91 92 func (suite *AccountTestSuite) TestDereferenceLocalAccountAsRemoteURL() { 93 fetchingAccount := suite.testAccounts["local_account_1"] 94 targetAccount := suite.testAccounts["local_account_2"] 95 96 fetchedAccount, _, err := suite.dereferencer.GetAccountByURI( 97 context.Background(), 98 fetchingAccount.Username, 99 testrig.URLMustParse(targetAccount.URI), 100 ) 101 suite.NoError(err) 102 suite.NotNil(fetchedAccount) 103 suite.Empty(fetchedAccount.Domain) 104 } 105 106 func (suite *AccountTestSuite) TestDereferenceLocalAccountAsRemoteURLNoSharedInboxYet() { 107 fetchingAccount := suite.testAccounts["local_account_1"] 108 targetAccount := suite.testAccounts["local_account_2"] 109 110 targetAccount.SharedInboxURI = nil 111 if err := suite.db.UpdateAccount(context.Background(), targetAccount); err != nil { 112 suite.FailNow(err.Error()) 113 } 114 115 fetchedAccount, _, err := suite.dereferencer.GetAccountByURI( 116 context.Background(), 117 fetchingAccount.Username, 118 testrig.URLMustParse(targetAccount.URI), 119 ) 120 suite.NoError(err) 121 suite.NotNil(fetchedAccount) 122 suite.Empty(fetchedAccount.Domain) 123 } 124 125 func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsername() { 126 fetchingAccount := suite.testAccounts["local_account_1"] 127 targetAccount := suite.testAccounts["local_account_2"] 128 129 fetchedAccount, _, err := suite.dereferencer.GetAccountByURI( 130 context.Background(), 131 fetchingAccount.Username, 132 testrig.URLMustParse(targetAccount.URI), 133 ) 134 suite.NoError(err) 135 suite.NotNil(fetchedAccount) 136 suite.Empty(fetchedAccount.Domain) 137 } 138 139 func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsernameDomain() { 140 fetchingAccount := suite.testAccounts["local_account_1"] 141 targetAccount := suite.testAccounts["local_account_2"] 142 143 fetchedAccount, _, err := suite.dereferencer.GetAccountByURI( 144 context.Background(), 145 fetchingAccount.Username, 146 testrig.URLMustParse(targetAccount.URI), 147 ) 148 suite.NoError(err) 149 suite.NotNil(fetchedAccount) 150 suite.Empty(fetchedAccount.Domain) 151 } 152 153 func (suite *AccountTestSuite) TestDereferenceLocalAccountAsUsernameDomainAndURL() { 154 fetchingAccount := suite.testAccounts["local_account_1"] 155 targetAccount := suite.testAccounts["local_account_2"] 156 157 fetchedAccount, _, err := suite.dereferencer.GetAccountByUsernameDomain( 158 context.Background(), 159 fetchingAccount.Username, 160 targetAccount.Username, 161 config.GetHost(), 162 ) 163 suite.NoError(err) 164 suite.NotNil(fetchedAccount) 165 suite.Empty(fetchedAccount.Domain) 166 } 167 168 func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsername() { 169 fetchingAccount := suite.testAccounts["local_account_1"] 170 171 fetchedAccount, _, err := suite.dereferencer.GetAccountByUsernameDomain( 172 context.Background(), 173 fetchingAccount.Username, 174 "thisaccountdoesnotexist", 175 config.GetHost(), 176 ) 177 var errNotRetrievable *dereferencing.ErrNotRetrievable 178 suite.ErrorAs(err, &errNotRetrievable) 179 suite.EqualError(err, "item could not be retrieved: no entries") 180 suite.Nil(fetchedAccount) 181 } 182 183 func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUsernameDomain() { 184 fetchingAccount := suite.testAccounts["local_account_1"] 185 186 fetchedAccount, _, err := suite.dereferencer.GetAccountByUsernameDomain( 187 context.Background(), 188 fetchingAccount.Username, 189 "thisaccountdoesnotexist", 190 "localhost:8080", 191 ) 192 var errNotRetrievable *dereferencing.ErrNotRetrievable 193 suite.ErrorAs(err, &errNotRetrievable) 194 suite.EqualError(err, "item could not be retrieved: no entries") 195 suite.Nil(fetchedAccount) 196 } 197 198 func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUserURI() { 199 fetchingAccount := suite.testAccounts["local_account_1"] 200 201 fetchedAccount, _, err := suite.dereferencer.GetAccountByURI( 202 context.Background(), 203 fetchingAccount.Username, 204 testrig.URLMustParse("http://localhost:8080/users/thisaccountdoesnotexist"), 205 ) 206 var errNotRetrievable *dereferencing.ErrNotRetrievable 207 suite.ErrorAs(err, &errNotRetrievable) 208 suite.EqualError(err, "item could not be retrieved: no entries") 209 suite.Nil(fetchedAccount) 210 } 211 212 func TestAccountTestSuite(t *testing.T) { 213 suite.Run(t, new(AccountTestSuite)) 214 }