gtsocial-umbx

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

get_test.go (3092B)


      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 followrequests_test
     19 
     20 import (
     21 	"bytes"
     22 	"context"
     23 	"encoding/json"
     24 	"fmt"
     25 	"io/ioutil"
     26 	"net/http"
     27 	"net/http/httptest"
     28 	"testing"
     29 	"time"
     30 
     31 	"github.com/stretchr/testify/assert"
     32 	"github.com/stretchr/testify/suite"
     33 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     34 )
     35 
     36 type GetTestSuite struct {
     37 	FollowRequestStandardTestSuite
     38 }
     39 
     40 func (suite *GetTestSuite) TestGet() {
     41 	requestingAccount := suite.testAccounts["remote_account_2"]
     42 	targetAccount := suite.testAccounts["local_account_1"]
     43 
     44 	// put a follow request in the database
     45 	fr := &gtsmodel.FollowRequest{
     46 		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3",
     47 		CreatedAt:       time.Now(),
     48 		UpdatedAt:       time.Now(),
     49 		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI),
     50 		AccountID:       requestingAccount.ID,
     51 		TargetAccountID: targetAccount.ID,
     52 	}
     53 
     54 	err := suite.db.Put(context.Background(), fr)
     55 	suite.NoError(err)
     56 
     57 	recorder := httptest.NewRecorder()
     58 	ctx := suite.newContext(recorder, http.MethodGet, []byte{}, "/api/v1/follow_requests", "")
     59 
     60 	// call the handler
     61 	suite.followRequestModule.FollowRequestGETHandler(ctx)
     62 
     63 	// 1. we should have OK because our request was valid
     64 	suite.Equal(http.StatusOK, recorder.Code)
     65 
     66 	// 2. we should have no error message in the result body
     67 	result := recorder.Result()
     68 	defer result.Body.Close()
     69 
     70 	// check the response
     71 	b, err := ioutil.ReadAll(result.Body)
     72 	assert.NoError(suite.T(), err)
     73 	dst := new(bytes.Buffer)
     74 	err = json.Indent(dst, b, "", "  ")
     75 	suite.NoError(err)
     76 	suite.Equal(`[
     77   {
     78     "id": "01FHMQX3GAABWSM0S2VZEC2SWC",
     79     "username": "Some_User",
     80     "acct": "Some_User@example.org",
     81     "display_name": "some user",
     82     "locked": true,
     83     "discoverable": true,
     84     "bot": false,
     85     "created_at": "2020-08-10T12:13:28.000Z",
     86     "note": "i'm a real son of a gun",
     87     "url": "http://example.org/@Some_User",
     88     "avatar": "",
     89     "avatar_static": "",
     90     "header": "http://localhost:8080/assets/default_header.png",
     91     "header_static": "http://localhost:8080/assets/default_header.png",
     92     "followers_count": 0,
     93     "following_count": 0,
     94     "statuses_count": 0,
     95     "last_status_at": null,
     96     "emojis": [],
     97     "fields": []
     98   }
     99 ]`, dst.String())
    100 }
    101 
    102 func TestGetTestSuite(t *testing.T) {
    103 	suite.Run(t, &GetTestSuite{})
    104 }