From 8daa4dae3435e45b4367c9d59bfa27a063fba2d4 Mon Sep 17 00:00:00 2001
From: tobi <31960611+tsmethurst@users.noreply.github.com>
Date: Wed, 8 Jan 2025 22:38:27 +0100
Subject: [bugfix] More permissive CSV parsing for perm subs, text parse fix
(#3638)
* [bugfix] More permissive CSV parsing for perm subs, text parse fix
* wee
* change the way dry works, slightly
* me oh my, i'm just a little guy
* we're just normal men
---
.../admin/domainpermissionsubscriptiontest_test.go | 204 +++++++++++++++++++++
.../admin/domainpermissionsubscruptiontest_test.go | 125 -------------
2 files changed, 204 insertions(+), 125 deletions(-)
create mode 100644 internal/api/client/admin/domainpermissionsubscriptiontest_test.go
delete mode 100644 internal/api/client/admin/domainpermissionsubscruptiontest_test.go
(limited to 'internal/api')
diff --git a/internal/api/client/admin/domainpermissionsubscriptiontest_test.go b/internal/api/client/admin/domainpermissionsubscriptiontest_test.go
new file mode 100644
index 000000000..c03b950a9
--- /dev/null
+++ b/internal/api/client/admin/domainpermissionsubscriptiontest_test.go
@@ -0,0 +1,204 @@
+// 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 .
+
+package admin_test
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/gin-gonic/gin"
+ "github.com/stretchr/testify/suite"
+ "github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
+ apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+)
+
+type DomainPermissionSubscriptionTestTestSuite struct {
+ AdminStandardTestSuite
+}
+
+func (suite *DomainPermissionSubscriptionTestTestSuite) TestDomainPermissionSubscriptionTestCSV() {
+ var (
+ ctx = context.Background()
+ testAccount = suite.testAccounts["admin_account"]
+ permSub = >smodel.DomainPermissionSubscription{
+ ID: "01JGE681TQSBPAV59GZXPKE62H",
+ Priority: 255,
+ Title: "whatever!",
+ PermissionType: gtsmodel.DomainPermissionBlock,
+ AsDraft: util.Ptr(false),
+ AdoptOrphans: util.Ptr(true),
+ CreatedByAccountID: testAccount.ID,
+ CreatedByAccount: testAccount,
+ URI: "https://lists.example.org/baddies.csv",
+ ContentType: gtsmodel.DomainPermSubContentTypeCSV,
+ }
+ )
+
+ // Create a subscription for a CSV list of baddies.
+ err := suite.state.DB.PutDomainPermissionSubscription(ctx, permSub)
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ // Prepare the request to the /test endpoint.
+ subPath := strings.ReplaceAll(
+ admin.DomainPermissionSubscriptionTestPath,
+ ":id", permSub.ID,
+ )
+ path := "/api" + subPath
+ recorder := httptest.NewRecorder()
+ ginCtx := suite.newContext(recorder, http.MethodPost, nil, path, "application/json")
+ ginCtx.Params = gin.Params{
+ gin.Param{
+ Key: apiutil.IDKey,
+ Value: permSub.ID,
+ },
+ }
+
+ // Trigger the handler.
+ suite.adminModule.DomainPermissionSubscriptionTestPOSTHandler(ginCtx)
+ suite.Equal(http.StatusOK, recorder.Code)
+
+ // Read the body back.
+ b, err := io.ReadAll(recorder.Body)
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ dst := new(bytes.Buffer)
+ if err := json.Indent(dst, b, "", " "); err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ // Ensure expected.
+ suite.Equal(`[
+ {
+ "domain": "bumfaces.net",
+ "public_comment": "big jerks"
+ },
+ {
+ "domain": "peepee.poopoo",
+ "public_comment": "harassment"
+ },
+ {
+ "domain": "nothanks.com"
+ }
+]`, dst.String())
+
+ // No permissions should be created
+ // since this is a dry run / test.
+ blocked, err := suite.state.DB.AreDomainsBlocked(
+ ctx,
+ []string{"bumfaces.net", "peepee.poopoo", "nothanks.com"},
+ )
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+ suite.False(blocked)
+}
+
+func (suite *DomainPermissionSubscriptionTestTestSuite) TestDomainPermissionSubscriptionTestText() {
+ var (
+ ctx = context.Background()
+ testAccount = suite.testAccounts["admin_account"]
+ permSub = >smodel.DomainPermissionSubscription{
+ ID: "01JGE681TQSBPAV59GZXPKE62H",
+ Priority: 255,
+ Title: "whatever!",
+ PermissionType: gtsmodel.DomainPermissionBlock,
+ AsDraft: util.Ptr(false),
+ AdoptOrphans: util.Ptr(true),
+ CreatedByAccountID: testAccount.ID,
+ CreatedByAccount: testAccount,
+ URI: "https://lists.example.org/baddies.txt",
+ ContentType: gtsmodel.DomainPermSubContentTypePlain,
+ }
+ )
+
+ // Create a subscription for a plaintext list of baddies.
+ err := suite.state.DB.PutDomainPermissionSubscription(ctx, permSub)
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ // Prepare the request to the /test endpoint.
+ subPath := strings.ReplaceAll(
+ admin.DomainPermissionSubscriptionTestPath,
+ ":id", permSub.ID,
+ )
+ path := "/api" + subPath
+ recorder := httptest.NewRecorder()
+ ginCtx := suite.newContext(recorder, http.MethodPost, nil, path, "application/json")
+ ginCtx.Params = gin.Params{
+ gin.Param{
+ Key: apiutil.IDKey,
+ Value: permSub.ID,
+ },
+ }
+
+ // Trigger the handler.
+ suite.adminModule.DomainPermissionSubscriptionTestPOSTHandler(ginCtx)
+ suite.Equal(http.StatusOK, recorder.Code)
+
+ // Read the body back.
+ b, err := io.ReadAll(recorder.Body)
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ dst := new(bytes.Buffer)
+ if err := json.Indent(dst, b, "", " "); err != nil {
+ suite.FailNow(err.Error())
+ }
+
+ // Ensure expected.
+ suite.Equal(`[
+ {
+ "domain": "bumfaces.net"
+ },
+ {
+ "domain": "peepee.poopoo"
+ },
+ {
+ "domain": "nothanks.com"
+ }
+]`, dst.String())
+
+ // No permissions should be created
+ // since this is a dry run / test.
+ blocked, err := suite.state.DB.AreDomainsBlocked(
+ ctx,
+ []string{"bumfaces.net", "peepee.poopoo", "nothanks.com"},
+ )
+ if err != nil {
+ suite.FailNow(err.Error())
+ }
+ suite.False(blocked)
+}
+
+func TestDomainPermissionSubscriptionTestTestSuite(t *testing.T) {
+ suite.Run(t, &DomainPermissionSubscriptionTestTestSuite{})
+}
diff --git a/internal/api/client/admin/domainpermissionsubscruptiontest_test.go b/internal/api/client/admin/domainpermissionsubscruptiontest_test.go
deleted file mode 100644
index 46861aba1..000000000
--- a/internal/api/client/admin/domainpermissionsubscruptiontest_test.go
+++ /dev/null
@@ -1,125 +0,0 @@
-// 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 .
-
-package admin_test
-
-import (
- "bytes"
- "context"
- "encoding/json"
- "io"
- "net/http"
- "net/http/httptest"
- "strings"
- "testing"
-
- "github.com/gin-gonic/gin"
- "github.com/stretchr/testify/suite"
- "github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
- apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
- "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
- "github.com/superseriousbusiness/gotosocial/internal/util"
-)
-
-type DomainPermissionSubscriptionTestTestSuite struct {
- AdminStandardTestSuite
-}
-
-func (suite *DomainPermissionSubscriptionTestTestSuite) TestDomainPermissionSubscriptionTest() {
- var (
- ctx = context.Background()
- testAccount = suite.testAccounts["admin_account"]
- permSub = >smodel.DomainPermissionSubscription{
- ID: "01JGE681TQSBPAV59GZXPKE62H",
- Priority: 255,
- Title: "whatever!",
- PermissionType: gtsmodel.DomainPermissionBlock,
- AsDraft: util.Ptr(false),
- AdoptOrphans: util.Ptr(true),
- CreatedByAccountID: testAccount.ID,
- CreatedByAccount: testAccount,
- URI: "https://lists.example.org/baddies.csv",
- ContentType: gtsmodel.DomainPermSubContentTypeCSV,
- }
- )
-
- // Create a subscription for a CSV list of baddies.
- err := suite.state.DB.PutDomainPermissionSubscription(ctx, permSub)
- if err != nil {
- suite.FailNow(err.Error())
- }
-
- // Prepare the request to the /test endpoint.
- subPath := strings.ReplaceAll(
- admin.DomainPermissionSubscriptionTestPath,
- ":id", permSub.ID,
- )
- path := "/api" + subPath
- recorder := httptest.NewRecorder()
- ginCtx := suite.newContext(recorder, http.MethodPost, nil, path, "application/json")
- ginCtx.Params = gin.Params{
- gin.Param{
- Key: apiutil.IDKey,
- Value: permSub.ID,
- },
- }
-
- // Trigger the handler.
- suite.adminModule.DomainPermissionSubscriptionTestPOSTHandler(ginCtx)
- suite.Equal(http.StatusOK, recorder.Code)
-
- // Read the body back.
- b, err := io.ReadAll(recorder.Body)
- if err != nil {
- suite.FailNow(err.Error())
- }
-
- dst := new(bytes.Buffer)
- if err := json.Indent(dst, b, "", " "); err != nil {
- suite.FailNow(err.Error())
- }
-
- // Ensure expected.
- suite.Equal(`[
- {
- "domain": "bumfaces.net",
- "public_comment": "big jerks"
- },
- {
- "domain": "peepee.poopoo",
- "public_comment": "harassment"
- },
- {
- "domain": "nothanks.com"
- }
-]`, dst.String())
-
- // No permissions should be created
- // since this is a dry run / test.
- blocked, err := suite.state.DB.AreDomainsBlocked(
- ctx,
- []string{"bumfaces.net", "peepee.poopoo", "nothanks.com"},
- )
- if err != nil {
- suite.FailNow(err.Error())
- }
- suite.False(blocked)
-}
-
-func TestDomainPermissionSubscriptionTestTestSuite(t *testing.T) {
- suite.Run(t, &DomainPermissionSubscriptionTestTestSuite{})
-}
--
cgit v1.2.3