logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

pleroma_api_controller_test.exs (10189B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
      6   use Oban.Testing, repo: Pleroma.Repo
      7   use Pleroma.Web.ConnCase
      8 
      9   alias Pleroma.Conversation.Participation
     10   alias Pleroma.Notification
     11   alias Pleroma.Object
     12   alias Pleroma.Repo
     13   alias Pleroma.Tests.ObanHelpers
     14   alias Pleroma.User
     15   alias Pleroma.Web.CommonAPI
     16 
     17   import Pleroma.Factory
     18 
     19   test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
     20     user = insert(:user)
     21     other_user = insert(:user)
     22 
     23     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
     24 
     25     result =
     26       conn
     27       |> assign(:user, other_user)
     28       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
     29       |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
     30       |> json_response(200)
     31 
     32     # We return the status, but this our implementation detail.
     33     assert %{"id" => id} = result
     34     assert to_string(activity.id) == id
     35 
     36     assert result["pleroma"]["emoji_reactions"] == [
     37              %{"name" => "☕", "count" => 1, "me" => true}
     38            ]
     39   end
     40 
     41   test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
     42     user = insert(:user)
     43     other_user = insert(:user)
     44 
     45     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
     46     {:ok, _reaction_activity} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
     47 
     48     ObanHelpers.perform_all()
     49 
     50     result =
     51       conn
     52       |> assign(:user, other_user)
     53       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
     54       |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
     55 
     56     assert %{"id" => id} = json_response(result, 200)
     57     assert to_string(activity.id) == id
     58 
     59     ObanHelpers.perform_all()
     60 
     61     object = Object.get_by_ap_id(activity.data["object"])
     62 
     63     assert object.data["reaction_count"] == 0
     64   end
     65 
     66   test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
     67     user = insert(:user)
     68     other_user = insert(:user)
     69     doomed_user = insert(:user)
     70 
     71     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
     72 
     73     result =
     74       conn
     75       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
     76       |> json_response(200)
     77 
     78     assert result == []
     79 
     80     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
     81     {:ok, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
     82 
     83     User.perform(:delete, doomed_user)
     84 
     85     result =
     86       conn
     87       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
     88       |> json_response(200)
     89 
     90     [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
     91 
     92     assert represented_user["id"] == other_user.id
     93 
     94     result =
     95       conn
     96       |> assign(:user, other_user)
     97       |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
     98       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
     99       |> json_response(200)
    100 
    101     assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
    102              result
    103   end
    104 
    105   test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
    106     user = insert(:user)
    107     other_user = insert(:user)
    108 
    109     {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"})
    110 
    111     result =
    112       conn
    113       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
    114       |> json_response(200)
    115 
    116     assert result == []
    117 
    118     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
    119     {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
    120 
    121     result =
    122       conn
    123       |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
    124       |> json_response(200)
    125 
    126     [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
    127 
    128     assert represented_user["id"] == other_user.id
    129   end
    130 
    131   test "/api/v1/pleroma/conversations/:id" do
    132     user = insert(:user)
    133     %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
    134 
    135     {:ok, _activity} =
    136       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
    137 
    138     [participation] = Participation.for_user(other_user)
    139 
    140     result =
    141       conn
    142       |> get("/api/v1/pleroma/conversations/#{participation.id}")
    143       |> json_response(200)
    144 
    145     assert result["id"] == participation.id |> to_string()
    146   end
    147 
    148   test "/api/v1/pleroma/conversations/:id/statuses" do
    149     user = insert(:user)
    150     %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
    151     third_user = insert(:user)
    152 
    153     {:ok, _activity} =
    154       CommonAPI.post(user, %{status: "Hi @#{third_user.nickname}!", visibility: "direct"})
    155 
    156     {:ok, activity} =
    157       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
    158 
    159     [participation] = Participation.for_user(other_user)
    160 
    161     {:ok, activity_two} =
    162       CommonAPI.post(other_user, %{
    163         status: "Hi!",
    164         in_reply_to_status_id: activity.id,
    165         in_reply_to_conversation_id: participation.id
    166       })
    167 
    168     result =
    169       conn
    170       |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
    171       |> json_response(200)
    172 
    173     assert length(result) == 2
    174 
    175     id_one = activity.id
    176     id_two = activity_two.id
    177     assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
    178 
    179     {:ok, %{id: id_three}} =
    180       CommonAPI.post(other_user, %{
    181         status: "Bye!",
    182         in_reply_to_status_id: activity.id,
    183         in_reply_to_conversation_id: participation.id
    184       })
    185 
    186     assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
    187              conn
    188              |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
    189              |> json_response(:ok)
    190 
    191     assert [%{"id" => ^id_three}] =
    192              conn
    193              |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
    194              |> json_response(:ok)
    195   end
    196 
    197   test "PATCH /api/v1/pleroma/conversations/:id" do
    198     %{user: user, conn: conn} = oauth_access(["write:conversations"])
    199     other_user = insert(:user)
    200 
    201     {:ok, _activity} = CommonAPI.post(user, %{status: "Hi", visibility: "direct"})
    202 
    203     [participation] = Participation.for_user(user)
    204 
    205     participation = Repo.preload(participation, :recipients)
    206 
    207     user = User.get_cached_by_id(user.id)
    208     assert [user] == participation.recipients
    209     assert other_user not in participation.recipients
    210 
    211     result =
    212       conn
    213       |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
    214         "recipients" => [user.id, other_user.id]
    215       })
    216       |> json_response(200)
    217 
    218     assert result["id"] == participation.id |> to_string
    219 
    220     [participation] = Participation.for_user(user)
    221     participation = Repo.preload(participation, :recipients)
    222 
    223     assert user in participation.recipients
    224     assert other_user in participation.recipients
    225   end
    226 
    227   test "POST /api/v1/pleroma/conversations/read" do
    228     user = insert(:user)
    229     %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
    230 
    231     {:ok, _activity} =
    232       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
    233 
    234     {:ok, _activity} =
    235       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
    236 
    237     [participation2, participation1] = Participation.for_user(other_user)
    238     assert Participation.get(participation2.id).read == false
    239     assert Participation.get(participation1.id).read == false
    240     assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
    241 
    242     [%{"unread" => false}, %{"unread" => false}] =
    243       conn
    244       |> post("/api/v1/pleroma/conversations/read", %{})
    245       |> json_response(200)
    246 
    247     [participation2, participation1] = Participation.for_user(other_user)
    248     assert Participation.get(participation2.id).read == true
    249     assert Participation.get(participation1.id).read == true
    250     assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
    251   end
    252 
    253   describe "POST /api/v1/pleroma/notifications/read" do
    254     setup do: oauth_access(["write:notifications"])
    255 
    256     test "it marks a single notification as read", %{user: user1, conn: conn} do
    257       user2 = insert(:user)
    258       {:ok, activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
    259       {:ok, activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
    260       {:ok, [notification1]} = Notification.create_notifications(activity1)
    261       {:ok, [notification2]} = Notification.create_notifications(activity2)
    262 
    263       response =
    264         conn
    265         |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
    266         |> json_response(:ok)
    267 
    268       assert %{"pleroma" => %{"is_seen" => true}} = response
    269       assert Repo.get(Notification, notification1.id).seen
    270       refute Repo.get(Notification, notification2.id).seen
    271     end
    272 
    273     test "it marks multiple notifications as read", %{user: user1, conn: conn} do
    274       user2 = insert(:user)
    275       {:ok, _activity1} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
    276       {:ok, _activity2} = CommonAPI.post(user2, %{status: "hi @#{user1.nickname}"})
    277       {:ok, _activity3} = CommonAPI.post(user2, %{status: "HIE @#{user1.nickname}"})
    278 
    279       [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
    280 
    281       [response1, response2] =
    282         conn
    283         |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
    284         |> json_response(:ok)
    285 
    286       assert %{"pleroma" => %{"is_seen" => true}} = response1
    287       assert %{"pleroma" => %{"is_seen" => true}} = response2
    288       assert Repo.get(Notification, notification1.id).seen
    289       assert Repo.get(Notification, notification2.id).seen
    290       refute Repo.get(Notification, notification3.id).seen
    291     end
    292 
    293     test "it returns error when notification not found", %{conn: conn} do
    294       response =
    295         conn
    296         |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
    297         |> json_response(:bad_request)
    298 
    299       assert response == %{"error" => "Cannot get notification"}
    300     end
    301   end
    302 end