move to 2.5.5
[anni] / test / pleroma / web / mastodon_api / controllers / marker_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MarkerControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   import Pleroma.Factory
9
10   describe "GET /api/v1/markers" do
11     test "gets markers with correct scopes", %{conn: conn} do
12       user = insert(:user)
13       token = insert(:oauth_token, user: user, scopes: ["read:statuses"])
14       insert_list(7, :notification, user: user, activity: insert(:note_activity))
15
16       {:ok, %{"notifications" => marker}} =
17         Pleroma.Marker.upsert(
18           user,
19           %{"notifications" => %{"last_read_id" => "69420"}}
20         )
21
22       response =
23         conn
24         |> assign(:user, user)
25         |> assign(:token, token)
26         |> get("/api/v1/markers?timeline[]=notifications")
27         |> json_response_and_validate_schema(200)
28
29       assert response == %{
30                "notifications" => %{
31                  "last_read_id" => "69420",
32                  "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
33                  "version" => 0,
34                  "pleroma" => %{"unread_count" => 7}
35                }
36              }
37     end
38
39     test "gets markers with missed scopes", %{conn: conn} do
40       user = insert(:user)
41       token = insert(:oauth_token, user: user, scopes: [])
42
43       Pleroma.Marker.upsert(user, %{"notifications" => %{"last_read_id" => "69420"}})
44
45       response =
46         conn
47         |> assign(:user, user)
48         |> assign(:token, token)
49         |> get("/api/v1/markers", %{timeline: ["notifications"]})
50         |> json_response_and_validate_schema(403)
51
52       assert response == %{"error" => "Insufficient permissions: read:statuses."}
53     end
54   end
55
56   describe "POST /api/v1/markers" do
57     test "creates a marker with correct scopes", %{conn: conn} do
58       user = insert(:user)
59       token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
60
61       response =
62         conn
63         |> assign(:user, user)
64         |> assign(:token, token)
65         |> put_req_header("content-type", "application/json")
66         |> post("/api/v1/markers", %{
67           home: %{last_read_id: "777"},
68           notifications: %{"last_read_id" => "69420"}
69         })
70         |> json_response_and_validate_schema(200)
71
72       assert %{
73                "notifications" => %{
74                  "last_read_id" => "69420",
75                  "updated_at" => _,
76                  "version" => 0,
77                  "pleroma" => %{"unread_count" => 0}
78                }
79              } = response
80     end
81
82     test "updates exist marker", %{conn: conn} do
83       user = insert(:user)
84       token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
85
86       {:ok, %{"notifications" => marker}} =
87         Pleroma.Marker.upsert(
88           user,
89           %{"notifications" => %{"last_read_id" => "69477"}}
90         )
91
92       response =
93         conn
94         |> assign(:user, user)
95         |> assign(:token, token)
96         |> put_req_header("content-type", "application/json")
97         |> post("/api/v1/markers", %{
98           home: %{last_read_id: "777"},
99           notifications: %{"last_read_id" => "69888"}
100         })
101         |> json_response_and_validate_schema(200)
102
103       assert response == %{
104                "notifications" => %{
105                  "last_read_id" => "69888",
106                  "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
107                  "version" => 0,
108                  "pleroma" => %{"unread_count" => 0}
109                }
110              }
111     end
112
113     test "creates a marker with missed scopes", %{conn: conn} do
114       user = insert(:user)
115       token = insert(:oauth_token, user: user, scopes: [])
116
117       response =
118         conn
119         |> assign(:user, user)
120         |> assign(:token, token)
121         |> put_req_header("content-type", "application/json")
122         |> post("/api/v1/markers", %{
123           home: %{last_read_id: "777"},
124           notifications: %{"last_read_id" => "69420"}
125         })
126         |> json_response_and_validate_schema(403)
127
128       assert response == %{"error" => "Insufficient permissions: write:statuses."}
129     end
130   end
131 end