From 548ef87bc48c2cd60baa205525288a0a859d94a6 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Fri, 26 Jul 2024 09:29:42 -0400 Subject: [PATCH] Add tests --- config/test.exs | 3 ++- lib/chronoscope/nts/client.ex | 8 ++++---- lib/chronoscope_web/live/index_live.ex | 2 +- test/chronoscope/nts/client_test.exs | 10 ++++++++++ test/support/behaviours.ex | 4 ++++ test/support/mocks.ex | 1 + 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/config/test.exs b/config/test.exs index f997ecd..1f482af 100644 --- a/config/test.exs +++ b/config/test.exs @@ -28,4 +28,5 @@ config :chronoscope, registry: Chronoscope.RegistryMock, gen_server: Chronoscope.GenServerMock, nts: Chronoscope.NTSMock, - gemini: Chronoscope.GeminiMock + gemini: Chronoscope.GeminiMock, + endpoint: Chronoscope.EndpointMock diff --git a/lib/chronoscope/nts/client.ex b/lib/chronoscope/nts/client.ex index bd4ba3a..a1083c3 100644 --- a/lib/chronoscope/nts/client.ex +++ b/lib/chronoscope/nts/client.ex @@ -3,7 +3,6 @@ defmodule Chronoscope.NTS.Client do alias Chronoscope.NTS alias Chronoscope.NTS.KeyEstablishmentClient - alias ChronoscopeWeb.Endpoint @interval_in_seconds 30 @timeout_in_milliseconds 10_000 @@ -11,6 +10,7 @@ defmodule Chronoscope.NTS.Client do @topic Application.compile_env(:chronoscope, :nts_topic) @date_time Application.compile_env(:chronoscope, :date_time, DateTime) + @endpoint Application.compile_env(:chronoscope, :endpoint, ChronoscopeWeb.Endpoint) def start_link(server: server, name: name) do GenServer.start_link(__MODULE__, server, name: name) @@ -18,7 +18,7 @@ defmodule Chronoscope.NTS.Client do @impl true def init(server) do - Endpoint.broadcast(@topic, "initializing", server) + @endpoint.broadcast(@topic, "initializing", server) {:ok, %{ @@ -54,7 +54,7 @@ defmodule Chronoscope.NTS.Client do @impl true def handle_call(:cancel_auto_refresh, _from, %{timer: timer} = state) do :timer.cancel(timer) - Endpoint.broadcast(@topic, "cancel-auto-refresh", state.server) + @endpoint.broadcast(@topic, "cancel-auto-refresh", state.server) {:reply, :ok, Map.delete(state, :timer)} end @@ -82,7 +82,7 @@ defmodule Chronoscope.NTS.Client do if interval_surpassed?(now, state.last_key_establishment) do state |> Map.merge(current_data(state, now)) - |> tap(&Endpoint.broadcast(@topic, "key-exchange", Map.delete(&1, :timer))) + |> tap(&@endpoint.broadcast(@topic, "key-establishment", Map.delete(&1, :timer))) else state end diff --git a/lib/chronoscope_web/live/index_live.ex b/lib/chronoscope_web/live/index_live.ex index 74e4f7d..685b16d 100644 --- a/lib/chronoscope_web/live/index_live.ex +++ b/lib/chronoscope_web/live/index_live.ex @@ -15,7 +15,7 @@ defmodule ChronoscopeWeb.IndexLive do end @impl true - def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do + def handle_info(%{topic: @topic, event: "key-establishment", payload: client}, socket) do if client.server in socket.assigns.servers do {:noreply, update(socket, :clients, &update_client(&1, client))} else diff --git a/test/chronoscope/nts/client_test.exs b/test/chronoscope/nts/client_test.exs index b478248..94a870c 100644 --- a/test/chronoscope/nts/client_test.exs +++ b/test/chronoscope/nts/client_test.exs @@ -3,6 +3,7 @@ defmodule Chronoscope.NTS.ClientTest do alias Chronoscope.Certificate alias Chronoscope.DateTimeMock + alias Chronoscope.EndpointMock alias Chronoscope.NTS.Client alias Chronoscope.SSLMock @@ -19,6 +20,9 @@ defmodule Chronoscope.NTS.ClientTest do describe "Chronoscope.NTS.Client.init()" do test "initializes successfully" do + EndpointMock + |> expect(:broadcast, fn "nts-servers", "initializing", %{host: "localhost", port: 3333} -> :ok end) + assert Client.init(%{host: "localhost", port: 3333}) == {:ok, %{ @@ -48,6 +52,9 @@ defmodule Chronoscope.NTS.ClientTest do end test ":cancel_auto_refresh" do + EndpointMock + |> expect(:broadcast, fn "nts-servers", "cancel-auto-refresh", _ -> :ok end) + assert Client.handle_call(:cancel_auto_refresh, nil, %{server: true, timer: true}) == {:reply, :ok, %{server: true}} end @@ -79,6 +86,9 @@ defmodule Chronoscope.NTS.ClientTest do |> expect(:peercert, fn :socket -> {:ok, peercert} end) |> expect(:close, fn :socket -> :ok end) + EndpointMock + |> expect(:broadcast, fn "nts-servers", "key-establishment", _ -> :ok end) + assert {:reply, {:ok, %{cert_expiration: ^peercert_expiration}}, %{ server: %{host: "localhost", port: 3333}, diff --git a/test/support/behaviours.ex b/test/support/behaviours.ex index 4856f5a..b3e1e65 100644 --- a/test/support/behaviours.ex +++ b/test/support/behaviours.ex @@ -22,3 +22,7 @@ defmodule Chronoscope.GenServer.Behaviour do @callback call(pid(), any(), any()) :: any() @callback call(pid(), any()) :: any() end + +defmodule ChronoscopeWeb.Endpoint.Behaviour do + @callback broadcast(any(), any(), any()) :: any() +end diff --git a/test/support/mocks.ex b/test/support/mocks.ex index dcc3552..8a56fdf 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -5,3 +5,4 @@ Mox.defmock(Chronoscope.DynamicSupervisorMock, for: Chronoscope.DynamicSuperviso Mox.defmock(Chronoscope.GenServerMock, for: Chronoscope.GenServer.Behaviour) Mox.defmock(Chronoscope.NTSMock, for: Chronoscope.NTS.Behaviour) Mox.defmock(Chronoscope.GeminiMock, for: Chronoscope.Gemini.Behaviour) +Mox.defmock(Chronoscope.EndpointMock, for: ChronoscopeWeb.Endpoint.Behaviour)