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
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))
if toDelete > 0 && toDelete <= backups.count {

View File

@@ -492,6 +492,7 @@ func (emailer *Emailer) constructDeleted(username, reason string, placeholders b
reason = "{reason}"
}
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"),
"reasonString": emailer.lang.Strings.get("reason"),
"reason": reason,
@@ -506,6 +507,7 @@ func (emailer *Emailer) constructDisabled(username, reason string, placeholders
reason = "{reason}"
}
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"),
"reasonString": emailer.lang.Strings.get("reason"),
"reason": reason,
@@ -520,6 +522,7 @@ func (emailer *Emailer) constructEnabled(username, reason string, placeholders b
reason = "{reason}"
}
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"),
"reasonString": emailer.lang.Strings.get("reason"),
"reason": reason,

View File

@@ -1,8 +1,6 @@
package main
import (
"embed"
"errors"
"fmt"
"io/fs"
"log"
@@ -18,8 +16,6 @@ import (
"github.com/timshannon/badgerhold/v4"
)
//go:embed build/data/config-default.ini
var configFS embed.FS
var db *badgerhold.Store
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.
func NewTestEmailer() (*Emailer, error) {
if binaryType != "external" {
return nil, errors.New("test only supported with -tags \"external\"")
}
emailer := &Emailer{
fromAddr: "from@addr",
fromName: "fromName",
@@ -47,20 +40,16 @@ func NewTestEmailer() (*Emailer, error) {
},
sender: &DummyClient{},
}
dConfig, err := fs.ReadFile(configFS, "build/data/config-default.ini")
if err != nil {
return emailer, err
}
wd, err := os.Getwd()
// Assume our working directory is the root of the repo
wd, _ := os.Getwd()
loadFilesystems(filepath.Join(wd, "build"), logger.NewEmptyLogger())
dConfig, err := fs.ReadFile(localFS, "config-default.ini")
if err != nil {
return emailer, err
}
// Force emailer to construct markdown
discordEnabled = true
// Use working directory
localFS = dirFS(filepath.Join(wd, "build", "data"))
langFS = dirFS(filepath.Join(wd, "build", "data", "lang"))
noInfoLS := emailer.LoggerSet
noInfoLS.info = logger.NewEmptyLogger()
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) {
reason := shortuuid.New()
msg, err := e.constructDeleted(reason, false)
username := shortuuid.New()
msg, err := e.constructDeleted(username, reason, false)
if err != nil {
t.Fatalf("failed construct: %+v", err)
}
for _, content := range []string{msg.Text, msg.HTML} {
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) {
reason := shortuuid.New()
msg, err := e.constructDisabled(reason, false)
username := shortuuid.New()
msg, err := e.constructDisabled(username, reason, false)
if err != nil {
t.Fatalf("failed construct: %+v", err)
}
@@ -356,6 +350,9 @@ func TestDisabled(t *testing.T) {
if !strings.Contains(content, reason) {
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) {
reason := shortuuid.New()
msg, err := e.constructEnabled(reason, false)
username := shortuuid.New()
msg, err := e.constructEnabled(username, reason, false)
if err != nil {
t.Fatalf("failed construct: %+v", err)
}
@@ -377,6 +375,9 @@ func TestEnabled(t *testing.T) {
if !strings.Contains(content, reason) {
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
import (
"log"
"os"
"path/filepath"
"strings"
"github.com/hrfee/jfa-go/logger"
)
const binaryType = "external"
@@ -28,9 +29,12 @@ func FSJoin(elem ...string) string {
return strings.TrimSuffix(path, sep)
}
func loadFilesystems() {
log.Println("Using external storage")
executable, _ := os.Executable()
localFS = dirFS(filepath.Join(filepath.Dir(executable), "data"))
langFS = dirFS(filepath.Join(filepath.Dir(executable), "data", "lang"))
func loadFilesystems(rootDir string, logger *logger.Logger) {
logger.Println("Using external storage")
if rootDir == "" {
executable, _ := os.Executable()
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 (
"embed"
"io/fs"
"log"
"github.com/hrfee/jfa-go/logger"
)
const binaryType = "internal"
@@ -35,8 +36,8 @@ func FSJoin(elem ...string) string {
return out[:len(out)-1]
}
func loadFilesystems() {
func loadFilesystems(rootDir string, logger *logger.Logger) {
langFS = rewriteFS{laFS, "lang/"}
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-column>
<mj-text mj-class="text" font-size="16px" font-family="Noto Sans, Helvetica, Arial, sans-serif">
<p>{{ .helloUser }}</p>
<h3>{{ .yourAccountWas }}</h3>
<p>{{ .reasonString }}: <i>{{ .reason }}</i></p>
</mj-text>

View File

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

View File

@@ -784,7 +784,8 @@ func main() {
if flagPassed("test") {
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)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)