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