gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

finger_test.go (4277B)


      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 transport_test
     19 
     20 import (
     21 	"context"
     22 	"testing"
     23 
     24 	"github.com/stretchr/testify/suite"
     25 )
     26 
     27 type FingerTestSuite struct {
     28 	TransportTestSuite
     29 }
     30 
     31 func (suite *FingerTestSuite) TestFinger() {
     32 	wc := suite.state.Caches.GTS.Webfinger()
     33 	suite.Equal(0, wc.Len(), "expect webfinger cache to be empty")
     34 
     35 	_, err := suite.transport.Finger(context.TODO(), "brand_new_person", "unknown-instance.com")
     36 	if err != nil {
     37 		suite.FailNow(err.Error())
     38 	}
     39 
     40 	suite.Equal(0, wc.Len(), "expect webfinger cache to be empty for normal webfinger request")
     41 }
     42 
     43 func (suite *FingerTestSuite) TestFingerWithHostMeta() {
     44 	wc := suite.state.Caches.GTS.Webfinger()
     45 	suite.Equal(0, wc.Len(), "expect webfinger cache to be empty")
     46 
     47 	_, err := suite.transport.Finger(context.TODO(), "someone", "misconfigured-instance.com")
     48 	if err != nil {
     49 		suite.FailNow(err.Error())
     50 	}
     51 
     52 	suite.Equal(1, wc.Len(), "expect webfinger cache to hold one entry")
     53 	suite.True(wc.Has("misconfigured-instance.com"), "expect webfinger cache to have entry for misconfigured-instance.com")
     54 }
     55 
     56 func (suite *FingerTestSuite) TestFingerWithHostMetaCacheStrategy() {
     57 	wc := suite.state.Caches.GTS.Webfinger()
     58 	suite.Equal(0, wc.Len(), "expect webfinger cache to be empty")
     59 
     60 	_, err := suite.transport.Finger(context.TODO(), "someone", "misconfigured-instance.com")
     61 	if err != nil {
     62 		suite.FailNow(err.Error())
     63 	}
     64 
     65 	suite.Equal(1, wc.Len(), "expect webfinger cache to hold one entry")
     66 	wc.Lock()
     67 	suite.True(wc.Cache.Has("misconfigured-instance.com"), "expect webfinger cache to have entry for misconfigured-instance.com")
     68 	ent, _ := wc.Cache.Get("misconfigured-instance.com")
     69 	wc.Unlock()
     70 
     71 	initialTime := ent.Expiry
     72 
     73 	// finger them again
     74 	_, err = suite.transport.Finger(context.TODO(), "someone", "misconfigured-instance.com")
     75 	if err != nil {
     76 		suite.FailNow(err.Error())
     77 	}
     78 
     79 	// there should still only be 1 cache entry
     80 	suite.Equal(1, wc.Len(), "expect webfinger cache to hold one entry")
     81 	wc.Lock()
     82 	suite.True(wc.Cache.Has("misconfigured-instance.com"), "expect webfinger cache to have entry for misconfigured-instance.com")
     83 	rep, _ := wc.Cache.Get("misconfigured-instance.com")
     84 	wc.Unlock()
     85 
     86 	repeatTime := rep.Expiry
     87 
     88 	// the TTL of the entry should have extended because we did a second
     89 	// successful finger
     90 	if repeatTime == initialTime {
     91 		suite.FailNowf("expected webfinger cache entry to have different expiry times", "initial: '%s', repeat: '%s'", initialTime, repeatTime)
     92 	} else if repeatTime < initialTime {
     93 		suite.FailNowf("expected webfinger cache entry to not be a time traveller", "initial: '%s', repeat: '%s'", initialTime, repeatTime)
     94 	}
     95 
     96 	// finger a non-existing user on that same instance which will return an error
     97 	_, err = suite.transport.Finger(context.TODO(), "invalid", "misconfigured-instance.com")
     98 	if err == nil {
     99 		suite.FailNow("expected request for invalid user to fail")
    100 	}
    101 
    102 	// there should still only be 1 cache entry, because we don't evict from cache on failure
    103 	suite.Equal(1, wc.Len(), "expect webfinger cache to hold one entry")
    104 	wc.Lock()
    105 	suite.True(wc.Cache.Has("misconfigured-instance.com"), "expect webfinger cache to have entry for misconfigured-instance.com")
    106 	last, _ := wc.Cache.Get("misconfigured-instance.com")
    107 	wc.Unlock()
    108 
    109 	lastTime := last.Expiry
    110 
    111 	// The TTL of the previous and new entry should be the same since
    112 	// a failed request must not extend the entry TTL
    113 	suite.Equal(repeatTime, lastTime)
    114 }
    115 
    116 func TestFingerTestSuite(t *testing.T) {
    117 	suite.Run(t, &FingerTestSuite{})
    118 }