Skip to content

Commit

Permalink
Filter recent versions to exclude those with invalid package IDs #908
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Nov 10, 2024
1 parent 0ab94aa commit b43d749
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/api/v1/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def show
def recent
@registry = Registry.find_by_name!(params[:id])

scope = @registry.versions.includes(package: :registry)
scope = @registry.versions.includes(package: :registry).where("EXISTS (SELECT 1 FROM packages WHERE packages.id = versions.package_id)")

scope = scope.created_after(params[:created_after]) if params[:created_after].present?
scope = scope.published_after(params[:published_after]) if params[:published_after].present?
Expand Down
14 changes: 14 additions & 0 deletions test/controllers/api/v1/versions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ class ApiV1VersionsControllerTest < ActionDispatch::IntegrationTest
assert_equal first_version['metadata'], {"foo"=>"bar"}
end

test 'get recent versions excludes versions with invalid package_id' do
dangling_version = @registry.versions.new(number: '2.0.0', package_id: 999_999, registry_id: @registry.id)
dangling_version.save(validate: false)

get versions_api_v1_registry_path(id: @registry.name)
assert_response :success
actual_response = Oj.load(@response.body)

assert_equal 1, actual_response.length
first_version = actual_response.first
assert_equal first_version['number'], '1.0.0'
assert_equal first_version['metadata'], { 'foo' => 'bar' }
end

test 'get version numbers' do
get version_numbers_api_v1_registry_package_path(registry_id: @registry.name, id: @package.name)
assert_response :success
Expand Down

0 comments on commit b43d749

Please sign in to comment.