Compare commits

...

2 Commits

Author SHA1 Message Date
core-be 53571f6525 test(handlers): add PatchAbilities regression coverage
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 3s
CI / Detect changes (pull_request) Successful in 4s
CI / Shellcheck (E2E scripts) (pull_request) Successful in 9s
E2E API Smoke Test / detect-changes (pull_request) Successful in 4s
E2E Chat / detect-changes (pull_request) Successful in 4s
E2E Staging Canvas (Playwright) / detect-changes (pull_request) Successful in 4s
Handlers Postgres Integration / detect-changes (pull_request) Successful in 3s
Harness Replays / detect-changes (pull_request) Successful in 3s
Runtime PR-Built Compatibility / detect-changes (pull_request) Successful in 4s
Secret scan / Scan diff for credential-shaped strings (pull_request) Successful in 5s
lint-required-no-paths / lint-required-no-paths (pull_request) Successful in 53s
CI / Platform (Go) (pull_request) Successful in 4m46s
CI / Canvas (Next.js) (pull_request) Successful in 6m42s
CI / Python Lint & Test (pull_request) Successful in 6m32s
CI / all-required (pull_request) Successful in 4m35s
E2E Staging Canvas (Playwright) / Canvas tabs E2E (pull_request) Successful in 4s
E2E API Smoke Test / E2E API Smoke Test (pull_request) Successful in 48s
Harness Replays / Harness Replays (pull_request) Successful in 2s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (pull_request) Successful in 2s
CI / Canvas Deploy Reminder (pull_request) Has been skipped
Handlers Postgres Integration / Handlers Postgres Integration (pull_request) Successful in 1m27s
E2E Chat / E2E Chat (pull_request) Failing after 4m35s
gate-check-v3 / gate-check (pull_request) Successful in 2s
sop-checklist / na-declarations (pull_request) N/A: (none)
sop-checklist / all-items-acked (pull_request) Successful in 3s
sop-tier-check / tier-check (pull_request) Successful in 3s
qa-review / approved (pull_request) Refired via /qa-recheck by unknown
security-review / approved (pull_request) Refired via /security-recheck by unknown
Adds 10 test cases for PATCH /workspaces/:id/abilities:

Happy path:
- broadcast_enabled=true → 200
- broadcast_enabled=false → 200
- talk_to_user_enabled=true → 200
- both fields in one request → 200 (each UPDATE in order)

Input validation:
- empty body {} → 400
- non-JSON body → 400
- non-UUID workspace ID → 400

Database errors:
- workspace not found → 404
- DB error on existence check → 500
- DB error on broadcast_enabled UPDATE → 500
- DB error on talk_to_user_enabled UPDATE → 500

Covers workspace_abilities.go which was the only unreviewed handler
with zero test coverage. No production code changed.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 03:53:13 +00:00
core-be 610a5df5bc fix(workspace-server): distinguish DB error from not-found in PatchAbilities
The existence-check condition `err != nil || !exists` conflated two
semantically different outcomes into a single 404 response:

  - err != nil    → DB/internal error → should be 500
  - !exists       → workspace absent  → 404 is correct

Fix: split into two explicit branches. DB errors now return 500 with
a logged reason. The not-found case remains 404.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-17 03:53:13 +00:00
2 changed files with 271 additions and 1 deletions
@@ -51,7 +51,12 @@ func PatchAbilities(c *gin.Context) {
var exists bool
if err := db.DB.QueryRowContext(ctx,
`SELECT EXISTS(SELECT 1 FROM workspaces WHERE id = $1 AND status != 'removed')`, id,
).Scan(&exists); err != nil || !exists {
).Scan(&exists); err != nil {
log.Printf("PatchAbilities: workspace existence check for %s: %v", id, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
return
}
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": "workspace not found"})
return
}
@@ -0,0 +1,265 @@
package handlers
// workspace_abilities_test.go — regression tests for PATCH /workspaces/:id/abilities.
//
// The handler toggles two workspace-level ability flags:
// broadcast_enabled — workspace may POST /broadcast to send org-wide messages
// talk_to_user_enabled — workspace may deliver canvas chat messages via
// send_message_to_user / POST /notify
//
// Gated behind AdminAuth so workspace agents cannot self-modify their own
// ability flags. These tests cover the uncredentialed unit-path (AdminAuth
// middleware is tested separately).
import (
"bytes"
"database/sql"
"net/http"
"net/http/httptest"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/gin-gonic/gin"
)
// validUUID is a stable test workspace ID that passes uuid.Parse validation.
const validUUID = "00000000-0000-0000-0000-000000000001"
// buildAbilitiesCtx wires a gin.Context for PATCH /workspaces/:id/abilities.
func buildAbilitiesCtx(id string, body string) (*httptest.ResponseRecorder, *gin.Context) {
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Params = gin.Params{{Key: "id", Value: id}}
c.Request = httptest.NewRequest("PATCH", "/workspaces/"+id+"/abilities",
bytes.NewBufferString(body))
c.Request.Header.Set("Content-Type", "application/json")
return w, c
}
// -------- Happy path --------
// PatchAbilities writes broadcast_enabled=true and returns 200.
func TestPatchAbilities_BroadcastEnabled_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// PatchAbilities writes broadcast_enabled=false and returns 200.
func TestPatchAbilities_BroadcastEnabledFalse_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, false).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":false}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// PatchAbilities writes talk_to_user_enabled=true and returns 200.
func TestPatchAbilities_TalkToUserEnabled_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"talk_to_user_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// Both ability flags in the same request are each written with their own UPDATE.
func TestPatchAbilities_BothFields_200(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
// broadcast_enabled written first
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnResult(sqlmock.NewResult(0, 1))
// talk_to_user_enabled written second
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, false).
WillReturnResult(sqlmock.NewResult(0, 1))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true,"talk_to_user_enabled":false}`)
PatchAbilities(c)
if w.Code != http.StatusOK {
t.Errorf("expected 200, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// -------- Input validation --------
// Empty body (neither field) → 400.
func TestPatchAbilities_NoAbilityFields_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx(validUUID, `{}`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// Non-JSON body → 400.
func TestPatchAbilities_InvalidJSON_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx(validUUID, `not json at all`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// Invalid (non-UUID) workspace ID → 400.
func TestPatchAbilities_InvalidWorkspaceID_400(t *testing.T) {
setupTestDB(t)
setupTestRedis(t)
w, c := buildAbilitiesCtx("not-a-uuid", `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
// -------- Database errors --------
// Workspace does not exist → 404.
func TestPatchAbilities_WorkspaceNotFound_404(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusNotFound {
t.Errorf("expected 404, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on existence check → 500.
func TestPatchAbilities_DBErrorOnExistsCheck_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on broadcast_enabled UPDATE → 500.
func TestPatchAbilities_DBErrorOnBroadcastUpdate_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET broadcast_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"broadcast_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}
// DB error on talk_to_user_enabled UPDATE → 500.
func TestPatchAbilities_DBErrorOnTalkToUserUpdate_500(t *testing.T) {
mock := setupTestDB(t)
setupTestRedis(t)
mock.ExpectQuery(`SELECT EXISTS\(SELECT 1 FROM workspaces WHERE id = \$1 AND status != 'removed'\)`).
WithArgs(validUUID).
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(true))
mock.ExpectExec(`UPDATE workspaces SET talk_to_user_enabled = \$2, updated_at = now\(\) WHERE id = \$1`).
WithArgs(validUUID, true).
WillReturnError(sql.ErrConnDone)
w, c := buildAbilitiesCtx(validUUID, `{"talk_to_user_enabled":true}`)
PatchAbilities(c)
if w.Code != http.StatusInternalServerError {
t.Errorf("expected 500, got %d: %s", w.Code, w.Body.String())
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("unmet sqlmock expectations: %v", err)
}
}