Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/smart_proxy_container_gateway/container_gateway_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ class Api < ::Sinatra::Base
database.connection.transaction(isolation: :serializable, retry_on: [Sequel::SerializationFailure]) do
hosts_table = database.connection[:hosts]
hosts_table.delete
hosts_table.import(%i[uuid], hosts.map { |host| [host['uuid']] })
valid_hosts = hosts.filter_map { |host| [host['uuid']] if host['uuid'].present? }
hosts_table.import(%i[uuid], valid_hosts) unless valid_hosts.empty?
end
{}
end
Expand All @@ -277,6 +278,8 @@ class Api < ::Sinatra::Base
do_authorize_any
params['hosts'].flat_map do |host_map|
host_map.filter_map do |host_uuid, repos|
next if host_uuid.nil? || host_uuid.to_s.strip.empty?

if repos.nil? || repos.empty?
repo_names = []
else
Expand Down
6 changes: 6 additions & 0 deletions lib/smart_proxy_container_gateway/container_gateway_main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ def build_host_repository_mapping(host_repo_maps)
end

def build_host_entries(hosts, repositories, host_uuid, repos)
return if host_uuid.nil? || host_uuid.to_s.strip.empty?

host = hosts[{ uuid: host_uuid }]
return unless host
return if repos.nil? || repos.empty?
Expand All @@ -232,7 +234,11 @@ def extract_auth_required_repo_names(repos)
end

def update_host_repositories(uuid, repositories)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a test with nil UUIDs in test/container_gateway_backend_test.rb ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used claude and it added some tests that cover some blank uuid cases.. 🤖

return if uuid.nil? || uuid.to_s.strip.empty?

host = find_or_create_host(uuid)
return unless host

hosts_repositories = database.connection[:hosts_repositories]
database.connection.transaction(isolation: :serializable,
retry_on: [Sequel::SerializationFailure],
Expand Down
67 changes: 67 additions & 0 deletions test/container_gateway_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,71 @@ def test_flatpak_static_index_server_error
assert_equal 500, last_response.status
assert_equal '{"error": "internal server error"}', last_response.body
end

def test_update_hosts_filters_nil_uuids
hosts = [
{ 'uuid' => 'host-uuid-1' },
{ 'uuid' => nil },
{ 'uuid' => 'host-uuid-2' }
]

put '/update_hosts', { 'hosts' => hosts }
assert last_response.ok?

# Verify only valid hosts were inserted (old behavior would have failed or inserted nil)
assert @database.connection[:hosts].count >= 2
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
end

def test_update_hosts_filters_empty_string_uuids
hosts = [
{ 'uuid' => 'host-uuid-1' },
{ 'uuid' => '' },
{ 'uuid' => 'host-uuid-2' }
]

put '/update_hosts', { 'hosts' => hosts }
assert last_response.ok?

# Verify only valid hosts were inserted
assert @database.connection[:hosts].count >= 2
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
end

def test_update_hosts_handles_missing_uuid_key
hosts = [
{ 'uuid' => 'host-uuid-1' },
{ 'name' => 'host-without-uuid' },
{ 'uuid' => 'host-uuid-2' }
]

put '/update_hosts', { 'hosts' => hosts }
assert last_response.ok?

# Verify only valid hosts were inserted
assert @database.connection[:hosts].count >= 2
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-1')
assert_not_nil @database.connection[:hosts].first(uuid: 'host-uuid-2')
end

def test_update_hosts_with_empty_array
put '/update_hosts', { 'hosts' => [] }
assert last_response.ok?
end

def test_update_hosts_with_all_invalid_uuids
hosts = [
{ 'uuid' => nil },
{ 'uuid' => '' },
{ 'name' => 'no-uuid' }
]

put '/update_hosts', { 'hosts' => hosts }
assert last_response.ok?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't verify that no hosts were added to the DB.


# Verify that no hosts were added to the DB
assert_equal 0, @database.connection[:hosts].count
end
end
44 changes: 44 additions & 0 deletions test/container_gateway_backend_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,49 @@ def test_build_host_repository_mapping_handles_mixed_scenarios

assert_equal [[repo[:id], host[:id]]], result
end

def test_update_host_repositories_with_nil_uuid
@container_gateway_main.update_repository_list([{ 'repository' => 'test_repo1', 'auth_required' => true }])

# Should not raise an error and should not create any hosts
@container_gateway_main.update_host_repositories(nil, ['test_repo1'])

assert_equal 0, @database.connection[:hosts].count
assert_equal 0, @database.connection[:hosts_repositories].count
end

def test_update_host_repositories_with_empty_string_uuid
@container_gateway_main.update_repository_list([{ 'repository' => 'test_repo1', 'auth_required' => true }])

# Should not raise an error and should not create any hosts
@container_gateway_main.update_host_repositories('', ['test_repo1'])

assert_equal 0, @database.connection[:hosts].count
assert_equal 0, @database.connection[:hosts_repositories].count
end

def test_build_host_entries_with_nil_uuid
@container_gateway_main.update_repository_list([{ 'repository' => 'test_repo1', 'auth_required' => true }])

hosts = @database.connection[:hosts]
repositories = @database.connection[:repositories]
repos = [{ 'repository' => 'test_repo1', 'auth_required' => true }]

result = @container_gateway_main.build_host_entries(hosts, repositories, nil, repos)

assert_nil result
end

def test_build_host_entries_with_empty_string_uuid
@container_gateway_main.update_repository_list([{ 'repository' => 'test_repo1', 'auth_required' => true }])

hosts = @database.connection[:hosts]
repositories = @database.connection[:repositories]
repos = [{ 'repository' => 'test_repo1', 'auth_required' => true }]

result = @container_gateway_main.build_host_entries(hosts, repositories, '', repos)

assert_nil result
end
end
# rubocop:enable Metrics/AbcSize