From bb33c29c44f7cd8cbdc3c6e0fa4aa87055b2e8e1 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 31 Mar 2024 11:57:21 -0400 Subject: [PATCH] Allow the time to be mocked in tests --- config/test.exs | 4 +++- lib/chronoscope/nts/certificate.ex | 6 +++++- test/chronoscope/nts/certificate_test.exs | 20 +++++++++++++++++++- test/support/mocks.ex | 5 +++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/config/test.exs b/config/test.exs index 2b2f47e..400e1fa 100644 --- a/config/test.exs +++ b/config/test.exs @@ -19,4 +19,6 @@ config :logger, level: :warning # Initialize plugs at runtime for faster test compilation config :phoenix, :plug_init_mode, :runtime -config :chronoscope, Chronoscope.NTS, behaviour: Chronoscope.NTS.BehaviourMock +config :chronoscope, Chronoscope.NTS, + behaviour: Chronoscope.NTS.BehaviourMock, + datetime: Chronoscope.NTS.DateTimeMock diff --git a/lib/chronoscope/nts/certificate.ex b/lib/chronoscope/nts/certificate.ex index fae7a48..31000f2 100644 --- a/lib/chronoscope/nts/certificate.ex +++ b/lib/chronoscope/nts/certificate.ex @@ -20,7 +20,7 @@ defmodule Chronoscope.NTS.Certificate do defp short_year_to_full_year(short_year) do {century, current_year} = - DateTime.utc_now().year + datetime().utc_now().year |> to_string() |> String.split_at(-2) @@ -32,4 +32,8 @@ defmodule Chronoscope.NTS.Certificate do |> then(&"#{&1 + 1}#{short_year}") end end + + defp datetime() do + Application.get_env(:chronoscope, Chronoscope.NTS)[:datetime] || DateTime + end end diff --git a/test/chronoscope/nts/certificate_test.exs b/test/chronoscope/nts/certificate_test.exs index ea63d7c..e78cfb9 100644 --- a/test/chronoscope/nts/certificate_test.exs +++ b/test/chronoscope/nts/certificate_test.exs @@ -2,8 +2,14 @@ defmodule Chronoscope.NTS.CertificateTest do use ExUnit.Case import Chronoscope.NTS.Certificate + import Mox + + setup :verify_on_exit! test "parses the expiration date of a certificate" do + Chronoscope.NTS.DateTimeMock + |> stub(:utc_now, &DateTime.utc_now/0) + {:ok, expiration, _} = :secp256r1 |> X509.PrivateKey.new_ec() @@ -15,12 +21,24 @@ defmodule Chronoscope.NTS.CertificateTest do assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12 end - # todo - mock current time test "converts certificate datetime to iso8601" do + Chronoscope.NTS.DateTimeMock + |> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end) + assert cert_time_to_iso8601("240326110000Z") == "2024-03-26T11:00:00Z" end test "handles century rollover" do + Chronoscope.NTS.DateTimeMock + |> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end) + assert cert_time_to_iso8601("010326110000Z") == "2101-03-26T11:00:00Z" end + + test "handles millenium rollover" do + Chronoscope.NTS.DateTimeMock + |> expect(:utc_now, fn -> ~U[2999-03-31 01:23:45Z] end) + + assert cert_time_to_iso8601("010326110000Z") == "3001-03-26T11:00:00Z" + end end diff --git a/test/support/mocks.ex b/test/support/mocks.ex index e3911a0..1730f51 100644 --- a/test/support/mocks.ex +++ b/test/support/mocks.ex @@ -1 +1,6 @@ +defmodule DateTime.Behaviour do + @callback utc_now :: DateTime.t() +end + Mox.defmock(Chronoscope.NTS.BehaviourMock, for: Chronoscope.NTS.Behaviour) +Mox.defmock(Chronoscope.NTS.DateTimeMock, for: DateTime.Behaviour)