Files
keepassgo/vault/history_test.go
T
2026-03-29 11:04:38 -07:00

161 lines
4.1 KiB
Go

package vault
import "testing"
func TestUpsertEntryPreservesPreviousVersionInHistory(t *testing.T) {
t.Parallel()
model := Model{
Entries: []Entry{
{
ID: "vault-console",
Title: "Vault Console",
Username: "dannyocean",
Password: "old-token",
URL: "https://vault.crew.example.invalid",
Notes: "Original note",
Path: []string{"Root", "Internet"},
},
},
}
model.UpsertEntry(Entry{
ID: "vault-console",
Title: "Vault Console",
Username: "dannyocean",
Password: "new-token",
URL: "https://vault.crew.example.invalid",
Notes: "Updated note",
Path: []string{"Root", "Internet"},
})
got := model.EntriesInPath([]string{"Root", "Internet"})
if len(got) != 1 {
t.Fatalf("len(EntriesInPath()) = %d, want 1", len(got))
}
if got[0].Password != "new-token" {
t.Fatalf("Entry.Password = %q, want %q", got[0].Password, "new-token")
}
if len(got[0].History) != 1 {
t.Fatalf("len(Entry.History) = %d, want 1", len(got[0].History))
}
if got[0].History[0].Password != "old-token" || got[0].History[0].Notes != "Original note" {
t.Fatalf("Entry.History[0] = %#v, want prior entry version", got[0].History[0])
}
}
func TestDeleteEntryMovesItToRecycleBin(t *testing.T) {
t.Parallel()
model := Model{
Entries: []Entry{
{
ID: "surveillance-console",
Title: "Surveillance Console",
Username: "codex",
Password: "token-2",
URL: "https://surveillance.crew.example.invalid",
Path: []string{"Root", "Home Assistant"},
},
},
}
if err := model.DeleteEntry("surveillance-console"); err != nil {
t.Fatalf("DeleteEntry() error = %v", err)
}
if got := model.EntriesInPath([]string{"Root", "Home Assistant"}); len(got) != 0 {
t.Fatalf("EntriesInPath() = %#v, want empty after delete", got)
}
if len(model.RecycleBin) != 1 {
t.Fatalf("len(RecycleBin) = %d, want 1", len(model.RecycleBin))
}
if model.RecycleBin[0].Title != "Surveillance Console" {
t.Fatalf("RecycleBin[0].Title = %q, want %q", model.RecycleBin[0].Title, "Surveillance Console")
}
}
func TestRestoreEntryMovesItBackFromRecycleBin(t *testing.T) {
t.Parallel()
model := Model{
RecycleBin: []Entry{
{
ID: "bellagio",
Title: "Bellagio",
Username: "rustyryan",
Password: "token-3",
URL: "https://bellagio.example.invalid",
Path: []string{"Root", "Internet"},
},
},
}
if err := model.RestoreEntry("bellagio"); err != nil {
t.Fatalf("RestoreEntry() error = %v", err)
}
got := model.EntriesInPath([]string{"Root", "Internet"})
if len(got) != 1 {
t.Fatalf("len(EntriesInPath()) = %d, want 1", len(got))
}
if got[0].Title != "Bellagio" {
t.Fatalf("EntriesInPath()[0].Title = %q, want %q", got[0].Title, "Bellagio")
}
if len(model.RecycleBin) != 0 {
t.Fatalf("len(RecycleBin) = %d, want 0", len(model.RecycleBin))
}
}
func TestRestoreEntryVersionPromotesHistoricalVersionAndRetainsCurrentInHistory(t *testing.T) {
t.Parallel()
model := Model{
Entries: []Entry{
{
ID: "vault-console",
Title: "Vault Console",
Username: "dannyocean",
Password: "new-token",
Notes: "Current note",
Path: []string{"Root", "Internet"},
History: []Entry{
{
ID: "vault-console-history-1",
Title: "Vault Console",
Username: "dannyocean",
Password: "old-token",
Notes: "Previous note",
Path: []string{"Root", "Internet"},
},
},
},
},
}
if err := model.RestoreEntryVersion("vault-console", 0); err != nil {
t.Fatalf("RestoreEntryVersion() error = %v", err)
}
got := model.EntriesInPath([]string{"Root", "Internet"})
if len(got) != 1 {
t.Fatalf("len(EntriesInPath()) = %d, want 1", len(got))
}
if got[0].Password != "old-token" || got[0].Notes != "Previous note" {
t.Fatalf("restored entry = %#v, want old-token/Previous note current version", got[0])
}
if len(got[0].History) != 1 {
t.Fatalf("len(History) = %d, want 1", len(got[0].History))
}
if got[0].History[0].Password != "new-token" || got[0].History[0].Notes != "Current note" {
t.Fatalf("History[0] = %#v, want prior current version retained", got[0].History[0])
}
}