summaryrefslogtreecommitdiff
path: root/internal/api/client/media/mediacreate_test.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-01-30 16:10:53 +0100
committerLibravatar GitHub <noreply@github.com>2022-01-30 16:10:53 +0100
commit66175c8ad94485338068c264e6b0fb00044e9fd6 (patch)
treed2d5fc96a9af044a84490f49542f3d405a4a174e /internal/api/client/media/mediacreate_test.go
parentBump nanoid from 3.1.25 to 3.2.0 in /web/gotosocial-styling (#364) (diff)
downloadgotosocial-66175c8ad94485338068c264e6b0fb00044e9fd6.tar.xz
[bug] Fix minimum description length check (#369)
* add/update tests * don't check min description length on media create
Diffstat (limited to 'internal/api/client/media/mediacreate_test.go')
-rw-r--r--internal/api/client/media/mediacreate_test.go105
1 files changed, 93 insertions, 12 deletions
diff --git a/internal/api/client/media/mediacreate_test.go b/internal/api/client/media/mediacreate_test.go
index 2897d786b..2d8042e33 100644
--- a/internal/api/client/media/mediacreate_test.go
+++ b/internal/api/client/media/mediacreate_test.go
@@ -21,6 +21,8 @@ package media_test
import (
"bytes"
"context"
+ "crypto/rand"
+ "encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
@@ -31,10 +33,11 @@ import (
"codeberg.org/gruf/go-store/kv"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
- "github.com/stretchr/testify/assert"
+ "github.com/spf13/viper"
"github.com/stretchr/testify/suite"
mediamodule "github.com/superseriousbusiness/gotosocial/internal/api/client/media"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/email"
"github.com/superseriousbusiness/gotosocial/internal/federation"
@@ -117,7 +120,7 @@ func (suite *MediaCreateTestSuite) TearDownTest() {
ACTUAL TESTS
*/
-func (suite *MediaCreateTestSuite) TestStatusCreatePOSTImageHandlerSuccessful() {
+func (suite *MediaCreateTestSuite) TestMediaCreateSuccessful() {
// set up the context for the request
t := suite.testTokens["local_account_1"]
oauthToken := oauth.DBTokenToToken(t)
@@ -171,16 +174,16 @@ func (suite *MediaCreateTestSuite) TestStatusCreatePOSTImageHandlerSuccessful()
result := recorder.Result()
defer result.Body.Close()
b, err := ioutil.ReadAll(result.Body)
- assert.NoError(suite.T(), err)
+ suite.NoError(err)
fmt.Println(string(b))
attachmentReply := &model.Attachment{}
err = json.Unmarshal(b, attachmentReply)
- assert.NoError(suite.T(), err)
+ suite.NoError(err)
- assert.Equal(suite.T(), "this is a test image -- a cool background from somewhere", attachmentReply.Description)
- assert.Equal(suite.T(), "image", attachmentReply.Type)
- assert.EqualValues(suite.T(), model.MediaMeta{
+ suite.Equal("this is a test image -- a cool background from somewhere", attachmentReply.Description)
+ suite.Equal("image", attachmentReply.Type)
+ suite.EqualValues(model.MediaMeta{
Original: model.MediaDimensions{
Width: 1920,
Height: 1080,
@@ -198,11 +201,89 @@ func (suite *MediaCreateTestSuite) TestStatusCreatePOSTImageHandlerSuccessful()
Y: 0.5,
},
}, attachmentReply.Meta)
- assert.Equal(suite.T(), "LjBzUo#6RQR._NvzRjWF?urqV@a$", attachmentReply.Blurhash)
- assert.NotEmpty(suite.T(), attachmentReply.ID)
- assert.NotEmpty(suite.T(), attachmentReply.URL)
- assert.NotEmpty(suite.T(), attachmentReply.PreviewURL)
- assert.Equal(suite.T(), len(storageKeysBeforeRequest)+2, len(storageKeysAfterRequest)) // 2 images should be added to storage: the original and the thumbnail
+ suite.Equal("LjBzUo#6RQR._NvzRjWF?urqV@a$", attachmentReply.Blurhash)
+ suite.NotEmpty(attachmentReply.ID)
+ suite.NotEmpty(attachmentReply.URL)
+ suite.NotEmpty(attachmentReply.PreviewURL)
+ suite.Equal(len(storageKeysBeforeRequest)+2, len(storageKeysAfterRequest)) // 2 images should be added to storage: the original and the thumbnail
+}
+
+func (suite *MediaCreateTestSuite) TestMediaCreateLongDescription() {
+ // set up the context for the request
+ t := suite.testTokens["local_account_1"]
+ oauthToken := oauth.DBTokenToToken(t)
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
+ ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
+ ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
+ ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
+
+ // read a random string of a really long description
+ descriptionBytes := make([]byte, 5000)
+ if _, err := rand.Read(descriptionBytes); err != nil {
+ panic(err)
+ }
+ description := base64.RawStdEncoding.EncodeToString(descriptionBytes)
+
+ // create the request
+ buf, w, err := testrig.CreateMultipartFormData("file", "../../../../testrig/media/test-jpeg.jpg", map[string]string{
+ "description": description,
+ "focus": "-0.5,0.5",
+ })
+ if err != nil {
+ panic(err)
+ }
+ ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePath), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting
+ ctx.Request.Header.Set("Content-Type", w.FormDataContentType())
+ ctx.Request.Header.Set("accept", "application/json")
+
+ // do the actual request
+ suite.mediaModule.MediaCreatePOSTHandler(ctx)
+
+ // check response
+ suite.EqualValues(http.StatusUnprocessableEntity, recorder.Code)
+
+ result := recorder.Result()
+ defer result.Body.Close()
+ b, err := ioutil.ReadAll(result.Body)
+ suite.NoError(err)
+
+ expectedErr := fmt.Sprintf(`{"error":"image description length must be between 0 and 500 characters (inclusive), but provided image description was %d chars"}`, len(description))
+ suite.Equal(expectedErr, string(b))
+}
+
+func (suite *MediaCreateTestSuite) TestMediaCreateTooShortDescription() {
+ // set the min description length
+ viper.Set(config.Keys.MediaDescriptionMinChars, 500)
+
+ // set up the context for the request
+ t := suite.testTokens["local_account_1"]
+ oauthToken := oauth.DBTokenToToken(t)
+ recorder := httptest.NewRecorder()
+ ctx, _ := gin.CreateTestContext(recorder)
+ ctx.Set(oauth.SessionAuthorizedApplication, suite.testApplications["application_1"])
+ ctx.Set(oauth.SessionAuthorizedToken, oauthToken)
+ ctx.Set(oauth.SessionAuthorizedUser, suite.testUsers["local_account_1"])
+ ctx.Set(oauth.SessionAuthorizedAccount, suite.testAccounts["local_account_1"])
+
+ // create the request
+ buf, w, err := testrig.CreateMultipartFormData("file", "../../../../testrig/media/test-jpeg.jpg", map[string]string{
+ "description": "", // provide an empty description
+ "focus": "-0.5,0.5",
+ })
+ if err != nil {
+ panic(err)
+ }
+ ctx.Request = httptest.NewRequest(http.MethodPost, fmt.Sprintf("http://localhost:8080/%s", mediamodule.BasePath), bytes.NewReader(buf.Bytes())) // the endpoint we're hitting
+ ctx.Request.Header.Set("Content-Type", w.FormDataContentType())
+ ctx.Request.Header.Set("accept", "application/json")
+
+ // do the actual request
+ suite.mediaModule.MediaCreatePOSTHandler(ctx)
+
+ // check response -- there should be no error because minimum description length is checked on *UPDATE*, not initial upload
+ suite.EqualValues(http.StatusOK, recorder.Code)
}
func TestMediaCreateTestSuite(t *testing.T) {