74441d1e816145c50ebd472e5afa3a4caafce327
[anni] / test / pleroma / ecto_type / activity_pub / object_validators / date_time_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.DateTimeTest do
6   alias Pleroma.EctoType.ActivityPub.ObjectValidators.DateTime
7   use Pleroma.DataCase, async: true
8
9   test "it validates an xsd:Datetime" do
10     valid_strings = [
11       "2004-04-12T13:20:00",
12       "2004-04-12T13:20:15.5",
13       "2004-04-12T13:20:00-05:00",
14       "2004-04-12T13:20:00Z"
15     ]
16
17     invalid_strings = [
18       "2004-04-12T13:00",
19       "2004-04-1213:20:00",
20       "99-04-12T13:00",
21       "2004-04-12"
22     ]
23
24     assert {:ok, "2004-04-01T12:00:00Z"} == DateTime.cast("2004-04-01T12:00:00Z")
25
26     Enum.each(valid_strings, fn date_time ->
27       result = DateTime.cast(date_time)
28       assert {:ok, _} = result
29     end)
30
31     Enum.each(invalid_strings, fn date_time ->
32       result = DateTime.cast(date_time)
33       assert :error == result
34     end)
35   end
36 end