commit 7eacbd064b15d2a9ed2f6006c943e0ee65a31317
parent 5f00d4980bdc55bce8d23e38392b345d525dbf4a
Author: tobi <31960611+tsmethurst@users.noreply.github.com>
Date: Fri, 24 Jun 2022 10:43:21 +0200
[bugfix] allow setting empty email via instance patch (#665)
Diffstat:
2 files changed, 65 insertions(+), 3 deletions(-)
diff --git a/internal/api/client/instance/instancepatch_test.go b/internal/api/client/instance/instancepatch_test.go
@@ -187,6 +187,65 @@ func (suite *InstancePatchTestSuite) TestInstancePatch5() {
suite.Equal(`{"error":"Forbidden: user is not an admin so cannot update instance settings"}`, string(b))
}
+func (suite *InstancePatchTestSuite) TestInstancePatch6() {
+ requestBody, w, err := testrig.CreateMultipartFormData(
+ "", "",
+ map[string]string{
+ "contact_email": "",
+ })
+ if err != nil {
+ panic(err)
+ }
+ bodyBytes := requestBody.Bytes()
+
+ // set up the request
+ recorder := httptest.NewRecorder()
+ ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
+
+ // call the handler
+ suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
+
+ // we should have OK because our request was valid
+ suite.Equal(http.StatusOK, recorder.Code)
+
+ result := recorder.Result()
+ defer result.Body.Close()
+
+ b, err := io.ReadAll(result.Body)
+ suite.NoError(err)
+
+ suite.Equal(`{"uri":"http://localhost:8080","title":"localhost:8080","description":"","short_description":"","email":"","version":"","registrations":true,"approval_required":true,"invites_enabled":false,"urls":{"streaming_api":"wss://localhost:8080"},"stats":{"domain_count":2,"status_count":16,"user_count":4},"thumbnail":"","max_toot_chars":5000}`, string(b))
+}
+
+func (suite *InstancePatchTestSuite) TestInstancePatch7() {
+ requestBody, w, err := testrig.CreateMultipartFormData(
+ "", "",
+ map[string]string{
+ "contact_email": "not.an.email.address",
+ })
+ if err != nil {
+ panic(err)
+ }
+ bodyBytes := requestBody.Bytes()
+
+ // set up the request
+ recorder := httptest.NewRecorder()
+ ctx := suite.newContext(recorder, http.MethodPatch, bodyBytes, instance.InstanceInformationPath, w.FormDataContentType())
+
+ // call the handler
+ suite.instanceModule.InstanceUpdatePATCHHandler(ctx)
+
+ suite.Equal(http.StatusBadRequest, recorder.Code)
+
+ result := recorder.Result()
+ defer result.Body.Close()
+
+ b, err := io.ReadAll(result.Body)
+ suite.NoError(err)
+
+ suite.Equal(`{"error":"Bad Request: mail: missing '@' or angle-addr"}`, string(b))
+}
+
func TestInstancePatchTestSuite(t *testing.T) {
suite.Run(t, &InstancePatchTestSuite{})
}
diff --git a/internal/processing/instance.go b/internal/processing/instance.go
@@ -167,10 +167,13 @@ func (p *processor) InstancePatch(ctx context.Context, form *apimodel.InstanceSe
// validate & update site contact email if it's set on the form
if form.ContactEmail != nil {
- if err := validate.Email(*form.ContactEmail); err != nil {
- return nil, gtserror.NewErrorBadRequest(err, err.Error())
+ contactEmail := *form.ContactEmail
+ if contactEmail != "" {
+ if err := validate.Email(contactEmail); err != nil {
+ return nil, gtserror.NewErrorBadRequest(err, err.Error())
+ }
}
- i.ContactEmail = *form.ContactEmail
+ i.ContactEmail = contactEmail
}
// validate & update site short description if it's set on the form