feat: complete MVP epics E7-E10 (provisioning, console proxy, audit, frontend)
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
package template
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestListTemplatesReturnsTemplates(t *testing.T) {
|
||||
repository := &stubRepository{
|
||||
templates: []Template{{
|
||||
ID: "tmpl-1",
|
||||
ClusterID: "cluster-1",
|
||||
Name: "ubuntu-24.04",
|
||||
Description: "Ubuntu 24.04 LTS",
|
||||
ProxmoxTemplateVMID: 9000,
|
||||
ProxmoxNode: "pve1",
|
||||
CreatedAt: time.Date(2026, 6, 11, 12, 0, 0, 0, time.UTC),
|
||||
}},
|
||||
}
|
||||
handler := NewHandler(repository)
|
||||
req := httptest.NewRequest(http.MethodGet, "/internal/templates", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ListTemplates(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
var response struct {
|
||||
Data []Template `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
if len(response.Data) != 1 {
|
||||
t.Fatalf("len(data) = %d, want 1", len(response.Data))
|
||||
}
|
||||
if response.Data[0].ID != "tmpl-1" {
|
||||
t.Fatalf("template ID = %q, want tmpl-1", response.Data[0].ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListTemplatesReturnsEmptyList(t *testing.T) {
|
||||
handler := NewHandler(&stubRepository{templates: []Template{}})
|
||||
req := httptest.NewRequest(http.MethodGet, "/internal/templates", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.ListTemplates(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateTemplateReturnsCreated(t *testing.T) {
|
||||
repository := &stubRepository{
|
||||
upsertResult: Template{
|
||||
ID: "tmpl-1",
|
||||
ClusterID: "cluster-1",
|
||||
Name: "ubuntu-24.04",
|
||||
Description: "Ubuntu 24.04 LTS",
|
||||
ProxmoxTemplateVMID: 9000,
|
||||
ProxmoxNode: "pve1",
|
||||
},
|
||||
}
|
||||
handler := NewHandler(repository)
|
||||
body := `{"cluster_id":"cluster-1","name":"ubuntu-24.04","description":"Ubuntu 24.04 LTS","proxmox_template_vmid":9000,"proxmox_node":"pve1"}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/internal/templates", io.NopCloser(strings.NewReader(body)))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.CreateTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusCreated {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusCreated)
|
||||
}
|
||||
|
||||
var response Template
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
if response.ID != "tmpl-1" {
|
||||
t.Fatalf("template ID = %q, want tmpl-1", response.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateTemplateRejectsEmptyName(t *testing.T) {
|
||||
handler := NewHandler(&stubRepository{})
|
||||
body := `{"cluster_id":"cluster-1","name":"","proxmox_template_vmid":9000}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/internal/templates", io.NopCloser(strings.NewReader(body)))
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.CreateTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTemplateReturnsUpdated(t *testing.T) {
|
||||
repository := &stubRepository{
|
||||
getFound: true,
|
||||
getResult: Template{
|
||||
ID: "tmpl-1",
|
||||
ClusterID: "cluster-1",
|
||||
Name: "ubuntu-24.04",
|
||||
Description: "Ubuntu 24.04 LTS",
|
||||
ProxmoxTemplateVMID: 9000,
|
||||
ProxmoxNode: "pve1",
|
||||
},
|
||||
upsertResult: Template{
|
||||
ID: "tmpl-1",
|
||||
ClusterID: "cluster-1",
|
||||
Name: "ubuntu-24.04-updated",
|
||||
Description: "Updated",
|
||||
ProxmoxTemplateVMID: 9000,
|
||||
ProxmoxNode: "pve1",
|
||||
},
|
||||
}
|
||||
handler := NewHandler(repository)
|
||||
body := `{"name":"ubuntu-24.04-updated","description":"Updated"}`
|
||||
req := httptest.NewRequest(http.MethodPut, "/internal/templates/tmpl-1", io.NopCloser(strings.NewReader(body)))
|
||||
req.SetPathValue("templateID", "tmpl-1")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.UpdateTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK)
|
||||
}
|
||||
var response Template
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
if response.Name != "ubuntu-24.04-updated" {
|
||||
t.Fatalf("name = %q, want ubuntu-24.04-updated", response.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateTemplateReturnsNotFound(t *testing.T) {
|
||||
handler := NewHandler(&stubRepository{getFound: false})
|
||||
body := `{"name":"updated"}`
|
||||
req := httptest.NewRequest(http.MethodPut, "/internal/templates/tmpl-xxx", io.NopCloser(strings.NewReader(body)))
|
||||
req.SetPathValue("templateID", "tmpl-xxx")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.UpdateTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTemplateReturnsNoContent(t *testing.T) {
|
||||
handler := NewHandler(&stubRepository{deleteResult: true})
|
||||
req := httptest.NewRequest(http.MethodDelete, "/internal/templates/tmpl-1", nil)
|
||||
req.SetPathValue("templateID", "tmpl-1")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.DeleteTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTemplateReturnsNotFound(t *testing.T) {
|
||||
handler := NewHandler(&stubRepository{deleteResult: false})
|
||||
req := httptest.NewRequest(http.MethodDelete, "/internal/templates/tmpl-xxx", nil)
|
||||
req.SetPathValue("templateID", "tmpl-xxx")
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
handler.DeleteTemplate(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want %d", rec.Code, http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
type stubRepository struct {
|
||||
templates []Template
|
||||
getFound bool
|
||||
getResult Template
|
||||
upsertResult Template
|
||||
deleteResult bool
|
||||
}
|
||||
|
||||
func (s *stubRepository) List(_ context.Context) ([]Template, error) {
|
||||
return s.templates, nil
|
||||
}
|
||||
|
||||
func (s *stubRepository) Get(_ context.Context, _ string) (Template, bool, error) {
|
||||
return s.getResult, s.getFound, nil
|
||||
}
|
||||
|
||||
func (s *stubRepository) Upsert(_ context.Context, _ Template) (Template, error) {
|
||||
return s.upsertResult, nil
|
||||
}
|
||||
|
||||
func (s *stubRepository) Delete(_ context.Context, _ string) (bool, error) {
|
||||
return s.deleteResult, nil
|
||||
}
|
||||
Reference in New Issue
Block a user