18
18
package dereferencing_test
19
19
20
20
import (
21
+ "bytes"
21
22
"context"
22
23
"crypto/rsa"
23
24
"crypto/x509"
25
+ "encoding/json"
24
26
"encoding/pem"
25
27
"fmt"
28
+ "io"
29
+ "net/http"
26
30
"net/url"
27
31
"testing"
28
32
"time"
@@ -33,6 +37,7 @@ import (
33
37
"github.com/superseriousbusiness/gotosocial/internal/ap"
34
38
"github.com/superseriousbusiness/gotosocial/internal/config"
35
39
"github.com/superseriousbusiness/gotosocial/internal/db"
40
+ "github.com/superseriousbusiness/gotosocial/internal/federation/dereferencing"
36
41
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
37
42
"github.com/superseriousbusiness/gotosocial/testrig"
38
43
)
@@ -214,6 +219,111 @@ func (suite *AccountTestSuite) TestDereferenceLocalAccountWithUnknownUserURI() {
214
219
suite .Nil (fetchedAccount )
215
220
}
216
221
222
+ func (suite * AccountTestSuite ) TestDereferenceLocalAccountByRedirect () {
223
+ ctx , cncl := context .WithCancel (context .Background ())
224
+ defer cncl ()
225
+
226
+ fetchingAccount := suite .testAccounts ["local_account_1" ]
227
+ targetAccount := suite .testAccounts ["local_account_2" ]
228
+
229
+ // Convert the target account to ActivityStreams model for dereference.
230
+ targetAccountable , err := suite .converter .AccountToAS (ctx , targetAccount )
231
+ suite .NoError (err )
232
+ suite .NotNil (targetAccountable )
233
+
234
+ // Serialize to "raw" JSON map for response.
235
+ rawJSON , err := ap .Serialize (targetAccountable )
236
+ suite .NoError (err )
237
+
238
+ // Finally serialize to actual bytes.
239
+ json , err := json .Marshal (rawJSON )
240
+ suite .NoError (err )
241
+
242
+ // Replace test HTTP client with one that always returns the target account AS model.
243
+ suite .client = testrig .NewMockHTTPClient (func (req * http.Request ) (* http.Response , error ) {
244
+ return & http.Response {
245
+ Status : http .StatusText (http .StatusOK ),
246
+ StatusCode : http .StatusOK ,
247
+ ContentLength : int64 (len (json )),
248
+ Header : http.Header {"Content-Type" : {"application/activity+json" }},
249
+ Body : io .NopCloser (bytes .NewReader (json )),
250
+ Request : & http.Request {URL : testrig .URLMustParse (targetAccount .URI )},
251
+ }, nil
252
+ }, "" )
253
+
254
+ // Update dereferencer to use new test HTTP client.
255
+ suite .dereferencer = dereferencing .NewDereferencer (
256
+ & suite .state ,
257
+ suite .converter ,
258
+ testrig .NewTestTransportController (& suite .state , suite .client ),
259
+ suite .visFilter ,
260
+ suite .intFilter ,
261
+ suite .media ,
262
+ )
263
+
264
+ // Use any old input test URI, this doesn't actually matter what it is.
265
+ uri := testrig .URLMustParse ("https://this-will-be-redirected.butts/" )
266
+
267
+ // Try dereference the test URI, since it correctly redirects to us it should return our account.
268
+ account , accountable , err := suite .dereferencer .GetAccountByURI (ctx , fetchingAccount .Username , uri )
269
+ suite .NoError (err )
270
+ suite .Nil (accountable )
271
+ suite .NotNil (account )
272
+ suite .Equal (targetAccount .ID , account .ID )
273
+ }
274
+
275
+ func (suite * AccountTestSuite ) TestDereferenceMasqueradingLocalAccount () {
276
+ ctx , cncl := context .WithCancel (context .Background ())
277
+ defer cncl ()
278
+
279
+ fetchingAccount := suite .testAccounts ["local_account_1" ]
280
+ targetAccount := suite .testAccounts ["local_account_2" ]
281
+
282
+ // Convert the target account to ActivityStreams model for dereference.
283
+ targetAccountable , err := suite .converter .AccountToAS (ctx , targetAccount )
284
+ suite .NoError (err )
285
+ suite .NotNil (targetAccountable )
286
+
287
+ // Serialize to "raw" JSON map for response.
288
+ rawJSON , err := ap .Serialize (targetAccountable )
289
+ suite .NoError (err )
290
+
291
+ // Finally serialize to actual bytes.
292
+ json , err := json .Marshal (rawJSON )
293
+ suite .NoError (err )
294
+
295
+ // Use any old input test URI, this doesn't actually matter what it is.
296
+ uri := testrig .URLMustParse ("https://this-will-be-redirected.butts/" )
297
+
298
+ // Replace test HTTP client with one that returns OUR account, but at their URI endpoint.
299
+ suite .client = testrig .NewMockHTTPClient (func (req * http.Request ) (* http.Response , error ) {
300
+ return & http.Response {
301
+ Status : http .StatusText (http .StatusOK ),
302
+ StatusCode : http .StatusOK ,
303
+ ContentLength : int64 (len (json )),
304
+ Header : http.Header {"Content-Type" : {"application/activity+json" }},
305
+ Body : io .NopCloser (bytes .NewReader (json )),
306
+ Request : & http.Request {URL : uri },
307
+ }, nil
308
+ }, "" )
309
+
310
+ // Update dereferencer to use new test HTTP client.
311
+ suite .dereferencer = dereferencing .NewDereferencer (
312
+ & suite .state ,
313
+ suite .converter ,
314
+ testrig .NewTestTransportController (& suite .state , suite .client ),
315
+ suite .visFilter ,
316
+ suite .intFilter ,
317
+ suite .media ,
318
+ )
319
+
320
+ // Try dereference the test URI, since it correctly redirects to us it should return our account.
321
+ account , accountable , err := suite .dereferencer .GetAccountByURI (ctx , fetchingAccount .Username , uri )
322
+ suite .NotNil (err )
323
+ suite .Nil (account )
324
+ suite .Nil (accountable )
325
+ }
326
+
217
327
func (suite * AccountTestSuite ) TestDereferenceRemoteAccountWithNonMatchingURI () {
218
328
fetchingAccount := suite .testAccounts ["local_account_1" ]
219
329
0 commit comments