email_test: allow running with INTERNAL=on

This commit is contained in:
Harvey Tindall
2025-08-31 17:39:08 +01:00
parent febbe27a0d
commit 87c0f54a8d
8 changed files with 43 additions and 30 deletions

View File

@@ -214,7 +214,6 @@ func (app *appContext) makeBackup() (fileDetails CreateBackupDTO) {
count += 1 count += 1
backupsByCommit[b.Commit] = count backupsByCommit[b.Commit] = count
} }
fmt.Printf("remaining:%+v\n", backupsByCommit)
} }
// fmt.Printf("toDelete: %d, backCount: %d, keep: %d, length: %d\n", toDelete, backups.count, toKeep, len(backups.files)) // fmt.Printf("toDelete: %d, backCount: %d, keep: %d, length: %d\n", toDelete, backups.count, toKeep, len(backups.files))
if toDelete > 0 && toDelete <= backups.count { if toDelete > 0 && toDelete <= backups.count {

View File

@@ -492,6 +492,7 @@ func (emailer *Emailer) constructDeleted(username, reason string, placeholders b
reason = "{reason}" reason = "{reason}"
} }
contentInfo, template := emailer.baseValues("UserDeleted", username, placeholders, map[string]any{ contentInfo, template := emailer.baseValues("UserDeleted", username, placeholders, map[string]any{
"helloUser": emailer.lang.Strings.template("helloUser", tmpl{"username": username}),
"yourAccountWas": emailer.lang.UserDeleted.get("yourAccountWasDeleted"), "yourAccountWas": emailer.lang.UserDeleted.get("yourAccountWasDeleted"),
"reasonString": emailer.lang.Strings.get("reason"), "reasonString": emailer.lang.Strings.get("reason"),
"reason": reason, "reason": reason,
@@ -506,6 +507,7 @@ func (emailer *Emailer) constructDisabled(username, reason string, placeholders
reason = "{reason}" reason = "{reason}"
} }
contentInfo, template := emailer.baseValues("UserDisabled", username, placeholders, map[string]any{ contentInfo, template := emailer.baseValues("UserDisabled", username, placeholders, map[string]any{
"helloUser": emailer.lang.Strings.template("helloUser", tmpl{"username": username}),
"yourAccountWas": emailer.lang.UserDisabled.get("yourAccountWasDisabled"), "yourAccountWas": emailer.lang.UserDisabled.get("yourAccountWasDisabled"),
"reasonString": emailer.lang.Strings.get("reason"), "reasonString": emailer.lang.Strings.get("reason"),
"reason": reason, "reason": reason,
@@ -520,6 +522,7 @@ func (emailer *Emailer) constructEnabled(username, reason string, placeholders b
reason = "{reason}" reason = "{reason}"
} }
contentInfo, template := emailer.baseValues("UserEnabled", username, placeholders, map[string]any{ contentInfo, template := emailer.baseValues("UserEnabled", username, placeholders, map[string]any{
"helloUser": emailer.lang.Strings.template("helloUser", tmpl{"username": username}),
"yourAccountWas": emailer.lang.UserEnabled.get("yourAccountWasEnabled"), "yourAccountWas": emailer.lang.UserEnabled.get("yourAccountWasEnabled"),
"reasonString": emailer.lang.Strings.get("reason"), "reasonString": emailer.lang.Strings.get("reason"),
"reason": reason, "reason": reason,

View File

@@ -1,8 +1,6 @@
package main package main
import ( import (
"embed"
"errors"
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
@@ -18,8 +16,6 @@ import (
"github.com/timshannon/badgerhold/v4" "github.com/timshannon/badgerhold/v4"
) )
//go:embed build/data/config-default.ini
var configFS embed.FS
var db *badgerhold.Store var db *badgerhold.Store
func dbClose(e *Emailer) { func dbClose(e *Emailer) {
@@ -34,9 +30,6 @@ func Fatal(err any) {
// NewTestEmailer initialises most of what the emailer depends on, which happens to be most of the app. // NewTestEmailer initialises most of what the emailer depends on, which happens to be most of the app.
func NewTestEmailer() (*Emailer, error) { func NewTestEmailer() (*Emailer, error) {
if binaryType != "external" {
return nil, errors.New("test only supported with -tags \"external\"")
}
emailer := &Emailer{ emailer := &Emailer{
fromAddr: "from@addr", fromAddr: "from@addr",
fromName: "fromName", fromName: "fromName",
@@ -47,20 +40,16 @@ func NewTestEmailer() (*Emailer, error) {
}, },
sender: &DummyClient{}, sender: &DummyClient{},
} }
dConfig, err := fs.ReadFile(configFS, "build/data/config-default.ini") // Assume our working directory is the root of the repo
if err != nil { wd, _ := os.Getwd()
return emailer, err loadFilesystems(filepath.Join(wd, "build"), logger.NewEmptyLogger())
} dConfig, err := fs.ReadFile(localFS, "config-default.ini")
wd, err := os.Getwd()
if err != nil { if err != nil {
return emailer, err return emailer, err
} }
// Force emailer to construct markdown // Force emailer to construct markdown
discordEnabled = true discordEnabled = true
// Use working directory
localFS = dirFS(filepath.Join(wd, "build", "data"))
langFS = dirFS(filepath.Join(wd, "build", "data", "lang"))
noInfoLS := emailer.LoggerSet noInfoLS := emailer.LoggerSet
noInfoLS.info = logger.NewEmptyLogger() noInfoLS.info = logger.NewEmptyLogger()
emailer.config, err = NewConfig(dConfig, "/tmp/jfa-go-test", noInfoLS) emailer.config, err = NewConfig(dConfig, "/tmp/jfa-go-test", noInfoLS)
@@ -327,13 +316,17 @@ func TestDeleted(t *testing.T) {
} }
testContent(e, customContent["UserDeleted"], t, func(t *testing.T) { testContent(e, customContent["UserDeleted"], t, func(t *testing.T) {
reason := shortuuid.New() reason := shortuuid.New()
msg, err := e.constructDeleted(reason, false) username := shortuuid.New()
msg, err := e.constructDeleted(username, reason, false)
if err != nil { if err != nil {
t.Fatalf("failed construct: %+v", err) t.Fatalf("failed construct: %+v", err)
} }
for _, content := range []string{msg.Text, msg.HTML} { for _, content := range []string{msg.Text, msg.HTML} {
if !strings.Contains(content, reason) { if !strings.Contains(content, reason) {
t.Fatalf("reason n)ot found in output: %s", content) t.Fatalf("reason not found in output: %s", content)
}
if !strings.Contains(content, username) {
t.Fatalf("username not found in output: %s", content)
} }
} }
}) })
@@ -348,7 +341,8 @@ func TestDisabled(t *testing.T) {
} }
testContent(e, customContent["UserDeleted"], t, func(t *testing.T) { testContent(e, customContent["UserDeleted"], t, func(t *testing.T) {
reason := shortuuid.New() reason := shortuuid.New()
msg, err := e.constructDisabled(reason, false) username := shortuuid.New()
msg, err := e.constructDisabled(username, reason, false)
if err != nil { if err != nil {
t.Fatalf("failed construct: %+v", err) t.Fatalf("failed construct: %+v", err)
} }
@@ -356,6 +350,9 @@ func TestDisabled(t *testing.T) {
if !strings.Contains(content, reason) { if !strings.Contains(content, reason) {
t.Fatalf("reason not found in output: %s", content) t.Fatalf("reason not found in output: %s", content)
} }
if !strings.Contains(content, username) {
t.Fatalf("username not found in output: %s", content)
}
} }
}) })
} }
@@ -369,7 +366,8 @@ func TestEnabled(t *testing.T) {
} }
testContent(e, customContent["UserDeleted"], t, func(t *testing.T) { testContent(e, customContent["UserDeleted"], t, func(t *testing.T) {
reason := shortuuid.New() reason := shortuuid.New()
msg, err := e.constructEnabled(reason, false) username := shortuuid.New()
msg, err := e.constructEnabled(username, reason, false)
if err != nil { if err != nil {
t.Fatalf("failed construct: %+v", err) t.Fatalf("failed construct: %+v", err)
} }
@@ -377,6 +375,9 @@ func TestEnabled(t *testing.T) {
if !strings.Contains(content, reason) { if !strings.Contains(content, reason) {
t.Fatalf("reason not found in output: %s", content) t.Fatalf("reason not found in output: %s", content)
} }
if !strings.Contains(content, username) {
t.Fatalf("username not found in output: %s", content)
}
} }
}) })
} }

View File

@@ -4,10 +4,11 @@
package main package main
import ( import (
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/hrfee/jfa-go/logger"
) )
const binaryType = "external" const binaryType = "external"
@@ -28,9 +29,12 @@ func FSJoin(elem ...string) string {
return strings.TrimSuffix(path, sep) return strings.TrimSuffix(path, sep)
} }
func loadFilesystems() { func loadFilesystems(rootDir string, logger *logger.Logger) {
log.Println("Using external storage") logger.Println("Using external storage")
executable, _ := os.Executable() if rootDir == "" {
localFS = dirFS(filepath.Join(filepath.Dir(executable), "data")) executable, _ := os.Executable()
langFS = dirFS(filepath.Join(filepath.Dir(executable), "data", "lang")) rootDir = filepath.Dir(executable)
}
localFS = dirFS(filepath.Join(rootDir, "data"))
langFS = dirFS(filepath.Join(rootDir, "data", "lang"))
} }

View File

@@ -6,7 +6,8 @@ package main
import ( import (
"embed" "embed"
"io/fs" "io/fs"
"log"
"github.com/hrfee/jfa-go/logger"
) )
const binaryType = "internal" const binaryType = "internal"
@@ -35,8 +36,8 @@ func FSJoin(elem ...string) string {
return out[:len(out)-1] return out[:len(out)-1]
} }
func loadFilesystems() { func loadFilesystems(rootDir string, logger *logger.Logger) {
langFS = rewriteFS{laFS, "lang/"} langFS = rewriteFS{laFS, "lang/"}
localFS = rewriteFS{loFS, "data/"} localFS = rewriteFS{loFS, "data/"}
log.Println("Using internal storage") logger.Println("Using internal storage")
} }

View File

@@ -60,6 +60,8 @@
<mj-section mj-class="bg"> <mj-section mj-class="bg">
<mj-column> <mj-column>
<mj-text mj-class="text" font-size="16px" font-family="Noto Sans, Helvetica, Arial, sans-serif"> <mj-text mj-class="text" font-size="16px" font-family="Noto Sans, Helvetica, Arial, sans-serif">
<p>{{ .helloUser }}</p>
<h3>{{ .yourAccountWas }}</h3> <h3>{{ .yourAccountWas }}</h3>
<p>{{ .reasonString }}: <i>{{ .reason }}</i></p> <p>{{ .reasonString }}: <i>{{ .reason }}</i></p>
</mj-text> </mj-text>

View File

@@ -1,3 +1,5 @@
{{ .helloUser }}
{{ .yourAccountWas }} {{ .yourAccountWas }}
{{ .reasonString }}: {{ .reason }} {{ .reasonString }}: {{ .reason }}

View File

@@ -784,7 +784,8 @@ func main() {
if flagPassed("test") { if flagPassed("test") {
TEST = true TEST = true
} }
loadFilesystems() executable, _ := os.Executable()
loadFilesystems(filepath.Dir(executable), logger.NewLogger(os.Stdout, "[INFO] ", log.Ltime, color.FgHiWhite))
quit := make(chan os.Signal, 0) quit := make(chan os.Signal, 0)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM) signal.Notify(quit, os.Interrupt, syscall.SIGTERM)