test(accounts): regression guards for discover-org admin scope + 5xx-on-cred-failure (post-#212 CR)#222
Conversation
…on-cred-failure (post-#212 CR) Two test additions for the CR pass-2 nitpick on PR #212 (which landed without these regression guards). Both new cases lock down behaviour the CR-pass-1 fix introduced: * TestDiscoverOrgAccounts_RejectsNonAdmin — non-admin session must hit a 403 ClientError. Without this guard, a refactor that swaps requireAdmin back to requirePermission("create","accounts") silently re-opens org discovery to non-admin users, and discovered rows go straight into cloud_accounts (privilege-escalation surface, not just UX preference). * TestDiscoverOrgAccounts_CredResolutionFailureIs5xx — credential- resolution failures must surface as 5xx, not 4xx. Wraps a CredentialStore stub whose LoadRaw returns a transient-style error; asserts the handler returns a non-ClientError carrying the "resolve credentials" stage marker. Without this, a future refactor that re-applies NewClientError(400, ...) on resolver errors would silently make transient store/network failures non-retryable. Plus a small fakeCredStore-style helper, errCredStore, that satisfies the CredentialStore interface and always errors from LoadRaw — used only by the second test. CR also flagged a "AWSAuthMode = '' will violate the DB CHECK constraint" actionable; verified false alarm — the storage layer uses nullStringFromString(account.AWSAuthMode) which converts empty-string to sql.NullString{Valid: false} → SQL NULL → satisfies CHECK by Postgres semantics. No code change for that one; reply with justification on the CR thread. Verification: go test ./internal/api/... full-package green; new cases pass (and prove they are real guards by t.Fatal'ing the discoverOrgFn in the cred-failure case to assert the failure path exits before reaching discovery).
|
@coderabbitai review |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughAdded regression tests for ChangesTest Additions for discoverOrgAccounts Handler
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Review rate limit: 4/5 reviews remaining, refill in 12 minutes. Comment |
✅ Actions performedReview triggered.
|
Follow-up to PR #212 — addresses the only un-applied CR finding from that PR's reviews.
Background
PR #212 wired the AWS Organizations discovery endpoint and went through three CR rounds (15:25Z, 19:32Z, 19:42Z on 2026-04-30). It was merged at 20:34Z with most CR findings addressed in-line, but one nitpick from the 19:32Z review didn't make it into the merge:
Both behaviours are correctly implemented in the merged code; without tests they're a refactor away from quietly regressing.
What this PR adds
Two test cases in
internal/api/handler_accounts_test.go:TestDiscoverOrgAccounts_RejectsNonAdminNon-admin session must hit a 403 ClientError. Without this guard, a refactor that swaps
requireAdminback torequirePermission("create","accounts")silently re-opens org discovery to non-admin users — and discovered rows go straight intocloud_accounts, which is a privilege-escalation surface (not just a UX preference).TestDiscoverOrgAccounts_CredResolutionFailureIs5xxCredential-resolution failures must surface as 5xx (retryable), not 4xx. Wraps a
CredentialStorestub (errCredStore) whoseLoadRawreturns a transient-style error; asserts the handler returns a non-ClientErrorcarrying the"resolve credentials"stage marker so operators can find it in logs. Without this, a future refactor that re-appliesNewClientError(400, ...)on resolver errors would silently make transient store/network failures non-retryable.The test's
discoverOrgFninjection is at.Fatal— proving the failure path exits before reaching discovery (and that the test is exercising the credential-resolution failure path specifically, not a different shortcut).Notes on the other CR findings
PR #212 had 5 distinct CR concerns across 3 reviews. Status:
AWSAuthMode = ""will fail DB CHECK constraintdisco.Accountsaws_auth_mode=bastioncomments#3 false alarm
The store layer uses
nullStringFromString(account.AWSAuthMode)for the INSERT (internal/config/store_postgres.go:CreateCloudAccount). That converts""→sql.NullString{Valid: false}→ SQLNULL. Thecloud_accounts.aws_auth_modecolumn allowsNULL(noNOT NULLconstraint inmigrations/000011_cloud_accounts.up.sql:18-19), and PostgresCHECK (col IN (...))constraints satisfyNULLby default (the constraint is vacuously true onNULL). So persistingmember.AWSAuthMode = ""is correct as-is. I'll reply on the CR thread with this rationale.Verification
go build ./...cleango test ./internal/api/...— full package green (incl. the two new cases + all PR feat(accounts): wire AWS Organizations discovery endpoint (closes #208) #212 cases)Triage
type/chore,severity/medium,urgency/this-sprint,impact/internal,effort/xs,priority/p2,triaged. Pure-test PR; no behaviour change.Summary by CodeRabbit