gtsocial-umbx

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

create_test.go (4471B)


      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 federatingdb_test
     19 
     20 import (
     21 	"context"
     22 	"encoding/json"
     23 	"testing"
     24 
     25 	"github.com/stretchr/testify/suite"
     26 	"github.com/superseriousbusiness/activity/streams"
     27 	"github.com/superseriousbusiness/gotosocial/internal/ap"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     29 )
     30 
     31 type CreateTestSuite struct {
     32 	FederatingDBTestSuite
     33 }
     34 
     35 func (suite *CreateTestSuite) TestCreateNote() {
     36 	receivingAccount := suite.testAccounts["local_account_1"]
     37 	requestingAccount := suite.testAccounts["remote_account_1"]
     38 
     39 	ctx := createTestContext(receivingAccount, requestingAccount)
     40 
     41 	create := suite.testActivities["dm_for_zork"].Activity
     42 
     43 	err := suite.federatingDB.Create(ctx, create)
     44 	suite.NoError(err)
     45 
     46 	// should be a message heading to the processor now, which we can intercept here
     47 	msg := <-suite.fromFederator
     48 	suite.Equal(ap.ObjectNote, msg.APObjectType)
     49 	suite.Equal(ap.ActivityCreate, msg.APActivityType)
     50 
     51 	// shiny new status should be defined on the message
     52 	suite.NotNil(msg.GTSModel)
     53 	status := msg.GTSModel.(*gtsmodel.Status)
     54 
     55 	// status should have some expected values
     56 	suite.Equal(requestingAccount.ID, status.AccountID)
     57 	suite.Equal("hey zork here's a new private note for you", status.Content)
     58 
     59 	// status should be in the database
     60 	_, err = suite.db.GetStatusByID(context.Background(), status.ID)
     61 	suite.NoError(err)
     62 }
     63 
     64 func (suite *CreateTestSuite) TestCreateNoteForward() {
     65 	receivingAccount := suite.testAccounts["local_account_1"]
     66 	requestingAccount := suite.testAccounts["remote_account_1"]
     67 
     68 	ctx := createTestContext(receivingAccount, requestingAccount)
     69 
     70 	create := suite.testActivities["forwarded_message"].Activity
     71 
     72 	err := suite.federatingDB.Create(ctx, create)
     73 	suite.NoError(err)
     74 
     75 	// should be a message heading to the processor now, which we can intercept here
     76 	msg := <-suite.fromFederator
     77 	suite.Equal(ap.ObjectNote, msg.APObjectType)
     78 	suite.Equal(ap.ActivityCreate, msg.APActivityType)
     79 
     80 	// nothing should be set as the model since this is a forward
     81 	suite.Nil(msg.GTSModel)
     82 
     83 	// but we should have a uri set
     84 	suite.Equal("http://example.org/users/Some_User/statuses/afaba698-5740-4e32-a702-af61aa543bc1", msg.APIri.String())
     85 }
     86 
     87 func (suite *CreateTestSuite) TestCreateFlag1() {
     88 	reportedAccount := suite.testAccounts["local_account_1"]
     89 	reportingAccount := suite.testAccounts["remote_account_1"]
     90 	reportedStatus := suite.testStatuses["local_account_1_status_1"]
     91 
     92 	raw := `{
     93   "@context": "https://www.w3.org/ns/activitystreams",
     94   "actor": "` + reportingAccount.URI + `",
     95   "content": "Note: ` + reportedStatus.URL + `\n-----\nban this sick filth ⛔",
     96   "id": "http://fossbros-anonymous.io/db22128d-884e-4358-9935-6a7c3940535d",
     97   "object": "` + reportedAccount.URI + `",
     98   "type": "Flag"
     99 }`
    100 
    101 	m := make(map[string]interface{})
    102 	if err := json.Unmarshal([]byte(raw), &m); err != nil {
    103 		suite.FailNow(err.Error())
    104 	}
    105 
    106 	t, err := streams.ToType(context.Background(), m)
    107 	if err != nil {
    108 		suite.FailNow(err.Error())
    109 	}
    110 
    111 	ctx := createTestContext(reportedAccount, reportingAccount)
    112 	if err := suite.federatingDB.Create(ctx, t); err != nil {
    113 		suite.FailNow(err.Error())
    114 	}
    115 
    116 	// should be a message heading to the processor now, which we can intercept here
    117 	msg := <-suite.fromFederator
    118 	suite.Equal(ap.ActivityFlag, msg.APObjectType)
    119 	suite.Equal(ap.ActivityCreate, msg.APActivityType)
    120 
    121 	// shiny new report should be defined on the message
    122 	suite.NotNil(msg.GTSModel)
    123 	report := msg.GTSModel.(*gtsmodel.Report)
    124 
    125 	// report should be in the database
    126 	if _, err := suite.db.GetReportByID(context.Background(), report.ID); err != nil {
    127 		suite.FailNow(err.Error())
    128 	}
    129 }
    130 
    131 func TestCreateTestSuite(t *testing.T) {
    132 	suite.Run(t, &CreateTestSuite{})
    133 }