diff --git a/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex b/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex index 5f5a51a624..fc31f2ee31 100644 --- a/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex +++ b/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex @@ -128,9 +128,9 @@ defmodule Trento.Infrastructure.SoftwareUpdates.MockSuma do {:ok, [ %{ - name: "test" + name: "vmdrbddev01" }, - %{name: "test2"} + %{name: "vmdrbddev02"} ]} @impl true diff --git a/lib/trento_web/controllers/v1/suse_manager_controller.ex b/lib/trento_web/controllers/v1/suse_manager_controller.ex index 68db614285..15763a97bd 100644 --- a/lib/trento_web/controllers/v1/suse_manager_controller.ex +++ b/lib/trento_web/controllers/v1/suse_manager_controller.ex @@ -2,6 +2,9 @@ defmodule TrentoWeb.V1.SUSEManagerController do use TrentoWeb, :controller use OpenApiSpex.ControllerSpecs + alias Trento.Hosts + alias Trento.Hosts.Projections.HostReadModel + alias Trento.SoftwareUpdates alias Trento.SoftwareUpdates.Discovery @@ -94,6 +97,16 @@ defmodule TrentoWeb.V1.SUSEManagerController do @spec errata_details(Plug.Conn.t(), any) :: Plug.Conn.t() def errata_details(conn, %{advisory_name: advisory_name}) do + hosts = Hosts.get_all_hosts() + + registered_hosts = + Enum.flat_map(hosts, fn %HostReadModel{ + fully_qualified_domain_name: fqdn, + hostname: hostname + } -> + [fqdn, hostname] + end) + with {:ok, errata_details} <- Discovery.get_errata_details(advisory_name), {:ok, cves} <- Discovery.get_cves(advisory_name), {:ok, fixes} <- Discovery.get_bugzilla_fixes(advisory_name), @@ -104,7 +117,10 @@ defmodule TrentoWeb.V1.SUSEManagerController do cves: cves, fixes: fixes, affected_packages: affected_packages, - affected_systems: affected_systems + affected_systems: + Enum.filter(affected_systems, fn %{name: system} -> + Enum.member?(registered_hosts, system) + end) }) end end diff --git a/test/e2e/cypress/e2e/suse_manager_overviews.cy.js b/test/e2e/cypress/e2e/suse_manager_overviews.cy.js index c2911563c5..1accf80d64 100644 --- a/test/e2e/cypress/e2e/suse_manager_overviews.cy.js +++ b/test/e2e/cypress/e2e/suse_manager_overviews.cy.js @@ -62,8 +62,8 @@ context('SUSE Manager overviews', () => { cy.contains('Affected Packages').next().should('contain', 'kernel'); cy.contains('Affected Systems') .next() - .should('contain', 'test') - .should('contain', 'test2'); + .should('contain', 'vmdrbddev01') + .should('contain', 'vmdrbddev02'); cy.clearSUMASettings(); }); diff --git a/test/trento_web/controllers/v1/suse_manager_controller_test.exs b/test/trento_web/controllers/v1/suse_manager_controller_test.exs index 116fc5593f..c25a39e8a8 100644 --- a/test/trento_web/controllers/v1/suse_manager_controller_test.exs +++ b/test/trento_web/controllers/v1/suse_manager_controller_test.exs @@ -182,6 +182,10 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do affected_systems = build_list(10, :affected_system) + Enum.each(affected_systems, fn %{name: system} -> + insert(:host, hostname: system) + end) + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_systems, 1, fn _ -> {:ok, affected_systems} end) @@ -226,6 +230,58 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do } = result end + test "should filter out non-registered hosts from affected systems", %{ + conn: conn, + api_spec: api_spec + } do + insert_software_updates_settings() + + advisory_name = Faker.Pokemon.name() + + errata_details = build(:errata_details) + + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_errata_details, 1, fn _ -> + {:ok, errata_details} + end) + + cves = build_list(10, :cve) + + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_cves, 1, fn _ -> + {:ok, cves} + end) + + fixes = build(:bugzilla_fix) + + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_bugzilla_fixes, 1, fn _ -> + {:ok, fixes} + end) + + affected_packages = build_list(10, :affected_package) + + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_packages, 1, fn _ -> + {:ok, affected_packages} + end) + + [%{name: first_affected_system} | _] = affected_systems = build_list(10, :affected_system) + + insert(:host, fully_qualified_domain_name: first_affected_system) + + expect(Trento.SoftwareUpdates.Discovery.Mock, :get_affected_systems, 1, fn _ -> + {:ok, affected_systems} + end) + + json = + conn + |> get("/api/v1/software_updates/errata_details/#{advisory_name}") + |> json_response(:ok) + + result = assert_schema(json, "ErrataDetailsResponse", api_spec) + + %{ + affected_systems: [%{name: ^first_affected_system}] + } = result + end + error_scenarios = [ %{ name: "advisory details not found",