Files
Docwell/Server/internal/mail/smtp_test.go
T
tajniak81andClaude Opus 5.5 b9be8be156
CI / go (Server) (push) Failing after 22m50s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 8m47s
CI / desktop (ubuntu-latest) (push) Successful in 2h56m50s
CI / mobile (push) Failing after 5m33s
CI / docker (push) Successful in 1h43m8s
Docs / build (push) Successful in 16m2s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Run docwell-db as a stock PocketBase and let the server set it up
docwell-db no longer needs a Docwell image: the stack runs
tajniak81/pocketbase-docker-root as it is, and docwell-server does what
the migrations and pb_hooks used to do inside PocketBase.

On start, docwell-server now:
- waits for PocketBase and imports Docwell's collections from an
  embedded snapshot (internal/pbschema). Existing collections and
  fields are matched by name, so their data is kept; the admin-managed
  users options (OAuth2 providers, magic link) are left untouched.
- writes the DOCWELL_APP_NAME, DOCWELL_WEB_URL, DOCWELL_MAIL_FROM,
  DOCWELL_SMTP_* and DOCWELL_S3_* settings into PocketBase, as the
  boot hook did.

It also replaces the hook's two routes: token checks use PocketBase's
auth-refresh (the organization name is read as the superuser), and
Docwell's own emails go out over SMTP from the server. PocketBase never
returns the SMTP password, so the server keeps a copy in the
superuser-only instance_settings collection; a password saved before
this change has to be entered once more in the admin panel.

Database/ keeps pb_migrations as the source of the schema;
export-schema.sh regenerates the snapshot from them. Database/Dockerfile
and pb_hooks are removed, the release no longer builds docwell-db, the
AIO image runs PocketBase without migrations or hooks, and the compose
files default DB_IMAGE to the stock image and pass the settings to the
server instead of the database.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 20:09:38 +01:00

114 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: AGPL-3.0-or-later
package mail
import (
"bufio"
"context"
"errors"
"net"
"strconv"
"strings"
"testing"
)
// fakeSMTP accepts one message without TLS or auth and returns what it got.
func fakeSMTP(t *testing.T) (host string, port int, got chan string) {
t.Helper()
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { ln.Close() })
got = make(chan string, 1)
go func() {
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
r := bufio.NewReader(conn)
say := func(s string) { _, _ = conn.Write([]byte(s + "\r\n")) }
say("220 fake ESMTP")
var log strings.Builder
for {
line, err := r.ReadString('\n')
if err != nil {
return
}
cmd := strings.ToUpper(strings.TrimSpace(line))
log.WriteString(line)
switch {
case strings.HasPrefix(cmd, "EHLO"), strings.HasPrefix(cmd, "HELO"):
say("250 fake")
case strings.HasPrefix(cmd, "DATA"):
say("354 go ahead")
for {
l, err := r.ReadString('\n')
if err != nil {
return
}
if l == ".\r\n" {
break
}
log.WriteString(l)
}
say("250 queued")
case strings.HasPrefix(cmd, "QUIT"):
say("221 bye")
got <- log.String()
return
default:
say("250 ok")
}
}
}()
h, p, _ := net.SplitHostPort(ln.Addr().String())
n, _ := strconv.Atoi(p)
return h, n, got
}
func TestSMTPSender(t *testing.T) {
host, port, got := fakeSMTP(t)
s := &SMTPSender{Config: func(context.Context) (Config, error) {
return Config{Enabled: true, Host: host, Port: port, SenderName: "Docwell", SenderAddress: "no-reply@example.com"}, nil
}}
err := s.Send(context.Background(), Message{To: "ada@example.com", Subject: "Zaproszenie – Docwell", Text: "hello", HTML: "<p>hello</p>"})
if err != nil {
t.Fatal(err)
}
session := <-got
for _, want := range []string{
"MAIL FROM:<no-reply@example.com>",
"RCPT TO:<ada@example.com>",
`From: "Docwell" <no-reply@example.com>`,
"Subject: =?utf-8?q?",
"Content-Type: text/plain; charset=utf-8",
"Content-Type: text/html; charset=utf-8",
} {
if !strings.Contains(session, want) {
t.Errorf("session is missing %q:\n%s", want, session)
}
}
}
func TestSMTPSenderNotConfigured(t *testing.T) {
s := &SMTPSender{Config: func(context.Context) (Config, error) { return Config{Host: "smtp.example.com"}, nil }}
if err := s.Send(context.Background(), Message{To: "a@example.com"}); !errors.Is(err, ErrNotConfigured) {
t.Fatalf("err = %v, want ErrNotConfigured", err)
}
}
func TestSMTPSenderRefused(t *testing.T) {
ln, _ := net.Listen("tcp", "127.0.0.1:0")
addr := ln.Addr().(*net.TCPAddr)
ln.Close() // nothing listens there now
s := &SMTPSender{Config: func(context.Context) (Config, error) {
return Config{Enabled: true, Host: "127.0.0.1", Port: addr.Port, SenderAddress: "a@example.com"}, nil
}}
var de *DeliveryError
if err := s.Send(context.Background(), Message{To: "b@example.com", Text: "x"}); !errors.As(err, &de) {
t.Fatalf("err = %v, want a DeliveryError", err)
}
}