Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Software Updates service handle no SUMA settings saved #2457

Merged
merged 2 commits into from
Mar 22, 2024
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: 3 additions & 2 deletions lib/trento/software_updates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,15 @@ defmodule Trento.SoftwareUpdates do

@spec get_software_updates(Ecto.UUID.t()) ::
{:ok, map()}
| {:error,
| {:error, :settings_not_configured,
:system_id_not_found
| :not_found
| :fqdn_not_found
| :error_getting_patches
| :error_getting_packages}
def get_software_updates(host_id) do
with {:ok, fqdn} <- get_host_fqdn(host_id),
with {:ok, _} <- get_settings(),
{:ok, fqdn} <- get_host_fqdn(host_id),
{:ok, system_id} <- Discovery.get_system_id(fqdn),
{:ok, relevant_patches} <- Discovery.get_relevant_patches(system_id),
{:ok, upgradable_packages} <-
Expand Down
8 changes: 7 additions & 1 deletion lib/trento_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ defmodule TrentoWeb.FallbackController do
|> render(:"401", reason: "Invalid credentials.")
end

def call(conn, {:error, :settings_not_configured}) do
conn
|> put_status(:unauthorized)
|> put_view(ErrorView)
|> render(:"401", reason: "SUSE Manager settings not configured.")
end

def call(conn, {:error, :invalid_refresh_token}) do
conn
|> put_status(:unauthorized)
Expand All @@ -32,7 +39,6 @@ defmodule TrentoWeb.FallbackController do
:database_not_registered,
:application_instance_not_registered,
:database_instance_not_registered,
:settings_not_configured,
:api_key_settings_missing
] do
conn
Expand Down
8 changes: 6 additions & 2 deletions test/trento/software_updates_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -456,20 +456,22 @@ defmodule Trento.SoftwareUpdates.SettingsTest do

describe "getting software updates" do
test "successfully returns software updates" do
insert_software_updates_settings()
%{id: host_id} = insert(:host)

assert {:ok, %{relevant_patches: [_, _], upgradable_packages: [_, _]}} =
SoftwareUpdates.get_software_updates(host_id)
end

test "handles non existing hosts" do
insert_software_updates_settings()
host_id = Faker.UUID.v4()

assert {:error, :not_found} =
SoftwareUpdates.get_software_updates(host_id)
assert {:error, :not_found} = SoftwareUpdates.get_software_updates(host_id)
end

test "returns errors when fetching upgradable packages" do
insert_software_updates_settings()
%{id: host_id} = insert(:host)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_upgradable_packages, 1, fn _ ->
Expand All @@ -480,6 +482,7 @@ defmodule Trento.SoftwareUpdates.SettingsTest do
end

test "returns errors when fetching relevant_patches" do
insert_software_updates_settings()
%{id: host_id} = insert(:host)

expect(Trento.SoftwareUpdates.Discovery.Mock, :get_relevant_patches, 1, fn _ ->
Expand All @@ -490,6 +493,7 @@ defmodule Trento.SoftwareUpdates.SettingsTest do
end

test "errors when non existing fqdns are attempted" do
insert_software_updates_settings()
%{id: host_id} = insert(:host, fully_qualified_domain_name: nil)

assert {:error, :fqdn_not_found} = SoftwareUpdates.get_software_updates(host_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ defmodule TrentoWeb.V1.SUMACredentialsControllerTest do
|> assert_schema("SUMACredentials", api_spec)
end

test "should return 404 if no user settings have been saved", %{conn: conn} do
test "should return 401 if no user settings have been saved", %{conn: conn} do
api_spec = ApiSpec.spec()

conn
|> get("/api/v1/settings/suma_credentials")
|> json_response(:not_found)
|> json_response(:unauthorized)
|> assert_schema("NotFound", api_spec)
end
end
Expand Down Expand Up @@ -177,11 +177,11 @@ defmodule TrentoWeb.V1.SUMACredentialsControllerTest do
conn
|> put_req_header("content-type", "application/json")
|> patch("/api/v1/settings/suma_credentials", submission)
|> json_response(:not_found)
|> json_response(:unauthorized)

assert %{
"errors" => [
%{"detail" => "The requested resource cannot be found.", "title" => "Not Found"}
%{"detail" => "SUSE Manager settings not configured.", "title" => "Unauthorized"}
]
} == resp
end
Expand Down
18 changes: 18 additions & 0 deletions test/trento_web/controllers/v1/suse_manager_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do
conn: conn,
api_spec: api_spec
} do
insert_software_updates_settings()
%{id: host_id} = insert(:host)

%AvailableSoftwareUpdatesResponse{
Expand All @@ -48,10 +49,26 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do
|> assert_schema("AvailableSoftwareUpdatesResponse", api_spec)
end

test "should return 401 when no settings have been saved", %{conn: conn} do
%{id: host_id} = insert(:host)

resp =
conn
|> get("/api/v1/hosts/#{host_id}/software_updates")
|> json_response(:unauthorized)

assert %{
"errors" => [
%{"detail" => "SUSE Manager settings not configured.", "title" => "Unauthorized"}
]
} == resp
end

test "should return 404 when a host is not found", %{
conn: conn,
api_spec: api_spec
} do
insert_software_updates_settings()
host_id = Faker.UUID.v4()

conn
Expand All @@ -64,6 +81,7 @@ defmodule TrentoWeb.V1.SUSEManagerControllerTest do
conn: conn,
api_spec: api_spec
} do
insert_software_updates_settings()
%{id: host_id} = insert(:host, fully_qualified_domain_name: nil)

conn
Expand Down
Loading