gtsocial-umbx

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

followrequest_test.go (5714B)


      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 processing_test
     19 
     20 import (
     21 	"context"
     22 	"encoding/json"
     23 	"fmt"
     24 	"testing"
     25 	"time"
     26 
     27 	"github.com/stretchr/testify/suite"
     28 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     29 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     30 	"github.com/superseriousbusiness/gotosocial/testrig"
     31 )
     32 
     33 type FollowRequestTestSuite struct {
     34 	ProcessingStandardTestSuite
     35 }
     36 
     37 func (suite *FollowRequestTestSuite) TestFollowRequestAccept() {
     38 	requestingAccount := suite.testAccounts["remote_account_2"]
     39 	targetAccount := suite.testAccounts["local_account_1"]
     40 
     41 	// put a follow request in the database
     42 	fr := &gtsmodel.FollowRequest{
     43 		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3",
     44 		CreatedAt:       time.Now(),
     45 		UpdatedAt:       time.Now(),
     46 		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI),
     47 		AccountID:       requestingAccount.ID,
     48 		TargetAccountID: targetAccount.ID,
     49 	}
     50 
     51 	err := suite.db.Put(context.Background(), fr)
     52 	suite.NoError(err)
     53 
     54 	relationship, errWithCode := suite.processor.FollowRequestAccept(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID)
     55 	suite.NoError(errWithCode)
     56 	suite.EqualValues(&apimodel.Relationship{ID: "01FHMQX3GAABWSM0S2VZEC2SWC", Following: false, ShowingReblogs: false, Notifying: false, FollowedBy: true, Blocking: false, BlockedBy: false, Muting: false, MutingNotifications: false, Requested: false, DomainBlocking: false, Endorsed: false, Note: ""}, relationship)
     57 
     58 	// accept should be sent to Some_User
     59 	var sent [][]byte
     60 	if !testrig.WaitFor(func() bool {
     61 		sentI, ok := suite.httpClient.SentMessages.Load(requestingAccount.InboxURI)
     62 		if ok {
     63 			sent, ok = sentI.([][]byte)
     64 			if !ok {
     65 				panic("SentMessages entry was not []byte")
     66 			}
     67 			return true
     68 		}
     69 		return false
     70 	}) {
     71 		suite.FailNow("timed out waiting for message")
     72 	}
     73 
     74 	accept := &struct {
     75 		Actor  string `json:"actor"`
     76 		ID     string `json:"id"`
     77 		Object struct {
     78 			Actor  string `json:"actor"`
     79 			ID     string `json:"id"`
     80 			Object string `json:"object"`
     81 			To     string `json:"to"`
     82 			Type   string `json:"type"`
     83 		}
     84 		To   string `json:"to"`
     85 		Type string `json:"type"`
     86 	}{}
     87 	err = json.Unmarshal(sent[0], accept)
     88 	suite.NoError(err)
     89 
     90 	suite.Equal(targetAccount.URI, accept.Actor)
     91 	suite.Equal(requestingAccount.URI, accept.Object.Actor)
     92 	suite.Equal(fr.URI, accept.Object.ID)
     93 	suite.Equal(targetAccount.URI, accept.Object.Object)
     94 	suite.Equal(targetAccount.URI, accept.Object.To)
     95 	suite.Equal("Follow", accept.Object.Type)
     96 	suite.Equal(requestingAccount.URI, accept.To)
     97 	suite.Equal("Accept", accept.Type)
     98 }
     99 
    100 func (suite *FollowRequestTestSuite) TestFollowRequestReject() {
    101 	requestingAccount := suite.testAccounts["remote_account_2"]
    102 	targetAccount := suite.testAccounts["local_account_1"]
    103 
    104 	// put a follow request in the database
    105 	fr := &gtsmodel.FollowRequest{
    106 		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3",
    107 		CreatedAt:       time.Now(),
    108 		UpdatedAt:       time.Now(),
    109 		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI),
    110 		AccountID:       requestingAccount.ID,
    111 		TargetAccountID: targetAccount.ID,
    112 	}
    113 
    114 	err := suite.db.Put(context.Background(), fr)
    115 	suite.NoError(err)
    116 
    117 	relationship, errWithCode := suite.processor.FollowRequestReject(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID)
    118 	suite.NoError(errWithCode)
    119 	suite.EqualValues(&apimodel.Relationship{ID: "01FHMQX3GAABWSM0S2VZEC2SWC", Following: false, ShowingReblogs: false, Notifying: false, FollowedBy: false, Blocking: false, BlockedBy: false, Muting: false, MutingNotifications: false, Requested: false, DomainBlocking: false, Endorsed: false, Note: ""}, relationship)
    120 
    121 	// reject should be sent to Some_User
    122 	var sent [][]byte
    123 	if !testrig.WaitFor(func() bool {
    124 		sentI, ok := suite.httpClient.SentMessages.Load(requestingAccount.InboxURI)
    125 		if ok {
    126 			sent, ok = sentI.([][]byte)
    127 			if !ok {
    128 				panic("SentMessages entry was not []byte")
    129 			}
    130 			return true
    131 		}
    132 		return false
    133 	}) {
    134 		suite.FailNow("timed out waiting for message")
    135 	}
    136 
    137 	reject := &struct {
    138 		Actor  string `json:"actor"`
    139 		ID     string `json:"id"`
    140 		Object struct {
    141 			Actor  string `json:"actor"`
    142 			ID     string `json:"id"`
    143 			Object string `json:"object"`
    144 			To     string `json:"to"`
    145 			Type   string `json:"type"`
    146 		}
    147 		To   string `json:"to"`
    148 		Type string `json:"type"`
    149 	}{}
    150 	err = json.Unmarshal(sent[0], reject)
    151 	suite.NoError(err)
    152 
    153 	suite.Equal(targetAccount.URI, reject.Actor)
    154 	suite.Equal(requestingAccount.URI, reject.Object.Actor)
    155 	suite.Equal(fr.URI, reject.Object.ID)
    156 	suite.Equal(targetAccount.URI, reject.Object.Object)
    157 	suite.Equal(targetAccount.URI, reject.Object.To)
    158 	suite.Equal("Follow", reject.Object.Type)
    159 	suite.Equal(requestingAccount.URI, reject.To)
    160 	suite.Equal("Reject", reject.Type)
    161 }
    162 
    163 func TestFollowRequestTestSuite(t *testing.T) {
    164 	suite.Run(t, &FollowRequestTestSuite{})
    165 }