diff --git a/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex b/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex index ab122e6897..f185cb365e 100644 --- a/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex +++ b/lib/trento/infrastructure/software_updates/adapter/mock_suma.ex @@ -37,4 +37,6 @@ defmodule Trento.Infrastructure.SoftwareUpdates.MockSuma do update_date: "2024-02-26" } ]} + + def clear, do: :ok end diff --git a/lib/trento/infrastructure/software_updates/suma.ex b/lib/trento/infrastructure/software_updates/suma.ex index dd2041a251..989047f185 100644 --- a/lib/trento/infrastructure/software_updates/suma.ex +++ b/lib/trento/infrastructure/software_updates/suma.ex @@ -34,6 +34,12 @@ defmodule Trento.Infrastructure.SoftwareUpdates.Suma do |> process_identifier |> GenServer.call(:setup) + def clear(server_name \\ @default_name), + do: + server_name + |> process_identifier + |> GenServer.call(:clear) + @impl true def get_system_id(fully_qualified_domain_name, server_name \\ @default_name), do: @@ -59,6 +65,9 @@ defmodule Trento.Infrastructure.SoftwareUpdates.Suma do end end + @impl true + def handle_call(:clear, _, _), do: {:reply, :ok, %State{}} + @impl true def handle_call(request, _, %State{auth: nil} = state), do: authenticate_and_handle(request, state) diff --git a/test/trento/infrastructure/software_updates/suma_test.exs b/test/trento/infrastructure/software_updates/suma_test.exs index 0f250adddc..8958247e62 100644 --- a/test/trento/infrastructure/software_updates/suma_test.exs +++ b/test/trento/infrastructure/software_updates/suma_test.exs @@ -168,6 +168,58 @@ defmodule Trento.Infrastructure.SoftwareUpdates.SumaTest do end end + describe "clearing up integration service" do + test "should clear service state", %{ + settings: %Settings{url: url, username: username, password: password, ca_cert: ca_cert} + } do + {:ok, _} = start_supervised({Suma, @test_integration_name}) + + expect(SumaApiMock, :login, fn _, _, _ -> successful_login_response() end) + + assert :ok = Suma.setup(@test_integration_name) + + expected_state = %State{ + url: url, + username: username, + password: password, + ca_cert: ca_cert, + auth: "pxt-session-cookie=4321" + } + + assert @test_integration_name + |> Suma.identify() + |> :sys.get_state() == expected_state + + assert :ok = Suma.clear(@test_integration_name) + + assert @test_integration_name + |> Suma.identify() + |> :sys.get_state() == %State{} + end + + test "should support clearing an already empty service state" do + {:ok, _} = start_supervised({Suma, @test_integration_name}) + + empty_state = %State{ + url: nil, + username: nil, + password: nil, + ca_cert: nil, + auth: nil + } + + assert @test_integration_name + |> Suma.identify() + |> :sys.get_state() == empty_state + + assert :ok = Suma.clear(@test_integration_name) + + assert @test_integration_name + |> Suma.identify() + |> :sys.get_state() == empty_state + end + end + describe "Integration service" do test "should return an error when a system id was not found for a given fqdn" do {:ok, _} = start_supervised({Suma, @test_integration_name})