diff options
| author | 2022-01-11 17:49:14 +0100 | |
|---|---|---|
| committer | 2022-01-11 17:49:14 +0100 | |
| commit | 113f9d9ab4797de6ae17819c96ae866992214021 (patch) | |
| tree | c4e165e66e66db7b9e20d34ccb45f7bda096b532 /internal/api/client/admin | |
| parent | test the media manager a bit, add shutdown logic (diff) | |
| download | gotosocial-113f9d9ab4797de6ae17819c96ae866992214021.tar.xz | |
pass a function into the manager, start work on emoji
Diffstat (limited to 'internal/api/client/admin')
| -rw-r--r-- | internal/api/client/admin/admin.go | 2 | ||||
| -rw-r--r-- | internal/api/client/admin/admin_test.go | 123 | ||||
| -rw-r--r-- | internal/api/client/admin/emojicreate.go | 4 | ||||
| -rw-r--r-- | internal/api/client/admin/emojicreate_test.go | 50 | 
4 files changed, 176 insertions, 3 deletions
| diff --git a/internal/api/client/admin/admin.go b/internal/api/client/admin/admin.go index f8ea03cc6..f5256c996 100644 --- a/internal/api/client/admin/admin.go +++ b/internal/api/client/admin/admin.go @@ -58,7 +58,7 @@ func New(processor processing.Processor) api.ClientModule {  // Route attaches all routes from this module to the given router  func (m *Module) Route(r router.Router) error { -	r.AttachHandler(http.MethodPost, EmojiPath, m.emojiCreatePOSTHandler) +	r.AttachHandler(http.MethodPost, EmojiPath, m.EmojiCreatePOSTHandler)  	r.AttachHandler(http.MethodPost, DomainBlocksPath, m.DomainBlocksPOSTHandler)  	r.AttachHandler(http.MethodGet, DomainBlocksPath, m.DomainBlocksGETHandler)  	r.AttachHandler(http.MethodGet, DomainBlocksPathWithID, m.DomainBlockGETHandler) diff --git a/internal/api/client/admin/admin_test.go b/internal/api/client/admin/admin_test.go new file mode 100644 index 000000000..da5b03949 --- /dev/null +++ b/internal/api/client/admin/admin_test.go @@ -0,0 +1,123 @@ +/* +   GoToSocial +   Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org + +   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 admin_test + +import ( +	"bytes" +	"fmt" +	"net/http" +	"net/http/httptest" + +	"codeberg.org/gruf/go-store/kv" +	"github.com/gin-gonic/gin" +	"github.com/spf13/viper" +	"github.com/stretchr/testify/suite" +	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin" +	"github.com/superseriousbusiness/gotosocial/internal/config" +	"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/oauth" +	"github.com/superseriousbusiness/gotosocial/internal/processing" +	"github.com/superseriousbusiness/gotosocial/internal/typeutils" +	"github.com/superseriousbusiness/gotosocial/testrig" +) + +type AdminStandardTestSuite struct { +	// standard suite interfaces +	suite.Suite +	db           db.DB +	tc           typeutils.TypeConverter +	storage      *kv.KVStore +	mediaManager media.Manager +	federator    federation.Federator +	processor    processing.Processor +	emailSender  email.Sender +	sentEmails   map[string]string + +	// standard suite models +	testTokens       map[string]*gtsmodel.Token +	testClients      map[string]*gtsmodel.Client +	testApplications map[string]*gtsmodel.Application +	testUsers        map[string]*gtsmodel.User +	testAccounts     map[string]*gtsmodel.Account +	testAttachments  map[string]*gtsmodel.MediaAttachment +	testStatuses     map[string]*gtsmodel.Status + +	// module being tested +	adminModule *admin.Module +} + +func (suite *AdminStandardTestSuite) SetupSuite() { +	suite.testTokens = testrig.NewTestTokens() +	suite.testClients = testrig.NewTestClients() +	suite.testApplications = testrig.NewTestApplications() +	suite.testUsers = testrig.NewTestUsers() +	suite.testAccounts = testrig.NewTestAccounts() +	suite.testAttachments = testrig.NewTestAttachments() +	suite.testStatuses = testrig.NewTestStatuses() +} + +func (suite *AdminStandardTestSuite) SetupTest() { +	testrig.InitTestConfig() +	testrig.InitTestLog() + +	suite.db = testrig.NewTestDB() +	suite.storage = testrig.NewTestStorage() +	suite.mediaManager = testrig.NewTestMediaManager(suite.db, suite.storage) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db), suite.storage, suite.mediaManager) +	suite.sentEmails = make(map[string]string) +	suite.emailSender = testrig.NewEmailSender("../../../../web/template/", suite.sentEmails) +	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator, suite.emailSender, suite.mediaManager) +	suite.adminModule = admin.New(suite.processor).(*admin.Module) +	testrig.StandardDBSetup(suite.db, nil) +	testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media") +} + +func (suite *AdminStandardTestSuite) TearDownTest() { +	testrig.StandardDBTeardown(suite.db) +	testrig.StandardStorageTeardown(suite.storage) +} + +func (suite *AdminStandardTestSuite) newContext(recorder *httptest.ResponseRecorder, requestMethod string, requestBody []byte, requestPath string, bodyContentType string) *gin.Context { +	ctx, _ := gin.CreateTestContext(recorder) + +	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["admin_account"]) +	ctx.Set(oauth.SessionAuthorizedToken, oauth.DBTokenToToken(suite.testTokens["admin_account"])) +	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["admin_account"]) +	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["admin_account"]) + +	protocol := viper.GetString(config.Keys.Protocol) +	host := viper.GetString(config.Keys.Host) + +	baseURI := fmt.Sprintf("%s://%s", protocol, host) +	requestURI := fmt.Sprintf("%s/%s", baseURI, requestPath) + +	ctx.Request = httptest.NewRequest(http.MethodPatch, requestURI, bytes.NewReader(requestBody)) // the endpoint we're hitting + +	if bodyContentType != "" { +		ctx.Request.Header.Set("Content-Type", bodyContentType) +	} + +	ctx.Request.Header.Set("accept", "application/json") + +	return ctx +} diff --git a/internal/api/client/admin/emojicreate.go b/internal/api/client/admin/emojicreate.go index 882654ea9..de7a2979a 100644 --- a/internal/api/client/admin/emojicreate.go +++ b/internal/api/client/admin/emojicreate.go @@ -31,7 +31,7 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/validate"  ) -// emojiCreateRequest swagger:operation POST /api/v1/admin/custom_emojis emojiCreate +// EmojiCreatePOSTHandler swagger:operation POST /api/v1/admin/custom_emojis emojiCreate  //  // Upload and create a new instance emoji.  // @@ -73,7 +73,7 @@ import (  //      description: forbidden  //   '400':  //      description: bad request -func (m *Module) emojiCreatePOSTHandler(c *gin.Context) { +func (m *Module) EmojiCreatePOSTHandler(c *gin.Context) {  	l := logrus.WithFields(logrus.Fields{  		"func":        "emojiCreatePOSTHandler",  		"request_uri": c.Request.RequestURI, diff --git a/internal/api/client/admin/emojicreate_test.go b/internal/api/client/admin/emojicreate_test.go new file mode 100644 index 000000000..290b478f7 --- /dev/null +++ b/internal/api/client/admin/emojicreate_test.go @@ -0,0 +1,50 @@ +package admin_test + +import ( +	"io/ioutil" +	"net/http" +	"net/http/httptest" +	"testing" + +	"github.com/stretchr/testify/suite" +	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin" +	"github.com/superseriousbusiness/gotosocial/testrig" +) + +type EmojiCreateTestSuite struct { +	AdminStandardTestSuite +} + +func (suite *EmojiCreateTestSuite) TestEmojiCreate() { +	// set up the request +	requestBody, w, err := testrig.CreateMultipartFormData( +		"image", "../../../../testrig/media/rainbow-original.png", +		map[string]string{ +			"shortcode": "rainbow", +		}) +	if err != nil { +		panic(err) +	} +	bodyBytes := requestBody.Bytes() +	recorder := httptest.NewRecorder() +	ctx := suite.newContext(recorder, http.MethodPost, bodyBytes, admin.EmojiPath, w.FormDataContentType()) + +	// call the handler +	suite.adminModule.EmojiCreatePOSTHandler(ctx) + +	// 1. we should have OK because our request was valid +	suite.Equal(http.StatusOK, recorder.Code) + +	// 2. we should have no error message in the result body +	result := recorder.Result() +	defer result.Body.Close() + +	// check the response +	b, err := ioutil.ReadAll(result.Body) +	suite.NoError(err) +	suite.NotEmpty(b) +} + +func TestEmojiCreateTestSuite(t *testing.T) { +	suite.Run(t, &EmojiCreateTestSuite{}) +} | 
