gtsocial-umbx

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

emojiget_test.go (3865B)


      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 	"encoding/json"
     23 	"io"
     24 	"net/http"
     25 	"net/http/httptest"
     26 	"testing"
     27 
     28 	"github.com/stretchr/testify/suite"
     29 	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
     30 )
     31 
     32 type EmojiGetTestSuite struct {
     33 	AdminStandardTestSuite
     34 }
     35 
     36 func (suite *EmojiGetTestSuite) TestEmojiGet1() {
     37 	recorder := httptest.NewRecorder()
     38 	testEmoji := suite.testEmojis["rainbow"]
     39 
     40 	path := admin.EmojiPathWithID
     41 	ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json")
     42 	ctx.AddParam(admin.IDKey, testEmoji.ID)
     43 
     44 	suite.adminModule.EmojiGETHandler(ctx)
     45 	suite.Equal(http.StatusOK, recorder.Code)
     46 
     47 	b, err := io.ReadAll(recorder.Body)
     48 	suite.NoError(err)
     49 	suite.NotNil(b)
     50 	dst := new(bytes.Buffer)
     51 	err = json.Indent(dst, b, "", "  ")
     52 	suite.NoError(err)
     53 	suite.Equal(`{
     54   "shortcode": "rainbow",
     55   "url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
     56   "static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01F8MH9H8E4VG3KDYJR9EGPXCQ.png",
     57   "visible_in_picker": true,
     58   "category": "reactions",
     59   "id": "01F8MH9H8E4VG3KDYJR9EGPXCQ",
     60   "disabled": false,
     61   "updated_at": "2021-09-20T10:40:37.000Z",
     62   "total_file_size": 47115,
     63   "content_type": "image/png",
     64   "uri": "http://localhost:8080/emoji/01F8MH9H8E4VG3KDYJR9EGPXCQ"
     65 }`, dst.String())
     66 }
     67 
     68 func (suite *EmojiGetTestSuite) TestEmojiGet2() {
     69 	recorder := httptest.NewRecorder()
     70 	testEmoji := suite.testEmojis["yell"]
     71 
     72 	path := admin.EmojiPathWithID
     73 	ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json")
     74 	ctx.AddParam(admin.IDKey, testEmoji.ID)
     75 
     76 	suite.adminModule.EmojiGETHandler(ctx)
     77 	suite.Equal(http.StatusOK, recorder.Code)
     78 
     79 	b, err := io.ReadAll(recorder.Body)
     80 	suite.NoError(err)
     81 	suite.NotNil(b)
     82 	dst := new(bytes.Buffer)
     83 	err = json.Indent(dst, b, "", "  ")
     84 	suite.NoError(err)
     85 	suite.Equal(`{
     86   "shortcode": "yell",
     87   "url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01GD5KP5CQEE1R3X43Y1EHS2CW.png",
     88   "static_url": "http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/static/01GD5KP5CQEE1R3X43Y1EHS2CW.png",
     89   "visible_in_picker": false,
     90   "id": "01GD5KP5CQEE1R3X43Y1EHS2CW",
     91   "disabled": false,
     92   "domain": "fossbros-anonymous.io",
     93   "updated_at": "2020-03-18T12:12:00.000Z",
     94   "total_file_size": 21697,
     95   "content_type": "image/png",
     96   "uri": "http://fossbros-anonymous.io/emoji/01GD5KP5CQEE1R3X43Y1EHS2CW"
     97 }`, dst.String())
     98 }
     99 
    100 func (suite *EmojiGetTestSuite) TestEmojiGetNotFound() {
    101 	recorder := httptest.NewRecorder()
    102 
    103 	path := admin.EmojiPathWithID
    104 	ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json")
    105 	ctx.AddParam(admin.IDKey, "01GF8VRXX1R00X7XH8973Z29R1")
    106 
    107 	suite.adminModule.EmojiGETHandler(ctx)
    108 	suite.Equal(http.StatusNotFound, recorder.Code)
    109 
    110 	b, err := io.ReadAll(recorder.Body)
    111 	suite.NoError(err)
    112 	suite.NotNil(b)
    113 	suite.Equal(`{"error":"Not Found"}`, string(b))
    114 }
    115 
    116 func TestEmojiGetTestSuite(t *testing.T) {
    117 	suite.Run(t, &EmojiGetTestSuite{})
    118 }