From 678b6ce17c3d110f14a87a572c7275469a34b7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Wed, 5 Oct 2022 22:45:20 +0200 Subject: [PATCH] Add more tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- .../web/mastodon_api/views/status_view.ex | 3 +- .../tesla_mock/nominatim_single_result.json | 1 + test/pleroma/notification_test.exs | 67 ++++++++++++++++++- .../mastodon_api/views/status_view_test.exs | 19 ++++++ .../controllers/event_controller_test.exs | 33 ++++++++- test/support/http_request_mock.ex | 14 ++++ 6 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/tesla_mock/nominatim_single_result.json diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 0213ec2c86..c5f7fc8ad4 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -707,7 +707,8 @@ def get_quote(%{data: %{"object" => _object}} = activity, _) do end end - def render_content(%{data: %{"name" => name}} = object) when not is_nil(name) and name != "" do + def render_content(%{data: %{"name" => name, "type" => type}} = object) + when not is_nil(name) and name != "" and type != "Event" do url = object.data["url"] || object.data["id"] "

#{name}

#{object.data["content"]}" diff --git a/test/fixtures/tesla_mock/nominatim_single_result.json b/test/fixtures/tesla_mock/nominatim_single_result.json new file mode 100644 index 0000000000..f966214c1e --- /dev/null +++ b/test/fixtures/tesla_mock/nominatim_single_result.json @@ -0,0 +1 @@ +{"type":"FeatureCollection","geocoding":{"version":"0.1.0","attribution":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","licence":"ODbL","query":"N3726208425,R3726208425,W3726208425"},"features":[{"type":"Feature","properties":{"geocoding":{"place_id":45392146,"osm_type":"node","osm_id":3726208425,"osm_key":"place","osm_value":"village","type":"city","label":"Benis, بخش مرکزی, Shabestar County, East Azerbaijan Province, Iran","name":"Benis","country":"Iran","state":"East Azerbaijan Province","county":"Shabestar County","city":"بخش مرکزی","admin":{"level4":"East Azerbaijan Province","level5":"Shabestar County","level6":"بخش مرکزی"}}},"geometry":{"type":"Point","coordinates":[45.7285348,38.212263]}}]} \ No newline at end of file diff --git a/test/pleroma/notification_test.exs b/test/pleroma/notification_test.exs index 11ad539751..87e59820eb 100644 --- a/test/pleroma/notification_test.exs +++ b/test/pleroma/notification_test.exs @@ -147,8 +147,10 @@ test "creates notification for event join request" do CommonAPI.join(other_user, activity.id) - user_notifications = Notification.for_user(user) + [notification] = user_notifications = Notification.for_user(user) assert length(user_notifications) == 1 + + assert notification.type == "pleroma:participation_request" end test "creates notification for event join approval" do @@ -167,8 +169,10 @@ test "creates notification for event join approval" do CommonAPI.accept_join_request(user, other_user, activity.data["object"]) - user_notifications = Notification.for_user(other_user) + [notification] = user_notifications = Notification.for_user(other_user) assert length(user_notifications) == 1 + + assert notification.type == "pleroma:participation_accepted" end test "doesn't create notification for events without participation approval" do @@ -217,6 +221,65 @@ test "it sends edited notifications to those who repeated a status" do end end + test "creates notifications for edited events for participants" do + user = insert(:user) + other_user = insert(:user) + + {:ok, activity} = + CommonAPI.event(user, %{ + name: "test event", + status: "test evnet", + join_mode: "free", + start_time: DateTime.from_iso8601("2023-01-01T01:00:00.000Z") |> elem(1) + }) + + CommonAPI.join(other_user, activity.id) + + {:ok, _edit_activity} = + CommonAPI.update_event(user, activity, %{ + name: "test event", + status: "test event", + join_mode: "free", + start_time: DateTime.from_iso8601("2023-01-01T01:00:00.000Z") |> elem(1) + }) + + Pleroma.Tests.ObanHelpers.perform_all() + + [notification] = user_notifications = Notification.for_user(other_user) + assert length(user_notifications) == 1 + + assert notification.type == "pleroma:event_update" + end + + test "doesn't create multiple edit notifications for events" do + user = insert(:user) + other_user = insert(:user) + + {:ok, activity} = + CommonAPI.event(user, %{ + name: "test event", + status: "test evnet", + join_mode: "free", + start_time: DateTime.from_iso8601("2023-01-01T01:00:00.000Z") |> elem(1) + }) + + CommonAPI.join(other_user, activity.id) + CommonAPI.repeat(activity.id, other_user) + + {:ok, _edit_activity} = + CommonAPI.update_event(user, activity, %{ + name: "test event", + status: "test event", + join_mode: "free", + start_time: DateTime.from_iso8601("2023-01-01T01:00:00.000Z") |> elem(1) + }) + + Pleroma.Tests.ObanHelpers.perform_all() + + user_notifications = Notification.for_user(other_user) + assert length(user_notifications) == 1 + end + test "create_poll_notifications/1" do [user1, user2, user3, _, _] = insert_list(5, :user) question = insert(:question, user: user1) diff --git a/test/pleroma/web/mastodon_api/views/status_view_test.exs b/test/pleroma/web/mastodon_api/views/status_view_test.exs index b10ffdb6e9..e13dc51f83 100644 --- a/test/pleroma/web/mastodon_api/views/status_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/status_view_test.exs @@ -813,6 +813,25 @@ test "with a source object" do assert status.text == "object source" end + test "it shows an event" do + event = insert(:event) + + activity = insert(:event_activity, event: event) + + status = StatusView.render("show.json", activity: activity) + + assert status.pleroma.event == %{ + name: event.data["name"], + start_time: event.data["startTime"], + end_time: event.data["endTime"], + join_mode: event.data["joinMode"], + participants_count: nil, + location: nil, + join_state: nil, + participation_request_count: nil + } + end + describe "source.json" do test "with a source object, renders both source and content type" do note = diff --git a/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs index 58576c0282..f2a648c0f4 100644 --- a/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/event_controller_test.exs @@ -55,6 +55,37 @@ test "can't create event that ends before its start", %{conn: conn} do "error" => "Event can't end before its start" } end + + test "assigns location from location id", %{conn: conn} do + Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) + + conn = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/pleroma/events", %{ + "name" => "Event name", + "start_time" => "2023-01-01T01:00:00.000Z", + "end_time" => "2023-01-01T04:00:00.000Z", + "join_mode" => "free", + "location_id" => "3726208425" + }) + + assert %{ + "pleroma" => %{ + "event" => %{ + "location" => %{ + "name" => "Benis", + "longitude" => 45.7285348, + "latitude" => 38.212263, + "street" => " ", + "locality" => "بخش مرکزی", + "region" => "East Azerbaijan Province", + "country" => "Iran" + } + } + } + } = json_response_and_validate_schema(conn, 200) + end end test "GET /api/v1/pleroma/events/:id/participations" do @@ -276,7 +307,7 @@ test "accepts a participation request", %{user: user, conn: conn} do Utils.get_existing_join(other_user.ap_id, activity.data["object"]) end - test "it refuses to accept a request when event is not by the user", %{user: user, conn: conn} do + test "it refuses to accept a request when event is not by the user", %{conn: conn} do [second_user, third_user] = insert_pair(:user) {:ok, activity} = diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 646e680b13..fb9ad9c75a 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -1496,6 +1496,20 @@ def get( }} end + def get( + "https://nominatim.openstreetmap.org/lookup?format=geocodejson&osm_ids=N3726208425,R3726208425,W3726208425&accept-language=en&addressdetails=1&namedetails=1", + _, + _, + _ + ) do + {:ok, + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/tesla_mock/nominatim_single_result.json"), + headers: [{"content-type", "application/json"}] + }} + end + def get(url, query, body, headers) do {:error, "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{inspect(headers)}"}