Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / web / mastodon_api / controllers / conversation_controller_test.exs
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.MastodonAPI.ConversationControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.User
9 alias Pleroma.Web.CommonAPI
10
11 import Pleroma.Factory
12
13 test "Conversations", %{conn: conn} do
14 user_one = insert(:user)
15 user_two = insert(:user)
16 user_three = insert(:user)
17
18 {:ok, user_two} = User.follow(user_two, user_one)
19
20 {:ok, direct} =
21 CommonAPI.post(user_one, %{
22 "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
23 "visibility" => "direct"
24 })
25
26 {:ok, _follower_only} =
27 CommonAPI.post(user_one, %{
28 "status" => "Hi @#{user_two.nickname}!",
29 "visibility" => "private"
30 })
31
32 res_conn =
33 conn
34 |> assign(:user, user_one)
35 |> get("/api/v1/conversations")
36
37 assert response = json_response(res_conn, 200)
38
39 assert [
40 %{
41 "id" => res_id,
42 "accounts" => res_accounts,
43 "last_status" => res_last_status,
44 "unread" => unread
45 }
46 ] = response
47
48 account_ids = Enum.map(res_accounts, & &1["id"])
49 assert length(res_accounts) == 2
50 assert user_two.id in account_ids
51 assert user_three.id in account_ids
52 assert is_binary(res_id)
53 assert unread == true
54 assert res_last_status["id"] == direct.id
55
56 # Apparently undocumented API endpoint
57 res_conn =
58 conn
59 |> assign(:user, user_one)
60 |> post("/api/v1/conversations/#{res_id}/read")
61
62 assert response = json_response(res_conn, 200)
63 assert length(response["accounts"]) == 2
64 assert response["last_status"]["id"] == direct.id
65 assert response["unread"] == false
66
67 # (vanilla) Mastodon frontend behaviour
68 res_conn =
69 conn
70 |> assign(:user, user_one)
71 |> get("/api/v1/statuses/#{res_last_status["id"]}/context")
72
73 assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
74 end
75 end