Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix tag/status etc. filter in bulk deletion.

Kailash Nadh3918893cefa7
diff --git a/cmd/bounce.go b/cmd/bounce.go
index 86482107..c9d481f2 100644
--- a/cmd/bounce.go
+++ b/cmd/bounce.go
@@ -122,7 +122,7 @@ func (a *App) DeleteBounces(c echo.Context) error {
all, _ := strconv.ParseBool(c.QueryParam("all"))
var ids []int
- if !all {
+ if !all || len(c.QueryParams()["id"]) > 0 {
// There are multiple IDs in the query string.
res, err := parseStringIDs(c.Request().URL.Query()["id"])
if err != nil {
@@ -133,10 +133,16 @@ func (a *App) DeleteBounces(c echo.Context) error {
}
ids = res
+ all = false
+ }
+
+ campID, err := strconv.Atoi(c.QueryParam("campaign_id"))
+ if c.QueryParam("campaign_id") != "" && (err != nil || campID < 1) {
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("globals.messages.invalidID"))
}
// Delete bounces from the DB.
- if err := a.core.DeleteBounces(ids, all); err != nil {
+ if err := a.core.DeleteBounces(ids, all, campID, c.QueryParam("source"), c.QueryParam("type")); err != nil {
return err
}
@@ -147,16 +153,26 @@ func (a *App) DeleteBounces(c echo.Context) error {
func (a *App) DeleteBounce(c echo.Context) error {
// Delete bounces from the DB.
id := getID(c)
- if err := a.core.DeleteBounces([]int{id}, false); err != nil {
+ if err := a.core.DeleteBounces([]int{id}, false, 0, "", ""); err != nil {
return err
}
return c.JSON(http.StatusOK, okResp{true})
}
-// BlocklistBouncedSubscribers handles blocklisting of all bounced subscribers.
+// BlocklistBouncedSubscribers blocklists subscribers matching bounce IDs or filters.
func (a *App) BlocklistBouncedSubscribers(c echo.Context) error {
- if err := a.core.BlocklistBouncedSubscribers(); err != nil {
+ ids, err := parseStringIDs(c.QueryParams()["id"])
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("globals.messages.invalidID"))
+ }
+
+ campID, err := strconv.Atoi(c.QueryParam("campaign_id"))
+ if c.QueryParam("campaign_id") != "" && (err != nil || campID < 1) {
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("globals.messages.invalidID"))
+ }
+
+ if err := a.core.BlocklistBouncedSubscribers(ids, campID, c.QueryParam("source"), c.QueryParam("type")); err != nil {
return err
}
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index fe5cc46c..13c41ef0 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -661,7 +661,7 @@ func (a *App) DeleteCampaigns(c echo.Context) error {
}
// Delete the campaigns from the DB.
- if err := a.core.DeleteCampaigns(ids, query, hasAllPerm, permittedLists); err != nil {
+ if err := a.core.DeleteCampaigns(ids, query, c.QueryParams()["status"], c.QueryParams()["tag"], c.QueryParam("type"), hasAllPerm, permittedLists); err != nil {
return err
}
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 04e7909b..d229aa42 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -32,6 +32,7 @@ var subscribersQueryDefaults = map[string]string{
"order_by": "",
"order": "",
"subscription_status": "",
+ "list_id": "",
"status": "",
}
@@ -46,6 +47,7 @@ type subQueryReq struct {
Action string `json:"action"`
Status string `json:"status"`
SubscriptionStatus string `json:"subscription_status"`
+ SubscriberStatus string `json:"subscriber_status"`
All bool `json:"all"`
}
@@ -464,7 +466,7 @@ func (a *App) ExportSubscribers(c echo.Context) error {
}
// Get the batched export iterator.
- exp, err := a.core.ExportSubscribers(searchStr, query, subIDs, listIDs, subStatus, a.cfg.DBBatchSize)
+ exp, err := a.core.ExportSubscribers(searchStr, query, subIDs, listIDs, subStatus, c.QueryParam("status"), a.cfg.DBBatchSize)
if err != nil {
return err
}
@@ -859,11 +861,7 @@ func (a *App) DeleteSubscribersByQuery(c echo.Context) error {
req.Search = strings.TrimSpace(req.Search)
req.Query = formatSQLExp(req.Query)
- if req.All {
- // If the "all" flag is set, ignore any subquery that may be present.
- req.Search = ""
- req.Query = ""
- } else if req.Search == "" && req.Query == "" {
+ if !req.All && req.Search == "" && req.Query == "" {
return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.invalidFields", "name", "query"))
}
@@ -879,7 +877,7 @@ func (a *App) DeleteSubscribersByQuery(c echo.Context) error {
listIDs := user.GetPermittedListIDs(req.ListIDs)
// Delete the subscribers from the DB.
- if err := a.core.DeleteSubscribersByQuery(req.Search, req.Query, listIDs, req.SubscriptionStatus); err != nil {
+ if err := a.core.DeleteSubscribersByQuery(req.Search, req.Query, listIDs, req.SubscriptionStatus, req.SubscriberStatus); err != nil {
return err
}
@@ -899,11 +897,7 @@ func (a *App) BlocklistSubscribersByQuery(c echo.Context) error {
req.Search = strings.TrimSpace(req.Search)
req.Query = formatSQLExp(req.Query)
- if req.All {
- // If the "all" flag is set, ignore any subquery that may be present.
- req.Search = ""
- req.Query = ""
- } else if req.Search == "" && req.Query == "" {
+ if !req.All && req.Search == "" && req.Query == "" {
return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.invalidFields", "name", "query"))
}
// Does the user have the subscribers:sql_query permission?
@@ -918,7 +912,7 @@ func (a *App) BlocklistSubscribersByQuery(c echo.Context) error {
listIDs := user.GetPermittedListIDs(req.ListIDs)
// Update the subscribers in the DB.
- if err := a.core.BlocklistSubscribersByQuery(req.Search, req.Query, listIDs, req.SubscriptionStatus); err != nil {
+ if err := a.core.BlocklistSubscribersByQuery(req.Search, req.Query, listIDs, req.SubscriptionStatus, req.SubscriberStatus); err != nil {
return err
}
@@ -942,6 +936,9 @@ func (a *App) ManageSubscriberListsByQuery(c echo.Context) error {
req.Search = strings.TrimSpace(req.Search)
req.Query = formatSQLExp(req.Query)
+ if !req.All && req.Search == "" && req.Query == "" {
+ return echo.NewHTTPError(http.StatusBadRequest, a.i18n.Ts("globals.messages.invalidFields", "name", "query"))
+ }
// Does the user have the subscribers:sql_query permission?
if req.Query != "" {
@@ -959,11 +956,11 @@ func (a *App) ManageSubscriberListsByQuery(c echo.Context) error {
var err error
switch req.Action {
case "add":
- err = a.core.AddSubscriptionsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.Status, req.SubscriptionStatus)
+ err = a.core.AddSubscriptionsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.Status, req.SubscriptionStatus, req.SubscriberStatus)
case "remove":
- err = a.core.DeleteSubscriptionsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.SubscriptionStatus)
+ err = a.core.DeleteSubscriptionsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.SubscriptionStatus, req.SubscriberStatus)
case "unsubscribe":
- err = a.core.UnsubscribeListsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.SubscriptionStatus)
+ err = a.core.UnsubscribeListsByQuery(req.Search, req.Query, sourceListIDs, targetListIDs, req.SubscriptionStatus, req.SubscriberStatus)
default:
return echo.NewHTTPError(http.StatusBadRequest, a.i18n.T("subscribers.invalidAction"))
}
diff --git a/internal/auth/models.go b/internal/auth/models.go
index 9b8f7b46..af20e372 100644
--- a/internal/auth/models.go
+++ b/internal/auth/models.go
@@ -313,18 +313,21 @@ func (u *User) FilterListsByPerm(types PermType, listIDs []int) []int {
// GetPermittedListIDs filters the given list IDs by the user's get/manage
// permissions and returns the filtered set. Unlike `FilterListsByPerm()`, if no
-// IDs are present (empty input or 0 permitted lists), it falls back to the user's
+// IDs are requested (empty input), it falls back to the user's
// permitted list IDs if any. This is useful for endpoints which accept a few IDs
// or the lack of which implies "all".
func (u *User) GetPermittedListIDs(listIDs []int) []int {
- listIDs = u.FilterListsByPerm(PermTypeGet|PermTypeManage, listIDs)
- if len(listIDs) == 0 {
- if _, ok := u.PermissionsMap[PermSubscribersGetAll]; !ok {
- if len(u.GetListIDs) > 0 {
- return u.GetListIDs
- }
- return []int{-1}
+ if len(listIDs) > 0 {
+ if ids := u.FilterListsByPerm(PermTypeGet|PermTypeManage, listIDs); len(ids) > 0 {
+ return ids
+ }
+ return []int{-1}
+ }
+ if _, ok := u.PermissionsMap[PermSubscribersGetAll]; !ok {
+ if len(u.GetListIDs) > 0 {
+ return u.GetListIDs
}
+ return []int{-1}
}
return listIDs
}
diff --git a/internal/core/bounces.go b/internal/core/bounces.go
index 0252a5ac..ea3b6f5c 100644
--- a/internal/core/bounces.go
+++ b/internal/core/bounces.go
@@ -86,9 +86,9 @@ func (c *Core) RecordBounce(b models.Bounce) error {
return err
}
-// BlocklistBouncedSubscribers blocklists all bounced subscribers.
-func (c *Core) BlocklistBouncedSubscribers() error {
- if _, err := c.q.BlocklistBouncedSubscribers.Exec(); err != nil {
+// BlocklistBouncedSubscribers blocklists subscribers matching bounce IDs or filters.
+func (c *Core) BlocklistBouncedSubscribers(ids []int, campID int, source, typ string) error {
+ if _, err := c.q.BlocklistBouncedSubscribers.Exec(pq.Array(ids), campID, source, typ); err != nil {
c.log.Printf("error blocklisting bounced subscribers: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, c.i18n.Ts("subscribers.errorBlocklisting", "error", err.Error()))
}
@@ -96,14 +96,14 @@ func (c *Core) BlocklistBouncedSubscribers() error {
return nil
}
-// DeleteBounce deletes a list.
+// DeleteBounce deletes a bounce record.
func (c *Core) DeleteBounce(id int) error {
- return c.DeleteBounces([]int{id}, false)
+ return c.DeleteBounces([]int{id}, false, 0, "", "")
}
-// DeleteBounces deletes multiple lists.
-func (c *Core) DeleteBounces(ids []int, all bool) error {
- if _, err := c.q.DeleteBounces.Exec(pq.Array(ids), all); err != nil {
+// DeleteBounces deletes bounce records by IDs or filters.
+func (c *Core) DeleteBounces(ids []int, all bool, campID int, source, typ string) error {
+ if _, err := c.q.DeleteBounces.Exec(pq.Array(ids), all, campID, source, typ); err != nil {
c.log.Printf("error deleting lists: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.list}", "error", pqErrMsg(err)))
diff --git a/internal/core/campaigns.go b/internal/core/campaigns.go
index 2ef489b6..7006ad55 100644
--- a/internal/core/campaigns.go
+++ b/internal/core/campaigns.go
@@ -333,7 +333,7 @@ func (c *Core) DeleteCampaign(id int) error {
}
// DeleteCampaigns deletes multiple campaigns by IDs or by query.
-func (c *Core) DeleteCampaigns(ids []int, query string, hasAllPerm bool, permittedLists []int) error {
+func (c *Core) DeleteCampaigns(ids []int, query string, statuses, tags []string, typ string, hasAllPerm bool, permittedLists []int) error {
var queryStr string
if len(ids) > 0 {
@@ -342,7 +342,7 @@ func (c *Core) DeleteCampaigns(ids []int, query string, hasAllPerm bool, permitt
queryStr = makeSearchString(query)
}
- if _, err := c.q.DeleteCampaigns.Exec(pq.Array(ids), queryStr, hasAllPerm, pq.Array(permittedLists)); err != nil {
+ if _, err := c.q.DeleteCampaigns.Exec(pq.Array(ids), queryStr, hasAllPerm, pq.Array(permittedLists), pq.Array(statuses), pq.Array(tags), typ); err != nil {
c.log.Printf("error deleting campaigns: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorDeleting", "name", "{globals.terms.campaigns}", "error", pqErrMsg(err)))
diff --git a/internal/core/subscribers.go b/internal/core/subscribers.go
index 68cff671..8ca945c0 100644
--- a/internal/core/subscribers.go
+++ b/internal/core/subscribers.go
@@ -124,7 +124,7 @@ func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subSt
}
// Handle the special "none" subscription status for orphan subscribers.
- cond, subStatus = applyOrphanFilter(cond, subStatus)
+ cond, subStatus = models.ApplyOrphanFilter(cond, subStatus)
// stmt is the raw SQL query.
stmt := strings.ReplaceAll(c.q.QuerySubscribers, "%query%", cond)
@@ -256,7 +256,7 @@ func (c *Core) DeleteSubscriberActivity(id int, activityType string) error {
// on the given criteria in an exportable form. The iterator function returned can be called
// repeatedly until there are nil subscribers. It's an iterator because exports can be extremely
// large and may have to be fetched in batches from the DB and streamed somewhere.
-func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int, subStatus string, batchSize int) (func() ([]models.SubscriberExport, error), error) {
+func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int, subStatus, subscriberStatus string, batchSize int) (func() ([]models.SubscriberExport, error), error) {
if subIDs == nil {
subIDs = []int{}
}
@@ -271,13 +271,13 @@ func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int,
}
// Handle the special "none" subscription status for orphan subscribers.
- cond, subStatus = applyOrphanFilter(cond, subStatus)
+ cond, subStatus = models.ApplyOrphanFilter(cond, subStatus)
stmt := strings.ReplaceAll(c.q.QuerySubscribersForExport, "%query%", cond)
// Validate the tables used in the query.
if err := validateQueryTables(c.db, stmt, allowedSubQueryTables,
- pq.Array(listIDs), 0, pq.Array(subIDs), subStatus, searchStr, batchSize); err != nil {
+ pq.Array(listIDs), 0, pq.Array(subIDs), subStatus, searchStr, batchSize, subscriberStatus); err != nil {
c.log.Printf("error validating query tables: %v", err)
return nil, echo.NewHTTPError(http.StatusBadRequest,
c.i18n.Ts("subscribers.errorPreparingQuery", "error", err.Error()))
@@ -285,7 +285,7 @@ func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int,
// Create a readonly transaction that just does COUNT() to obtain the count of results
// and to ensure that the arbitrary query is indeed readonly.
- if _, err := c.getSubscriberCount(searchStr, cond, subStatus, "", listIDs); err != nil {
+ if _, err := c.getSubscriberCount(searchStr, cond, subStatus, subscriberStatus, listIDs); err != nil {
c.log.Printf("error getting subscriber count: %v", err)
return nil, err
}
@@ -301,7 +301,7 @@ func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int,
id := 0
return func() ([]models.SubscriberExport, error) {
var out []models.SubscriberExport
- if err := tx.Select(&out, pq.Array(listIDs), id, pq.Array(subIDs), subStatus, searchStr, batchSize); err != nil {
+ if err := tx.Select(&out, pq.Array(listIDs), id, pq.Array(subIDs), subStatus, searchStr, batchSize, subscriberStatus); err != nil {
c.log.Printf("error exporting subscribers by query: %v", err)
return nil, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
@@ -485,8 +485,8 @@ func (c *Core) BlocklistSubscribers(subIDs []int) error {
}
// BlocklistSubscribersByQuery blocklists the given list of subscribers.
-func (c *Core) BlocklistSubscribersByQuery(searchStr, queryExp string, listIDs []int, subStatus string) error {
- if err := c.q.ExecSubQueryTpl(searchStr, sanitizeSQLExp(queryExp), c.q.BlocklistSubscribersByQuery, listIDs, c.db, subStatus); err != nil {
+func (c *Core) BlocklistSubscribersByQuery(searchStr, queryExp string, listIDs []int, subStatus, subscriberStatus string) error {
+ if err := c.q.ExecSubQueryTpl(searchStr, sanitizeSQLExp(queryExp), c.q.BlocklistSubscribersByQuery, listIDs, c.db, subStatus, subscriberStatus); err != nil {
c.log.Printf("error blocklisting subscribers: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("subscribers.errorBlocklisting", "error", pqErrMsg(err)))
@@ -514,8 +514,8 @@ func (c *Core) DeleteSubscribers(subIDs []int, subUUIDs []string) error {
}
// DeleteSubscribersByQuery deletes subscribers by a given arbitrary query expression.
-func (c *Core) DeleteSubscribersByQuery(searchStr, queryExp string, listIDs []int, subStatus string) error {
- err := c.q.ExecSubQueryTpl(searchStr, sanitizeSQLExp(queryExp), c.q.DeleteSubscribersByQuery, listIDs, c.db, subStatus)
+func (c *Core) DeleteSubscribersByQuery(searchStr, queryExp string, listIDs []int, subStatus, subscriberStatus string) error {
+ err := c.q.ExecSubQueryTpl(searchStr, sanitizeSQLExp(queryExp), c.q.DeleteSubscribersByQuery, listIDs, c.db, subStatus, subscriberStatus)
if err != nil {
c.log.Printf("error deleting subscribers: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
@@ -698,14 +698,3 @@ func traverseQueryPlan(node map[string]any, tables map[string]struct{}) {
}
}
}
-
-// applyOrphanFilter handles the special subscription_status value "none", which
-// filters orphan subscribers (no list subscriptions).
-func applyOrphanFilter(cond, subStatus string) (string, string) {
- const orphanSubCond = "NOT EXISTS (SELECT 1 FROM subscriber_lists WHERE subscriber_id = subscribers.id)"
- if subStatus == "none" {
- return "(" + cond + ") AND " + orphanSubCond, ""
- }
-
- return cond, subStatus
-}
diff --git a/internal/core/subscriptions.go b/internal/core/subscriptions.go
index 1fff8fba..d30edd22 100644
--- a/internal/core/subscriptions.go
+++ b/internal/core/subscriptions.go
@@ -35,12 +35,12 @@ func (c *Core) AddSubscriptions(subIDs, listIDs []int, status string) error {
// AddSubscriptionsByQuery adds list subscriptions to subscribers by a given arbitrary query expression.
// sourceListIDs is the list of list IDs to filter the subscriber query with.
-func (c *Core) AddSubscriptionsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, status string, subStatus string) error {
+func (c *Core) AddSubscriptionsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, status string, subStatus, subscriberStatus string) error {
if sourceListIDs == nil {
sourceListIDs = []int{}
}
- err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.AddSubscribersToListsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs), status)
+ err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.AddSubscribersToListsByQuery, sourceListIDs, c.db, subStatus, subscriberStatus, pq.Array(targetListIDs), status)
if err != nil {
c.log.Printf("error adding subscriptions by query: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
@@ -64,12 +64,12 @@ func (c *Core) DeleteSubscriptions(subIDs, listIDs []int) error {
// DeleteSubscriptionsByQuery deletes list subscriptions from subscribers by a given arbitrary query expression.
// sourceListIDs is the list of list IDs to filter the subscriber query with.
-func (c *Core) DeleteSubscriptionsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, subStatus string) error {
+func (c *Core) DeleteSubscriptionsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, subStatus, subscriberStatus string) error {
if sourceListIDs == nil {
sourceListIDs = []int{}
}
- err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.DeleteSubscriptionsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs))
+ err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.DeleteSubscriptionsByQuery, sourceListIDs, c.db, subStatus, subscriberStatus, pq.Array(targetListIDs))
if err != nil {
c.log.Printf("error deleting subscriptions by query: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
@@ -92,12 +92,12 @@ func (c *Core) UnsubscribeLists(subIDs, listIDs []int, listUUIDs []string) error
// UnsubscribeListsByQuery sets list subscriptions to 'unsubscribed' by a given arbitrary query expression.
// sourceListIDs is the list of list IDs to filter the subscriber query with.
-func (c *Core) UnsubscribeListsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, subStatus string) error {
+func (c *Core) UnsubscribeListsByQuery(searchStr, queryExp string, sourceListIDs, targetListIDs []int, subStatus, subscriberStatus string) error {
if sourceListIDs == nil {
sourceListIDs = []int{}
}
- err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.UnsubscribeSubscribersFromListsByQuery, sourceListIDs, c.db, subStatus, pq.Array(targetListIDs))
+ err := c.q.ExecSubQueryTpl(searchStr, queryExp, c.q.UnsubscribeSubscribersFromListsByQuery, sourceListIDs, c.db, subStatus, subscriberStatus, pq.Array(targetListIDs))
if err != nil {
c.log.Printf("error unsubscribing from lists by query: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
diff --git a/models/queries.go b/models/queries.go
index 8be8a12b..987287f3 100644
--- a/models/queries.go
+++ b/models/queries.go
@@ -147,7 +147,7 @@ type Queries struct {
// out of it using the raw `query-subscribers-template` query template.
// While doing this, a readonly transaction is created and the query is
// dry run on it to ensure that it is indeed readonly.
-func (q *Queries) compileSubscriberQueryTpl(searchStr, queryExp string, db *sqlx.DB, subStatus string) (string, error) {
+func (q *Queries) compileSubscriberQueryTpl(searchStr, queryExp string, db *sqlx.DB, subStatus, subscriberStatus string) (string, error) {
tx, err := db.BeginTxx(context.Background(), &sql.TxOptions{ReadOnly: true})
if err != nil {
return "", err
@@ -162,7 +162,7 @@ func (q *Queries) compileSubscriberQueryTpl(searchStr, queryExp string, db *sqlx
// Perform the dry run.
stmt := strings.ReplaceAll(q.QuerySubscribersTpl, "%query%", cond)
- if _, err := tx.Exec(stmt, true, pq.Int64Array{}, subStatus, searchStr); err != nil {
+ if _, err := tx.Exec(stmt, true, pq.Int64Array{}, subStatus, searchStr, subscriberStatus); err != nil {
return "", err
}
@@ -172,9 +172,11 @@ func (q *Queries) compileSubscriberQueryTpl(searchStr, queryExp string, db *sqlx
// compileSubscriberQueryTpl takes an arbitrary WHERE expressions and a subscriber
// query template that depends on the filter (eg: delete by query, blocklist by query etc.)
// combines and executes them.
-func (q *Queries) ExecSubQueryTpl(searchStr, queryExp, baseQueryTpl string, listIDs []int, db *sqlx.DB, subStatus string, args ...any) error {
+func (q *Queries) ExecSubQueryTpl(searchStr, queryExp, baseQueryTpl string, listIDs []int, db *sqlx.DB, subStatus, subscriberStatus string, args ...any) error {
+ queryExp, subStatus = ApplyOrphanFilter(queryExp, subStatus)
+
// Perform a dry run.
- filterExp, err := q.compileSubscriberQueryTpl(searchStr, queryExp, db, subStatus)
+ filterExp, err := q.compileSubscriberQueryTpl(searchStr, queryExp, db, subStatus, subscriberStatus)
if err != nil {
return err
}
@@ -187,7 +189,7 @@ func (q *Queries) ExecSubQueryTpl(searchStr, queryExp, baseQueryTpl string, list
stmt := strings.ReplaceAll(baseQueryTpl, "%query%", filterExp)
// First argument is the boolean indicating if the query is a dry run.
- a := append([]any{false, pq.Array(listIDs), subStatus, searchStr}, args...)
+ a := append([]any{false, pq.Array(listIDs), subStatus, searchStr, subscriberStatus}, args...)
// Execute the query on the DB.
if _, err := db.Exec(stmt, a...); err != nil {
@@ -195,3 +197,17 @@ func (q *Queries) ExecSubQueryTpl(searchStr, queryExp, baseQueryTpl string, list
}
return nil
}
+
+// ApplyOrphanFilter handles the special subscription_status value "none", which
+// filters orphan subscribers (no list subscriptions).
+func ApplyOrphanFilter(cond, subStatus string) (string, string) {
+ const orphanSubCond = "NOT EXISTS (SELECT 1 FROM subscriber_lists WHERE subscriber_id = subscribers.id)"
+ if subStatus == "none" {
+ if cond == "" {
+ cond = "TRUE"
+ }
+ return "(" + cond + ") AND " + orphanSubCond, ""
+ }
+
+ return cond, subStatus
+}
diff --git a/queries/bounces.sql b/queries/bounces.sql
index 1ab2e339..7e3ec05e 100644
--- a/queries/bounces.sql
+++ b/queries/bounces.sql
@@ -57,7 +57,11 @@ WHERE ($1 = 0 OR bounces.id = $1)
ORDER BY %order% OFFSET $6 LIMIT (CASE WHEN $7 < 1 THEN NULL ELSE $7 END);
-- name: delete-bounces
-DELETE FROM bounces WHERE $2 = TRUE OR id = ANY($1);
+DELETE FROM bounces WHERE CASE WHEN $2 THEN
+ ($3 = 0 OR campaign_id = $3)
+ AND ($4 = '' OR source = $4)
+ AND ($5 = '' OR type = $5::bounce_type)
+ELSE id = ANY($1) END;
-- name: delete-bounces-by-subscriber
WITH sub AS (
@@ -68,6 +72,11 @@ DELETE FROM bounces WHERE subscriber_id = (SELECT id FROM sub);
-- name: blocklist-bounced-subscribers
WITH subs AS (
SELECT subscriber_id FROM bounces
+ WHERE CASE WHEN CARDINALITY($1::INT[]) > 0 THEN id = ANY($1)
+ ELSE ($2 = 0 OR campaign_id = $2)
+ AND ($3 = '' OR source = $3)
+ AND ($4 = '' OR type = $4::bounce_type)
+ END
),
b AS (
UPDATE subscribers SET status='blocklisted', updated_at=NOW()
diff --git a/queries/campaigns.sql b/queries/campaigns.sql
index c75531c4..f6a0739f 100644
--- a/queries/campaigns.sql
+++ b/queries/campaigns.sql
@@ -469,7 +469,10 @@ DELETE FROM campaigns c
WHERE (
CASE
WHEN CARDINALITY($1::INT[]) > 0 THEN id = ANY($1)
- ELSE $2 = '' OR TO_TSVECTOR(CONCAT(name, ' ', subject)) @@ PLAINTO_TSQUERY($2) OR CONCAT(c.name, ' ', c.subject) ILIKE ('%' || $2 || '%')
+ ELSE ($2 = '' OR TO_TSVECTOR(CONCAT(name, ' ', subject)) @@ PLAINTO_TSQUERY($2) OR CONCAT(c.name, ' ', c.subject) ILIKE ('%' || $2 || '%'))
+ AND (COALESCE(CARDINALITY($5::campaign_status[]), 0) = 0 OR status = ANY($5))
+ AND (COALESCE(CARDINALITY($6::VARCHAR(100)[]), 0) = 0 OR $6 <@ tags)
+ AND ($7 = '' OR type = $7::campaign_type)
END
)
-- Get all campaigns or filter by permitted list IDs.
diff --git a/queries/subscribers.sql b/queries/subscribers.sql
index 4f12b17c..92d9b17f 100644
--- a/queries/subscribers.sql
+++ b/queries/subscribers.sql
@@ -289,34 +289,36 @@ DELETE FROM subscriber_lists
-- there's a COUNT() OVER() that still returns the total result count
-- for pagination in the frontend, albeit being a field that'll repeat
-- with every resultant row.
-SELECT subscribers.* FROM subscribers
+SELECT DISTINCT subscribers.* FROM subscribers
LEFT JOIN subscriber_lists
ON (
-- Optional list filtering.
- (CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
+ (CARDINALITY($1::INT[]) > 0 OR $2 != '')
AND subscriber_lists.subscriber_id = subscribers.id
AND ($2 = '' OR subscriber_lists.status = $2::subscription_status)
)
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
+ AND ($2 = '' OR subscriber_lists.subscriber_id IS NOT NULL)
AND (CASE WHEN $3 != '' THEN name ~* $3 OR email ~* $3 ELSE TRUE END)
AND ($6 = '' OR subscribers.status = $6::subscriber_status)
- AND %query%
+ AND (%query%)
ORDER BY %order% OFFSET $4 LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END);
-- name: query-subscribers-count
-- Replica of query-subscribers for obtaining the results count.
-SELECT COUNT(*) AS total FROM subscribers
+SELECT COUNT(DISTINCT subscribers.id) AS total FROM subscribers
LEFT JOIN subscriber_lists
ON (
-- Optional list filtering.
- (CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
+ (CARDINALITY($1::INT[]) > 0 OR $2 != '')
AND subscriber_lists.subscriber_id = subscribers.id
AND ($2 = '' OR subscriber_lists.status = $2::subscription_status)
)
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
+ AND ($2 = '' OR subscriber_lists.subscriber_id IS NOT NULL)
AND (CASE WHEN $3 != '' THEN name ~* $3 OR email ~* $3 ELSE TRUE END)
AND ($4 = '' OR subscribers.status = $4::subscriber_status)
- AND %query%;
+ AND (%query%);
-- name: query-subscribers-count-all
-- Cached query for getting the "all" subscriber count without arbitrary conditions.
@@ -328,7 +330,7 @@ SELECT COALESCE(SUM(subscriber_count), 0) AS total FROM mat_list_subscriber_stat
-- raw: true
-- Unprepared statement for issuring arbitrary WHERE conditions for
-- searching subscribers to do bulk CSV export.
-SELECT subscribers.id,
+SELECT DISTINCT subscribers.id,
subscribers.uuid,
subscribers.email,
subscribers.name,
@@ -340,14 +342,16 @@ SELECT subscribers.id,
LEFT JOIN subscriber_lists
ON (
-- Optional list filtering.
- (CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
+ (CARDINALITY($1::INT[]) > 0 OR $4 != '')
AND subscriber_lists.subscriber_id = subscribers.id
AND ($4 = '' OR subscriber_lists.status = $4::subscription_status)
)
- WHERE subscriber_lists.list_id = ALL($1::INT[]) AND id > $2
+ WHERE (CARDINALITY($1::INT[]) = 0 OR subscriber_lists.list_id = ANY($1)) AND id > $2
+ AND ($4 = '' OR subscriber_lists.subscriber_id IS NOT NULL)
AND (CASE WHEN CARDINALITY($3::INT[]) > 0 THEN id=ANY($3) ELSE true END)
AND (CASE WHEN $5 != '' THEN name ~* $5 OR email ~* $5 ELSE TRUE END)
- AND %query%
+ AND ($7 = '' OR subscribers.status = $7::subscriber_status)
+ AND (%query%)
ORDER BY subscribers.id ASC LIMIT (CASE WHEN $6 < 1 THEN NULL ELSE $6 END);
-- name: query-subscribers-template
@@ -357,19 +361,21 @@ SELECT subscribers.id,
-- and for the same reason, it is not terminated with a semicolon.
--
-- All queries that embed this query should expect
--- $1=true/false (dry-run or not) and $2=[]INT (option list IDs).
--- That is, their positional arguments should start from $4.
-SELECT subscribers.id FROM subscribers
+-- $1=dry-run, $2=list IDs, $3=subscription status, $4=search, $5=subscriber status.
+-- Their positional arguments should start from $6.
+SELECT DISTINCT subscribers.id FROM subscribers
LEFT JOIN subscriber_lists
ON (
-- Optional list filtering.
- (CASE WHEN CARDINALITY($2::INT[]) > 0 THEN true ELSE false END)
+ (CARDINALITY($2::INT[]) > 0 OR $3 != '')
AND subscriber_lists.subscriber_id = subscribers.id
AND ($3 = '' OR subscriber_lists.status = $3::subscription_status)
)
-WHERE subscriber_lists.list_id = ALL($2::INT[])
+WHERE (CARDINALITY($2::INT[]) = 0 OR subscriber_lists.list_id = ANY($2))
+ AND ($3 = '' OR subscriber_lists.subscriber_id IS NOT NULL)
AND (CASE WHEN $4 != '' THEN name ~* $4 OR email ~* $4 ELSE TRUE END)
- AND %query%
+ AND ($5 = '' OR subscribers.status = $5::subscriber_status)
+ AND (%query%)
LIMIT (CASE WHEN $1 THEN 1 END)
-- name: delete-subscribers-by-query
@@ -391,20 +397,20 @@ UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
-- raw: true
WITH subs AS (%query%)
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
- (SELECT a, b, (CASE WHEN $6 != '' THEN $6::subscription_status ELSE 'unconfirmed' END) FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($5::INT[]) b)
+ (SELECT a, b, (CASE WHEN $7 != '' THEN $7::subscription_status ELSE 'unconfirmed' END) FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($6::INT[]) b)
ON CONFLICT (subscriber_id, list_id) DO NOTHING;
-- name: delete-subscriptions-by-query
-- raw: true
WITH subs AS (%query%)
DELETE FROM subscriber_lists
- WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($5::INT[]) b);
+ WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($6::INT[]) b);
-- name: unsubscribe-subscribers-from-lists-by-query
-- raw: true
WITH subs AS (%query%)
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
- WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($5::INT[]) b);
+ WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($6::INT[]) b);
-- privacy
diff --git a/static/admin/assets/js/views/bounces.js b/static/admin/assets/js/views/bounces.js
index f7ea65e5..b0bb0a2e 100644
--- a/static/admin/assets/js/views/bounces.js
+++ b/static/admin/assets/js/views/bounces.js
@@ -28,8 +28,7 @@ function component() {
// ===============
// Bulk actions.
- async onDeleteSelected(allSelected, selected, total) {
- const num = allSelected ? total : selected.length;
+ async onDeleteSelected(num, params) {
if (num === 0 || !(await u.confirm(i18n.ts('globals.messages.confirmDelete', {
num,
name: i18n.tc('globals.terms.bounce', num).toLowerCase(),
@@ -37,13 +36,7 @@ function component() {
return;
}
- if (allSelected) {
- await api('bounces', '/bounces?all=true', 'DELETE');
- } else {
- const q = new URLSearchParams();
- selected.forEach((id) => q.append('id', id));
- await api('bounces', `/bounces?${q.toString()}`, 'DELETE');
- }
+ await api('bounces', `/bounces?${params}`, 'DELETE');
u.reload({
message: i18n.ts('globals.messages.deletedCount', {
@@ -53,22 +46,20 @@ function component() {
});
},
- async onBlocklistSelected(allSelected, total) {
- const subIDs = Array.from(document.querySelectorAll('input[name="id"]:checked'))
+ async onBlocklistSelected(allSelected, total, params) {
+ const subIDs = [...new Set(Array.from(this.root.querySelectorAll('input[name="id"]:checked'))
.map((el) => Number(el.dataset.subscriberId))
- .filter((id) => id > 0);
+ .filter((id) => id > 0))];
const num = allSelected ? total : subIDs.length;
- if (num === 0 || !(await u.confirm(i18n.ts('subscribers.confirmBlocklist', { num })))) {
+ const message = i18n.ts('subscribers.confirmBlocklist', {
+ num: allSelected ? i18n.t('globals.terms.all') : num,
+ });
+ if (num === 0 || !(await u.confirm(message))) {
return;
}
- // "Select all" blocklists every bounced subscriber or blocklist selected rows.
- if (allSelected) {
- await api('bounces', '/bounces/blocklist', 'PUT');
- } else {
- await api('bounces', '/subscribers/blocklist', 'PUT', { ids: subIDs });
- }
+ await api('bounces', `/bounces/blocklist?${params}`, 'PUT');
u.reload({ message: i18n.t('globals.messages.done') });
},
diff --git a/static/admin/assets/js/views/subscribers.js b/static/admin/assets/js/views/subscribers.js
index 5bff35ab..0636d853 100644
--- a/static/admin/assets/js/views/subscribers.js
+++ b/static/admin/assets/js/views/subscribers.js
@@ -10,6 +10,7 @@ import * as u from '../utils.js';
function component(sub = null) {
// The current view's filters are injected by the template.
const filters = window._filters || {};
+ filters.listIDs = (filters.listIDs || []).filter((id) => id !== '').map(Number);
// Init a fresh form payload.
const makeForm = () => ({
@@ -30,7 +31,7 @@ function component(sub = null) {
});
return {
- filters,
+ subscriberFilters: filters,
query: filters.query || '',
isSqlOpen: false,
get advanced() {
@@ -189,16 +190,18 @@ function component(sub = null) {
}
const q = new URLSearchParams();
- if (this.filters.search) {
- q.append('search', this.filters.search);
- } else if (this.filters.query) {
- q.append('query', this.filters.query);
+ if (this.subscriberFilters.search) {
+ q.append('search', this.subscriberFilters.search);
}
- if (this.filters.listID) {
- q.append('list_id', this.filters.listID);
+ if (this.subscriberFilters.query) {
+ q.append('query', this.subscriberFilters.query);
}
- if (this.filters.subStatus) {
- q.append('subscription_status', this.filters.subStatus);
+ this.subscriberFilters.listIDs.forEach((id) => q.append('list_id', id));
+ if (this.subscriberFilters.status) {
+ q.append('status', this.subscriberFilters.status);
+ }
+ if (this.subscriberFilters.subStatus) {
+ q.append('subscription_status', this.subscriberFilters.subStatus);
}
if (!allSelected && selected.length > 0) {
selected.forEach((id) => q.append('id', id));
@@ -303,14 +306,15 @@ function component(sub = null) {
// Private functions.
// Body for the "by query" bulk endpoints.
_queryBody() {
- const search = this.filters.search || '';
- const query = this.filters.query || '';
+ const search = this.subscriberFilters.search || '';
+ const query = this.subscriberFilters.query || '';
return {
all: query.trim() === '' && search.trim() === '',
search,
query,
- list_ids: this.filters.listID ? [this.filters.listID] : null,
- subscription_status: this.filters.subStatus || null,
+ list_ids: this.subscriberFilters.listIDs,
+ subscription_status: this.subscriberFilters.subStatus || null,
+ subscriber_status: this.subscriberFilters.status || '',
};
},
diff --git a/static/admin/i18n.txt b/static/admin/i18n.txt
index 867e80af..4de93921 100644
--- a/static/admin/i18n.txt
+++ b/static/admin/i18n.txt
@@ -1,6 +1,7 @@
# List of i18n keys whose translated strings are rendered on admin HTML pages as a map for JS to use.
# Globals.
+globals.terms.all
globals.buttons.back
globals.buttons.cancel
globals.buttons.ok
diff --git a/static/admin/views/bounces.html b/static/admin/views/bounces.html
index 0ad6c7e5..8635c107 100644
--- a/static/admin/views/bounces.html
+++ b/static/admin/views/bounces.html
@@ -36,9 +36,9 @@
</button>
{{- end }}
{{- if $d.Can "bounces:manage" }}
- <button type="button" role="menuitem" class="ghost" data-testid="btn-manage-blocklist" @click.prevent="onBlocklistSelected(allSelected, total)">{{ Icon "user-x" }} {{ .L.T "import.blocklist" }}</button>
+ <button type="button" role="menuitem" class="ghost" data-testid="btn-manage-blocklist" @click.prevent="onBlocklistSelected(allSelected, total, selectedQueryParams().toString())">{{ Icon "user-x" }} {{ .L.T "import.blocklist" }}</button>
<hr />
- <button type="button" role="menuitem" class="ghost" data-variant="danger" data-testid="btn-delete-bounces" :disabled="isLoading('bounces')" @click.prevent="onDeleteSelected(allSelected, selected, total)">{{ Icon "trash-2" }} {{ .L.T "globals.buttons.delete" }} <span x-text="numSelected"></span></button>
+ <button type="button" role="menuitem" class="ghost" data-variant="danger" data-testid="btn-delete-bounces" :disabled="isLoading('bounces')" @click.prevent="onDeleteSelected(numSelected, selectedQueryParams().toString())">{{ Icon "trash-2" }} {{ .L.T "globals.buttons.delete" }} <span x-text="numSelected"></span></button>
{{- end }}
</menu>
</ot-dropdown>
diff --git a/static/admin/views/subscribers.html b/static/admin/views/subscribers.html
index d6c35978..1be35c3a 100644
--- a/static/admin/views/subscribers.html
+++ b/static/admin/views/subscribers.html
@@ -10,10 +10,11 @@
<script>
window._lists = {{ ToJSON $d.AllLists }};
window._filters = {
- listID: {{ if $d.CurrentList }}{{ $d.CurrentList.ID }}{{ else }}null{{ end }},
+ listIDs: {{ if $d.CurrentList }}[{{ $d.CurrentList.ID }}]{{ else }}{{ ToJSON (index $d.Page.QueryParams "list_id") }}{{ end }},
search: {{ ToJSON $d.Page.Search }},
query: {{ ToJSON $d.Page.Query }},
- subStatus: {{ ToJSON $subStatus }}
+ subStatus: {{ ToJSON $subStatus }},
+ status: {{ ToJSON $status }}
};
</script>
diff --git a/tests/specs/bounces.spec.js b/tests/specs/bounces.spec.js
index 17c714af..7282d466 100644
--- a/tests/specs/bounces.spec.js
+++ b/tests/specs/bounces.spec.js
@@ -80,3 +80,109 @@ test.describe('Bounces', () => {
await expect(bounceRow(page)).toHaveCount(before - 1);
});
});
+
+test.describe('Bounces: bulk actions', () => {
+ test.beforeEach(async ({ browser }) => {
+ await resetDB(browser, { bounces: true });
+ });
+
+ test('rejects invalid scopes, preserves explicit IDs, and uses bounce permissions for blocklisting', async ({ page, browser }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const subs = (await data(await page.request.get('/api/subscribers'))).results;
+ for (const sub of subs) await postBounce(page, 'soft', sub.email);
+ const bounces = (await data(await page.request.get('/api/bounces'))).results;
+ expect(bounces).toHaveLength(2);
+ for (const url of ['?all=true&campaign_id=invalid', '?all=true&id=invalid', '']) {
+ expect((await page.request.delete(`/api/bounces${url}`)).status()).toBe(400);
+ }
+ expect((await page.request.put('/api/bounces/blocklist?id=invalid')).status()).toBe(400);
+ await data(await page.request.delete('/api/bounces?all=true&source=missing'));
+ await data(await page.request.put('/api/bounces/blocklist?source=missing'));
+ for (const sub of subs) expect(await subStatus(page, sub.id)).toBe('enabled');
+
+ const role = await data(await page.request.post('/api/roles/users', { data: {
+ name: 'Bounce manager', permissions: ['bounces:get', 'bounces:manage'],
+ } }));
+ const user = await data(await page.request.post('/api/users', { data: {
+ username: 'bounce-manager', type: 'api', status: 'enabled', user_role_id: role.id,
+ } }));
+ const context = await browser.newContext({
+ storageState: { cookies: [], origins: [] },
+ extraHTTPHeaders: { Authorization: `Basic ${Buffer.from(`${user.username}:${user.password}`).toString('base64')}` },
+ });
+ try {
+ await data(await context.request.put(`/api/bounces/blocklist?id=${bounces[0].id}`));
+ } finally {
+ await context.close();
+ }
+ expect(await subStatus(page, bounces[0].subscriber_id)).toBe('blocklisted');
+ expect(await subStatus(page, bounces[1].subscriber_id)).toBe('enabled');
+ await data(await page.request.delete(`/api/bounces?all=true&id=${bounces[0].id}`));
+ expect((await data(await page.request.get('/api/bounces'))).results.map((b) => b.id)).toEqual([bounces[1].id]);
+ });
+
+ for (const filter of ['source', 'type', 'campaign_id', 'combined', 'selected IDs', 'deselected row']) {
+ for (const action of ['delete', 'blocklist']) {
+ test(`${action} preserves bounces and subscribers outside ${filter}`, async ({ page }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const campaign = await data(await page.request.post('/api/campaigns', { data: {
+ name: 'Bounce target', subject: 'Bounce target', type: 'regular', content_type: 'html', body: 'Test', lists: [1],
+ } }));
+ const targets = [];
+ const outsiders = [];
+ for (let i = 0; i < 25; i += 1) {
+ const sub = await data(await page.request.post('/api/subscribers', { data: {
+ email: `bounce${i}@example.com`, name: `Bounce ${i}`, status: 'enabled', lists: [1],
+ } }));
+ const outside = i >= 22;
+ const source = outside && ['source', 'combined'].includes(filter) ? 'other' : 'target';
+ const type = outside && ['type', 'combined'].includes(filter) ? 'hard' : 'soft';
+ const campaignUUID = outside && ['campaign_id', 'combined'].includes(filter) ? '' : campaign.uuid;
+ await data(await page.request.post('/webhooks/bounce', { data: { email: sub.email, source, type, campaign_uuid: campaignUUID } }));
+ // Hard-bounce processing blocklists automatically; restore the fixture so
+ // accidental bulk blocklisting of an unrelated subscriber is observable.
+ if (type === 'hard') await data(await page.request.put(`/api/subscribers/${sub.id}`, { data: {
+ email: sub.email, name: sub.name, status: 'enabled', lists: [1],
+ } }));
+ (outside ? outsiders : targets).push(sub.id);
+ }
+ const query = {
+ source: 'source=target', type: 'type=soft', campaign_id: `campaign_id=${campaign.id}`,
+ combined: `source=target&type=soft&campaign_id=${campaign.id}`,
+ 'selected IDs': '', 'deselected row': '',
+ }[filter];
+ await page.goto(`${BOUNCES}?${query}`);
+ await page.locator('thead input[type=checkbox]').check();
+ await page.getByTestId('btn-bulk-actions').click();
+ if (filter !== 'selected IDs') await page.getByTestId('select-all-bounces').click();
+ if (filter === 'deselected row') {
+ await page.getByTestId('btn-bulk-actions').click();
+ await bounceRow(page).first().locator('input[name=id]').uncheck();
+ await page.getByTestId('btn-bulk-actions').click();
+ }
+ const selected = filter.includes('selected')
+ ? await page.locator('input[name=id]:checked').evaluateAll((els) => els.map((el) => Number(el.dataset.subscriberId)))
+ : targets;
+ await page.getByTestId(action === 'delete' ? 'btn-delete-bounces' : 'btn-manage-blocklist').click();
+ const [res] = await Promise.all([
+ page.waitForResponse((r) => r.request().method() === (action === 'delete' ? 'DELETE' : 'PUT')),
+ confirm(page),
+ ]);
+ expect(res.ok()).toBeTruthy();
+ const bounces = (await data(await page.request.get('/api/bounces?per_page=100'))).results;
+ const subscribers = (await data(await page.request.get('/api/subscribers?per_page=100'))).results;
+ for (const id of [...targets, ...outsiders]) {
+ const affected = selected.includes(id);
+ expect(bounces.some((b) => b.subscriber_id === id)).toBe(action !== 'delete' || !affected);
+ expect(subscribers.find((s) => s.id === id).status).toBe(action === 'blocklist' && affected ? 'blocklisted' : 'enabled');
+ }
+ });
+ }
+ }
+});
diff --git a/tests/specs/campaigns.spec.js b/tests/specs/campaigns.spec.js
index c42e438c..b8372417 100644
--- a/tests/specs/campaigns.spec.js
+++ b/tests/specs/campaigns.spec.js
@@ -348,10 +348,38 @@ test('searches, filters and sorts campaigns through SSR navigation', async ({ pa
}
});
-for (const all of [false, true]) {
- test(`bulk deletes ${all ? 'all matching pages' : 'selected IDs'} and preserves other campaigns`, async ({ page }) => {
- for (let i = 0; i < (all ? 25 : 3); i += 1) await create(page, { name: `Bulk ${i}` });
- await page.goto(`${CAMPAIGNS}?query=Bulk`);
+for (const { label, all, filter, unrelated } of [
+ { label: 'selected IDs', all: false, filter: 'query=Bulk' },
+ { label: 'all matching pages', all: true, filter: 'query=Bulk' },
+ { label: 'all tagged campaigns', all: true, filter: 'tag=target', unrelated: [{ tags: ['other'] }] },
+ { label: 'all campaigns with a status', all: true, filter: 'status=draft', unrelated: [{ status: 'scheduled' }] },
+ { label: 'all campaigns of a type', all: true, filter: 'type=regular', unrelated: [{ type: 'optin', lists: [2] }] },
+ {
+ label: 'all campaigns matching combined filters', all: true,
+ filter: 'query=Bulk&tag=target&tag=news&status=draft&type=regular',
+ unrelated: [
+ { name: 'Other' }, { tags: ['target'] }, { status: 'scheduled' }, { type: 'optin', lists: [2] },
+ ],
+ },
+]) {
+ test(`bulk deletes ${label} and preserves other campaigns`, async ({ page }) => {
+ const preserved = [];
+ if (unrelated) {
+ await data(await page.request.delete('/api/campaigns?all=true'));
+ for (const { status, ...overrides } of unrelated) {
+ const c = await create(page, {
+ name: 'Bulk unrelated', tags: ['target', 'news'],
+ ...(status ? { send_at: new Date(Date.now() + 86400000).toISOString() } : {}),
+ ...overrides,
+ });
+ if (status) await data(await page.request.put(`/api/campaigns/${c.id}/status`, { data: { status } }));
+ preserved.push(c.id);
+ }
+ }
+ for (let i = 0; i < (all ? 22 : 3); i += 1) {
+ await create(page, { name: `Bulk ${i}`, tags: ['target', 'news'] });
+ }
+ await page.goto(`${CAMPAIGNS}?${filter}`);
await page.locator('thead input[type="checkbox"]').check();
await page.locator('button[popovertarget="camp-bulk-actions"]').click();
if (all) await page.getByRole('menuitem', { name: /Select all/ }).click();
@@ -360,10 +388,54 @@ for (const all of [false, true]) {
await expect(rows(page)).toHaveCount(0);
await expect(page.locator('.empty-state')).toBeVisible();
await page.goto(CAMPAIGNS);
- await expect(rows(page)).toHaveCount(1);
+ await expect(rows(page)).toHaveCount(unrelated ? preserved.length : 1);
+ if (unrelated) {
+ expect(await rows(page).evaluateAll((els) => els.map((el) => Number(el.dataset.campaignId)).sort((a, b) => a - b)))
+ .toEqual(preserved.sort((a, b) => a - b));
+ }
});
}
+test('bulk deletion handles no matches, explicit IDs and unfiltered requests', async ({ page }) => {
+ const c = await create(page, { tags: ['target'] });
+ const ids = async () => (await data(await page.request.get('/api/campaigns'))).results.map((r) => r.id).sort((a, b) => a - b);
+ const before = await ids();
+ await data(await page.request.delete('/api/campaigns?all=true&tag=missing'));
+ expect(await ids()).toEqual(before);
+ await data(await page.request.delete(`/api/campaigns?id=${c.id}&tag=missing&status=paused&type=optin`));
+ expect(await ids()).toEqual(before.filter((id) => id !== c.id));
+ await data(await page.request.delete('/api/campaigns?all=true'));
+ expect(await ids()).toEqual([]);
+});
+
+test('bulk deletion respects list permissions as well as filters', async ({ page, browser }) => {
+ const permitted = await create(page, { tags: ['target'], lists: [1] });
+ const forbidden = await create(page, { tags: ['target'], lists: [2] });
+ const unrelated = await create(page, { tags: ['other'], lists: [1] });
+ const role = await data(await page.request.post('/api/roles/users', { data: {
+ name: 'Campaign manager', permissions: ['campaigns:get', 'campaigns:manage'],
+ } }));
+ const listRole = await data(await page.request.post('/api/roles/lists', { data: {
+ name: 'Default list manager', lists: [{ id: 1, permissions: ['list:get', 'list:manage'] }],
+ } }));
+ const user = await data(await page.request.post('/api/users', { data: {
+ username: 'bulk-manager', type: 'api', status: 'enabled', user_role_id: role.id, list_role_id: listRole.id,
+ } }));
+ const context = await browser.newContext({
+ storageState: { cookies: [], origins: [] },
+ extraHTTPHeaders: { Authorization: `Basic ${Buffer.from(`${user.username}:${user.password}`).toString('base64')}` },
+ });
+ try {
+ await data(await context.request.delete('/api/campaigns?all=true&tag=target'));
+ } finally {
+ await context.close();
+ }
+ const remaining = (await data(await page.request.get('/api/campaigns'))).results.map((c) => c.id);
+ expect(remaining).not.toContain(permitted.id);
+ expect(remaining).toContain(forbidden.id);
+ expect(remaining).toContain(unrelated.id);
+});
+
test('uploads an attachment, persists it and delivers it when starting from the editor', async ({ page }) => {
const c = await create(page, { subject: 'Attachment delivery' });
await page.goto(`${CAMPAIGNS}/${c.id}`);
diff --git a/tests/specs/lists.spec.js b/tests/specs/lists.spec.js
index c9a99412..67d8dd29 100644
--- a/tests/specs/lists.spec.js
+++ b/tests/specs/lists.spec.js
@@ -319,3 +319,53 @@ test.describe('Lists', () => {
await expect(page.locator('.empty-state')).toBeVisible();
});
});
+
+test.describe('Lists: filtered bulk deletion', () => {
+ test.beforeEach(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ for (const { filter, unrelated } of [
+ { filter: '', unrelated: [{ status: 'archived' }] },
+ { filter: 'tag=target', unrelated: [{ tags: ['other'] }] },
+ { filter: 'type=private', unrelated: [{ type: 'public' }] },
+ { filter: 'optin=single', unrelated: [{ optin: 'double' }] },
+ { filter: 'status=archived', unrelated: [{ status: 'active' }] },
+ { filter: 'query=Bulk', unrelated: [{ name: 'Other' }] },
+ {
+ filter: 'query=Bulk&tag=target&tag=news&type=private&optin=single',
+ unrelated: [{ name: 'Other' }, { tags: ['target'] }, { type: 'public' }, { optin: 'double' }, { status: 'archived' }],
+ },
+ ]) {
+ test(`preserves unrelated lists when deleting all with ${filter || 'default active status'}`, async ({ page }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ await data(await page.request.delete('/api/lists?all=true'));
+ const preserved = [];
+ for (const overrides of [...unrelated, ...Array.from({ length: 22 }, () => ({}))]) {
+ const list = await data(await page.request.post('/api/lists', { data: {
+ name: 'Bulk list', type: 'private', optin: 'single', status: filter === 'status=archived' ? 'archived' : 'active',
+ tags: ['target', 'news'], ...overrides,
+ } }));
+ if (Object.keys(overrides).length) preserved.push(list.id);
+ }
+ await page.goto(`${LISTS}?${filter}`);
+ await page.locator('thead input[type=checkbox]').check();
+ await page.getByTestId('btn-bulk-actions').click();
+ await page.getByTestId('select-all-lists').click();
+ await withListAPI(page, 'DELETE', async () => {
+ await page.getByTestId('btn-delete-lists').click();
+ await confirm(page);
+ });
+ await expect(page.locator('.empty-state')).toBeVisible();
+ const remaining = [];
+ for (const status of ['active', 'archived']) {
+ remaining.push(...(await data(await page.request.get(`/api/lists?status=${status}&per_page=100`))).results);
+ }
+ expect(remaining.map((l) => l.id).sort((a, b) => a - b)).toEqual(preserved.sort((a, b) => a - b));
+ expect((await data(await page.request.get('/api/subscribers'))).results).toHaveLength(2);
+ });
+ }
+});
diff --git a/tests/specs/settings.spec.js b/tests/specs/settings.spec.js
index 9a69184b..6718ef1f 100644
--- a/tests/specs/settings.spec.js
+++ b/tests/specs/settings.spec.js
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test';
-import { resetDB, getMail } from '../helpers.js';
+import { resetDB, getMail, confirm } from '../helpers.js';
const SETTINGS = '/admin/settings';
const smtpServers = (page) => page.locator('.mail-servers details');
@@ -288,3 +288,45 @@ test.describe('Settings', () => {
});
});
+
+test.describe('Maintenance: bulk deletion', () => {
+ test.beforeEach(async ({ browser }) => { await resetDB(browser); });
+
+ test('limits subscriber cleanup to its selected type and subscription cleanup to its cutoff', async ({ page }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const original = (await data(await page.request.get('/api/subscribers'))).results;
+ const blocked = await data(await page.request.post('/api/subscribers', { data: {
+ email: '[email protected]', name: 'Blocked', status: 'blocklisted', lists: [1],
+ } }));
+ const orphan = await data(await page.request.post('/api/subscribers', { data: {
+ email: '[email protected]', name: 'Orphan', status: 'enabled', lists: [],
+ } }));
+ await page.goto('/admin/settings/maintenance');
+ for (const type of ['blocklisted', 'orphan']) {
+ await page.locator('select[x-model=subscriberType]').selectOption(type);
+ await page.locator('[data-cy=btn-delete-subscribers]').click();
+ const [res] = await Promise.all([
+ page.waitForResponse((r) => r.url().endsWith(`/api/maintenance/subscribers/${type}`)), confirm(page),
+ ]);
+ expect(res.ok()).toBeTruthy();
+ const remaining = (await data(await page.request.get('/api/subscribers'))).results;
+ expect(remaining.some((s) => s.id === blocked.id)).toBe(false);
+ expect(remaining.some((s) => s.id === orphan.id)).toBe(type !== 'orphan');
+ expect(remaining.filter((s) => original.some((o) => o.id === s.id))).toEqual(original);
+ }
+ expect((await page.request.delete('/api/maintenance/subscriptions/unconfirmed?before_date=invalid')).status()).toBe(400);
+ await data(await page.request.delete('/api/maintenance/subscriptions/unconfirmed?before_date=1970-01-01T00:00:00Z'));
+ expect((await data(await page.request.get('/api/subscribers'))).results).toEqual(original);
+ const future = new Date(Date.now() + 86400000).toISOString();
+ await data(await page.request.delete(`/api/maintenance/subscriptions/unconfirmed?before_date=${encodeURIComponent(future)}`));
+ const remaining = (await data(await page.request.get('/api/subscribers'))).results;
+ expect(remaining.map((s) => s.id)).toEqual(original.map((s) => s.id));
+ for (const sub of remaining) {
+ const before = original.find((s) => s.id === sub.id);
+ expect(sub.lists.map((l) => l.id)).toEqual(before.lists.filter((l) => !(l.optin === 'double' && l.subscription_status === 'unconfirmed')).map((l) => l.id));
+ }
+ });
+});
diff --git a/tests/specs/subscribers.spec.js b/tests/specs/subscribers.spec.js
index 2d4c0e20..b7c8250c 100644
--- a/tests/specs/subscribers.spec.js
+++ b/tests/specs/subscribers.spec.js
@@ -1,4 +1,5 @@
import { test, expect } from '@playwright/test';
+import { readFile } from 'node:fs/promises';
import { setTimeout as sleep } from 'node:timers/promises';
import {
openMenu, confirm, resetDB, selectList, getMail, clearMail,
@@ -467,6 +468,93 @@ test.describe('Subscribers: bulk actions', () => {
});
});
+test.describe('Subscribers: filtered bulk actions', () => {
+ test.beforeEach(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ for (const scope of ['status', 'list', 'orphan', 'search', 'query', 'subscription', 'list query params']) {
+ for (const action of ['delete', 'blocklist', 'export', 'add', 'remove', 'unsubscribe']) {
+ test(`${action} applies only to subscribers matching the ${scope} filter`, async ({ page }) => {
+ expect((await page.request.post('/api/subscribers/query/delete', { data: { all: true } })).ok()).toBeTruthy();
+ const status = scope === 'status' && ['delete', 'export'].includes(action) ? 'blocklisted' : 'disabled';
+ const sourceList = scope === 'subscription' ? LIST_OPTIN : LIST_DEFAULT;
+ const targetList = sourceList === LIST_DEFAULT ? LIST_OPTIN : LIST_DEFAULT;
+ await seedSubscribers(page, 22, { prefix: 'target', status, lists: scope === 'orphan' ? [] : [sourceList] });
+ await seedSubscribers(page, 3, { prefix: 'unrelated', lists: ['list', 'list query params', 'orphan', 'query'].includes(scope) ? [LIST_OPTIN] : [sourceList] });
+ const getSubscribers = async () => {
+ const res = await page.request.get('/api/subscribers?per_page=100');
+ expect(res.ok()).toBeTruthy();
+ return (await res.json()).data.results;
+ };
+ if (scope === 'subscription') {
+ const ids = (await getSubscribers()).filter((s) => s.status === 'enabled').map((s) => s.id);
+ expect((await page.request.put('/api/subscribers/lists', { data: {
+ ids, action: 'add', target_list_ids: [sourceList], status: 'confirmed',
+ } })).ok()).toBeTruthy();
+ }
+ const before = await getSubscribers();
+ const unrelated = before.filter((s) => s.status === 'enabled');
+ expect(unrelated).toHaveLength(3);
+ const url = {
+ status: `?status=${status}`,
+ list: `/lists/${sourceList}`,
+ orphan: '?subscription_status=none',
+ search: '?search=target',
+ query: `/lists/${sourceList}?query=${encodeURIComponent("subscribers.email LIKE 'target%' OR subscribers.status = 'enabled'")}`,
+ subscription: '?subscription_status=unconfirmed',
+ 'list query params': `?list_id=${sourceList}`,
+ }[scope];
+ await page.goto(`${SUBSCRIBERS}${url}`);
+ expect(await readTotal(page)).toBe(22);
+
+ if (action === 'delete' || action === 'blocklist') {
+ await bulkConfirm(page, {
+ testid: action === 'delete' ? 'btn-delete-subscribers' : 'btn-manage-blocklist',
+ selectAll: true,
+ match: (r) => r.url().endsWith(`/api/subscribers/query/${action}`)
+ && r.request().method() === (action === 'delete' ? 'POST' : 'PUT'),
+ });
+ if (action === 'delete' || scope === 'status') await expect(page.locator('.empty-state')).toBeVisible();
+ } else if (action === 'export') {
+ await openBulkMenu(page, { selectAll: true });
+ const [download] = await Promise.all([
+ page.waitForEvent('download'),
+ (async () => {
+ await page.getByTestId('btn-export-subscribers').click();
+ await confirm(page);
+ })(),
+ ]);
+ const csv = await readFile(await download.path(), 'utf8');
+ expect(csv.trim().split('\n')).toHaveLength(23);
+ for (const sub of before) expect(csv.includes(sub.email)).toBe(sub.email.startsWith('target'));
+ } else {
+ await bulkManageLists(page, {
+ action, lists: [(action === 'add' ? targetList : sourceList) === LIST_DEFAULT ? 'Default list' : 'Opt-in list'],
+ preconfirm: action === 'add' && targetList === LIST_OPTIN, selectAll: true,
+ });
+ await page.waitForLoadState('load');
+ }
+
+ const after = await getSubscribers();
+ expect(after.filter((s) => unrelated.some((u) => u.id === s.id))).toEqual(unrelated);
+ const targets = after.filter((s) => s.email.startsWith('target'));
+ expect(targets).toHaveLength(action === 'delete' ? 0 : 22);
+ for (const sub of targets) {
+ if (action === 'blocklist') expect(sub.status).toBe('blocklisted');
+ if (action === 'add') {
+ expect(sub.lists.find((l) => l.id === targetList)?.subscription_status).toBe(targetList === LIST_OPTIN ? 'confirmed' : 'unconfirmed');
+ } else if (action === 'remove') {
+ expect(sub.lists.some((l) => l.id === sourceList)).toBe(false);
+ } else if (action === 'unsubscribe' && scope !== 'orphan') {
+ expect(sub.lists.find((l) => l.id === sourceList)?.subscription_status).toBe('unsubscribed');
+ }
+ }
+ });
+ }
+ }
+});
+
test.describe('Subscriber detail: lists & subscriptions', () => {
const listRow = (page, name) => page.locator('.subscriptions tbody tr').filter({ hasText: name });
@@ -615,3 +703,98 @@ test.describe('Domain blocklist', () => {
expect(res.status()).toBe(400);
});
});
+
+test.describe('Subscribers: bulk scope safeguards', () => {
+ test.beforeEach(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ test('exports the intersection of search and SQL filters', async ({ page }) => {
+ await seedSubscribers(page, 22, { prefix: 'target', status: 'disabled' });
+ await seedSubscribers(page, 1, { prefix: 'target-other', status: 'enabled' });
+ await seedSubscribers(page, 1, { prefix: 'other', status: 'disabled' });
+ await page.goto(`${SUBSCRIBERS}?search=target&query=${encodeURIComponent("subscribers.status = 'disabled'")}`);
+ expect(await readTotal(page)).toBe(22);
+ await openBulkMenu(page, { selectAll: true });
+ const [download] = await Promise.all([
+ page.waitForEvent('download'),
+ (async () => {
+ await page.getByTestId('btn-export-subscribers').click();
+ await confirm(page);
+ })(),
+ ]);
+ const csv = await readFile(await download.path(), 'utf8');
+ expect(csv.trim().split('\n')).toHaveLength(23);
+ expect(csv).not.toContain('[email protected]');
+ });
+
+ test('all does not discard search or SQL filters, and empty list actions are rejected', async ({ page }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const original = (await data(await page.request.get('/api/subscribers'))).results;
+ for (const body of [{ all: true, search: 'no-such-subscriber' }, { all: true, query: 'FALSE' }]) {
+ await data(await page.request.post('/api/subscribers/query/delete', { data: body }));
+ await data(await page.request.put('/api/subscribers/query/blocklist', { data: body }));
+ }
+ for (const action of ['add', 'remove', 'unsubscribe']) {
+ const res = await page.request.put('/api/subscribers/query/lists', { data: { action, target_list_ids: [1] } });
+ expect(res.status()).toBe(400);
+ }
+ expect((await data(await page.request.get('/api/subscribers'))).results).toEqual(original);
+ });
+
+ test('multiple source lists form a union without duplicate exports or omitted subscribers', async ({ page }) => {
+ expect((await page.request.post('/api/subscribers/query/delete', { data: { all: true } })).ok()).toBeTruthy();
+ for (const [prefix, lists] of [['first', [1]], ['second', [2]], ['both', [1, 2]], ['orphan', []]]) {
+ await seedSubscribers(page, 1, { prefix, lists, status: 'disabled' });
+ }
+ const listed = await page.request.get('/api/subscribers?list_id=1&list_id=2');
+ expect(listed.ok()).toBeTruthy();
+ expect((await listed.json()).data.results).toHaveLength(3);
+ const exported = await page.request.get('/api/subscribers/export?list_id=1&list_id=2');
+ expect(exported.ok()).toBeTruthy();
+ expect((await exported.text()).trim().split('\n')).toHaveLength(4);
+ const deleted = await page.request.post('/api/subscribers/query/delete', { data: { all: true, list_ids: [1, 2] } });
+ expect(deleted.ok()).toBeTruthy();
+ const remaining = (await (await page.request.get('/api/subscribers')).json()).data.results;
+ expect(remaining.map((s) => s.email)).toEqual(['[email protected]']);
+ });
+
+ test('inaccessible source lists never fall back to other permitted lists', async ({ page, browser }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const role = await data(await page.request.post('/api/roles/users', { data: {
+ name: 'Subscriber manager', permissions: ['subscribers:get', 'subscribers:manage'],
+ } }));
+ const listRole = await data(await page.request.post('/api/roles/lists', { data: {
+ name: 'Default list manager', lists: [{ id: 1, permissions: ['list:get', 'list:manage'] }],
+ } }));
+ const user = await data(await page.request.post('/api/users', { data: {
+ username: 'bulk-manager', type: 'api', status: 'enabled', user_role_id: role.id, list_role_id: listRole.id,
+ } }));
+ const before = (await data(await page.request.get('/api/subscribers'))).results;
+ const context = await browser.newContext({
+ storageState: { cookies: [], origins: [] },
+ extraHTTPHeaders: { Authorization: `Basic ${Buffer.from(`${user.username}:${user.password}`).toString('base64')}` },
+ });
+ try {
+ await data(await context.request.post('/api/subscribers/query/delete', { data: { all: true, list_ids: [2] } }));
+ await data(await context.request.put('/api/subscribers/query/blocklist', { data: { all: true, list_ids: [2] } }));
+ for (const action of ['add', 'remove', 'unsubscribe']) {
+ await data(await context.request.put('/api/subscribers/query/lists', { data: {
+ all: true, action, list_ids: [2], target_list_ids: [1], status: 'confirmed',
+ } }));
+ }
+ const exported = await context.request.get('/api/subscribers/export?list_id=2');
+ expect(exported.ok()).toBeTruthy();
+ expect((await exported.text()).trim().split('\n')).toHaveLength(1);
+ } finally {
+ await context.close();
+ }
+ expect((await data(await page.request.get('/api/subscribers'))).results).toEqual(before);
+ });
+});
diff --git a/tests/specs/users.spec.js b/tests/specs/users.spec.js
index 6047f490..a0542ea0 100644
--- a/tests/specs/users.spec.js
+++ b/tests/specs/users.spec.js
@@ -303,3 +303,39 @@ test.describe('Users, roles & login', () => {
await context.close();
});
});
+
+test.describe('Users: bulk deletion', () => {
+ test.beforeEach(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ test('deletes only checked users in the filtered view and protects the last admin', async ({ page }) => {
+ const data = async (res) => {
+ expect(res.ok(), await res.text()).toBeTruthy();
+ return (await res.json()).data;
+ };
+ const users = [];
+ for (let i = 0; i < 3; i += 1) {
+ users.push(await data(await page.request.post('/api/users', { data: {
+ username: `bulk-${i}`, type: 'api', status: i < 2 ? 'disabled' : 'enabled', user_role_id: 1,
+ } })));
+ }
+ await page.goto(`${USERS}?status=disabled`);
+ await expect(userRow(page)).toHaveCount(2);
+ await page.locator('thead input[type=checkbox]').check();
+ await userRow(page, users[1].username).locator('input[name=id]').uncheck();
+ await page.getByTestId('btn-bulk-actions').click();
+ await page.getByTestId('btn-delete-users').click();
+ await confirm(page);
+ await expect(userRow(page)).toHaveCount(1);
+ await page.goto(USERS);
+ await expect(userRow(page)).toHaveCount(3);
+ await expect(userRow(page, users[0].username)).toHaveCount(0);
+ await expect(userRow(page, users[1].username)).toBeVisible();
+ await expect(userRow(page, users[2].username)).toBeVisible();
+ const res = await page.request.delete('/api/users?id=1');
+ expect(res.ok()).toBe(false);
+ await page.reload();
+ await expect(userRow(page).filter({ has: page.getByRole('link', { name: 'admin', exact: true }) })).toBeVisible();
+ });
+});