feat: add projects flow and consolidate default project handling
This commit is contained in:
@@ -136,6 +136,160 @@ func TestConfigurationVersioningAPI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestProjectArchiveHidesConfigsAndCloneIntoProject(t *testing.T) {
|
||||
moveToRepoRoot(t)
|
||||
|
||||
local, connMgr, configService := newAPITestStack(t)
|
||||
_ = configService
|
||||
|
||||
cfg := &config.Config{}
|
||||
setConfigDefaults(cfg)
|
||||
router, _, err := setupRouter(cfg, local, connMgr, nil, "tester", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup router: %v", err)
|
||||
}
|
||||
|
||||
createProjectReq := httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader([]byte(`{"name":"P1"}`)))
|
||||
createProjectReq.Header.Set("Content-Type", "application/json")
|
||||
createProjectRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(createProjectRec, createProjectReq)
|
||||
if createProjectRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create project status=%d body=%s", createProjectRec.Code, createProjectRec.Body.String())
|
||||
}
|
||||
var project models.Project
|
||||
if err := json.Unmarshal(createProjectRec.Body.Bytes(), &project); err != nil {
|
||||
t.Fatalf("unmarshal project: %v", err)
|
||||
}
|
||||
|
||||
createCfgBody := []byte(`{"name":"Cfg A","items":[{"lot_name":"CPU","quantity":1,"unit_price":100}],"server_count":1}`)
|
||||
createCfgReq := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.UUID+"/configs", bytes.NewReader(createCfgBody))
|
||||
createCfgReq.Header.Set("Content-Type", "application/json")
|
||||
createCfgRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(createCfgRec, createCfgReq)
|
||||
if createCfgRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create project config status=%d body=%s", createCfgRec.Code, createCfgRec.Body.String())
|
||||
}
|
||||
var createdCfg models.Configuration
|
||||
if err := json.Unmarshal(createCfgRec.Body.Bytes(), &createdCfg); err != nil {
|
||||
t.Fatalf("unmarshal project config: %v", err)
|
||||
}
|
||||
if createdCfg.ProjectUUID == nil || *createdCfg.ProjectUUID != project.UUID {
|
||||
t.Fatalf("expected config project_uuid=%s got=%v", project.UUID, createdCfg.ProjectUUID)
|
||||
}
|
||||
|
||||
cloneReq := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.UUID+"/configs/"+createdCfg.UUID+"/clone", bytes.NewReader([]byte(`{"name":"Cfg A Clone"}`)))
|
||||
cloneReq.Header.Set("Content-Type", "application/json")
|
||||
cloneRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(cloneRec, cloneReq)
|
||||
if cloneRec.Code != http.StatusCreated {
|
||||
t.Fatalf("clone in project status=%d body=%s", cloneRec.Code, cloneRec.Body.String())
|
||||
}
|
||||
var cloneCfg models.Configuration
|
||||
if err := json.Unmarshal(cloneRec.Body.Bytes(), &cloneCfg); err != nil {
|
||||
t.Fatalf("unmarshal clone config: %v", err)
|
||||
}
|
||||
if cloneCfg.ProjectUUID == nil || *cloneCfg.ProjectUUID != project.UUID {
|
||||
t.Fatalf("expected clone project_uuid=%s got=%v", project.UUID, cloneCfg.ProjectUUID)
|
||||
}
|
||||
|
||||
projectConfigsReq := httptest.NewRequest(http.MethodGet, "/api/projects/"+project.UUID+"/configs", nil)
|
||||
projectConfigsRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(projectConfigsRec, projectConfigsReq)
|
||||
if projectConfigsRec.Code != http.StatusOK {
|
||||
t.Fatalf("project configs status=%d body=%s", projectConfigsRec.Code, projectConfigsRec.Body.String())
|
||||
}
|
||||
var projectConfigsResp struct {
|
||||
Configurations []models.Configuration `json:"configurations"`
|
||||
}
|
||||
if err := json.Unmarshal(projectConfigsRec.Body.Bytes(), &projectConfigsResp); err != nil {
|
||||
t.Fatalf("unmarshal project configs response: %v", err)
|
||||
}
|
||||
if len(projectConfigsResp.Configurations) != 2 {
|
||||
t.Fatalf("expected 2 project configs after clone, got %d", len(projectConfigsResp.Configurations))
|
||||
}
|
||||
|
||||
archiveReq := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.UUID+"/archive", nil)
|
||||
archiveRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(archiveRec, archiveReq)
|
||||
if archiveRec.Code != http.StatusOK {
|
||||
t.Fatalf("archive project status=%d body=%s", archiveRec.Code, archiveRec.Body.String())
|
||||
}
|
||||
|
||||
activeReq := httptest.NewRequest(http.MethodGet, "/api/configs?status=active&page=1&per_page=20", nil)
|
||||
activeRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(activeRec, activeReq)
|
||||
if activeRec.Code != http.StatusOK {
|
||||
t.Fatalf("active configs status=%d body=%s", activeRec.Code, activeRec.Body.String())
|
||||
}
|
||||
var activeResp struct {
|
||||
Configurations []models.Configuration `json:"configurations"`
|
||||
}
|
||||
if err := json.Unmarshal(activeRec.Body.Bytes(), &activeResp); err != nil {
|
||||
t.Fatalf("unmarshal active configs response: %v", err)
|
||||
}
|
||||
if len(activeResp.Configurations) != 0 {
|
||||
t.Fatalf("expected no active configs after project archive, got %d", len(activeResp.Configurations))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigMoveToProjectEndpoint(t *testing.T) {
|
||||
moveToRepoRoot(t)
|
||||
|
||||
local, connMgr, _ := newAPITestStack(t)
|
||||
cfg := &config.Config{}
|
||||
setConfigDefaults(cfg)
|
||||
router, _, err := setupRouter(cfg, local, connMgr, nil, "tester", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("setup router: %v", err)
|
||||
}
|
||||
|
||||
createProjectReq := httptest.NewRequest(http.MethodPost, "/api/projects", bytes.NewReader([]byte(`{"name":"Move Project"}`)))
|
||||
createProjectReq.Header.Set("Content-Type", "application/json")
|
||||
createProjectRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(createProjectRec, createProjectReq)
|
||||
if createProjectRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create project status=%d body=%s", createProjectRec.Code, createProjectRec.Body.String())
|
||||
}
|
||||
var project models.Project
|
||||
if err := json.Unmarshal(createProjectRec.Body.Bytes(), &project); err != nil {
|
||||
t.Fatalf("unmarshal project: %v", err)
|
||||
}
|
||||
|
||||
createConfigReq := httptest.NewRequest(http.MethodPost, "/api/configs", bytes.NewReader([]byte(`{"name":"Move Me","items":[],"notes":"","server_count":1}`)))
|
||||
createConfigReq.Header.Set("Content-Type", "application/json")
|
||||
createConfigRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(createConfigRec, createConfigReq)
|
||||
if createConfigRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create config status=%d body=%s", createConfigRec.Code, createConfigRec.Body.String())
|
||||
}
|
||||
var created models.Configuration
|
||||
if err := json.Unmarshal(createConfigRec.Body.Bytes(), &created); err != nil {
|
||||
t.Fatalf("unmarshal config: %v", err)
|
||||
}
|
||||
|
||||
moveReq := httptest.NewRequest(http.MethodPatch, "/api/configs/"+created.UUID+"/project", bytes.NewReader([]byte(`{"project_uuid":"`+project.UUID+`"}`)))
|
||||
moveReq.Header.Set("Content-Type", "application/json")
|
||||
moveRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(moveRec, moveReq)
|
||||
if moveRec.Code != http.StatusOK {
|
||||
t.Fatalf("move config status=%d body=%s", moveRec.Code, moveRec.Body.String())
|
||||
}
|
||||
|
||||
getReq := httptest.NewRequest(http.MethodGet, "/api/configs/"+created.UUID, nil)
|
||||
getRec := httptest.NewRecorder()
|
||||
router.ServeHTTP(getRec, getReq)
|
||||
if getRec.Code != http.StatusOK {
|
||||
t.Fatalf("get config status=%d body=%s", getRec.Code, getRec.Body.String())
|
||||
}
|
||||
var updated models.Configuration
|
||||
if err := json.Unmarshal(getRec.Body.Bytes(), &updated); err != nil {
|
||||
t.Fatalf("unmarshal updated config: %v", err)
|
||||
}
|
||||
if updated.ProjectUUID == nil || *updated.ProjectUUID != project.UUID {
|
||||
t.Fatalf("expected moved project_uuid=%s, got %v", project.UUID, updated.ProjectUUID)
|
||||
}
|
||||
}
|
||||
|
||||
func newAPITestStack(t *testing.T) (*localdb.LocalDB, *db.ConnectionManager, *services.LocalConfigurationService) {
|
||||
t.Helper()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user