logo

pleroma

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

pleroma_api_controller_test.exs (5058B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
      6   use Pleroma.Web.ConnCase
      7 
      8   alias Pleroma.Conversation.Participation
      9   alias Pleroma.Notification
     10   alias Pleroma.Repo
     11   alias Pleroma.Web.CommonAPI
     12 
     13   import Pleroma.Factory
     14 
     15   test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
     16     user = insert(:user)
     17     other_user = insert(:user)
     18 
     19     {:ok, _activity} =
     20       CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
     21 
     22     [participation] = Participation.for_user(other_user)
     23 
     24     result =
     25       conn
     26       |> assign(:user, other_user)
     27       |> get("/api/v1/pleroma/conversations/#{participation.id}")
     28       |> json_response(200)
     29 
     30     assert result["id"] == participation.id |> to_string()
     31   end
     32 
     33   test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
     34     user = insert(:user)
     35     other_user = insert(:user)
     36     third_user = insert(:user)
     37 
     38     {:ok, _activity} =
     39       CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
     40 
     41     {:ok, activity} =
     42       CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
     43 
     44     [participation] = Participation.for_user(other_user)
     45 
     46     {:ok, activity_two} =
     47       CommonAPI.post(other_user, %{
     48         "status" => "Hi!",
     49         "in_reply_to_status_id" => activity.id,
     50         "in_reply_to_conversation_id" => participation.id
     51       })
     52 
     53     result =
     54       conn
     55       |> assign(:user, other_user)
     56       |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
     57       |> json_response(200)
     58 
     59     assert length(result) == 2
     60 
     61     id_one = activity.id
     62     id_two = activity_two.id
     63     assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
     64   end
     65 
     66   test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
     67     user = insert(:user)
     68     other_user = insert(:user)
     69 
     70     {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
     71 
     72     [participation] = Participation.for_user(user)
     73 
     74     participation = Repo.preload(participation, :recipients)
     75 
     76     assert [user] == participation.recipients
     77     assert other_user not in participation.recipients
     78 
     79     result =
     80       conn
     81       |> assign(:user, user)
     82       |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
     83         "recipients" => [user.id, other_user.id]
     84       })
     85       |> json_response(200)
     86 
     87     assert result["id"] == participation.id |> to_string
     88 
     89     [participation] = Participation.for_user(user)
     90     participation = Repo.preload(participation, :recipients)
     91 
     92     assert user in participation.recipients
     93     assert other_user in participation.recipients
     94   end
     95 
     96   describe "POST /api/v1/pleroma/notifications/read" do
     97     test "it marks a single notification as read", %{conn: conn} do
     98       user1 = insert(:user)
     99       user2 = insert(:user)
    100       {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
    101       {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
    102       {:ok, [notification1]} = Notification.create_notifications(activity1)
    103       {:ok, [notification2]} = Notification.create_notifications(activity2)
    104 
    105       response =
    106         conn
    107         |> assign(:user, user1)
    108         |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
    109         |> json_response(:ok)
    110 
    111       assert %{"pleroma" => %{"is_seen" => true}} = response
    112       assert Repo.get(Notification, notification1.id).seen
    113       refute Repo.get(Notification, notification2.id).seen
    114     end
    115 
    116     test "it marks multiple notifications as read", %{conn: conn} do
    117       user1 = insert(:user)
    118       user2 = insert(:user)
    119       {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
    120       {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
    121       {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
    122 
    123       [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
    124 
    125       [response1, response2] =
    126         conn
    127         |> assign(:user, user1)
    128         |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
    129         |> json_response(:ok)
    130 
    131       assert %{"pleroma" => %{"is_seen" => true}} = response1
    132       assert %{"pleroma" => %{"is_seen" => true}} = response2
    133       assert Repo.get(Notification, notification1.id).seen
    134       assert Repo.get(Notification, notification2.id).seen
    135       refute Repo.get(Notification, notification3.id).seen
    136     end
    137 
    138     test "it returns error when notification not found", %{conn: conn} do
    139       user1 = insert(:user)
    140 
    141       response =
    142         conn
    143         |> assign(:user, user1)
    144         |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
    145         |> json_response(:bad_request)
    146 
    147       assert response == %{"error" => "Cannot get notification"}
    148     end
    149   end
    150 end