summaryrefslogtreecommitdiff
path: root/internal/api/client/followrequests/get_test.go
blob: b9b6db7c64ba357eb69a3a03e96ade80d7c7727c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package followrequests_test

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"io"
	"math/rand"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strconv"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/suite"
	"github.com/superseriousbusiness/gotosocial/internal/api/model"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/tomnomnom/linkheader"
)

// random reader according to current-time source seed.
var randRd = rand.New(rand.NewSource(time.Now().Unix()))

type GetTestSuite struct {
	FollowRequestStandardTestSuite
}

func (suite *GetTestSuite) TestGet() {
	requestingAccount := suite.testAccounts["remote_account_2"]
	targetAccount := suite.testAccounts["local_account_1"]

	// put a follow request in the database
	fr := &gtsmodel.FollowRequest{
		ID:              "01FJ1S8DX3STJJ6CEYPMZ1M0R3",
		CreatedAt:       time.Now(),
		UpdatedAt:       time.Now(),
		URI:             fmt.Sprintf("%s/follow/01FJ1S8DX3STJJ6CEYPMZ1M0R3", requestingAccount.URI),
		AccountID:       requestingAccount.ID,
		TargetAccountID: targetAccount.ID,
	}

	err := suite.db.Put(context.Background(), fr)
	suite.NoError(err)

	recorder := httptest.NewRecorder()
	ctx := suite.newContext(recorder, http.MethodGet, []byte{}, "/api/v1/follow_requests", "")

	// call the handler
	suite.followRequestModule.FollowRequestGETHandler(ctx)

	// 1. we should have OK because our request was valid
	suite.Equal(http.StatusOK, recorder.Code)

	// 2. we should have no error message in the result body
	result := recorder.Result()
	defer result.Body.Close()

	// check the response
	b, err := io.ReadAll(result.Body)
	assert.NoError(suite.T(), err)
	dst := new(bytes.Buffer)
	err = json.Indent(dst, b, "", "  ")
	suite.NoError(err)
	suite.Equal(`[
  {
    "id": "01FHMQX3GAABWSM0S2VZEC2SWC",
    "username": "Some_User",
    "acct": "Some_User@example.org",
    "display_name": "some user",
    "locked": true,
    "discoverable": true,
    "bot": false,
    "created_at": "2020-08-10T12:13:28.000Z",
    "note": "i'm a real son of a gun",
    "url": "http://example.org/@Some_User",
    "avatar": "",
    "avatar_static": "",
    "header": "http://localhost:8080/assets/default_header.webp",
    "header_static": "http://localhost:8080/assets/default_header.webp",
    "followers_count": 0,
    "following_count": 0,
    "statuses_count": 1,
    "last_status_at": "2023-11-02T10:44:25.000Z",
    "emojis": [],
    "fields": []
  }
]`, dst.String())
}

func (suite *GetTestSuite) TestGetPageNewestToOldestLimit2() {
	suite.testGetPage(2, "newestToOldest")
}

func (suite *GetTestSuite) TestGetPageNewestToOldestLimit4() {
	suite.testGetPage(4, "newestToOldest")
}

func (suite *GetTestSuite) TestGetPageNewestToOldestLimit6() {
	suite.testGetPage(6, "newestToOldest")
}

func (suite *GetTestSuite) TestGetPageOldestToNewestLimit2() {
	suite.testGetPage(2, "oldestToNewest")
}

func (suite *GetTestSuite) TestGetPageOldestToNewestLimit4() {
	suite.testGetPage(4, "oldestToNewest")
}

func (suite *GetTestSuite) TestGetPageOldestToNewestLimit6() {
	suite.testGetPage(6, "oldestToNewest")
}

func (suite *GetTestSuite) testGetPage(limit int, direction string) {
	ctx := context.Background()

	// The authed local account we are going to use for HTTP requests
	requestingAccount := suite.testAccounts["local_account_1"]
	suite.clearAccountRelations(requestingAccount.ID)

	// Get current time.
	now := time.Now()

	var i int

	// Have each account in the testrig follow req the
	// account requesting their followers from the API.
	for _, account := range suite.testAccounts {
		targetAccount := requestingAccount
		if account.ID == requestingAccount.ID {
			// we cannot be our own target...
			continue
		}

		// Get next simple ID.
		id := strconv.Itoa(i)
		i++

		// put a follow request in the database
		err := suite.db.PutFollowRequest(ctx, &gtsmodel.FollowRequest{
			ID:              id,
			CreatedAt:       now,
			UpdatedAt:       now,
			URI:             fmt.Sprintf("%s/follow/%s", account.URI, id),
			AccountID:       account.ID,
			TargetAccountID: targetAccount.ID,
		})
		suite.NoError(err)

		// Bump now by 1 second.
		now = now.Add(time.Second)
	}

	// Get _ALL_ follow requests we expect to see without any paging (this filters invisible).
	apiRsp, err := suite.processor.Account().FollowRequestsGet(ctx, requestingAccount, nil)
	suite.NoError(err)
	expectAccounts := apiRsp.Items // interfaced{} account slice

	// Iteratively set
	// link query string.
	var query string

	switch direction {
	case "newestToOldest":
		// Set the starting query to page from
		// newest (ie., first entry in slice).
		acc := expectAccounts[0].(*model.Account)
		newest, _ := suite.db.GetFollowRequest(ctx, acc.ID, requestingAccount.ID)
		expectAccounts = expectAccounts[1:]
		query = fmt.Sprintf("limit=%d&max_id=%s", limit, newest.ID)

	case "oldestToNewest":
		// Set the starting query to page from
		// oldest (ie., last entry in slice).
		acc := expectAccounts[len(expectAccounts)-1].(*model.Account)
		oldest, _ := suite.db.GetFollowRequest(ctx, acc.ID, requestingAccount.ID)
		expectAccounts = expectAccounts[:len(expectAccounts)-1]
		query = fmt.Sprintf("limit=%d&min_id=%s", limit, oldest.ID)
	}

	for p := 0; ; p++ {
		// Prepare new request for endpoint
		recorder := httptest.NewRecorder()
		ctx := suite.newContext(recorder, http.MethodGet, []byte{}, "/api/v1/follow_requests", "")
		ctx.Request.URL.RawQuery = query // setting provided next query value

		// call the handler and check for valid response code.
		suite.T().Logf("direction=%q page=%d query=%q", direction, p, query)
		suite.followRequestModule.FollowRequestGETHandler(ctx)
		suite.Equal(http.StatusOK, recorder.Code)

		var accounts []*model.Account

		// Decode response body into API account models
		result := recorder.Result()
		dec := json.NewDecoder(result.Body)
		err := dec.Decode(&accounts)
		suite.NoError(err)
		_ = result.Body.Close()

		var (

			// start provides the starting index for loop in accounts.
			start func([]*model.Account) int

			// iter performs the loop iter step with index.
			iter func(int) int

			// check performs the loop conditional check against index and accounts.
			check func(int, []*model.Account) bool

			// expect pulls the next account to check against from expectAccounts.
			expect func([]interface{}) interface{}

			// trunc drops the last checked account from expectAccounts.
			trunc func([]interface{}) []interface{}
		)

		switch direction {
		case "newestToOldest":
			// When paging newest to oldest (ie., first page to last page):
			// - iter from start of received accounts
			// - iterate backward through received accounts
			// - stop when we reach last index of received accounts
			// - compare each received with the first index of expected accounts
			// - after each compare, drop the first index of expected accounts
			start = func([]*model.Account) int { return 0 }
			iter = func(i int) int { return i + 1 }
			check = func(idx int, i []*model.Account) bool { return idx < len(i) }
			expect = func(i []interface{}) interface{} { return i[0] }
			trunc = func(i []interface{}) []interface{} { return i[1:] }

		case "oldestToNewest":
			// When paging oldest to newest (ie., last page to first page):
			// - iter from end of received accounts
			// - iterate backward through received accounts
			// - stop when we reach first index of received accounts
			// - compare each received with the last index of expected accounts
			// - after each compare, drop the last index of expected accounts
			start = func(i []*model.Account) int { return len(i) - 1 }
			iter = func(i int) int { return i - 1 }
			check = func(idx int, _ []*model.Account) bool { return idx >= 0 }
			expect = func(i []interface{}) interface{} { return i[len(i)-1] }
			trunc = func(i []interface{}) []interface{} { return i[:len(i)-1] }
		}

		for i := start(accounts); check(i, accounts); i = iter(i) {
			// Get next expected account.
			iface := expect(expectAccounts)

			// Check that expected account matches received.
			expectAccID := iface.(*model.Account).ID
			receivdAccID := accounts[i].ID
			suite.Equal(expectAccID, receivdAccID, "unexpected account at position in response on page=%d", p)

			// Drop checked from expected accounts.
			expectAccounts = trunc(expectAccounts)
		}

		if len(expectAccounts) == 0 {
			// Reached end.
			break
		}

		// Parse response link header values.
		values := result.Header.Values("Link")
		links := linkheader.ParseMultiple(values)

		var filteredLinks linkheader.Links
		if direction == "newestToOldest" {
			filteredLinks = links.FilterByRel("next")
		} else {
			filteredLinks = links.FilterByRel("prev")
		}

		suite.NotEmpty(filteredLinks, "no next link provided with more remaining accounts on page=%d", p)

		// A ref link header was set.
		link := filteredLinks[0]

		// Parse URI from URI string.
		uri, err := url.Parse(link.URL)
		suite.NoError(err)

		// Set next raw query value.
		query = uri.RawQuery
	}
}

func (suite *GetTestSuite) clearAccountRelations(id string) {
	// Esnure no account blocks exist between accounts.
	_ = suite.db.DeleteAccountBlocks(
		context.Background(),
		id,
	)

	// Ensure no account follows exist between accounts.
	_ = suite.db.DeleteAccountFollows(
		context.Background(),
		id,
	)

	// Ensure no account follow_requests exist between accounts.
	_ = suite.db.DeleteAccountFollowRequests(
		context.Background(),
		id,
	)
}

func TestGetTestSuite(t *testing.T) {
	suite.Run(t, &GetTestSuite{})
}