diff options
Diffstat (limited to 'test/web')
| -rw-r--r-- | test/web/activity_pub/activity_pub_test.exs | 17 | ||||
| -rw-r--r-- | test/web/admin_api/admin_api_controller_test.exs | 144 | ||||
| -rw-r--r-- | test/web/common_api/common_api_test.exs | 44 | ||||
| -rw-r--r-- | test/web/common_api/common_api_utils_test.exs | 6 | ||||
| -rw-r--r-- | test/web/mastodon_api/controllers/account_controller_test.exs | 10 | ||||
| -rw-r--r-- | test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs | 46 | ||||
| -rw-r--r-- | test/web/mastodon_api/controllers/status_controller_test.exs | 50 | ||||
| -rw-r--r-- | test/web/mastodon_api/views/status_view_test.exs | 12 | ||||
| -rw-r--r-- | test/web/node_info_test.exs | 1 | ||||
| -rw-r--r-- | test/web/pleroma_api/controllers/pleroma_api_controller_test.exs | 24 | ||||
| -rw-r--r-- | test/web/rich_media/parsers/twitter_card_test.exs | 22 | ||||
| -rw-r--r-- | test/web/twitter_api/twitter_api_controller_test.exs | 142 | 
12 files changed, 453 insertions, 65 deletions
| diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index ea6e79b44..ce68e7d0e 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1174,6 +1174,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        assert embedded_object["object"] == followed.ap_id        assert embedded_object["id"] == follow_activity.data["id"]      end + +    test "creates an undo activity for a pending follow request" do +      follower = insert(:user) +      followed = insert(:user, %{locked: true}) + +      {:ok, follow_activity} = ActivityPub.follow(follower, followed) +      {:ok, activity} = ActivityPub.unfollow(follower, followed) + +      assert activity.data["type"] == "Undo" +      assert activity.data["actor"] == follower.ap_id + +      embedded_object = activity.data["object"] +      assert is_map(embedded_object) +      assert embedded_object["type"] == "Follow" +      assert embedded_object["object"] == followed.ap_id +      assert embedded_object["id"] == follow_activity.data["id"] +    end    end    describe "blocking / unblocking" do diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index 5fbdf96f6..60db58144 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -6,7 +6,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    use Pleroma.Web.ConnCase    use Oban.Testing, repo: Pleroma.Repo +  import Pleroma.Factory +  import ExUnit.CaptureLog +    alias Pleroma.Activity +  alias Pleroma.Config    alias Pleroma.ConfigDB    alias Pleroma.HTML    alias Pleroma.ModerationLog @@ -19,7 +23,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.MastodonAPI.StatusView    alias Pleroma.Web.MediaProxy -  import Pleroma.Factory    setup_all do      Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) @@ -41,7 +44,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "with [:auth, :enforce_oauth_admin_scope_usage]," do      clear_config([:auth, :enforce_oauth_admin_scope_usage]) do -      Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], true) +      Config.put([:auth, :enforce_oauth_admin_scope_usage], true)      end      test "GET /api/pleroma/admin/users/:nickname requires admin:read:accounts or broader scope", @@ -91,7 +94,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "unless [:auth, :enforce_oauth_admin_scope_usage]," do      clear_config([:auth, :enforce_oauth_admin_scope_usage]) do -      Pleroma.Config.put([:auth, :enforce_oauth_admin_scope_usage], false) +      Config.put([:auth, :enforce_oauth_admin_scope_usage], false)      end      test "GET /api/pleroma/admin/users/:nickname requires " <> @@ -579,11 +582,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "POST /api/pleroma/admin/email_invite, with valid config" do      clear_config([:instance, :registrations_open]) do -      Pleroma.Config.put([:instance, :registrations_open], false) +      Config.put([:instance, :registrations_open], false)      end      clear_config([:instance, :invites_enabled]) do -      Pleroma.Config.put([:instance, :invites_enabled], true) +      Config.put([:instance, :invites_enabled], true)      end      test "sends invitation and returns 204", %{admin: admin, conn: conn} do @@ -602,8 +605,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        assert token_record        refute token_record.used -      notify_email = Pleroma.Config.get([:instance, :notify_email]) -      instance_name = Pleroma.Config.get([:instance, :name]) +      notify_email = Config.get([:instance, :notify_email]) +      instance_name = Config.get([:instance, :name])        email =          Pleroma.Emails.UserEmail.user_invitation_email( @@ -639,8 +642,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      clear_config([:instance, :invites_enabled])      test "it returns 500 if `invites_enabled` is not enabled", %{conn: conn} do -      Pleroma.Config.put([:instance, :registrations_open], false) -      Pleroma.Config.put([:instance, :invites_enabled], false) +      Config.put([:instance, :registrations_open], false) +      Config.put([:instance, :invites_enabled], false)        conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD") @@ -648,8 +651,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end      test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do -      Pleroma.Config.put([:instance, :registrations_open], true) -      Pleroma.Config.put([:instance, :invites_enabled], true) +      Config.put([:instance, :registrations_open], true) +      Config.put([:instance, :invites_enabled], true)        conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD") @@ -1886,13 +1889,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "GET /api/pleroma/admin/config" do      clear_config(:configurable_from_database) do -      Pleroma.Config.put(:configurable_from_database, true) +      Config.put(:configurable_from_database, true)      end      test "when configuration from database is off", %{conn: conn} do -      initial = Pleroma.Config.get(:configurable_from_database) -      Pleroma.Config.put(:configurable_from_database, false) -      on_exit(fn -> Pleroma.Config.put(:configurable_from_database, initial) end) +      initial = Config.get(:configurable_from_database) +      Config.put(:configurable_from_database, false) +      on_exit(fn -> Config.put(:configurable_from_database, initial) end)        conn = get(conn, "/api/pleroma/admin/config")        assert json_response(conn, 400) == @@ -2036,11 +2039,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          Application.delete_env(:pleroma, Pleroma.Captcha.NotReal)          Application.put_env(:pleroma, :http, http)          Application.put_env(:tesla, :adapter, Tesla.Mock) +        Restarter.Pleroma.refresh()        end)      end      clear_config(:configurable_from_database) do -      Pleroma.Config.put(:configurable_from_database, true) +      Config.put(:configurable_from_database, true)      end      @tag capture_log: true @@ -2249,21 +2253,63 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      end      test "saving config which need pleroma reboot", %{conn: conn} do -      chat = Pleroma.Config.get(:chat) -      on_exit(fn -> Pleroma.Config.put(:chat, chat) end) +      chat = Config.get(:chat) +      on_exit(fn -> Config.put(:chat, chat) end) -      conn = -        post( -          conn, -          "/api/pleroma/admin/config", -          %{ -            configs: [ -              %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} -            ] -          } -        ) +      assert post( +               conn, +               "/api/pleroma/admin/config", +               %{ +                 configs: [ +                   %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} +                 ] +               } +             ) +             |> json_response(200) == %{ +               "configs" => [ +                 %{ +                   "db" => [":enabled"], +                   "group" => ":pleroma", +                   "key" => ":chat", +                   "value" => [%{"tuple" => [":enabled", true]}] +                 } +               ], +               "need_reboot" => true +             } -      assert json_response(conn, 200) == %{ +      configs = +        conn +        |> get("/api/pleroma/admin/config") +        |> json_response(200) + +      assert configs["need_reboot"] + +      capture_log(fn -> +        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} +      end) =~ "pleroma restarted" + +      configs = +        conn +        |> get("/api/pleroma/admin/config") +        |> json_response(200) + +      refute Map.has_key?(configs, "need_reboot") +    end + +    test "update setting which need reboot, don't change reboot flag until reboot", %{conn: conn} do +      chat = Config.get(:chat) +      on_exit(fn -> Config.put(:chat, chat) end) + +      assert post( +               conn, +               "/api/pleroma/admin/config", +               %{ +                 configs: [ +                   %{group: ":pleroma", key: ":chat", value: [%{"tuple" => [":enabled", true]}]} +                 ] +               } +             ) +             |> json_response(200) == %{                 "configs" => [                   %{                     "db" => [":enabled"], @@ -2274,6 +2320,36 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 ],                 "need_reboot" => true               } + +      assert post(conn, "/api/pleroma/admin/config", %{ +               configs: [ +                 %{group: ":pleroma", key: ":key1", value: [%{"tuple" => [":key3", 3]}]} +               ] +             }) +             |> json_response(200) == %{ +               "configs" => [ +                 %{ +                   "group" => ":pleroma", +                   "key" => ":key1", +                   "value" => [ +                     %{"tuple" => [":key3", 3]} +                   ], +                   "db" => [":key3"] +                 } +               ], +               "need_reboot" => true +             } + +      capture_log(fn -> +        assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{} +      end) =~ "pleroma restarted" + +      configs = +        conn +        |> get("/api/pleroma/admin/config") +        |> json_response(200) + +      refute Map.has_key?(configs, "need_reboot")      end      test "saving config with nested merge", %{conn: conn} do @@ -2410,7 +2486,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 {ExSyslogger, :ex_syslogger}               ] -      ExUnit.CaptureLog.capture_log(fn -> +      capture_log(fn ->          require Logger          Logger.warn("Ooops...")        end) =~ "Ooops..." @@ -2543,7 +2619,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          })        assert Application.get_env(:tesla, :adapter) == Tesla.Adapter.Httpc -      assert Pleroma.Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma" +      assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma"        assert json_response(conn, 200) == %{                 "configs" => [ @@ -2979,13 +3055,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "GET /api/pleroma/admin/restart" do      clear_config(:configurable_from_database) do -      Pleroma.Config.put(:configurable_from_database, true) +      Config.put(:configurable_from_database, true)      end      test "pleroma restarts", %{conn: conn} do -      ExUnit.CaptureLog.capture_log(fn -> +      capture_log(fn ->          assert conn |> get("/api/pleroma/admin/restart") |> json_response(200) == %{}        end) =~ "pleroma restarted" + +      refute Restarter.Pleroma.need_reboot?()      end    end diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 214cbdd7c..11f7c068f 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -551,6 +551,50 @@ defmodule Pleroma.Web.CommonAPITest do        refute User.subscribed_to?(follower, followed)      end + +    test "cancels a pending follow for a local user" do +      follower = insert(:user) +      followed = insert(:user, locked: true) + +      assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} = +               CommonAPI.follow(follower, followed) + +      assert User.get_follow_state(follower, followed) == "pending" +      assert {:ok, follower} = CommonAPI.unfollow(follower, followed) +      assert User.get_follow_state(follower, followed) == nil + +      assert %{id: ^activity_id, data: %{"state" => "cancelled"}} = +               Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed) + +      assert %{ +               data: %{ +                 "type" => "Undo", +                 "object" => %{"type" => "Follow", "state" => "cancelled"} +               } +             } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower) +    end + +    test "cancels a pending follow for a remote user" do +      follower = insert(:user) +      followed = insert(:user, locked: true, local: false, ap_enabled: true) + +      assert {:ok, follower, followed, %{id: activity_id, data: %{"state" => "pending"}}} = +               CommonAPI.follow(follower, followed) + +      assert User.get_follow_state(follower, followed) == "pending" +      assert {:ok, follower} = CommonAPI.unfollow(follower, followed) +      assert User.get_follow_state(follower, followed) == nil + +      assert %{id: ^activity_id, data: %{"state" => "cancelled"}} = +               Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(follower, followed) + +      assert %{ +               data: %{ +                 "type" => "Undo", +                 "object" => %{"type" => "Follow", "state" => "cancelled"} +               } +             } = Pleroma.Web.ActivityPub.Utils.fetch_latest_undo(follower) +    end    end    describe "accept_follow_request/2" do diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index 4b761e039..848300ef3 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -575,11 +575,11 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do    end    describe "maybe_add_attachments/3" do -    test "returns parsed results when no_links is true" do +    test "returns parsed results when attachment_links is false" do        assert Utils.maybe_add_attachments(                 {"test", [], ["tags"]},                 [], -               true +               false               ) == {"test", [], ["tags"]}      end @@ -589,7 +589,7 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do        assert Utils.maybe_add_attachments(                 {"test", [], ["tags"]},                 [attachment], -               false +               true               ) == {                 "test<br><a href=\"SakuraPM.png\" class='attachment'>SakuraPM.png</a>",                 [], diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index ec1e18002..e2abcd7c5 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -457,6 +457,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        assert id == to_string(other_user.id)      end +    test "cancelling follow request", %{conn: conn} do +      %{id: other_user_id} = insert(:user, %{locked: true}) + +      assert %{"id" => ^other_user_id, "following" => false, "requested" => true} = +               conn |> post("/api/v1/accounts/#{other_user_id}/follow") |> json_response(:ok) + +      assert %{"id" => ^other_user_id, "following" => false, "requested" => false} = +               conn |> post("/api/v1/accounts/#{other_user_id}/unfollow") |> json_response(:ok) +    end +      test "following without reblogs" do        %{conn: conn} = oauth_access(["follow", "read:statuses"])        followed = insert(:user) diff --git a/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs b/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs index 9666a7f2e..6317d1b47 100644 --- a/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs +++ b/test/web/mastodon_api/controllers/scheduled_activity_controller_test.exs @@ -9,6 +9,9 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do    alias Pleroma.ScheduledActivity    import Pleroma.Factory +  import Ecto.Query + +  clear_config([ScheduledActivity, :enabled])    test "shows scheduled activities" do      %{user: user, conn: conn} = oauth_access(["read:statuses"]) @@ -52,11 +55,26 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do    end    test "updates a scheduled activity" do +    Pleroma.Config.put([ScheduledActivity, :enabled], true)      %{user: user, conn: conn} = oauth_access(["write:statuses"]) -    scheduled_activity = insert(:scheduled_activity, user: user) -    new_scheduled_at = -      NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(120), :millisecond) +    scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60) + +    {:ok, scheduled_activity} = +      ScheduledActivity.create( +        user, +        %{ +          scheduled_at: scheduled_at, +          params: build(:note).data +        } +      ) + +    job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities")) + +    assert job.args == %{"activity_id" => scheduled_activity.id} +    assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(scheduled_at) + +    new_scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 120)      res_conn =        put(conn, "/api/v1/scheduled_statuses/#{scheduled_activity.id}", %{ @@ -65,6 +83,9 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do      assert %{"scheduled_at" => expected_scheduled_at} = json_response(res_conn, 200)      assert expected_scheduled_at == Pleroma.Web.CommonAPI.Utils.to_masto_date(new_scheduled_at) +    job = refresh_record(job) + +    assert DateTime.truncate(job.scheduled_at, :second) == to_datetime(new_scheduled_at)      res_conn = put(conn, "/api/v1/scheduled_statuses/404", %{scheduled_at: new_scheduled_at}) @@ -72,8 +93,22 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do    end    test "deletes a scheduled activity" do +    Pleroma.Config.put([ScheduledActivity, :enabled], true)      %{user: user, conn: conn} = oauth_access(["write:statuses"]) -    scheduled_activity = insert(:scheduled_activity, user: user) +    scheduled_at = Timex.shift(NaiveDateTime.utc_now(), minutes: 60) + +    {:ok, scheduled_activity} = +      ScheduledActivity.create( +        user, +        %{ +          scheduled_at: scheduled_at, +          params: build(:note).data +        } +      ) + +    job = Repo.one(from(j in Oban.Job, where: j.queue == "scheduled_activities")) + +    assert job.args == %{"activity_id" => scheduled_activity.id}      res_conn =        conn @@ -81,7 +116,8 @@ defmodule Pleroma.Web.MastodonAPI.ScheduledActivityControllerTest do        |> delete("/api/v1/scheduled_statuses/#{scheduled_activity.id}")      assert %{} = json_response(res_conn, 200) -    assert nil == Repo.get(ScheduledActivity, scheduled_activity.id) +    refute Repo.get(ScheduledActivity, scheduled_activity.id) +    refute Repo.get(Oban.Job, job.id)      res_conn =        conn diff --git a/test/web/mastodon_api/controllers/status_controller_test.exs b/test/web/mastodon_api/controllers/status_controller_test.exs index b03b4b344..fd2e40ce2 100644 --- a/test/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/web/mastodon_api/controllers/status_controller_test.exs @@ -121,6 +121,32 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do                 NaiveDateTime.to_iso8601(expiration.scheduled_at)      end +    test "it fails to create a status if `expires_in` is less or equal than an hour", %{ +      conn: conn +    } do +      # 1 hour +      expires_in = 60 * 60 + +      assert %{"error" => "Expiry date is too soon"} = +               conn +               |> post("api/v1/statuses", %{ +                 "status" => "oolong", +                 "expires_in" => expires_in +               }) +               |> json_response(422) + +      # 30 minutes +      expires_in = 30 * 60 + +      assert %{"error" => "Expiry date is too soon"} = +               conn +               |> post("api/v1/statuses", %{ +                 "status" => "oolong", +                 "expires_in" => expires_in +               }) +               |> json_response(422) +    end +      test "posting an undefined status with an attachment", %{user: user, conn: conn} do        file = %Plug.Upload{          content_type: "image/jpg", @@ -370,6 +396,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do        assert NaiveDateTime.diff(NaiveDateTime.from_iso8601!(response["poll"]["expires_at"]), time) in 420..430        refute response["poll"]["expred"] + +      question = Object.get_by_id(response["poll"]["id"]) + +      # closed contains utc timezone +      assert question.data["closed"] =~ "Z"      end      test "option limit is enforced", %{conn: conn} do @@ -1223,4 +1254,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do      assert [] = json_response(third_conn, 200)    end + +  test "expires_at is nil for another user" do +    %{conn: conn, user: user} = oauth_access(["read:statuses"]) +    {:ok, activity} = CommonAPI.post(user, %{"status" => "foobar", "expires_in" => 1_000_000}) + +    expires_at = +      activity.id +      |> ActivityExpiration.get_by_activity_id() +      |> Map.get(:scheduled_at) +      |> NaiveDateTime.to_iso8601() + +    assert %{"pleroma" => %{"expires_at" => ^expires_at}} = +             conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok) + +    %{conn: conn} = oauth_access(["read:statuses"]) + +    assert %{"pleroma" => %{"expires_at" => nil}} = +             conn |> get("/api/v1/statuses/#{activity.id}") |> json_response(:ok) +  end  end diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index fc110417c..560f8179f 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -37,15 +37,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do      status = StatusView.render("show.json", activity: activity)      assert status[:pleroma][:emoji_reactions] == [ -             %{emoji: "☕", count: 2, reacted: false}, -             %{emoji: "🍵", count: 1, reacted: false} +             %{name: "☕", count: 2, me: false}, +             %{name: "🍵", count: 1, me: false}             ]      status = StatusView.render("show.json", activity: activity, for: user)      assert status[:pleroma][:emoji_reactions] == [ -             %{emoji: "☕", count: 2, reacted: true}, -             %{emoji: "🍵", count: 1, reacted: false} +             %{name: "☕", count: 2, me: true}, +             %{name: "🍵", count: 1, me: false}             ]    end @@ -491,7 +491,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do          title: "Example website"        } -      %{provider_name: "Example site name"} = +      %{provider_name: "example.com"} =          StatusView.render("card.json", %{page_url: page_url, rich_media: card})      end @@ -506,7 +506,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do          description: "Example description"        } -      %{provider_name: "Example site name"} = +      %{provider_name: "example.com"} =          StatusView.render("card.json", %{page_url: page_url, rich_media: card})      end    end diff --git a/test/web/node_info_test.exs b/test/web/node_info_test.exs index 9a574a38d..39dd72cec 100644 --- a/test/web/node_info_test.exs +++ b/test/web/node_info_test.exs @@ -6,6 +6,7 @@ defmodule Pleroma.Web.NodeInfoTest do    use Pleroma.Web.ConnCase    import Pleroma.Factory +  clear_config([:mrf_simple])    test "GET /.well-known/nodeinfo", %{conn: conn} do      links = diff --git a/test/web/pleroma_api/controllers/pleroma_api_controller_test.exs b/test/web/pleroma_api/controllers/pleroma_api_controller_test.exs index be5007de5..36868db38 100644 --- a/test/web/pleroma_api/controllers/pleroma_api_controller_test.exs +++ b/test/web/pleroma_api/controllers/pleroma_api_controller_test.exs @@ -14,7 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do    import Pleroma.Factory -  test "POST /api/v1/pleroma/statuses/:id/react_with_emoji", %{conn: conn} do +  test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do      user = insert(:user)      other_user = insert(:user) @@ -24,18 +24,19 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do        conn        |> assign(:user, other_user)        |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"])) -      |> post("/api/v1/pleroma/statuses/#{activity.id}/react_with_emoji", %{"emoji" => "☕"}) +      |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")        |> json_response(200) +    # We return the status, but this our implementation detail.      assert %{"id" => id} = result      assert to_string(activity.id) == id      assert result["pleroma"]["emoji_reactions"] == [ -             %{"emoji" => "☕", "count" => 1, "reacted" => true} +             %{"name" => "☕", "count" => 1, "me" => true}             ]    end -  test "POST /api/v1/pleroma/statuses/:id/unreact_with_emoji", %{conn: conn} do +  test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do      user = insert(:user)      other_user = insert(:user) @@ -46,7 +47,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do        conn        |> assign(:user, other_user)        |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"])) -      |> post("/api/v1/pleroma/statuses/#{activity.id}/unreact_with_emoji", %{"emoji" => "☕"}) +      |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")      assert %{"id" => id} = json_response(result, 200)      assert to_string(activity.id) == id @@ -56,7 +57,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do      assert object.data["reaction_count"] == 0    end -  test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do +  test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do      user = insert(:user)      other_user = insert(:user)      doomed_user = insert(:user) @@ -65,7 +66,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do      result =        conn -      |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by") +      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")        |> json_response(200)      assert result == [] @@ -77,11 +78,10 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do      result =        conn -      |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by") +      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")        |> json_response(200) -    [%{"emoji" => "🎅", "count" => 1, "accounts" => [represented_user], "reacted" => false}] = -      result +    [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result      assert represented_user["id"] == other_user.id @@ -89,10 +89,10 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do        conn        |> assign(:user, other_user)        |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"])) -      |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by") +      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")        |> json_response(200) -    assert [%{"emoji" => "🎅", "count" => 1, "accounts" => [_represented_user], "reacted" => true}] = +    assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =               result    end diff --git a/test/web/rich_media/parsers/twitter_card_test.exs b/test/web/rich_media/parsers/twitter_card_test.exs index 751ca614c..f2ebbde7e 100644 --- a/test/web/rich_media/parsers/twitter_card_test.exs +++ b/test/web/rich_media/parsers/twitter_card_test.exs @@ -7,11 +7,14 @@ defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do    alias Pleroma.Web.RichMedia.Parsers.TwitterCard    test "returns error when html not contains twitter card" do -    assert TwitterCard.parse("", %{}) == {:error, "No twitter card metadata found"} +    assert TwitterCard.parse([{"html", [], [{"head", [], []}, {"body", [], []}]}], %{}) == +             {:error, "No twitter card metadata found"}    end    test "parses twitter card with only name attributes" do -    html = File.read!("test/fixtures/nypd-facial-recognition-children-teenagers3.html") +    html = +      File.read!("test/fixtures/nypd-facial-recognition-children-teenagers3.html") +      |> Floki.parse_document!()      assert TwitterCard.parse(html, %{}) ==               {:ok, @@ -26,7 +29,9 @@ defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do    end    test "parses twitter card with only property attributes" do -    html = File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html") +    html = +      File.read!("test/fixtures/nypd-facial-recognition-children-teenagers2.html") +      |> Floki.parse_document!()      assert TwitterCard.parse(html, %{}) ==               {:ok, @@ -45,7 +50,9 @@ defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do    end    test "parses twitter card with name & property attributes" do -    html = File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html") +    html = +      File.read!("test/fixtures/nypd-facial-recognition-children-teenagers.html") +      |> Floki.parse_document!()      assert TwitterCard.parse(html, %{}) ==               {:ok, @@ -73,7 +80,8 @@ defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do          "YTQ5MF9EQVIgZXhodW1hdGlvbiBvZiBNYXJnYXJldCBDb3JiaW4gZ3JhdmUgMTkyNi5qcGciXSxbInAiLCJjb252ZXJ0IiwiIl0sWyJwIiwiY29udmVydCIsIi1xdWFsaXR5IDgxIC1hdXRvLW9" <>          "yaWVudCJdLFsicCIsInRodW1iIiwiNjAweD4iXV0/DAR%20exhumation%20of%20Margaret%20Corbin%20grave%201926.jpg" -    html = File.read!("test/fixtures/margaret-corbin-grave-west-point.html") +    html = +      File.read!("test/fixtures/margaret-corbin-grave-west-point.html") |> Floki.parse_document!()      assert TwitterCard.parse(html, %{}) ==               {:ok, @@ -87,7 +95,9 @@ defmodule Pleroma.Web.RichMedia.Parsers.TwitterCardTest do    end    test "takes first founded title in html head if there is html markup error" do -    html = File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html") +    html = +      File.read!("test/fixtures/nypd-facial-recognition-children-teenagers4.html") +      |> Floki.parse_document!()      assert TwitterCard.parse(html, %{}) ==               {:ok, diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs new file mode 100644 index 000000000..ab0a2c3df --- /dev/null +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -0,0 +1,142 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.TwitterAPI.ControllerTest do +  use Pleroma.Web.ConnCase + +  alias Pleroma.Builders.ActivityBuilder +  alias Pleroma.Repo +  alias Pleroma.User +  alias Pleroma.Web.OAuth.Token + +  import Pleroma.Factory + +  describe "POST /api/qvitter/statuses/notifications/read" do +    test "without valid credentials", %{conn: conn} do +      conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567}) +      assert json_response(conn, 403) == %{"error" => "Invalid credentials."} +    end + +    test "with credentials, without any params" do +      %{user: current_user, conn: conn} = +        oauth_access(["read:notifications", "write:notifications"]) + +      conn = +        conn +        |> assign(:user, current_user) +        |> post("/api/qvitter/statuses/notifications/read") + +      assert json_response(conn, 400) == %{ +               "error" => "You need to specify latest_id", +               "request" => "/api/qvitter/statuses/notifications/read" +             } +    end + +    test "with credentials, with params" do +      %{user: current_user, conn: conn} = +        oauth_access(["read:notifications", "write:notifications"]) + +      other_user = insert(:user) + +      {:ok, _activity} = +        ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user}) + +      response_conn = +        conn +        |> assign(:user, current_user) +        |> get("/api/v1/notifications") + +      [notification] = response = json_response(response_conn, 200) + +      assert length(response) == 1 + +      assert notification["pleroma"]["is_seen"] == false + +      response_conn = +        conn +        |> assign(:user, current_user) +        |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]}) + +      [notification] = response = json_response(response_conn, 200) + +      assert length(response) == 1 + +      assert notification["pleroma"]["is_seen"] == true +    end +  end + +  describe "GET /api/account/confirm_email/:id/:token" do +    setup do +      {:ok, user} = +        insert(:user) +        |> User.confirmation_changeset(need_confirmation: true) +        |> Repo.update() + +      assert user.confirmation_pending + +      [user: user] +    end + +    test "it redirects to root url", %{conn: conn, user: user} do +      conn = get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}") + +      assert 302 == conn.status +    end + +    test "it confirms the user account", %{conn: conn, user: user} do +      get(conn, "/api/account/confirm_email/#{user.id}/#{user.confirmation_token}") + +      user = User.get_cached_by_id(user.id) + +      refute user.confirmation_pending +      refute user.confirmation_token +    end + +    test "it returns 500 if user cannot be found by id", %{conn: conn, user: user} do +      conn = get(conn, "/api/account/confirm_email/0/#{user.confirmation_token}") + +      assert 500 == conn.status +    end + +    test "it returns 500 if token is invalid", %{conn: conn, user: user} do +      conn = get(conn, "/api/account/confirm_email/#{user.id}/wrong_token") + +      assert 500 == conn.status +    end +  end + +  describe "GET /api/oauth_tokens" do +    setup do +      token = insert(:oauth_token) |> Repo.preload(:user) + +      %{token: token} +    end + +    test "renders list", %{token: token} do +      response = +        build_conn() +        |> assign(:user, token.user) +        |> get("/api/oauth_tokens") + +      keys = +        json_response(response, 200) +        |> hd() +        |> Map.keys() + +      assert keys -- ["id", "app_name", "valid_until"] == [] +    end + +    test "revoke token", %{token: token} do +      response = +        build_conn() +        |> assign(:user, token.user) +        |> delete("/api/oauth_tokens/#{token.id}") + +      tokens = Token.get_user_tokens(token.user) + +      assert tokens == [] +      assert response.status == 201 +    end +  end +end | 
