From db35f46b307a995b576182d1154126b31742ffd1 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 23 Nov 2024 10:29:45 +0000 Subject: [PATCH] test(be): add tests for vault --- db/bolt/template_vault_test.go | 136 +++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 db/bolt/template_vault_test.go diff --git a/db/bolt/template_vault_test.go b/db/bolt/template_vault_test.go new file mode 100644 index 00000000..2a295c94 --- /dev/null +++ b/db/bolt/template_vault_test.go @@ -0,0 +1,136 @@ +package bolt + +import ( + "testing" + "time" + + "github.com/semaphoreui/semaphore/db" +) + +func TestGetTemplateVaults(t *testing.T) { + store := CreateTestStore() + + proj, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "TestProject", + }) + if err != nil { + t.Fatal(err.Error()) + } + + template, err := store.CreateTemplate(db.Template{ + ProjectID: proj.ID, + Name: "TestTemplate", + Playbook: "test.yml", + }) + if err != nil { + t.Fatal(err.Error()) + } + + vault, err := store.CreateTemplateVault(db.TemplateVault{ + ProjectID: proj.ID, + TemplateID: template.ID, + Type: "password", + }) + if err != nil { + t.Fatal(err.Error()) + } + + vaults, err := store.GetTemplateVaults(proj.ID, template.ID) + if err != nil { + t.Fatal(err.Error()) + } + + if len(vaults) != 1 || vaults[0].ID != vault.ID { + t.Fatalf("expected 1 vault, got %d", len(vaults)) + } +} + +func TestCreateTemplateVault(t *testing.T) { + store := CreateTestStore() + + proj, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "TestProject", + }) + if err != nil { + t.Fatal(err.Error()) + } + + template, err := store.CreateTemplate(db.Template{ + ProjectID: proj.ID, + Name: "TestTemplate", + Playbook: "test.yml", + }) + if err != nil { + t.Fatal(err.Error()) + } + + vault, err := store.CreateTemplateVault(db.TemplateVault{ + ProjectID: proj.ID, + TemplateID: template.ID, + Type: "password", + }) + if err != nil { + t.Fatal(err.Error()) + } + + foundVaults, err := store.GetTemplateVaults(proj.ID, template.ID) + if err != nil { + t.Fatal(err.Error()) + } + + if len(foundVaults) != 1 || foundVaults[0].ID != vault.ID { + t.Fatalf("expected 1 vault, got %d", len(foundVaults)) + } +} + +func TestUpdateTemplateVaults(t *testing.T) { + store := CreateTestStore() + + proj, err := store.CreateProject(db.Project{ + Created: time.Now(), + Name: "TestProject", + }) + if err != nil { + t.Fatal(err.Error()) + } + + template, err := store.CreateTemplate(db.Template{ + ProjectID: proj.ID, + Name: "TestTemplate", + Playbook: "test.yml", + }) + if err != nil { + t.Fatal(err.Error()) + } + + _, err = store.CreateTemplateVault(db.TemplateVault{ + ProjectID: proj.ID, + TemplateID: template.ID, + Type: "password", + }) + if err != nil { + t.Fatal(err.Error()) + } + + vault2 := db.TemplateVault{ + ProjectID: proj.ID, + TemplateID: template.ID, + Type: "script", + } + + err = store.UpdateTemplateVaults(proj.ID, template.ID, []db.TemplateVault{vault2}) + if err != nil { + t.Fatal(err.Error()) + } + + vaults, err := store.GetTemplateVaults(proj.ID, template.ID) + if err != nil { + t.Fatal(err.Error()) + } + + if len(vaults) != 1 || vaults[0].Type != "script" { + t.Fatalf("expected 1 vault with type 'script', got %d", len(vaults)) + } +}