gotosocial/internal/api/client/accounts/accountupdate_test.go

497 lines
15 KiB
Go
Raw Normal View History

// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 accounts_test
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/api/client/accounts"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type AccountUpdateTestSuite struct {
AccountStandardTestSuite
}
func (suite *AccountUpdateTestSuite) updateAccountFromForm(data map[string]string, expectedHTTPStatus int, expectedBody string) (*apimodel.Account, error) {
form := url.Values{}
for key, val := range data {
form[key] = []string{val}
}
return suite.updateAccount([]byte(form.Encode()), "application/x-www-form-urlencoded", expectedHTTPStatus, expectedBody)
}
func (suite *AccountUpdateTestSuite) updateAccountFromFormData(data map[string]string, expectedHTTPStatus int, expectedBody string) (*apimodel.Account, error) {
requestBody, w, err := testrig.CreateMultipartFormData("", "", data)
if err != nil {
suite.FailNow(err.Error())
}
return suite.updateAccount(requestBody.Bytes(), w.FormDataContentType(), expectedHTTPStatus, expectedBody)
}
func (suite *AccountUpdateTestSuite) updateAccountFromFormDataWithFile(fieldName string, fileName string, data map[string]string, expectedHTTPStatus int, expectedBody string) (*apimodel.Account, error) {
requestBody, w, err := testrig.CreateMultipartFormData(fieldName, fileName, data)
if err != nil {
suite.FailNow(err.Error())
}
return suite.updateAccount(requestBody.Bytes(), w.FormDataContentType(), expectedHTTPStatus, expectedBody)
}
func (suite *AccountUpdateTestSuite) updateAccountFromJSON(data string, expectedHTTPStatus int, expectedBody string) (*apimodel.Account, error) {
return suite.updateAccount([]byte(data), "application/json", expectedHTTPStatus, expectedBody)
}
func (suite *AccountUpdateTestSuite) updateAccount(
bodyBytes []byte,
contentType string,
expectedHTTPStatus int,
expectedBody string,
) (*apimodel.Account, error) {
// Initialize http test context.
recorder := httptest.NewRecorder()
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdatePath, contentType)
// Trigger the handler.
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
// Read the result.
result := recorder.Result()
defer result.Body.Close()
b, err := io.ReadAll(result.Body)
if err != nil {
return nil, err
}
errs := gtserror.NewMultiError(2)
// Check expected code + body.
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
errs.Appendf("expected %d got %d", expectedHTTPStatus, resultCode)
}
// If we got an expected body, return early.
if expectedBody != "" && string(b) != expectedBody {
errs.Appendf("expected %s got %s", expectedBody, string(b))
}
if err := errs.Combine(); err != nil {
return nil, fmt.Errorf("%v (body %s)", err, string(b))
}
// Return account response.
resp := &apimodel.Account{}
if err := json.Unmarshal(b, resp); err != nil {
return nil, err
}
return resp, nil
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountBasicForm() {
data := map[string]string{
"note": "this is my new bio read it and weep",
"fields_attributes[0][name]": "pronouns",
"fields_attributes[0][value]": "they/them",
"fields_attributes[1][name]": "Website",
"fields_attributes[1][value]": "https://example.com",
}
apimodelAccount, err := suite.updateAccountFromForm(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
suite.Equal("this is my new bio read it and weep", apimodelAccount.Source.Note)
if l := len(apimodelAccount.Fields); l != 2 {
suite.FailNow("", "expected %d fields, got %d", 2, l)
}
suite.Equal(`pronouns`, apimodelAccount.Fields[0].Name)
suite.Equal(`they/them`, apimodelAccount.Fields[0].Value)
suite.Equal(`Website`, apimodelAccount.Fields[1].Name)
suite.Equal(`<a href="https://example.com" rel="nofollow noreferrer noopener" target="_blank">https://example.com</a>`, apimodelAccount.Fields[1].Value)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountBasicFormData() {
data := map[string]string{
"note": "this is my new bio read it and weep",
"fields_attributes[0][name]": "pronouns",
"fields_attributes[0][value]": "they/them",
"fields_attributes[1][name]": "Website",
"fields_attributes[1][value]": "https://example.com",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
suite.Equal("this is my new bio read it and weep", apimodelAccount.Source.Note)
if l := len(apimodelAccount.Fields); l != 2 {
suite.FailNow("", "expected %d fields, got %d", 2, l)
}
suite.Equal(`pronouns`, apimodelAccount.Fields[0].Name)
suite.Equal(`they/them`, apimodelAccount.Fields[0].Value)
suite.Equal(`Website`, apimodelAccount.Fields[1].Name)
suite.Equal(`<a href="https://example.com" rel="nofollow noreferrer noopener" target="_blank">https://example.com</a>`, apimodelAccount.Fields[1].Value)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountBasicJSON() {
data := `
{
"note": "this is my new bio read it and weep",
"fields_attributes": {
"0": {
"name": "pronouns",
"value": "they/them"
},
"1": {
"name": "Website",
"value": "https://example.com"
}
}
}
`
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
suite.Equal("this is my new bio read it and weep", apimodelAccount.Source.Note)
if l := len(apimodelAccount.Fields); l != 2 {
suite.FailNow("", "expected %d fields, got %d", 2, l)
}
suite.Equal(`pronouns`, apimodelAccount.Fields[0].Name)
suite.Equal(`they/them`, apimodelAccount.Fields[0].Value)
suite.Equal(`Website`, apimodelAccount.Fields[1].Name)
suite.Equal(`<a href="https://example.com" rel="nofollow noreferrer noopener" target="_blank">https://example.com</a>`, apimodelAccount.Fields[1].Value)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountLockForm() {
data := map[string]string{
"locked": "true",
}
apimodelAccount, err := suite.updateAccountFromForm(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountLockFormData() {
data := map[string]string{
"locked": "true",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountLockJSON() {
data := `
{
"locked": true
}`
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountUnlockForm() {
data := map[string]string{
"locked": "false",
}
apimodelAccount, err := suite.updateAccountFromForm(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountUnlockFormData() {
data := map[string]string{
"locked": "false",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountUnlockJSON() {
data := `
{
"locked": false
}`
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountCache() {
// Get the account first to make sure it's in the database
// cache. When the account is updated via the PATCH handler,
// it should invalidate the cache and return the new version.
if _, err := suite.db.GetAccountByID(context.Background(), suite.testAccounts["local_account_1"].ID); err != nil {
suite.FailNow(err.Error())
}
data := map[string]string{
"note": "this is my new bio read it and weep",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountDiscoverableForm() {
data := map[string]string{
"discoverable": "false",
}
apimodelAccount, err := suite.updateAccountFromForm(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Discoverable)
// Check the account in the database too.
dbZork, err := suite.db.GetAccountByID(context.Background(), apimodelAccount.ID)
suite.NoError(err)
suite.False(*dbZork.Discoverable)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountDiscoverableFormData() {
data := map[string]string{
"discoverable": "false",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Discoverable)
// Check the account in the database too.
dbZork, err := suite.db.GetAccountByID(context.Background(), apimodelAccount.ID)
suite.NoError(err)
suite.False(*dbZork.Discoverable)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountDiscoverableJSON() {
data := `
{
"discoverable": false
}`
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.False(apimodelAccount.Discoverable)
// Check the account in the database too.
dbZork, err := suite.db.GetAccountByID(context.Background(), apimodelAccount.ID)
suite.NoError(err)
suite.False(*dbZork.Discoverable)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountWithImageFormData() {
data := map[string]string{
"display_name": "updated zork display name!!!",
"note": "",
"locked": "true",
}
apimodelAccount, err := suite.updateAccountFromFormDataWithFile("header", "../../../../testrig/media/test-jpeg.jpg", data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(data["display_name"], apimodelAccount.DisplayName)
suite.True(apimodelAccount.Locked)
suite.Empty(apimodelAccount.Note)
[bugfix] Fix existing bio text showing as HTML (#531) * fix existing bio text showing as HTML - updated replaced mentions to include instance - strips HTML from account source note in Verify handler - update text formatter to use buffers for string writes Signed-off-by: kim <grufwub@gmail.com> * go away linter Signed-off-by: kim <grufwub@gmail.com> * change buf reset location, change html mention tags Signed-off-by: kim <grufwub@gmail.com> * reduce FindLinks code complexity Signed-off-by: kim <grufwub@gmail.com> * fix HTML to text conversion Signed-off-by: kim <grufwub@gmail.com> * Update internal/regexes/regexes.go Co-authored-by: Mina Galić <mina.galic@puppet.com> * use improved html2text lib with more options Signed-off-by: kim <grufwub@gmail.com> * fix to produce actual plaintext from html Signed-off-by: kim <grufwub@gmail.com> * fix span tags instead written as space Signed-off-by: kim <grufwub@gmail.com> * performance improvements to regex replacements, fix link replace logic for un-html-ing in the future Signed-off-by: kim <grufwub@gmail.com> * fix tag/mention replacements to use input string, fix link replace to not include scheme Signed-off-by: kim <grufwub@gmail.com> * use matched input string for link replace href text Signed-off-by: kim <grufwub@gmail.com> * remove unused code (to appease linter :sobs:) Signed-off-by: kim <grufwub@gmail.com> * improve hashtagFinger regex to be more compliant Signed-off-by: kim <grufwub@gmail.com> * update breakReplacer to include both unix and windows line endings Signed-off-by: kim <grufwub@gmail.com> * add NoteRaw field to Account to store plaintext account bio, add migration for this, set for sensitive accounts Signed-off-by: kim <grufwub@gmail.com> * drop unnecessary code Signed-off-by: kim <grufwub@gmail.com> * update text package tests to fix logic changes Signed-off-by: kim <grufwub@gmail.com> * add raw note content testing to account update and account verify Signed-off-by: kim <grufwub@gmail.com> * remove unused modules Signed-off-by: kim <grufwub@gmail.com> * fix emoji regex Signed-off-by: kim <grufwub@gmail.com> * fix replacement of hashtags Signed-off-by: kim <grufwub@gmail.com> * update code comment Signed-off-by: kim <grufwub@gmail.com> Co-authored-by: Mina Galić <mina.galic@puppet.com>
2022-05-07 18:55:27 +03:00
suite.Empty(apimodelAccount.Source.Note)
suite.NotEmpty(apimodelAccount.Header)
suite.NotEmpty(apimodelAccount.HeaderStatic)
// Can't predict IDs generated for new media
// so just ensure it's different than before.
[performance] media processing improvements (#1288) * media processor consolidation and reformatting, reduce amount of required syscalls Signed-off-by: kim <grufwub@gmail.com> * update go-store library, stream jpeg/png encoding + use buffer pools, improved media processing AlreadyExists error handling Signed-off-by: kim <grufwub@gmail.com> * fix duration not being set, fix mp4 test expecting error Signed-off-by: kim <grufwub@gmail.com> * fix test expecting media files with different extension Signed-off-by: kim <grufwub@gmail.com> * remove unused code Signed-off-by: kim <grufwub@gmail.com> * fix expected storage paths in tests, update expected test thumbnails Signed-off-by: kim <grufwub@gmail.com> * remove dead code Signed-off-by: kim <grufwub@gmail.com> * fix cached presigned s3 url fetching Signed-off-by: kim <grufwub@gmail.com> * fix tests Signed-off-by: kim <grufwub@gmail.com> * fix test models Signed-off-by: kim <grufwub@gmail.com> * update media processing to use sync.Once{} for concurrency protection Signed-off-by: kim <grufwub@gmail.com> * shutup linter Signed-off-by: kim <grufwub@gmail.com> * fix passing in KVStore GetStream() as stream to PutStream() Signed-off-by: kim <grufwub@gmail.com> * fix unlocks of storage keys Signed-off-by: kim <grufwub@gmail.com> * whoops, return the error... Signed-off-by: kim <grufwub@gmail.com> * pour one out for tobi's code <3 Signed-off-by: kim <grufwub@gmail.com> * add back the byte slurping code Signed-off-by: kim <grufwub@gmail.com> * check for both ErrUnexpectedEOF and EOF Signed-off-by: kim <grufwub@gmail.com> * add back links to file format header information Signed-off-by: kim <grufwub@gmail.com> Signed-off-by: kim <grufwub@gmail.com>
2023-01-11 14:13:13 +03:00
suite.NotEqual("http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", apimodelAccount.Header)
suite.NotEqual("http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/small/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg", apimodelAccount.HeaderStatic)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountEmptyForm() {
data := make(map[string]string)
_, err := suite.updateAccountFromForm(data, http.StatusBadRequest, `{"error":"Bad Request: empty form submitted"}`)
if err != nil {
suite.FailNow(err.Error())
}
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountEmptyFormData() {
data := make(map[string]string)
_, err := suite.updateAccountFromFormData(data, http.StatusBadRequest, `{"error":"Bad Request: empty form submitted"}`)
if err != nil {
suite.FailNow(err.Error())
}
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceForm() {
data := map[string]string{
"source[privacy]": string(apimodel.VisibilityPrivate),
"source[language]": "de",
"source[sensitive]": "true",
"locked": "true",
}
apimodelAccount, err := suite.updateAccountFromForm(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(data["source[language]"], apimodelAccount.Source.Language)
suite.EqualValues(apimodel.VisibilityPrivate, apimodelAccount.Source.Privacy)
suite.True(apimodelAccount.Source.Sensitive)
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceFormData() {
data := map[string]string{
"source[privacy]": string(apimodel.VisibilityPrivate),
"source[language]": "de",
"source[sensitive]": "true",
"locked": "true",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(data["source[language]"], apimodelAccount.Source.Language)
suite.EqualValues(apimodel.VisibilityPrivate, apimodelAccount.Source.Privacy)
suite.True(apimodelAccount.Source.Sensitive)
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceJSON() {
data := `
{
"source": {
"privacy": "private",
"language": "de",
"sensitive": true
},
"locked": true
}
`
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal("de", apimodelAccount.Source.Language)
suite.EqualValues(apimodel.VisibilityPrivate, apimodelAccount.Source.Privacy)
suite.True(apimodelAccount.Source.Sensitive)
suite.True(apimodelAccount.Locked)
}
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceBadContentTypeFormData() {
data := map[string]string{
"source[status_content_type]": "text/markdown",
}
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(data["source[status_content_type]"], apimodelAccount.Source.StatusContentType)
// Check the account in the database too.
dbAccount, err := suite.db.GetAccountByID(context.Background(), suite.testAccounts["local_account_1"].ID)
if err != nil {
suite.FailNow(err.Error())
}
suite.Equal(data["source[status_content_type]"], dbAccount.StatusContentType)
}
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusContentTypeBad() {
data := map[string]string{
"source[status_content_type]": "peepeepoopoo",
}
_, err := suite.updateAccountFromFormData(data, http.StatusBadRequest, `{"error":"Bad Request: status content type 'peepeepoopoo' was not recognized, valid options are 'text/plain', 'text/markdown'"}`)
if err != nil {
suite.FailNow(err.Error())
}
}
func TestAccountUpdateTestSuite(t *testing.T) {
suite.Run(t, new(AccountUpdateTestSuite))
}