|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +// deploy_ttl_guards_test.go — bug-bash #3/#5/#6: SetTTL must refuse to flip a |
| 4 | +// permanent deploy back to an expiring TTL (409 already_permanent) and refuse a |
| 5 | +// terminal deploy (409 invalid_state); the model's WHERE-guard is the |
| 6 | +// defense-in-depth backstop for the permanent case. Reuses the |
| 7 | +// seedDeploy/patchEnvApp/requireCoverageDB harness in deploy_stack_coverage_test.go. |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "database/sql" |
| 12 | + "net/http" |
| 13 | + "net/http/httptest" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/google/uuid" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + |
| 21 | + "instant.dev/internal/models" |
| 22 | + "instant.dev/internal/testhelpers" |
| 23 | +) |
| 24 | + |
| 25 | +// TestSetTTL_PermanentRejected: make a deploy permanent, then SetTTL → 409 |
| 26 | +// already_permanent, and the row stays permanent (expires_at NULL). |
| 27 | +func TestSetTTL_PermanentRejected(t *testing.T) { |
| 28 | + requireCoverageDB(t) |
| 29 | + db, cleanDB := testhelpers.SetupTestDB(t) |
| 30 | + defer cleanDB() |
| 31 | + teamIDStr := testhelpers.MustCreateTeamDB(t, db, "pro") |
| 32 | + teamID := uuid.MustParse(teamIDStr) |
| 33 | + deployID, _ := seedDeploy(t, db, teamID, "healthy", "pro") |
| 34 | + jwt := testhelpers.MustSignSessionJWT(t, uuid.NewString(), teamIDStr, "ttlperm@example.com") |
| 35 | + app, _ := patchEnvApp(t, db) |
| 36 | + |
| 37 | + // Make it permanent first. |
| 38 | + mp := httptest.NewRequest(http.MethodPost, "/api/v1/deployments/"+deployID.String()+"/make-permanent", nil) |
| 39 | + mp.Header.Set("Authorization", "Bearer "+jwt) |
| 40 | + mpResp, err := app.Test(mp, 5000) |
| 41 | + require.NoError(t, err) |
| 42 | + require.Equal(t, http.StatusOK, mpResp.StatusCode) |
| 43 | + _ = mpResp.Body.Close() |
| 44 | + |
| 45 | + // SetTTL must now 409. |
| 46 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/deployments/"+deployID.String()+"/ttl", |
| 47 | + strings.NewReader(`{"hours":48}`)) |
| 48 | + req.Header.Set("Authorization", "Bearer "+jwt) |
| 49 | + req.Header.Set("Content-Type", "application/json") |
| 50 | + resp, err := app.Test(req, 5000) |
| 51 | + require.NoError(t, err) |
| 52 | + defer resp.Body.Close() |
| 53 | + assert.Equal(t, http.StatusConflict, resp.StatusCode, "SetTTL on a permanent deploy must 409") |
| 54 | + |
| 55 | + // Row stayed permanent. |
| 56 | + var policy string |
| 57 | + var expiresAt sql.NullTime |
| 58 | + require.NoError(t, db.QueryRow(`SELECT ttl_policy, expires_at FROM deployments WHERE id=$1`, deployID).Scan(&policy, &expiresAt)) |
| 59 | + assert.Equal(t, "permanent", policy) |
| 60 | + assert.False(t, expiresAt.Valid, "expires_at must stay NULL") |
| 61 | +} |
| 62 | + |
| 63 | +// TestSetTTL_TerminalRejected: a terminal (expired) deploy → 409 invalid_state. |
| 64 | +func TestSetTTL_TerminalRejected(t *testing.T) { |
| 65 | + requireCoverageDB(t) |
| 66 | + db, cleanDB := testhelpers.SetupTestDB(t) |
| 67 | + defer cleanDB() |
| 68 | + teamIDStr := testhelpers.MustCreateTeamDB(t, db, "pro") |
| 69 | + teamID := uuid.MustParse(teamIDStr) |
| 70 | + deployID, _ := seedDeploy(t, db, teamID, "expired", "pro") |
| 71 | + jwt := testhelpers.MustSignSessionJWT(t, uuid.NewString(), teamIDStr, "ttlterm@example.com") |
| 72 | + app, _ := patchEnvApp(t, db) |
| 73 | + |
| 74 | + req := httptest.NewRequest(http.MethodPost, "/api/v1/deployments/"+deployID.String()+"/ttl", |
| 75 | + strings.NewReader(`{"hours":48}`)) |
| 76 | + req.Header.Set("Authorization", "Bearer "+jwt) |
| 77 | + req.Header.Set("Content-Type", "application/json") |
| 78 | + resp, err := app.Test(req, 5000) |
| 79 | + require.NoError(t, err) |
| 80 | + defer resp.Body.Close() |
| 81 | + assert.Equal(t, http.StatusConflict, resp.StatusCode, "SetTTL on a terminal deploy must 409") |
| 82 | +} |
| 83 | + |
| 84 | +// TestSetDeploymentTTL_PermanentGuard: the model WHERE-guard leaves a permanent |
| 85 | +// row untouched even if called directly (the race backstop, #6). |
| 86 | +func TestSetDeploymentTTL_PermanentGuard(t *testing.T) { |
| 87 | + requireCoverageDB(t) |
| 88 | + db, cleanDB := testhelpers.SetupTestDB(t) |
| 89 | + defer cleanDB() |
| 90 | + teamID := uuid.MustParse(testhelpers.MustCreateTeamDB(t, db, "pro")) |
| 91 | + deployID, _ := seedDeploy(t, db, teamID, "healthy", "pro") |
| 92 | + _, err := db.Exec(`UPDATE deployments SET ttl_policy='permanent', expires_at=NULL WHERE id=$1`, deployID) |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + // Direct model call must NOT flip the permanent row. |
| 96 | + require.NoError(t, models.SetDeploymentTTL(context.Background(), db, deployID, 24)) |
| 97 | + var policy string |
| 98 | + var expiresAt sql.NullTime |
| 99 | + require.NoError(t, db.QueryRow(`SELECT ttl_policy, expires_at FROM deployments WHERE id=$1`, deployID).Scan(&policy, &expiresAt)) |
| 100 | + assert.Equal(t, "permanent", policy, "WHERE guard must leave a permanent deploy untouched") |
| 101 | + assert.False(t, expiresAt.Valid) |
| 102 | +} |
0 commit comments