Move single used schemas to Marker operation schema
[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_and_validate_schema(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_and_validate_schema(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 |> put_req_header("content-type", "application/json")
64 |> post("/api/v1/markers", %{
65 home: %{last_read_id: "777"},
66 notifications: %{"last_read_id" => "69420"}
67 })
68 |> json_response_and_validate_schema(200)
69
70 assert %{
71 "notifications" => %{
72 "last_read_id" => "69420",
73 "updated_at" => _,
74 "version" => 0
75 }
76 } = response
77 end
78
79 test "updates exist marker", %{conn: conn} do
80 user = insert(:user)
81 token = insert(:oauth_token, user: user, scopes: ["write:statuses"])
82
83 {:ok, %{"notifications" => marker}} =
84 Pleroma.Marker.upsert(
85 user,
86 %{"notifications" => %{"last_read_id" => "69477"}}
87 )
88
89 response =
90 conn
91 |> assign(:user, user)
92 |> assign(:token, token)
93 |> put_req_header("content-type", "application/json")
94 |> post("/api/v1/markers", %{
95 home: %{last_read_id: "777"},
96 notifications: %{"last_read_id" => "69888"}
97 })
98 |> json_response_and_validate_schema(200)
99
100 assert response == %{
101 "notifications" => %{
102 "last_read_id" => "69888",
103 "updated_at" => NaiveDateTime.to_iso8601(marker.updated_at),
104 "version" => 0
105 }
106 }
107 end
108
109 test "creates a marker with missed scopes", %{conn: conn} do
110 user = insert(:user)
111 token = insert(:oauth_token, user: user, scopes: [])
112
113 response =
114 conn
115 |> assign(:user, user)
116 |> assign(:token, token)
117 |> put_req_header("content-type", "application/json")
118 |> post("/api/v1/markers", %{
119 home: %{last_read_id: "777"},
120 notifications: %{"last_read_id" => "69420"}
121 })
122 |> json_response_and_validate_schema(403)
123
124 assert response == %{"error" => "Insufficient permissions: write:statuses."}
125 end
126 end
127 end