instancepeersget_test.go (9326B)
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 instance_test 19 20 import ( 21 "bytes" 22 "context" 23 "encoding/json" 24 "fmt" 25 "io" 26 "net/http" 27 "net/http/httptest" 28 "testing" 29 30 "github.com/gin-gonic/gin/render" 31 "github.com/stretchr/testify/suite" 32 "github.com/superseriousbusiness/gotosocial/internal/api/client/instance" 33 "github.com/superseriousbusiness/gotosocial/internal/config" 34 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 35 "github.com/superseriousbusiness/gotosocial/testrig" 36 ) 37 38 type InstancePeersGetTestSuite struct { 39 InstanceStandardTestSuite 40 } 41 42 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParams() { 43 recorder := httptest.NewRecorder() 44 ctx, r := testrig.CreateGinTestContext(recorder, nil) 45 r.HTMLRender = render.HTMLDebug{} 46 47 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 48 requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) 49 ctx.Request = httptest.NewRequest(http.MethodGet, requestURI, nil) 50 51 suite.instanceModule.InstancePeersGETHandler(ctx) 52 53 suite.Equal(http.StatusOK, recorder.Code) 54 55 result := recorder.Result() 56 defer result.Body.Close() 57 58 b, err := io.ReadAll(result.Body) 59 suite.NoError(err) 60 dst := new(bytes.Buffer) 61 err = json.Indent(dst, b, "", " ") 62 suite.NoError(err) 63 suite.Equal(`[ 64 "example.org", 65 "fossbros-anonymous.io" 66 ]`, dst.String()) 67 } 68 69 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsUnauthorized() { 70 config.SetInstanceExposePeers(false) 71 72 recorder := httptest.NewRecorder() 73 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 74 requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) 75 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) 76 77 suite.instanceModule.InstancePeersGETHandler(ctx) 78 79 suite.Equal(http.StatusUnauthorized, recorder.Code) 80 81 result := recorder.Result() 82 defer result.Body.Close() 83 84 b, err := io.ReadAll(result.Body) 85 suite.NoError(err) 86 87 suite.Equal(`{"error":"Unauthorized: peers open query requires an authenticated account/user"}`, string(b)) 88 } 89 90 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetNoParamsAuthorized() { 91 config.SetInstanceExposePeers(false) 92 93 recorder := httptest.NewRecorder() 94 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 95 requestURI := fmt.Sprintf("%s/%s", baseURI, instance.InstancePeersPath) 96 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) 97 98 suite.instanceModule.InstancePeersGETHandler(ctx) 99 100 suite.Equal(http.StatusOK, recorder.Code) 101 102 result := recorder.Result() 103 defer result.Body.Close() 104 105 b, err := io.ReadAll(result.Body) 106 suite.NoError(err) 107 dst := new(bytes.Buffer) 108 err = json.Indent(dst, b, "", " ") 109 suite.NoError(err) 110 suite.Equal(`[ 111 "example.org", 112 "fossbros-anonymous.io" 113 ]`, dst.String()) 114 } 115 116 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspended() { 117 recorder := httptest.NewRecorder() 118 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 119 requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) 120 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) 121 122 suite.instanceModule.InstancePeersGETHandler(ctx) 123 124 suite.Equal(http.StatusOK, recorder.Code) 125 126 result := recorder.Result() 127 defer result.Body.Close() 128 129 b, err := io.ReadAll(result.Body) 130 suite.NoError(err) 131 dst := new(bytes.Buffer) 132 err = json.Indent(dst, b, "", " ") 133 suite.NoError(err) 134 suite.Equal(`[ 135 { 136 "domain": "replyguys.com", 137 "suspended_at": "2020-05-13T13:29:12.000Z", 138 "public_comment": "reply-guying to tech posts" 139 } 140 ]`, dst.String()) 141 } 142 143 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedUnauthorized() { 144 config.SetInstanceExposeSuspended(false) 145 146 recorder := httptest.NewRecorder() 147 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 148 requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) 149 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) 150 151 suite.instanceModule.InstancePeersGETHandler(ctx) 152 153 suite.Equal(http.StatusUnauthorized, recorder.Code) 154 155 result := recorder.Result() 156 defer result.Body.Close() 157 158 b, err := io.ReadAll(result.Body) 159 suite.NoError(err) 160 161 suite.Equal(`{"error":"Unauthorized: peers suspended query requires an authenticated account/user"}`, string(b)) 162 } 163 164 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetOnlySuspendedAuthorized() { 165 config.SetInstanceExposeSuspended(false) 166 167 recorder := httptest.NewRecorder() 168 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 169 requestURI := fmt.Sprintf("%s/%s?filter=suspended", baseURI, instance.InstancePeersPath) 170 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) 171 172 suite.instanceModule.InstancePeersGETHandler(ctx) 173 174 suite.Equal(http.StatusOK, recorder.Code) 175 176 result := recorder.Result() 177 defer result.Body.Close() 178 179 b, err := io.ReadAll(result.Body) 180 suite.NoError(err) 181 dst := new(bytes.Buffer) 182 err = json.Indent(dst, b, "", " ") 183 suite.NoError(err) 184 suite.Equal(`[ 185 { 186 "domain": "replyguys.com", 187 "suspended_at": "2020-05-13T13:29:12.000Z", 188 "public_comment": "reply-guying to tech posts" 189 } 190 ]`, dst.String()) 191 } 192 193 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAll() { 194 recorder := httptest.NewRecorder() 195 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 196 requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath) 197 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) 198 199 suite.instanceModule.InstancePeersGETHandler(ctx) 200 201 suite.Equal(http.StatusOK, recorder.Code) 202 203 result := recorder.Result() 204 defer result.Body.Close() 205 206 b, err := io.ReadAll(result.Body) 207 suite.NoError(err) 208 dst := new(bytes.Buffer) 209 err = json.Indent(dst, b, "", " ") 210 suite.NoError(err) 211 suite.Equal(`[ 212 { 213 "domain": "example.org" 214 }, 215 { 216 "domain": "fossbros-anonymous.io" 217 }, 218 { 219 "domain": "replyguys.com", 220 "suspended_at": "2020-05-13T13:29:12.000Z", 221 "public_comment": "reply-guying to tech posts" 222 } 223 ]`, dst.String()) 224 } 225 226 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetAllWithObfuscated() { 227 err := suite.db.Put(context.Background(), >smodel.DomainBlock{ 228 ID: "01G633XTNK51GBADQZFZQDP6WR", 229 CreatedAt: testrig.TimeMustParse("2021-06-09T12:34:55+02:00"), 230 UpdatedAt: testrig.TimeMustParse("2021-06-09T12:34:55+02:00"), 231 Domain: "omg.just.the.worst.org.ever", 232 CreatedByAccountID: "01F8MH17FWEB39HZJ76B6VXSKF", 233 PublicComment: "just absolutely the worst, wowza", 234 Obfuscate: testrig.TrueBool(), 235 }) 236 suite.NoError(err) 237 238 recorder := httptest.NewRecorder() 239 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 240 requestURI := fmt.Sprintf("%s/%s?filter=suspended,open", baseURI, instance.InstancePeersPath) 241 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", false) 242 243 suite.instanceModule.InstancePeersGETHandler(ctx) 244 245 suite.Equal(http.StatusOK, recorder.Code) 246 247 result := recorder.Result() 248 defer result.Body.Close() 249 250 b, err := io.ReadAll(result.Body) 251 suite.NoError(err) 252 dst := new(bytes.Buffer) 253 err = json.Indent(dst, b, "", " ") 254 suite.NoError(err) 255 suite.Equal(`[ 256 { 257 "domain": "example.org" 258 }, 259 { 260 "domain": "fossbros-anonymous.io" 261 }, 262 { 263 "domain": "o*g.*u**.t**.*or*t.*r**ev**", 264 "suspended_at": "2021-06-09T10:34:55.000Z", 265 "public_comment": "just absolutely the worst, wowza" 266 }, 267 { 268 "domain": "replyguys.com", 269 "suspended_at": "2020-05-13T13:29:12.000Z", 270 "public_comment": "reply-guying to tech posts" 271 } 272 ]`, dst.String()) 273 } 274 275 func (suite *InstancePeersGetTestSuite) TestInstancePeersGetFunkyParams() { 276 recorder := httptest.NewRecorder() 277 baseURI := fmt.Sprintf("%s://%s", config.GetProtocol(), config.GetHost()) 278 requestURI := fmt.Sprintf("%s/%s?filter=aaaaaaaaaaaaaaaaa,open", baseURI, instance.InstancePeersPath) 279 ctx := suite.newContext(recorder, http.MethodGet, requestURI, nil, "", true) 280 281 suite.instanceModule.InstancePeersGETHandler(ctx) 282 283 suite.Equal(http.StatusBadRequest, recorder.Code) 284 285 result := recorder.Result() 286 defer result.Body.Close() 287 288 b, err := io.ReadAll(result.Body) 289 suite.NoError(err) 290 291 suite.Equal(`{"error":"Bad Request: filter aaaaaaaaaaaaaaaaa not recognized; accepted values are 'open', 'suspended'"}`, string(b)) 292 } 293 294 func TestInstancePeersGetTestSuite(t *testing.T) { 295 suite.Run(t, &InstancePeersGetTestSuite{}) 296 }