#8: implement ManageSieve protocol server (RFC 5804)
This commit is contained in:
1 parent
5a7dcc002a
commit
0e6fc27bc1
13 files changed
+1634
No files matched your search
@@ -0,0 +1,120 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"lambdamail/protocols/internal/application/port"
|
||||
)
|
||||
|
||||
// SieveRepository implements port.SieveRepository against PostgreSQL sieve_scripts table.
|
||||
type SieveRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewSieveRepository(pool *pgxpool.Pool) *SieveRepository {
|
||||
return &SieveRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *SieveRepository) GetScript(ctx context.Context, mailboxID string, name string) (*port.SieveScriptRecord, error) {
|
||||
var rec port.SieveScriptRecord
|
||||
err := r.pool.QueryRow(ctx, `
|
||||
SELECT id, mailbox_id, name, script, is_active
|
||||
FROM sieve_scripts
|
||||
WHERE mailbox_id = $1 AND name = $2
|
||||
`, mailboxID, name).Scan(&rec.ID, &rec.MailboxID, &rec.Name, &rec.Script, &rec.IsActive)
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, fmt.Errorf("get sieve script %s: %w", name, err)
|
||||
}
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
func (r *SieveRepository) PutScript(ctx context.Context, mailboxID string, name string, script string) error {
|
||||
_, err := r.pool.Exec(ctx, `
|
||||
INSERT INTO sieve_scripts (mailbox_id, name, script, is_active)
|
||||
VALUES ($1, $2, $3, false)
|
||||
ON CONFLICT (mailbox_id, name) DO UPDATE SET script = EXCLUDED.script
|
||||
`, mailboxID, name, script)
|
||||
if err != nil {
|
||||
return fmt.Errorf("put sieve script %s: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SieveRepository) ListScripts(ctx context.Context, mailboxID string) ([]port.SieveScriptRecord, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, mailbox_id, name, script, is_active
|
||||
FROM sieve_scripts
|
||||
WHERE mailbox_id = $1
|
||||
ORDER BY name ASC
|
||||
`, mailboxID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list sieve scripts: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []port.SieveScriptRecord
|
||||
for rows.Next() {
|
||||
var rec port.SieveScriptRecord
|
||||
if err := rows.Scan(&rec.ID, &rec.MailboxID, &rec.Name, &rec.Script, &rec.IsActive); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, rec)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (r *SieveRepository) SetActiveScript(ctx context.Context, mailboxID string, name string) error {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
if _, err := tx.Exec(ctx, `
|
||||
UPDATE sieve_scripts SET is_active = false WHERE mailbox_id = $1
|
||||
`, mailboxID); err != nil {
|
||||
return fmt.Errorf("deactivate sieve scripts: %w", err)
|
||||
}
|
||||
|
||||
if name != "" {
|
||||
tag, err := tx.Exec(ctx, `
|
||||
UPDATE sieve_scripts SET is_active = true WHERE mailbox_id = $1 AND name = $2
|
||||
`, mailboxID, name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("activate sieve script %s: %w", name, err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return fmt.Errorf("script %s not found", name)
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
func (r *SieveRepository) DeleteScript(ctx context.Context, mailboxID string, name string) error {
|
||||
_, err := r.pool.Exec(ctx, `
|
||||
DELETE FROM sieve_scripts WHERE mailbox_id = $1 AND name = $2 AND is_active = false
|
||||
`, mailboxID, name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete sieve script %s: %w", name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SieveRepository) RenameScript(ctx context.Context, mailboxID string, oldName string, newName string) error {
|
||||
_, err := r.pool.Exec(ctx, `
|
||||
UPDATE sieve_scripts SET name = $3 WHERE mailbox_id = $1 AND name = $2
|
||||
`, mailboxID, oldName, newName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rename sieve script %s to %s: %w", oldName, newName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestSieveRepository_CRUD(t *testing.T) {
|
||||
dbURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL not set - skipping Postgres integration test")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := NewPool(ctx, dbURL)
|
||||
if err != nil {
|
||||
t.Fatalf("connect pool: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
domainID := uuid.New()
|
||||
mailboxID := uuid.New()
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO domains (id, name, punycode_name) VALUES ($1, 'sieve.test', 'sieve.test')`, domainID); err != nil {
|
||||
t.Fatalf("seed domain: %v", err)
|
||||
}
|
||||
defer pool.Exec(ctx, `DELETE FROM domains WHERE id = $1`, domainID)
|
||||
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO mailboxes (id, domain_id, local_part, email_address, password_hash) VALUES ($1, $2, 'sieve', 'sieve@sieve.test', 'hash')`, mailboxID, domainID); err != nil {
|
||||
t.Fatalf("seed mailbox: %v", err)
|
||||
}
|
||||
|
||||
repo := NewSieveRepository(pool)
|
||||
|
||||
// 1. PutScript
|
||||
if err := repo.PutScript(ctx, mailboxID.String(), "script1", "keep;"); err != nil {
|
||||
t.Fatalf("PutScript: %v", err)
|
||||
}
|
||||
|
||||
// 2. GetScript
|
||||
rec, err := repo.GetScript(ctx, mailboxID.String(), "script1")
|
||||
if err != nil || rec == nil || rec.Script != "keep;" {
|
||||
t.Fatalf("GetScript: rec=%+v, err=%v", rec, err)
|
||||
}
|
||||
|
||||
// 3. SetActive
|
||||
if err := repo.SetActiveScript(ctx, mailboxID.String(), "script1"); err != nil {
|
||||
t.Fatalf("SetActiveScript: %v", err)
|
||||
}
|
||||
rec, _ = repo.GetScript(ctx, mailboxID.String(), "script1")
|
||||
if !rec.IsActive {
|
||||
t.Fatal("expected is_active = true")
|
||||
}
|
||||
|
||||
// 4. RenameScript
|
||||
if err := repo.RenameScript(ctx, mailboxID.String(), "script1", "script1_renamed"); err != nil {
|
||||
t.Fatalf("RenameScript: %v", err)
|
||||
}
|
||||
|
||||
// 5. Deactivate and Delete
|
||||
if err := repo.SetActiveScript(ctx, mailboxID.String(), ""); err != nil {
|
||||
t.Fatalf("SetActiveScript empty: %v", err)
|
||||
}
|
||||
if err := repo.DeleteScript(ctx, mailboxID.String(), "script1_renamed"); err != nil {
|
||||
t.Fatalf("DeleteScript: %v", err)
|
||||
}
|
||||
|
||||
// 6. Confirm deleted
|
||||
rec, _ = repo.GetScript(ctx, mailboxID.String(), "script1_renamed")
|
||||
if rec != nil {
|
||||
t.Fatal("expected script to be deleted, got non-nil record")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user