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