Merge branch 'develop' into issue/1276
[akkoma] / test / web / mastodon_api / controllers / marker_controller_test.exs
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.MastodonAPI.MarkerControllerTest do
6 use Pleroma.Web.ConnCase
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)
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(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(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 |> post("/api/v1/markers", %{
66 home: %{last_read_id: "777"},
67 notifications: %{"last_read_id" => "69420"}
68 })
69 |> json_response(200)
70
71 assert %{
72 "notifications" => %{
73 "last_read_id" => "69420",
74 "updated_at" => _,
75 "version" => 0,
76 "pleroma" => %{"unread_count" => 0}
77 }
78 } = response
79 end
80
81 test "updates exist marker", %{conn: conn} do
82 user = insert(:user)
83 token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
84
85 {:ok, %{"notifications" => marker}} =
86 Pleroma.Marker.upsert(
87 user,
88 %{"notifications" => %{"last_read_id" => "69477"}}
89 )
90
91 response =
92 conn
93 |> assign(:user, user)
94 |> assign(:token, token)
95 |> post("/api/v1/markers", %{
96 home: %{last_read_id: "777"},
97 notifications: %{"last_read_id" => "69888"}
98 })
99 |> json_response(200)
100
101 assert response == %{
102 "notifications" => %{
103 "last_read_id" => "69888",
104 "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
105 "version" => 0,
106 "pleroma" => %{"unread_count" => 0}
107 }
108 }
109 end
110
111 test "creates a marker with missed scopes", %{conn: conn} do
112 user = insert(:user)
113 token = insert(:oauth_token, user: user, scopes: [])
114
115 response =
116 conn
117 |> assign(:user, user)
118 |> assign(:token, token)
119 |> post("/api/v1/markers", %{
120 home: %{last_read_id: "777"},
121 notifications: %{"last_read_id" => "69420"}
122 })
123 |> json_response(403)
124
125 assert response == %{"error" => "Insufficient permissions: write:statuses."}
126 end
127 end
128 end