Update Copyrights
[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
15 {:ok, %{"notifications" => marker}} =
16 Pleroma.Marker.upsert(
17 user,
18 %{"notifications" => %{"last_read_id" => "69420"}}
19 )
20
21 response =
22 conn
23 |> assign(:user, user)
24 |> assign(:token, token)
25 |> get("/api/v1/markers", %{timeline: ["notifications"]})
26 |> json_response(200)
27
28 assert response == %{
29 "notifications" => %{
30 "last_read_id" => "69420",
31 "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
32 "version" => 0
33 }
34 }
35 end
36
37 test "gets markers with missed scopes", %{conn: conn} do
38 user = insert(:user)
39 token = insert(:oauth_token, user: user, scopes: [])
40
41 Pleroma.Marker.upsert(user, %{"notifications" => %{"last_read_id" => "69420"}})
42
43 response =
44 conn
45 |> assign(:user, user)
46 |> assign(:token, token)
47 |> get("/api/v1/markers", %{timeline: ["notifications"]})
48 |> json_response(403)
49
50 assert response == %{"error" => "Insufficient permissions: read:statuses."}
51 end
52 end
53
54 describe "POST /api/v1/markers" do
55 test "creates a marker with correct scopes", %{conn: conn} do
56 user = insert(:user)
57 token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
58
59 response =
60 conn
61 |> assign(:user, user)
62 |> assign(:token, token)
63 |> post("/api/v1/markers", %{
64 home: %{last_read_id: "777"},
65 notifications: %{"last_read_id" => "69420"}
66 })
67 |> json_response(200)
68
69 assert %{
70 "notifications" => %{
71 "last_read_id" => "69420",
72 "updated_at" => _,
73 "version" => 0
74 }
75 } = response
76 end
77
78 test "updates exist marker", %{conn: conn} do
79 user = insert(:user)
80 token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
81
82 {:ok, %{"notifications" => marker}} =
83 Pleroma.Marker.upsert(
84 user,
85 %{"notifications" => %{"last_read_id" => "69477"}}
86 )
87
88 response =
89 conn
90 |> assign(:user, user)
91 |> assign(:token, token)
92 |> post("/api/v1/markers", %{
93 home: %{last_read_id: "777"},
94 notifications: %{"last_read_id" => "69888"}
95 })
96 |> json_response(200)
97
98 assert response == %{
99 "notifications" => %{
100 "last_read_id" => "69888",
101 "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
102 "version" => 0
103 }
104 }
105 end
106
107 test "creates a marker with missed scopes", %{conn: conn} do
108 user = insert(:user)
109 token = insert(:oauth_token, user: user, scopes: [])
110
111 response =
112 conn
113 |> assign(:user, user)
114 |> assign(:token, token)
115 |> post("/api/v1/markers", %{
116 home: %{last_read_id: "777"},
117 notifications: %{"last_read_id" => "69420"}
118 })
119 |> json_response(403)
120
121 assert response == %{"error" => "Insufficient permissions: write:statuses."}
122 end
123 end
124 end