Allow the time to be mocked in tests
This commit is contained in:
parent
2abffa1120
commit
bb33c29c44
|
@ -19,4 +19,6 @@ config :logger, level: :warning
|
||||||
# Initialize plugs at runtime for faster test compilation
|
# Initialize plugs at runtime for faster test compilation
|
||||||
config :phoenix, :plug_init_mode, :runtime
|
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
|
||||||
|
|
|
@ -20,7 +20,7 @@ defmodule Chronoscope.NTS.Certificate do
|
||||||
|
|
||||||
defp short_year_to_full_year(short_year) do
|
defp short_year_to_full_year(short_year) do
|
||||||
{century, current_year} =
|
{century, current_year} =
|
||||||
DateTime.utc_now().year
|
datetime().utc_now().year
|
||||||
|> to_string()
|
|> to_string()
|
||||||
|> String.split_at(-2)
|
|> String.split_at(-2)
|
||||||
|
|
||||||
|
@ -32,4 +32,8 @@ defmodule Chronoscope.NTS.Certificate do
|
||||||
|> then(&"#{&1 + 1}#{short_year}")
|
|> then(&"#{&1 + 1}#{short_year}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp datetime() do
|
||||||
|
Application.get_env(:chronoscope, Chronoscope.NTS)[:datetime] || DateTime
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,8 +2,14 @@ defmodule Chronoscope.NTS.CertificateTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
|
|
||||||
import Chronoscope.NTS.Certificate
|
import Chronoscope.NTS.Certificate
|
||||||
|
import Mox
|
||||||
|
|
||||||
|
setup :verify_on_exit!
|
||||||
|
|
||||||
test "parses the expiration date of a certificate" do
|
test "parses the expiration date of a certificate" do
|
||||||
|
Chronoscope.NTS.DateTimeMock
|
||||||
|
|> stub(:utc_now, &DateTime.utc_now/0)
|
||||||
|
|
||||||
{:ok, expiration, _} =
|
{:ok, expiration, _} =
|
||||||
:secp256r1
|
:secp256r1
|
||||||
|> X509.PrivateKey.new_ec()
|
|> X509.PrivateKey.new_ec()
|
||||||
|
@ -15,12 +21,24 @@ defmodule Chronoscope.NTS.CertificateTest do
|
||||||
assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12
|
assert DateTime.diff(expiration, DateTime.utc_now(), :day) == 12
|
||||||
end
|
end
|
||||||
|
|
||||||
# todo - mock current time
|
|
||||||
test "converts certificate datetime to iso8601" do
|
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"
|
assert cert_time_to_iso8601("240326110000Z") == "2024-03-26T11:00:00Z"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "handles century rollover" do
|
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"
|
assert cert_time_to_iso8601("010326110000Z") == "2101-03-26T11:00:00Z"
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -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.BehaviourMock, for: Chronoscope.NTS.Behaviour)
|
||||||
|
Mox.defmock(Chronoscope.NTS.DateTimeMock, for: DateTime.Behaviour)
|
||||||
|
|
Loading…
Reference in New Issue