fix: validate agency namespace for stops #1099
Conversation
|
Warning Review limit reached
More reviews will be available in 42 minutes and 4 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesAgency Namespace Validation for Stop Endpoint
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
🚥 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 unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai full review |
✅ Action performedFull review finished. |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/restapi/stop_handler_test.go (1)
515-583: ⚡ Quick winAdd a test for the orphan-stop fallback branch (
len(routes)==0).This test only exercises the
len(routes) > 0path. Please add a case where a stop has no associated routes and an existing agency namespace still returns200, while a non-existent agency returns404.Proposed test extension
func TestStopHandler_WrongAgency(t *testing.T) { @@ // Test 3: Request with an agency that doesn't even exist in the DB -> 404 Not Found respGhost, modelGhost := callAPIHandler[StopEntryResponse](t, api, stopURL(utils.FormCombinedID("GhostAgency", stopID))) require.Equal(t, http.StatusNotFound, respGhost.StatusCode) assert.Equal(t, http.StatusNotFound, modelGhost.Code) + + // Test 4: Orphan stop (no routes) with existing agency namespace -> 200 OK + orphanStopID := "OrphanStop" + _, err = q.CreateStop(ctx, gtfsdb.CreateStopParams{ + ID: orphanStopID, Name: nulls.String("Orphan Stop"), Lat: 47.61, Lon: -122.31, + }) + require.NoError(t, err) + + respOrphanValid, modelOrphanValid := callAPIHandler[StopEntryResponse](t, api, stopURL(utils.FormCombinedID(validAgency, orphanStopID))) + require.Equal(t, http.StatusOK, respOrphanValid.StatusCode) + assert.Equal(t, http.StatusOK, modelOrphanValid.Code) + + // Test 5: Orphan stop with non-existent agency namespace -> 404 Not Found + respOrphanGhost, modelOrphanGhost := callAPIHandler[StopEntryResponse](t, api, stopURL(utils.FormCombinedID("GhostAgency", orphanStopID))) + require.Equal(t, http.StatusNotFound, respOrphanGhost.StatusCode) + assert.Equal(t, http.StatusNotFound, modelOrphanGhost.Code) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/restapi/stop_handler_test.go` around lines 515 - 583, The TestStopHandler_WrongAgency test only covers the case where a stop has associated routes (len(routes) > 0). Add test cases for an orphan stop with no associated routes by creating a second stop in the test data that is not linked to any route, trip, or stop_time. Then add test assertions after the existing three test cases to verify that requesting this orphan stop with an existing agency namespace (like validAgency) returns 200 OK, and requesting it with a non-existent agency namespace (like GhostAgency) returns 404 Not Found.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/restapi/stop_handler_test.go`:
- Around line 515-583: The TestStopHandler_WrongAgency test only covers the case
where a stop has associated routes (len(routes) > 0). Add test cases for an
orphan stop with no associated routes by creating a second stop in the test data
that is not linked to any route, trip, or stop_time. Then add test assertions
after the existing three test cases to verify that requesting this orphan stop
with an existing agency namespace (like validAgency) returns 200 OK, and
requesting it with a non-existent agency namespace (like GhostAgency) returns
404 Not Found.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 5be52f3e-1e9c-4127-86e8-5c4feea5f713
📒 Files selected for processing (2)
internal/restapi/stop_handler.gointernal/restapi/stop_handler_test.go
- Ensure the `/stop` endpoint verifies the requested agency namespace serves the retrieved stop. - Return 404 Not Found when requesting a valid stop under an incorrect or non-existent agency namespace. - Add a fallback to allow retrieval of orphaned stops (no associated routes) as long as the requested agency exists. - Add test coverage for invalid agency and orphaned stop scenarios.
|



Description
Resolves #1094 by ensuring the
/stopendpoint strictly validates the requested agency namespace.Changes Made
agencyIDactually serves the retrieved stop. If the agency does not have any routes serving the stop, it returns a404 Not Foundinstead of falsely returning the stop.stopstable lacks an explicitagency_idcolumn, if a stop has no routes at all, the handler allows retrieval as long as the requestedagencyIDexists in the database.TestStopHandler_WrongAgencyto guarantee that requesting a valid stop under an incorrect or non-existent agency namespace correctly triggers a 404.Testing
make testto verify the newTestStopHandler_WrongAgencyassertions pass successfully.Summary by CodeRabbit