Add tests

This commit is contained in:
Mike Cifelli 2024-07-26 09:29:42 -04:00
parent bb05e5fad8
commit 548ef87bc4
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
6 changed files with 22 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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},

View File

@ -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

View File

@ -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)