aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2025-06-29 16:25:30 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2025-06-29 16:25:30 -0700
commitaeb920895dc2d624d9c68d1cde3e85f7456bf04c (patch)
tree1c41d79153183ab09347dfe6c073abdf41c577a9 /internal
parent123567e1eb72a001ba15f05003db811024dcf917 (diff)
Fix tests modifying test fixture files by using temporary directories
Previously, running tests would modify test/config/Users/guest.yaml and create test/config/Users/test-user.yaml due to the automatic migration logic in YAMLAccountManager. This caused git diff to show changes after running tests. Solution: - Modified TestNewYAMLAccountManager to use t.TempDir() for test isolation - Added copyTestFiles helper to copy test fixtures to temporary directory - Tests now run against copies, leaving original fixtures untouched This ensures tests are properly isolated and don't have side effects on the repository's test fixture files.
Diffstat (limited to 'internal')
-rw-r--r--internal/mobius/account_manager_test.go131
1 files changed, 55 insertions, 76 deletions
diff --git a/internal/mobius/account_manager_test.go b/internal/mobius/account_manager_test.go
index fe834a4..a6f39ca 100644
--- a/internal/mobius/account_manager_test.go
+++ b/internal/mobius/account_manager_test.go
@@ -1,89 +1,68 @@
package mobius
import (
- "fmt"
+ "os"
+ "path"
"github.com/jhalter/mobius/hotline"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"testing"
)
-func TestNewYAMLAccountManager(t *testing.T) {
- type args struct {
- accountDir string
- }
- tests := []struct {
- name string
- args args
- want *YAMLAccountManager
- wantErr assert.ErrorAssertionFunc
- }{
- {
- name: "loads accounts from a directory",
- args: args{
- accountDir: "test/config/Users",
- },
- want: &YAMLAccountManager{
- accountDir: "test/config/Users",
- accounts: map[string]hotline.Account{
- "admin": {
- Name: "admin",
- Login: "admin",
- Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a",
- Access: hotline.AccessBitmap{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00},
- },
- "Test User Name": {
- Name: "test-user",
- Login: "Test User Name",
- Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS",
- Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00},
- },
- "guest": {
- Name: "guest",
- Login: "guest",
- Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y",
- Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00},
- },
- },
- },
- wantErr: assert.NoError,
- },
+// copyTestFiles copies test config files to a temporary directory
+func copyTestFiles(t *testing.T, srcDir, dstDir string) {
+ files := []string{"admin.yaml", "guest.yaml", "user-with-old-access-format.yaml"}
+
+ for _, file := range files {
+ srcPath := path.Join(srcDir, file)
+ dstPath := path.Join(dstDir, file)
+
+ data, err := os.ReadFile(srcPath)
+ require.NoError(t, err, "Failed to read %s", srcPath)
+
+ err = os.WriteFile(dstPath, data, 0644)
+ require.NoError(t, err, "Failed to write %s", dstPath)
}
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := NewYAMLAccountManager(tt.args.accountDir)
- if !tt.wantErr(t, err, fmt.Sprintf("NewYAMLAccountManager(%v)", tt.args.accountDir)) {
- return
- }
+}
- assert.Equal(t,
- &hotline.Account{
- Name: "admin",
- Login: "admin",
- Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a",
- Access: hotline.AccessBitmap{0xff, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00},
- },
- got.Get("admin"),
- )
+func TestNewYAMLAccountManager(t *testing.T) {
+ t.Run("loads accounts from a directory", func(t *testing.T) {
+ // Create temporary directory and copy test files
+ tempDir := t.TempDir()
+ copyTestFiles(t, "test/config/Users", tempDir)
+
+ // Use temp directory instead of original test directory
+ got, err := NewYAMLAccountManager(tempDir)
+ require.NoError(t, err)
- assert.Equal(t,
- &hotline.Account{
- Login: "guest",
- Name: "guest",
- Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y",
- Access: hotline.AccessBitmap{0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00},
- },
- got.Get("guest"),
- )
+ assert.Equal(t,
+ &hotline.Account{
+ Name: "admin",
+ Login: "admin",
+ Password: "$2a$04$2itGEYx8C1N5bsfRSoC9JuonS3I4YfnyVPZHLSwp7kEInRX0yoB.a",
+ Access: hotline.AccessBitmap{0xff, 0xff, 0xef, 0xff, 0xff, 0x80, 0x00, 0x00},
+ },
+ got.Get("admin"),
+ )
- assert.Equal(t,
- &hotline.Account{
- Login: "test-user",
- Name: "Test User Name",
- Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS",
- Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00},
- },
- got.Get("test-user"),
- )
- })
- }
+ assert.Equal(t,
+ &hotline.Account{
+ Login: "guest",
+ Name: "guest",
+ Password: "$2a$04$6Yq/TIlgjSD.FbARwtYs9ODnkHawonu1TJ5W2jJKfhnHwBIQTk./y",
+ Access: hotline.AccessBitmap{0x60, 0x70, 0x0c, 0x20, 0x03, 0x80, 0x00, 0x00},
+ },
+ got.Get("guest"),
+ )
+
+ assert.Equal(t,
+ &hotline.Account{
+ Login: "test-user",
+ Name: "Test User Name",
+ Password: "$2a$04$9P/jgLn1fR9TjSoWL.rKxuN6g.1TSpf2o6Hw.aaRuBwrWIJNwsKkS",
+ Access: hotline.AccessBitmap{0x7d, 0xf0, 0x0c, 0xef, 0xab, 0x80, 0x00, 0x00},
+ },
+ got.Get("test-user"),
+ )
+ })
}