statusdelete_test.go (3082B)
1 /* 2 GoToSocial 3 Copyright (C) GoToSocial Authors admin@gotosocial.org 4 This program is free software: you can redistribute it and/or modify 5 it under the terms of the GNU Affero General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU Affero General Public License for more details. 12 You should have received a copy of the GNU Affero General Public License 13 along with this program. If not, see <http://www.gnu.org/licenses/>. 14 */ 15 16 package statuses_test 17 18 import ( 19 "encoding/json" 20 "errors" 21 "fmt" 22 "io/ioutil" 23 "net/http" 24 "net/http/httptest" 25 "strings" 26 "testing" 27 28 "github.com/gin-gonic/gin" 29 "github.com/stretchr/testify/suite" 30 "github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" 31 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 32 "github.com/superseriousbusiness/gotosocial/internal/db" 33 "github.com/superseriousbusiness/gotosocial/internal/oauth" 34 "github.com/superseriousbusiness/gotosocial/testrig" 35 ) 36 37 type StatusDeleteTestSuite struct { 38 StatusStandardTestSuite 39 } 40 41 func (suite *StatusDeleteTestSuite) TestPostDelete() { 42 t := suite.testTokens["local_account_1"] 43 oauthToken := oauth.DBTokenToToken(t) 44 targetStatus := suite.testStatuses["local_account_1_status_1"] 45 46 // setup 47 recorder := httptest.NewRecorder() 48 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 49 ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"]) 50 ctx.Set(oauth.SessionAuthorizedToken, oauthToken) 51 ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"]) 52 ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"]) 53 ctx.Request = httptest.NewRequest(http.MethodDelete, fmt.Sprintf("http://localhost:8080%s", strings.Replace(statuses.BasePathWithID, ":id", targetStatus.ID, 1)), nil) // the endpoint we're hitting 54 ctx.Request.Header.Set("accept", "application/json") 55 56 // normally the router would populate these params from the path values, 57 // but because we're calling the function directly, we need to set them manually. 58 ctx.Params = gin.Params{ 59 gin.Param{ 60 Key: statuses.IDKey, 61 Value: targetStatus.ID, 62 }, 63 } 64 65 suite.statusModule.StatusDELETEHandler(ctx) 66 67 // check response 68 suite.EqualValues(http.StatusOK, recorder.Code) 69 70 result := recorder.Result() 71 defer result.Body.Close() 72 b, err := ioutil.ReadAll(result.Body) 73 suite.NoError(err) 74 75 statusReply := &apimodel.Status{} 76 err = json.Unmarshal(b, statusReply) 77 suite.NoError(err) 78 suite.NotNil(statusReply) 79 80 if !testrig.WaitFor(func() bool { 81 _, err := suite.db.GetStatusByID(ctx, targetStatus.ID) 82 return errors.Is(err, db.ErrNoEntries) 83 }) { 84 suite.FailNow("time out waiting for status to be deleted") 85 } 86 87 } 88 89 func TestStatusDeleteTestSuite(t *testing.T) { 90 suite.Run(t, new(StatusDeleteTestSuite)) 91 }