emojiget_test.go (5369B)
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 emoji_test 19 20 import ( 21 "io/ioutil" 22 "net/http" 23 "net/http/httptest" 24 "testing" 25 26 "github.com/gin-gonic/gin" 27 "github.com/stretchr/testify/suite" 28 "github.com/superseriousbusiness/gotosocial/internal/api/activitypub/emoji" 29 "github.com/superseriousbusiness/gotosocial/internal/db" 30 "github.com/superseriousbusiness/gotosocial/internal/email" 31 "github.com/superseriousbusiness/gotosocial/internal/federation" 32 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 33 "github.com/superseriousbusiness/gotosocial/internal/media" 34 "github.com/superseriousbusiness/gotosocial/internal/middleware" 35 "github.com/superseriousbusiness/gotosocial/internal/processing" 36 "github.com/superseriousbusiness/gotosocial/internal/state" 37 "github.com/superseriousbusiness/gotosocial/internal/storage" 38 "github.com/superseriousbusiness/gotosocial/internal/typeutils" 39 "github.com/superseriousbusiness/gotosocial/internal/visibility" 40 "github.com/superseriousbusiness/gotosocial/testrig" 41 ) 42 43 type EmojiGetTestSuite struct { 44 suite.Suite 45 db db.DB 46 tc typeutils.TypeConverter 47 mediaManager *media.Manager 48 federator federation.Federator 49 emailSender email.Sender 50 processor *processing.Processor 51 storage *storage.Driver 52 state state.State 53 54 testEmojis map[string]*gtsmodel.Emoji 55 testAccounts map[string]*gtsmodel.Account 56 57 emojiModule *emoji.Module 58 59 signatureCheck gin.HandlerFunc 60 } 61 62 func (suite *EmojiGetTestSuite) SetupSuite() { 63 suite.testAccounts = testrig.NewTestAccounts() 64 suite.testEmojis = testrig.NewTestEmojis() 65 } 66 67 func (suite *EmojiGetTestSuite) SetupTest() { 68 suite.state.Caches.Init() 69 testrig.StartWorkers(&suite.state) 70 71 testrig.InitTestConfig() 72 testrig.InitTestLog() 73 74 suite.db = testrig.NewTestDB(&suite.state) 75 suite.state.DB = suite.db 76 suite.storage = testrig.NewInMemoryStorage() 77 suite.state.Storage = suite.storage 78 suite.tc = testrig.NewTestTypeConverter(suite.db) 79 80 testrig.StartTimelines( 81 &suite.state, 82 visibility.NewFilter(&suite.state), 83 suite.tc, 84 ) 85 86 suite.mediaManager = testrig.NewTestMediaManager(&suite.state) 87 suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")), suite.mediaManager) 88 suite.emailSender = testrig.NewEmailSender("../../../../web/template/", nil) 89 suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager) 90 suite.emojiModule = emoji.New(suite.processor) 91 testrig.StandardDBSetup(suite.db, suite.testAccounts) 92 testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media") 93 94 suite.signatureCheck = middleware.SignatureCheck(suite.db.IsURIBlocked) 95 } 96 97 func (suite *EmojiGetTestSuite) TearDownTest() { 98 testrig.StandardDBTeardown(suite.db) 99 testrig.StandardStorageTeardown(suite.storage) 100 testrig.StopWorkers(&suite.state) 101 } 102 103 func (suite *EmojiGetTestSuite) TestGetEmoji() { 104 // the dereference we're gonna use 105 derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts) 106 signedRequest := derefRequests["foss_satan_dereference_emoji"] 107 targetEmoji := suite.testEmojis["rainbow"] 108 109 // setup request 110 recorder := httptest.NewRecorder() 111 ctx, _ := testrig.CreateGinTestContext(recorder, nil) 112 ctx.Request = httptest.NewRequest(http.MethodGet, targetEmoji.URI, nil) // the endpoint we're hitting 113 ctx.Request.Header.Set("accept", "application/activity+json") 114 ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader) 115 ctx.Request.Header.Set("Date", signedRequest.DateHeader) 116 117 // we need to pass the context through signature check first to set appropriate values on it 118 suite.signatureCheck(ctx) 119 120 // normally the router would populate these params from the path values, 121 // but because we're calling the function directly, we need to set them manually. 122 ctx.Params = gin.Params{ 123 gin.Param{ 124 Key: emoji.EmojiIDKey, 125 Value: targetEmoji.ID, 126 }, 127 } 128 129 // trigger the function being tested 130 suite.emojiModule.EmojiGetHandler(ctx) 131 132 // check response 133 suite.EqualValues(http.StatusOK, recorder.Code) 134 135 result := recorder.Result() 136 defer result.Body.Close() 137 b, err := ioutil.ReadAll(result.Body) 138 suite.NoError(err) 139 140 suite.Contains(string(b), `"icon":{"mediaType":"image/png","type":"Image","url":"http://localhost:8080/fileserver/01AY6P665V14JJR0AFVRT7311Y/emoji/original/01F8MH9H8E4VG3KDYJR9EGPXCQ.png"},"id":"http://localhost:8080/emoji/01F8MH9H8E4VG3KDYJR9EGPXCQ","name":":rainbow:","type":"Emoji"`) 141 } 142 143 func TestEmojiGetTestSuite(t *testing.T) { 144 suite.Run(t, new(EmojiGetTestSuite)) 145 }