gtsocial-umbx

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

emojidelete_test.go (3778B)


      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 admin_test
     19 
     20 import (
     21 	"bytes"
     22 	"context"
     23 	"encoding/json"
     24 	"io"
     25 	"net/http"
     26 	"net/http/httptest"
     27 	"testing"
     28 
     29 	"github.com/stretchr/testify/suite"
     30 	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
     31 	"github.com/superseriousbusiness/gotosocial/internal/db"
     32 )
     33 
     34 type EmojiDeleteTestSuite struct {
     35 	AdminStandardTestSuite
     36 }
     37 
     38 func (suite *EmojiDeleteTestSuite) TestEmojiDelete1() {
     39 	recorder := httptest.NewRecorder()
     40 	testEmoji := suite.testEmojis["rainbow"]
     41 
     42 	path := admin.EmojiPathWithID
     43 	ctx := suite.newContext(recorder, http.MethodDelete, nil, path, "application/json")
     44 	ctx.AddParam(admin.IDKey, testEmoji.ID)
     45 
     46 	suite.adminModule.EmojiDELETEHandler(ctx)
     47 	suite.Equal(http.StatusOK, recorder.Code)
     48 
     49 	b, err := io.ReadAll(recorder.Body)
     50 	suite.NoError(err)
     51 	suite.NotNil(b)
     52 	dst := new(bytes.Buffer)
     53 	err = json.Indent(dst, b, "", "  ")
     54 	suite.NoError(err)
     55 	suite.Equal(`{
     56   "shortcode": "rainbow",
     57   "url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
     58   "static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
     59   "visible_in_picker": true,
     60   "category": "reactions",
     61   "id": "01F8MH9H8E4VG3KDYJR9EGPXCQ",
     62   "disabled": false,
     63   "updated_at": "2021-09-20T10:40:37.000Z",
     64   "total_file_size": 47115,
     65   "content_type": "image/png",
     66   "uri": "http://localhost:8080/emoji/01F8MH9H8E4VG3KDYJR9EGPXCQ"
     67 }`, dst.String())
     68 
     69 	// emoji should no longer be in the db
     70 	dbEmoji, err := suite.db.GetEmojiByID(context.Background(), testEmoji.ID)
     71 	suite.Nil(dbEmoji)
     72 	suite.ErrorIs(err, db.ErrNoEntries)
     73 }
     74 
     75 func (suite *EmojiDeleteTestSuite) TestEmojiDelete2() {
     76 	recorder := httptest.NewRecorder()
     77 	testEmoji := suite.testEmojis["yell"]
     78 
     79 	path := admin.EmojiPathWithID
     80 	ctx := suite.newContext(recorder, http.MethodDelete, nil, path, "application/json")
     81 	ctx.AddParam(admin.IDKey, testEmoji.ID)
     82 
     83 	suite.adminModule.EmojiDELETEHandler(ctx)
     84 	suite.Equal(http.StatusBadRequest, recorder.Code)
     85 
     86 	b, err := io.ReadAll(recorder.Body)
     87 	suite.NoError(err)
     88 	suite.NotNil(b)
     89 
     90 	suite.Equal(`{"error":"Bad Request: EmojiDelete: emoji with id 01GD5KP5CQEE1R3X43Y1EHS2CW was not a local emoji, will not delete"}`, string(b))
     91 
     92 	// emoji should still be in the db
     93 	dbEmoji, err := suite.db.GetEmojiByID(context.Background(), testEmoji.ID)
     94 	suite.NoError(err)
     95 	suite.NotNil(dbEmoji)
     96 }
     97 
     98 func (suite *EmojiDeleteTestSuite) TestEmojiDeleteNotFound() {
     99 	recorder := httptest.NewRecorder()
    100 
    101 	path := admin.EmojiPathWithID
    102 	ctx := suite.newContext(recorder, http.MethodDelete, nil, path, "application/json")
    103 	ctx.AddParam(admin.IDKey, "01GF8VRXX1R00X7XH8973Z29R1")
    104 
    105 	suite.adminModule.EmojiDELETEHandler(ctx)
    106 	suite.Equal(http.StatusNotFound, recorder.Code)
    107 
    108 	b, err := io.ReadAll(recorder.Body)
    109 	suite.NoError(err)
    110 	suite.NotNil(b)
    111 	suite.Equal(`{"error":"Not Found"}`, string(b))
    112 }
    113 
    114 func TestEmojiDeleteTestSuite(t *testing.T) {
    115 	suite.Run(t, &EmojiDeleteTestSuite{})
    116 }