2022-09-06 12:42:55 +02:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 12:43:00 +01:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2022-09-06 12:42:55 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package emoji_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/activitypub/emoji"
|
2022-09-06 12:42:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/concurrency"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/email"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/federation"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/messages"
|
2023-01-02 13:10:50 +01:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/middleware"
|
2022-09-06 12:42:55 +02:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/processing"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/storage"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EmojiGetTestSuite struct {
|
|
|
|
suite.Suite
|
2023-01-02 13:10:50 +01:00
|
|
|
db db.DB
|
|
|
|
tc typeutils.TypeConverter
|
|
|
|
mediaManager media.Manager
|
|
|
|
federator federation.Federator
|
|
|
|
emailSender email.Sender
|
|
|
|
processor processing.Processor
|
|
|
|
storage *storage.Driver
|
2022-09-06 12:42:55 +02:00
|
|
|
|
|
|
|
testEmojis map[string]*gtsmodel.Emoji
|
|
|
|
testAccounts map[string]*gtsmodel.Account
|
|
|
|
|
|
|
|
emojiModule *emoji.Module
|
2023-01-02 13:10:50 +01:00
|
|
|
|
|
|
|
signatureCheck gin.HandlerFunc
|
2022-09-06 12:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *EmojiGetTestSuite) SetupSuite() {
|
|
|
|
suite.testAccounts = testrig.NewTestAccounts()
|
|
|
|
suite.testEmojis = testrig.NewTestEmojis()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *EmojiGetTestSuite) SetupTest() {
|
|
|
|
testrig.InitTestConfig()
|
|
|
|
testrig.InitTestLog()
|
|
|
|
|
|
|
|
clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1)
|
|
|
|
fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1)
|
|
|
|
|
|
|
|
suite.db = testrig.NewTestDB()
|
|
|
|
suite.tc = testrig.NewTestTypeConverter(suite.db)
|
|
|
|
suite.storage = testrig.NewInMemoryStorage()
|
|
|
|
suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage)
|
|
|
|
suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker), suite.storage, suite.mediaManager, fedWorker)
|
|
|
|
suite.emailSender = testrig.NewEmailSender("../../../../web/template/", nil)
|
|
|
|
suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator, suite.emailSender, suite.mediaManager, clientWorker, fedWorker)
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.emojiModule = emoji.New(suite.processor)
|
2022-09-06 12:42:55 +02:00
|
|
|
testrig.StandardDBSetup(suite.db, suite.testAccounts)
|
|
|
|
testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
|
2022-09-28 19:30:40 +02:00
|
|
|
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.signatureCheck = middleware.SignatureCheck(suite.db.IsURIBlocked)
|
|
|
|
|
2022-09-28 19:30:40 +02:00
|
|
|
suite.NoError(suite.processor.Start())
|
2022-09-06 12:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *EmojiGetTestSuite) TearDownTest() {
|
|
|
|
testrig.StandardDBTeardown(suite.db)
|
|
|
|
testrig.StandardStorageTeardown(suite.storage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *EmojiGetTestSuite) TestGetEmoji() {
|
|
|
|
// the dereference we're gonna use
|
|
|
|
derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts)
|
|
|
|
signedRequest := derefRequests["foss_satan_dereference_emoji"]
|
|
|
|
targetEmoji := suite.testEmojis["rainbow"]
|
|
|
|
|
|
|
|
// setup request
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
ctx, _ := testrig.CreateGinTestContext(recorder, nil)
|
|
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, targetEmoji.URI, nil) // the endpoint we're hitting
|
|
|
|
ctx.Request.Header.Set("accept", "application/activity+json")
|
|
|
|
ctx.Request.Header.Set("Signature", signedRequest.SignatureHeader)
|
|
|
|
ctx.Request.Header.Set("Date", signedRequest.DateHeader)
|
|
|
|
|
|
|
|
// we need to pass the context through signature check first to set appropriate values on it
|
2023-01-02 13:10:50 +01:00
|
|
|
suite.signatureCheck(ctx)
|
2022-09-06 12:42:55 +02:00
|
|
|
|
|
|
|
// normally the router would populate these params from the path values,
|
|
|
|
// but because we're calling the function directly, we need to set them manually.
|
|
|
|
ctx.Params = gin.Params{
|
|
|
|
gin.Param{
|
|
|
|
Key: emoji.EmojiIDKey,
|
|
|
|
Value: targetEmoji.ID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// trigger the function being tested
|
|
|
|
suite.emojiModule.EmojiGetHandler(ctx)
|
|
|
|
|
|
|
|
// check response
|
|
|
|
suite.EqualValues(http.StatusOK, recorder.Code)
|
|
|
|
|
|
|
|
result := recorder.Result()
|
|
|
|
defer result.Body.Close()
|
|
|
|
b, err := ioutil.ReadAll(result.Body)
|
|
|
|
suite.NoError(err)
|
|
|
|
|
2022-11-30 16:20:57 +01:00
|
|
|
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"`)
|
2022-09-06 12:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmojiGetTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(EmojiGetTestSuite))
|
|
|
|
}
|