|
| 1 | +package account |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestSaveAndLoad(t *testing.T) { |
| 10 | + dir := t.TempDir() |
| 11 | + path := filepath.Join(dir, "account.json") |
| 12 | + |
| 13 | + acct := &Account{Email: "user@example.com"} |
| 14 | + if err := Save(path, acct); err != nil { |
| 15 | + t.Fatalf("Save: %v", err) |
| 16 | + } |
| 17 | + |
| 18 | + loaded, err := Load(path) |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Load: %v", err) |
| 21 | + } |
| 22 | + if loaded == nil { |
| 23 | + t.Fatal("Load returned nil") |
| 24 | + } |
| 25 | + if loaded.Email != "user@example.com" { |
| 26 | + t.Errorf("Email = %q, want %q", loaded.Email, "user@example.com") |
| 27 | + } |
| 28 | + |
| 29 | + // Check file permissions |
| 30 | + info, err := os.Stat(path) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("Stat: %v", err) |
| 33 | + } |
| 34 | + if perm := info.Mode().Perm(); perm != 0600 { |
| 35 | + t.Errorf("permissions = %o, want 0600", perm) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestLoadNotFound(t *testing.T) { |
| 40 | + dir := t.TempDir() |
| 41 | + path := filepath.Join(dir, "nonexistent.json") |
| 42 | + |
| 43 | + acct, err := Load(path) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("Load: %v", err) |
| 46 | + } |
| 47 | + if acct != nil { |
| 48 | + t.Errorf("expected nil account for nonexistent file, got %+v", acct) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestSaveOverwrite(t *testing.T) { |
| 53 | + dir := t.TempDir() |
| 54 | + path := filepath.Join(dir, "account.json") |
| 55 | + |
| 56 | + if err := Save(path, &Account{Email: "old@example.com"}); err != nil { |
| 57 | + t.Fatalf("Save: %v", err) |
| 58 | + } |
| 59 | + if err := Save(path, &Account{Email: "new@example.com"}); err != nil { |
| 60 | + t.Fatalf("Save overwrite: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + loaded, err := Load(path) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("Load: %v", err) |
| 66 | + } |
| 67 | + if loaded.Email != "new@example.com" { |
| 68 | + t.Errorf("Email = %q, want %q", loaded.Email, "new@example.com") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestPathFromIdentity(t *testing.T) { |
| 73 | + cases := []struct { |
| 74 | + input string |
| 75 | + want string |
| 76 | + }{ |
| 77 | + {"/var/lib/pilot/identity.json", "/var/lib/pilot/account.json"}, |
| 78 | + {"/home/user/.pilot/identity.json", "/home/user/.pilot/account.json"}, |
| 79 | + {"identity.json", "account.json"}, |
| 80 | + } |
| 81 | + for _, tc := range cases { |
| 82 | + got := PathFromIdentity(tc.input) |
| 83 | + if got != tc.want { |
| 84 | + t.Errorf("PathFromIdentity(%q) = %q, want %q", tc.input, got, tc.want) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestSaveCreatesDirectory(t *testing.T) { |
| 90 | + dir := t.TempDir() |
| 91 | + path := filepath.Join(dir, "subdir", "account.json") |
| 92 | + |
| 93 | + if err := Save(path, &Account{Email: "user@example.com"}); err != nil { |
| 94 | + t.Fatalf("Save with nested dir: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + loaded, err := Load(path) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Load: %v", err) |
| 100 | + } |
| 101 | + if loaded.Email != "user@example.com" { |
| 102 | + t.Errorf("Email = %q, want %q", loaded.Email, "user@example.com") |
| 103 | + } |
| 104 | +} |
0 commit comments