mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[bugfix] Ensure account fields can be set by JSON (#1762)
This commit is contained in:
@@ -20,7 +20,8 @@ package accounts_test
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -28,6 +29,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -35,460 +37,351 @@ type AccountUpdateTestSuite struct {
|
||||
AccountStandardTestSuite
|
||||
}
|
||||
|
||||
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandler() {
|
||||
// set up the request
|
||||
// we're updating the note and profile fields of zork
|
||||
newBio := "this is my new bio read it and weep"
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"note": newBio,
|
||||
"fields_attributes[0][name]": "pronouns",
|
||||
"fields_attributes[0][value]": "they/them",
|
||||
})
|
||||
func (suite *AccountUpdateTestSuite) updateAccountFromFormData(data map[string]string, expectedHTTPStatus int, expectedBody string) (*apimodel.Account, error) {
|
||||
requestBody, w, err := testrig.CreateMultipartFormData("", "", data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
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.UpdateCredentialsPath, contentType)
|
||||
|
||||
// Trigger the handler.
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(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
|
||||
// Read the result.
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
|
||||
// check the response
|
||||
b, err := ioutil.ReadAll(result.Body)
|
||||
suite.NoError(err)
|
||||
b, err := io.ReadAll(result.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
errs := gtserror.MultiError{}
|
||||
|
||||
// Check expected code + body.
|
||||
if resultCode := recorder.Code; expectedHTTPStatus != resultCode {
|
||||
errs = append(errs, fmt.Sprintf("expected %d got %d", expectedHTTPStatus, resultCode))
|
||||
}
|
||||
|
||||
// If we got an expected body, return early.
|
||||
if expectedBody != "" && string(b) != expectedBody {
|
||||
errs = append(errs, fmt.Sprintf("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) 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())
|
||||
}
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
|
||||
suite.Equal("they/them", apimodelAccount.Fields[0].Value)
|
||||
suite.Equal(newBio, apimodelAccount.Source.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) TestAccountUpdateCredentialsPATCHHandlerUnlockLock() {
|
||||
// set up the first request
|
||||
requestBody1, w1, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"locked": "false",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bodyBytes1 := requestBody1.Bytes()
|
||||
recorder1 := httptest.NewRecorder()
|
||||
ctx1 := suite.newContext(recorder1, http.MethodPatch, bodyBytes1, accounts.UpdateCredentialsPath, w1.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx1)
|
||||
|
||||
// 1. we should have OK because our request was valid
|
||||
suite.Equal(http.StatusOK, recorder1.Code)
|
||||
|
||||
// 2. we should have no error message in the result body
|
||||
result1 := recorder1.Result()
|
||||
defer result1.Body.Close()
|
||||
|
||||
// check the response
|
||||
b1, err := ioutil.ReadAll(result1.Body)
|
||||
suite.NoError(err)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount1 := &apimodel.Account{}
|
||||
err = json.Unmarshal(b1, apimodelAccount1)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.False(apimodelAccount1.Locked)
|
||||
|
||||
// set up the first request
|
||||
requestBody2, w2, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"locked": "true",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
bodyBytes2 := requestBody2.Bytes()
|
||||
recorder2 := httptest.NewRecorder()
|
||||
ctx2 := suite.newContext(recorder2, http.MethodPatch, bodyBytes2, accounts.UpdateCredentialsPath, w2.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx2)
|
||||
|
||||
// 1. we should have OK because our request was valid
|
||||
suite.Equal(http.StatusOK, recorder1.Code)
|
||||
|
||||
// 2. we should have no error message in the result body
|
||||
result2 := recorder2.Result()
|
||||
defer result2.Body.Close()
|
||||
|
||||
// check the response
|
||||
b2, err := ioutil.ReadAll(result2.Body)
|
||||
suite.NoError(err)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount2 := &apimodel.Account{}
|
||||
err = json.Unmarshal(b2, apimodelAccount2)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.True(apimodelAccount2.Locked)
|
||||
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"
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerGetAccountFirst() {
|
||||
// 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 not return the old version
|
||||
_, err := suite.db.GetAccountByID(context.Background(), suite.testAccounts["local_account_1"].ID)
|
||||
suite.NoError(err)
|
||||
|
||||
// set up the request
|
||||
// we're updating the note of zork
|
||||
newBio := "this is my new bio read it and weep"
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"note": newBio,
|
||||
})
|
||||
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(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)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal("<p>this is my new bio read it and weep</p>", apimodelAccount.Note)
|
||||
suite.Equal(newBio, apimodelAccount.Source.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) TestAccountUpdateCredentialsPATCHHandlerTwoFields() {
|
||||
// set up the request
|
||||
// we're updating the note of zork, and setting locked to true
|
||||
newBio := "this is my new bio read it and weep :rainbow:"
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"note": newBio,
|
||||
"locked": "true",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountLockFormData() {
|
||||
data := map[string]string{
|
||||
"locked": "true",
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal("<p>this is my new bio read it and weep :rainbow:</p>", apimodelAccount.Note)
|
||||
suite.Equal(newBio, apimodelAccount.Source.Note)
|
||||
suite.True(apimodelAccount.Locked)
|
||||
suite.NotEmpty(apimodelAccount.Emojis)
|
||||
suite.Equal(apimodelAccount.Emojis[0].Shortcode, "rainbow")
|
||||
|
||||
// check the account in the database
|
||||
dbZork, err := suite.db.GetAccountByID(context.Background(), apimodelAccount.ID)
|
||||
suite.NoError(err)
|
||||
suite.Equal(newBio, dbZork.NoteRaw)
|
||||
suite.Equal("<p>this is my new bio read it and weep :rainbow:</p>", dbZork.Note)
|
||||
suite.True(*dbZork.Locked)
|
||||
suite.NotEmpty(dbZork.EmojiIDs)
|
||||
}
|
||||
|
||||
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerDiscoverable() {
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"discoverable": "false",
|
||||
})
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountLockJSON() {
|
||||
data := `
|
||||
{
|
||||
"locked": true
|
||||
}`
|
||||
|
||||
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
suite.True(apimodelAccount.Locked)
|
||||
}
|
||||
|
||||
// 1. we should have OK because our request was valid
|
||||
suite.Equal(http.StatusOK, recorder.Code)
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountUnlockFormData() {
|
||||
data := map[string]string{
|
||||
"locked": "false",
|
||||
}
|
||||
|
||||
// 2. we should have no error message in the result body
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
// check the response
|
||||
b, err := ioutil.ReadAll(result.Body)
|
||||
suite.NoError(err)
|
||||
suite.False(apimodelAccount.Locked)
|
||||
}
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
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) TestUpdateAccountDiscoverableFormData() {
|
||||
data := map[string]string{
|
||||
"discoverable": "false",
|
||||
}
|
||||
|
||||
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.False(apimodelAccount.Discoverable)
|
||||
|
||||
// check the account in the database
|
||||
// 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) TestAccountUpdateCredentialsPATCHHandlerWithMedia() {
|
||||
// set up the request
|
||||
// we're updating the header image, the display name, and the locked status of zork
|
||||
// we're removing the note/bio
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"header", "../../../../testrig/media/test-jpeg.jpg",
|
||||
map[string]string{
|
||||
"display_name": "updated zork display name!!!",
|
||||
"note": "",
|
||||
"locked": "true",
|
||||
})
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountDiscoverableJSON() {
|
||||
data := `
|
||||
{
|
||||
"discoverable": false
|
||||
}`
|
||||
|
||||
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
suite.False(apimodelAccount.Discoverable)
|
||||
|
||||
// 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)
|
||||
// Check the account in the database too.
|
||||
dbZork, err := suite.db.GetAccountByID(context.Background(), apimodelAccount.ID)
|
||||
suite.NoError(err)
|
||||
suite.False(*dbZork.Discoverable)
|
||||
}
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountWithImageFormData() {
|
||||
data := map[string]string{
|
||||
"display_name": "updated zork display name!!!",
|
||||
"note": "",
|
||||
"locked": "true",
|
||||
}
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal("updated zork display name!!!", apimodelAccount.DisplayName)
|
||||
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)
|
||||
suite.Empty(apimodelAccount.Source.Note)
|
||||
|
||||
// header values...
|
||||
// should be set
|
||||
suite.NotEmpty(apimodelAccount.Header)
|
||||
suite.NotEmpty(apimodelAccount.HeaderStatic)
|
||||
|
||||
// should be different from the values set before
|
||||
// Can't predict IDs generated for new media
|
||||
// so just ensure it's different than before.
|
||||
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) TestAccountUpdateCredentialsPATCHHandlerEmptyForm() {
|
||||
// set up the request
|
||||
bodyBytes := []byte{}
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, "")
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountEmptyFormData() {
|
||||
data := make(map[string]string)
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
|
||||
// 1. we should have OK because our request was valid
|
||||
suite.Equal(http.StatusBadRequest, 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.Equal(`{"error":"Bad Request: empty form submitted"}`, string(b))
|
||||
_, err := suite.updateAccountFromFormData(data, http.StatusBadRequest, `{"error":"Bad Request: empty form submitted"}`)
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateSource() {
|
||||
// set up the request
|
||||
// we're updating the language of zork
|
||||
newLanguage := "de"
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"source[privacy]": string(apimodel.VisibilityPrivate),
|
||||
"source[language]": "de",
|
||||
"source[sensitive]": "true",
|
||||
"locked": "true",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceFormData() {
|
||||
data := map[string]string{
|
||||
"source[privacy]": string(apimodel.VisibilityPrivate),
|
||||
"source[language]": "de",
|
||||
"source[sensitive]": "true",
|
||||
"locked": "true",
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal(newLanguage, apimodelAccount.Source.Language)
|
||||
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) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusContentTypeOK() {
|
||||
// set up the request
|
||||
// we're updating the language of zork
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"source[status_content_type]": "text/markdown",
|
||||
})
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceJSON() {
|
||||
data := `
|
||||
{
|
||||
"source": {
|
||||
"privacy": "private",
|
||||
"language": "de",
|
||||
"sensitive": true
|
||||
},
|
||||
"locked": true
|
||||
}
|
||||
`
|
||||
|
||||
apimodelAccount, err := suite.updateAccountFromJSON(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
suite.Equal("de", apimodelAccount.Source.Language)
|
||||
suite.EqualValues(apimodel.VisibilityPrivate, apimodelAccount.Source.Privacy)
|
||||
suite.True(apimodelAccount.Source.Sensitive)
|
||||
suite.True(apimodelAccount.Locked)
|
||||
}
|
||||
|
||||
// 1. we should have OK because our request was valid
|
||||
suite.Equal(http.StatusOK, recorder.Code)
|
||||
func (suite *AccountUpdateTestSuite) TestUpdateAccountSourceBadContentTypeFormData() {
|
||||
data := map[string]string{
|
||||
"source[status_content_type]": "text/markdown",
|
||||
}
|
||||
|
||||
// 2. we should have no error message in the result body
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
apimodelAccount, err := suite.updateAccountFromFormData(data, http.StatusOK, "")
|
||||
if err != nil {
|
||||
suite.FailNow(err.Error())
|
||||
}
|
||||
|
||||
// check the response
|
||||
b, err := ioutil.ReadAll(result.Body)
|
||||
suite.NoError(err)
|
||||
|
||||
// unmarshal the returned account
|
||||
apimodelAccount := &apimodel.Account{}
|
||||
err = json.Unmarshal(b, apimodelAccount)
|
||||
suite.NoError(err)
|
||||
|
||||
// check the returned api model account
|
||||
// fields should be updated
|
||||
suite.Equal("text/markdown", apimodelAccount.Source.StatusContentType)
|
||||
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(dbAccount.StatusContentType, "text/markdown")
|
||||
suite.Equal(data["source[status_content_type]"], dbAccount.StatusContentType)
|
||||
}
|
||||
|
||||
func (suite *AccountUpdateTestSuite) TestAccountUpdateCredentialsPATCHHandlerUpdateStatusContentTypeBad() {
|
||||
// set up the request
|
||||
// we're updating the language of zork
|
||||
requestBody, w, err := testrig.CreateMultipartFormData(
|
||||
"", "",
|
||||
map[string]string{
|
||||
"source[status_content_type]": "peepeepoopoo",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
data := map[string]string{
|
||||
"source[status_content_type]": "peepeepoopoo",
|
||||
}
|
||||
bodyBytes := requestBody.Bytes()
|
||||
recorder := httptest.NewRecorder()
|
||||
ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, accounts.UpdateCredentialsPath, w.FormDataContentType())
|
||||
|
||||
// call the handler
|
||||
suite.accountsModule.AccountUpdateCredentialsPATCHHandler(ctx)
|
||||
|
||||
suite.Equal(http.StatusBadRequest, recorder.Code)
|
||||
|
||||
result := recorder.Result()
|
||||
defer result.Body.Close()
|
||||
|
||||
// check the response
|
||||
b, err := ioutil.ReadAll(result.Body)
|
||||
suite.NoError(err)
|
||||
|
||||
suite.Equal(`{"error":"Bad Request: status content type 'peepeepoopoo' was not recognized, valid options are 'text/plain', 'text/markdown'"}`, string(b))
|
||||
_, 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) {
|
||||
|
Reference in New Issue
Block a user