emojisget_test.go (4090B)
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 "encoding/json" 22 "io" 23 "net/http" 24 "net/http/httptest" 25 "testing" 26 27 "github.com/stretchr/testify/suite" 28 "github.com/superseriousbusiness/gotosocial/internal/api/client/admin" 29 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 30 ) 31 32 type EmojisGetTestSuite struct { 33 AdminStandardTestSuite 34 } 35 36 func (suite *EmojisGetTestSuite) TestEmojiGet() { 37 recorder := httptest.NewRecorder() 38 39 path := admin.EmojiPath + "?filter=domain:all&limit=1" 40 ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json") 41 42 suite.adminModule.EmojisGETHandler(ctx) 43 suite.Equal(http.StatusOK, recorder.Code) 44 45 b, err := io.ReadAll(recorder.Body) 46 suite.NoError(err) 47 suite.NotNil(b) 48 49 apiEmojis := []*apimodel.AdminEmoji{} 50 if err := json.Unmarshal(b, &apiEmojis); err != nil { 51 suite.FailNow(err.Error()) 52 } 53 54 suite.Len(apiEmojis, 1) 55 suite.Equal("rainbow", apiEmojis[0].Shortcode) 56 suite.Equal("", apiEmojis[0].Domain) 57 58 suite.Equal(`<http://localhost:8080/api/v1/admin/custom_emojis?limit=1&max_shortcode_domain=rainbow@&filter=domain:all>; rel="next", <http://localhost:8080/api/v1/admin/custom_emojis?limit=1&min_shortcode_domain=rainbow@&filter=domain:all>; rel="prev"`, recorder.Header().Get("link")) 59 } 60 61 func (suite *EmojisGetTestSuite) TestEmojiGet2() { 62 recorder := httptest.NewRecorder() 63 64 path := admin.EmojiPath + "?filter=domain:all&limit=1&max_shortcode_domain=rainbow@" 65 ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json") 66 67 suite.adminModule.EmojisGETHandler(ctx) 68 suite.Equal(http.StatusOK, recorder.Code) 69 70 b, err := io.ReadAll(recorder.Body) 71 suite.NoError(err) 72 suite.NotNil(b) 73 74 apiEmojis := []*apimodel.AdminEmoji{} 75 if err := json.Unmarshal(b, &apiEmojis); err != nil { 76 suite.FailNow(err.Error()) 77 } 78 79 suite.Len(apiEmojis, 1) 80 suite.Equal("yell", apiEmojis[0].Shortcode) 81 suite.Equal("fossbros-anonymous.io", apiEmojis[0].Domain) 82 83 suite.Equal(`<http://localhost:8080/api/v1/admin/custom_emojis?limit=1&max_shortcode_domain=yell@fossbros-anonymous.io&filter=domain:all>; rel="next", <http://localhost:8080/api/v1/admin/custom_emojis?limit=1&min_shortcode_domain=yell@fossbros-anonymous.io&filter=domain:all>; rel="prev"`, recorder.Header().Get("link")) 84 } 85 86 func (suite *EmojisGetTestSuite) TestEmojiGet3() { 87 recorder := httptest.NewRecorder() 88 89 path := admin.EmojiPath + "?filter=domain:all&limit=1&min_shortcode_domain=yell@fossbros-anonymous.io" 90 ctx := suite.newContext(recorder, http.MethodGet, nil, path, "application/json") 91 92 suite.adminModule.EmojisGETHandler(ctx) 93 suite.Equal(http.StatusOK, recorder.Code) 94 95 b, err := io.ReadAll(recorder.Body) 96 suite.NoError(err) 97 suite.NotNil(b) 98 99 apiEmojis := []*apimodel.AdminEmoji{} 100 if err := json.Unmarshal(b, &apiEmojis); err != nil { 101 suite.FailNow(err.Error()) 102 } 103 104 suite.Len(apiEmojis, 1) 105 suite.Equal("rainbow", apiEmojis[0].Shortcode) 106 suite.Equal("", apiEmojis[0].Domain) 107 108 suite.Equal(`<http://localhost:8080/api/v1/admin/custom_emojis?limit=1&max_shortcode_domain=rainbow@&filter=domain:all>; rel="next", <http://localhost:8080/api/v1/admin/custom_emojis?limit=1&min_shortcode_domain=rainbow@&filter=domain:all>; rel="prev"`, recorder.Header().Get("link")) 109 } 110 111 func TestEmojisGetTestSuite(t *testing.T) { 112 suite.Run(t, &EmojisGetTestSuite{}) 113 }