gtsocial-umbx

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

mediaupdate_test.go (8993B)


      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 media_test
     19 
     20 import (
     21 	"bytes"
     22 	"context"
     23 	"encoding/json"
     24 	"fmt"
     25 	"io/ioutil"
     26 	"net/http"
     27 	"net/http/httptest"
     28 	"testing"
     29 
     30 	"github.com/stretchr/testify/suite"
     31 	mediamodule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
     32 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     33 	"github.com/superseriousbusiness/gotosocial/internal/config"
     34 	"github.com/superseriousbusiness/gotosocial/internal/db"
     35 	"github.com/superseriousbusiness/gotosocial/internal/email"
     36 	"github.com/superseriousbusiness/gotosocial/internal/federation"
     37 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     38 	"github.com/superseriousbusiness/gotosocial/internal/log"
     39 	"github.com/superseriousbusiness/gotosocial/internal/media"
     40 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     41 	"github.com/superseriousbusiness/gotosocial/internal/processing"
     42 	"github.com/superseriousbusiness/gotosocial/internal/state"
     43 	"github.com/superseriousbusiness/gotosocial/internal/storage"
     44 	"github.com/superseriousbusiness/gotosocial/internal/typeutils"
     45 	"github.com/superseriousbusiness/gotosocial/internal/visibility"
     46 	"github.com/superseriousbusiness/gotosocial/testrig"
     47 )
     48 
     49 type MediaUpdateTestSuite struct {
     50 	// standard suite interfaces
     51 	suite.Suite
     52 	db           db.DB
     53 	storage      *storage.Driver
     54 	federator    federation.Federator
     55 	tc           typeutils.TypeConverter
     56 	mediaManager *media.Manager
     57 	oauthServer  oauth.Server
     58 	emailSender  email.Sender
     59 	processor    *processing.Processor
     60 	state        state.State
     61 
     62 	// standard suite models
     63 	testTokens       map[string]*gtsmodel.Token
     64 	testClients      map[string]*gtsmodel.Client
     65 	testApplications map[string]*gtsmodel.Application
     66 	testUsers        map[string]*gtsmodel.User
     67 	testAccounts     map[string]*gtsmodel.Account
     68 	testAttachments  map[string]*gtsmodel.MediaAttachment
     69 
     70 	// item being tested
     71 	mediaModule *mediamodule.Module
     72 }
     73 
     74 /*
     75 	TEST INFRASTRUCTURE
     76 */
     77 
     78 func (suite *MediaUpdateTestSuite) SetupSuite() {
     79 	testrig.StartWorkers(&suite.state)
     80 
     81 	// setup standard items
     82 	testrig.InitTestConfig()
     83 	testrig.InitTestLog()
     84 
     85 	suite.db = testrig.NewTestDB(&suite.state)
     86 	suite.state.DB = suite.db
     87 	suite.storage = testrig.NewInMemoryStorage()
     88 	suite.state.Storage = suite.storage
     89 
     90 	suite.tc = testrig.NewTestTypeConverter(suite.db)
     91 
     92 	testrig.StartTimelines(
     93 		&suite.state,
     94 		visibility.NewFilter(&suite.state),
     95 		suite.tc,
     96 	)
     97 
     98 	suite.mediaManager = testrig.NewTestMediaManager(&suite.state)
     99 	suite.oauthServer = testrig.NewTestOauthServer(suite.db)
    100 	suite.federator = testrig.NewTestFederator(&suite.state, testrig.NewTestTransportController(&suite.state, testrig.NewMockHTTPClient(nil, "../../../../testrig/media")), suite.mediaManager)
    101 	suite.emailSender = testrig.NewEmailSender("../../../../web/template/", nil)
    102 	suite.processor = testrig.NewTestProcessor(&suite.state, suite.federator, suite.emailSender, suite.mediaManager)
    103 
    104 	// setup module being tested
    105 	suite.mediaModule = mediamodule.New(suite.processor)
    106 }
    107 
    108 func (suite *MediaUpdateTestSuite) TearDownSuite() {
    109 	if err := suite.db.Stop(context.Background()); err != nil {
    110 		log.Panicf(nil, "error closing db connection: %s", err)
    111 	}
    112 	testrig.StopWorkers(&suite.state)
    113 }
    114 
    115 func (suite *MediaUpdateTestSuite) SetupTest() {
    116 	suite.state.Caches.Init()
    117 
    118 	testrig.StandardDBSetup(suite.db, nil)
    119 	testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
    120 
    121 	suite.testTokens = testrig.NewTestTokens()
    122 	suite.testClients = testrig.NewTestClients()
    123 	suite.testApplications = testrig.NewTestApplications()
    124 	suite.testUsers = testrig.NewTestUsers()
    125 	suite.testAccounts = testrig.NewTestAccounts()
    126 	suite.testAttachments = testrig.NewTestAttachments()
    127 }
    128 
    129 func (suite *MediaUpdateTestSuite) TearDownTest() {
    130 	testrig.StandardDBTeardown(suite.db)
    131 	testrig.StandardStorageTeardown(suite.storage)
    132 }
    133 
    134 /*
    135 	ACTUAL TESTS
    136 */
    137 
    138 func (suite *MediaUpdateTestSuite) TestUpdateImage() {
    139 	toUpdate := suite.testAttachments["local_account_1_unattached_1"]
    140 
    141 	// set up the context for the request
    142 	t := suite.testTokens["local_account_1"]
    143 	oauthToken := oauth.DBTokenToToken(t)
    144 	recorder := httptest.NewRecorder()
    145 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    146 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
    147 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
    148 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
    149 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
    150 
    151 	// create the request
    152 	buf, w, err := testrig.CreateMultipartFormData("", "", map[string]string{
    153 		"id":          toUpdate.ID,
    154 		"description": "new description!",
    155 		"focus":       "-0.1,0.3",
    156 	})
    157 	if err != nil {
    158 		panic(err)
    159 	}
    160 	ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/api/v1/media/%s", toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting
    161 	ctx.Request.Header.Set("Content-Type", w.FormDataContentType())
    162 	ctx.Request.Header.Set("accept", "application/json")
    163 	ctx.AddParam(mediamodule.APIVersionKey, mediamodule.APIv1)
    164 	ctx.AddParam(mediamodule.IDKey, toUpdate.ID)
    165 
    166 	// do the actual request
    167 	suite.mediaModule.MediaPUTHandler(ctx)
    168 
    169 	// check response
    170 	suite.EqualValues(http.StatusOK, recorder.Code)
    171 
    172 	result := recorder.Result()
    173 	defer result.Body.Close()
    174 	b, err := ioutil.ReadAll(result.Body)
    175 	suite.NoError(err)
    176 
    177 	// reply should be an attachment
    178 	attachmentReply := &apimodel.Attachment{}
    179 	err = json.Unmarshal(b, attachmentReply)
    180 	suite.NoError(err)
    181 
    182 	// the reply should contain the updated fields
    183 	suite.Equal("new description!", *attachmentReply.Description)
    184 	suite.EqualValues("image", attachmentReply.Type)
    185 	suite.EqualValues(apimodel.MediaMeta{
    186 		Original: apimodel.MediaDimensions{Width: 800, Height: 450, FrameRate: "", Duration: 0, Bitrate: 0, Size: "800x450", Aspect: 1.7777778},
    187 		Small:    apimodel.MediaDimensions{Width: 256, Height: 144, FrameRate: "", Duration: 0, Bitrate: 0, Size: "256x144", Aspect: 1.7777778},
    188 		Focus:    &apimodel.MediaFocus{X: -0.1, Y: 0.3},
    189 	}, attachmentReply.Meta)
    190 	suite.Equal(toUpdate.Blurhash, attachmentReply.Blurhash)
    191 	suite.Equal(toUpdate.ID, attachmentReply.ID)
    192 	suite.Equal(toUpdate.URL, *attachmentReply.URL)
    193 	suite.NotEmpty(toUpdate.Thumbnail.URL, attachmentReply.PreviewURL)
    194 }
    195 
    196 func (suite *MediaUpdateTestSuite) TestUpdateImageShortDescription() {
    197 	// set the min description length
    198 	config.SetMediaDescriptionMinChars(50)
    199 
    200 	toUpdate := suite.testAttachments["local_account_1_unattached_1"]
    201 
    202 	// set up the context for the request
    203 	t := suite.testTokens["local_account_1"]
    204 	oauthToken := oauth.DBTokenToToken(t)
    205 	recorder := httptest.NewRecorder()
    206 	ctx, _ := testrig.CreateGinTestContext(recorder, nil)
    207 	ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
    208 	ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
    209 	ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
    210 	ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
    211 
    212 	// create the request
    213 	buf, w, err := testrig.CreateMultipartFormData("", "", map[string]string{
    214 		"id":          toUpdate.ID,
    215 		"description": "new description!",
    216 		"focus":       "-0.1,0.3",
    217 	})
    218 	if err != nil {
    219 		panic(err)
    220 	}
    221 	ctx.Request = httptest.NewRequest(http.MethodPut, fmt.Sprintf("http://localhost:8080/api/v1/media/%s", toUpdate.ID), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting
    222 	ctx.Request.Header.Set("Content-Type", w.FormDataContentType())
    223 	ctx.Request.Header.Set("accept", "application/json")
    224 	ctx.AddParam(mediamodule.APIVersionKey, mediamodule.APIv1)
    225 	ctx.AddParam(mediamodule.IDKey, toUpdate.ID)
    226 
    227 	// do the actual request
    228 	suite.mediaModule.MediaPUTHandler(ctx)
    229 
    230 	// check response
    231 	suite.EqualValues(http.StatusBadRequest, recorder.Code)
    232 
    233 	result := recorder.Result()
    234 	defer result.Body.Close()
    235 	b, err := ioutil.ReadAll(result.Body)
    236 	suite.NoError(err)
    237 
    238 	// reply should be an error message
    239 	suite.Equal(`{"error":"Bad Request: image description length must be between 50 and 500 characters (inclusive), but provided image description was 16 chars"}`, string(b))
    240 }
    241 
    242 func TestMediaUpdateTestSuite(t *testing.T) {
    243 	suite.Run(t, new(MediaUpdateTestSuite))
    244 }