chronoscope/test/chronoscope/certificate_test.exs

51 lines
1.5 KiB
Elixir

defmodule Chronoscope.CertificateTest do
use Chronoscope.Case, async: true
alias Chronoscope.Certificate
alias Chronoscope.DateTimeMock
import Mox
setup :verify_on_exit!
describe "Chronoscope.Certificate.expiration_date()" do
test "parses the expiration date of a certificate" do
{:ok, expiration, _} =
:secp256r1
|> X509.PrivateKey.new_ec()
|> X509.Certificate.self_signed("/C=US/ST=CA/L=San Francisco/O=Acme/CN=Test", validity: 12)
|> X509.Certificate.to_der()
|> Certificate.expiration_date()
|> DateTime.from_iso8601()
expiration_time_in_minutes = DateTime.diff(expiration, DateTime.utc_now(), :minute)
assert expiration_time_in_minutes > 12 * 24 * 60 - 5
assert expiration_time_in_minutes < 12 * 24 * 60 + 5
end
end
describe "Chronoscope.Certificate.cert_time_to_iso8601()" do
test "converts certificate datetime to iso8601" do
DateTimeMock
|> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end)
assert Certificate.cert_time_to_iso8601("240326110000Z") == "2024-03-26T11:00:00Z"
end
test "handles century rollover" do
DateTimeMock
|> expect(:utc_now, fn -> ~U[2024-03-31 01:23:45Z] end)
assert Certificate.cert_time_to_iso8601("010326110000Z") == "2101-03-26T11:00:00Z"
end
test "handles millenium rollover" do
DateTimeMock
|> expect(:utc_now, fn -> ~U[2999-03-31 01:23:45Z] end)
assert Certificate.cert_time_to_iso8601("010326110000Z") == "3001-03-26T11:00:00Z"
end
end
end