lists_test.go (3440B)
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 accounts_test 19 20 import ( 21 "encoding/json" 22 "fmt" 23 "io" 24 "net/http" 25 "net/http/httptest" 26 "testing" 27 28 "github.com/stretchr/testify/suite" 29 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 30 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 31 "github.com/superseriousbusiness/gotosocial/internal/oauth" 32 "github.com/superseriousbusiness/gotosocial/testrig" 33 ) 34 35 type ListsTestSuite struct { 36 AccountStandardTestSuite 37 } 38 39 func (suite *ListsTestSuite) getLists(targetAccountID string, expectedHTTPStatus int, expectedBody string) []*apimodel.List { 40 var ( 41 recorder = httptest.NewRecorder() 42 ctx, _ = testrig.CreateGinTestContext(recorder, nil) 43 request = httptest.NewRequest(http.MethodGet, "http://localhost:8080/api/v1/accounts/"+targetAccountID+"/lists", nil) 44 ) 45 46 // Set up the test context. 47 ctx.Request = request 48 ctx.AddParam("id", targetAccountID) 49 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) 50 ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["local_account_1"])) 51 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 52 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 53 54 // Trigger the handler. 55 suite.accountsModule.AccountListsGETHandler(ctx) 56 57 // Read the result. 58 result := recorder.Result() 59 defer result.Body.Close() 60 61 b, err := io.ReadAll(result.Body) 62 if err != nil { 63 suite.FailNow(err.Error()) 64 } 65 66 errs := gtserror.MultiError{} 67 68 // Check expected code + body. 69 if resultCode := recorder.Code; expectedHTTPStatus != resultCode { 70 errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode)) 71 } 72 73 // If we got an expected body, return early. 74 if expectedBody != "" && string(b) != expectedBody { 75 errs = append(errs, fmt.Sprintf("expected %s got %s", expectedBody, string(b))) 76 } 77 78 if err := errs.Combine(); err != nil { 79 suite.FailNow("", "%v (body %s)", err, string(b)) 80 } 81 82 // Return list response. 83 resp := new([]*apimodel.List) 84 if err := json.Unmarshal(b, resp); err != nil { 85 suite.FailNow(err.Error()) 86 } 87 88 return *resp 89 } 90 91 func (suite *ListsTestSuite) TestGetListsHit() { 92 targetAccount := suite.testAccounts["admin_account"] 93 suite.getLists(targetAccount.ID, http.StatusOK, `[{"id":"01H0G8E4Q2J3FE3JDWJVWEDCD1","title":"Cool Ass Posters From This Instance","replies_policy":"followed"}]`) 94 } 95 96 func (suite *ListsTestSuite) TestGetListsNoHit() { 97 targetAccount := suite.testAccounts["remote_account_1"] 98 suite.getLists(targetAccount.ID, http.StatusOK, `[]`) 99 } 100 101 func TestListsTestSuite(t *testing.T) { 102 suite.Run(t, new(ListsTestSuite)) 103 }