From 0c78ab4a88d59358a0a5e24a76cbb4cdb2c2d402 Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Fri, 18 Mar 2022 09:36:13 -0400 Subject: [PATCH] Use utc_datetime in db schema --- lib/pleroma/announcement.ex | 8 ++-- .../announcement_controller_test.exs | 47 +++++++++++++++++++ .../announcement_controller_test.exs | 24 ++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/announcement.ex b/lib/pleroma/announcement.ex index ad372629d..d97c5e728 100644 --- a/lib/pleroma/announcement.ex +++ b/lib/pleroma/announcement.ex @@ -16,11 +16,11 @@ defmodule Pleroma.Announcement do schema "announcements" do field(:data, :map) - field(:starts_at, :naive_datetime) - field(:ends_at, :naive_datetime) + field(:starts_at, :utc_datetime) + field(:ends_at, :utc_datetime) field(:rendered, :map) - timestamps() + timestamps(type: :utc_datetime) end def change(struct, params \\ %{}) do @@ -155,6 +155,6 @@ defmodule Pleroma.Announcement do end def list_all_visible do - list_all_visible_when(NaiveDateTime.utc_now()) + list_all_visible_when(DateTime.now("Etc/UTC") |> elem(1)) end end diff --git a/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs b/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs index 56da1c6aa..5b8148c05 100644 --- a/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/announcement_controller_test.exs @@ -156,6 +156,25 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do assert NaiveDateTime.compare(new.starts_at, starts_at) == :eq end + test "it updates with time with utc timezone", %{conn: conn} do + %{id: id} = insert(:announcement) + + now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second) + starts_at = DateTime.add(now, -10, :second) + + _response = + conn + |> put_req_header("content-type", "application/json") + |> patch("/api/v1/pleroma/admin/announcements/#{id}", %{ + starts_at: DateTime.to_iso8601(starts_at) + }) + |> json_response_and_validate_schema(:ok) + + new = Pleroma.Announcement.get_by_id(id) + + assert DateTime.compare(new.starts_at, starts_at) == :eq + end + test "it updates a data field", %{conn: conn} do %{id: id} = announcement = insert(:announcement, data: %{"all_day" => true}) @@ -230,5 +249,33 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementControllerTest do assert NaiveDateTime.compare(announcement.starts_at, starts_at) == :eq assert NaiveDateTime.compare(announcement.ends_at, ends_at) == :eq end + + test "creating with time with utc timezones", %{conn: conn} do + content = "test post announcement api" + + now = DateTime.now("Etc/UTC") |> elem(1) |> DateTime.truncate(:second) + starts_at = DateTime.add(now, -10, :second) + ends_at = DateTime.add(now, 10, :second) + + response = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/pleroma/admin/announcements", %{ + "content" => content, + "starts_at" => DateTime.to_iso8601(starts_at), + "ends_at" => DateTime.to_iso8601(ends_at), + "all_day" => true + }) + |> json_response_and_validate_schema(:ok) + + assert %{"content" => ^content, "all_day" => true} = response + + announcement = Pleroma.Announcement.get_by_id(response["id"]) + + assert not is_nil(announcement) + + assert DateTime.compare(announcement.starts_at, starts_at) == :eq + assert DateTime.compare(announcement.ends_at, ends_at) == :eq + end end end diff --git a/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs index 1c748064c..3957cc3ed 100644 --- a/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/announcement_controller_test.exs @@ -23,6 +23,30 @@ defmodule Pleroma.Web.MastodonAPI.AnnouncementControllerTest do refute Map.has_key?(Enum.at(response, 0), "read") end + test "it returns time with utc timezone" do + start_time = + NaiveDateTime.utc_now() + |> NaiveDateTime.add(-999_999, :second) + |> NaiveDateTime.truncate(:second) + + end_time = + NaiveDateTime.utc_now() + |> NaiveDateTime.add(999_999, :second) + |> NaiveDateTime.truncate(:second) + + %{id: id} = insert(:announcement, %{starts_at: start_time, ends_at: end_time}) + + response = + build_conn() + |> get("/api/v1/announcements") + |> json_response_and_validate_schema(:ok) + + assert [%{"id" => ^id}] = [announcement] = response + + assert String.ends_with?(announcement["starts_at"], "Z") + assert String.ends_with?(announcement["ends_at"], "Z") + end + test "it does not list announcements starting after current time" do time = NaiveDateTime.utc_now() |> NaiveDateTime.add(999_999, :second) insert(:announcement, starts_at: time)