summaryrefslogtreecommitdiff
path: root/test/pleroma/web/mastodon_api/controllers/subscription_controller_test.exs
blob: 5a3f93d2d6c57ecc9cc795932352d4878eade77b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.SubscriptionControllerTest do
  use Pleroma.Web.ConnCase, async: true

  import Pleroma.Factory

  alias Pleroma.Web.Push
  alias Pleroma.Web.Push.Subscription

  @sub %{
    "endpoint" => "https://example.com/example/1234",
    "keys" => %{
      "auth" => "8eDyX_uCN0XRhSbY5hs7Hg==",
      "p256dh" =>
        "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA="
    }
  }
  @server_key Keyword.get(Push.vapid_config(), :public_key)

  setup do
    user = insert(:user)
    token = insert(:oauth_token, user: user, scopes: ["push"])

    conn =
      build_conn()
      |> assign(:user, user)
      |> assign(:token, token)
      |> put_req_header("content-type", "application/json")

    %{conn: conn, user: user, token: token}
  end

  defmacro assert_error_when_disable_push(do: yield) do
    quote do
      vapid_details = Application.get_env(:web_push_encryption, :vapid_details, [])
      Application.put_env(:web_push_encryption, :vapid_details, [])

      assert %{"error" => "Web push subscription is disabled on this Pleroma instance"} ==
               unquote(yield)

      Application.put_env(:web_push_encryption, :vapid_details, vapid_details)
    end
  end

  describe "when disabled" do
    test "POST returns error", %{conn: conn} do
      assert_error_when_disable_push do
        conn
        |> post("/api/v1/push/subscription", %{
          "data" => %{"alerts" => %{"mention" => true}},
          "subscription" => @sub
        })
        |> json_response_and_validate_schema(403)
      end
    end

    test "GET returns error", %{conn: conn} do
      assert_error_when_disable_push do
        conn
        |> get("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(403)
      end
    end

    test "PUT returns error", %{conn: conn} do
      assert_error_when_disable_push do
        conn
        |> put("/api/v1/push/subscription", %{data: %{"alerts" => %{"mention" => false}}})
        |> json_response_and_validate_schema(403)
      end
    end

    test "DELETE returns error", %{conn: conn} do
      assert_error_when_disable_push do
        conn
        |> delete("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(403)
      end
    end
  end

  describe "creates push subscription" do
    test "ignores unsupported types", %{conn: conn} do
      result =
        conn
        |> post("/api/v1/push/subscription", %{
          "data" => %{
            "alerts" => %{
              "fake_unsupported_type" => true
            }
          },
          "subscription" => @sub
        })
        |> json_response_and_validate_schema(200)

      refute %{
               "alerts" => %{
                 "fake_unsupported_type" => true
               }
             } == result
    end

    test "successful creation", %{conn: conn} do
      result =
        conn
        |> post("/api/v1/push/subscription", %{
          "data" => %{
            "alerts" => %{
              "mention" => true,
              "favourite" => true,
              "follow" => true,
              "reblog" => true,
              "pleroma:chat_mention" => true,
              "pleroma:emoji_reaction" => true
            }
          },
          "subscription" => @sub
        })
        |> json_response_and_validate_schema(200)

      [subscription] = Pleroma.Repo.all(Subscription)

      assert %{
               "alerts" => %{
                 "mention" => true,
                 "favourite" => true,
                 "follow" => true,
                 "reblog" => true,
                 "pleroma:chat_mention" => true,
                 "pleroma:emoji_reaction" => true
               },
               "endpoint" => subscription.endpoint,
               "id" => to_string(subscription.id),
               "server_key" => @server_key
             } == result
    end
  end

  describe "gets a user subscription" do
    test "returns error when user hasn't subscription", %{conn: conn} do
      res =
        conn
        |> get("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(404)

      assert %{"error" => "Record not found"} == res
    end

    test "returns a user subsciption", %{conn: conn, user: user, token: token} do
      subscription =
        insert(:push_subscription,
          user: user,
          token: token,
          data: %{"alerts" => %{"mention" => true}}
        )

      res =
        conn
        |> get("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(200)

      expect = %{
        "alerts" => %{"mention" => true},
        "endpoint" => "https://example.com/example/1234",
        "id" => to_string(subscription.id),
        "server_key" => @server_key
      }

      assert expect == res
    end
  end

  describe "updates a user subsciption" do
    setup %{conn: conn, user: user, token: token} do
      subscription =
        insert(:push_subscription,
          user: user,
          token: token,
          data: %{
            "alerts" => %{
              "mention" => true,
              "favourite" => true,
              "follow" => true,
              "reblog" => true,
              "pleroma:chat_mention" => true,
              "pleroma:emoji_reaction" => true
            }
          }
        )

      %{conn: conn, user: user, token: token, subscription: subscription}
    end

    test "returns updated subsciption", %{conn: conn, subscription: subscription} do
      res =
        conn
        |> put("/api/v1/push/subscription", %{
          data: %{
            "alerts" => %{
              "mention" => false,
              "favourite" => false,
              "follow" => false,
              "reblog" => false,
              "pleroma:chat_mention" => false,
              "pleroma:emoji_reaction" => false
            }
          }
        })
        |> json_response_and_validate_schema(200)

      expect = %{
        "alerts" => %{
          "mention" => false,
          "favourite" => false,
          "follow" => false,
          "reblog" => false,
          "pleroma:chat_mention" => false,
          "pleroma:emoji_reaction" => false
        },
        "endpoint" => "https://example.com/example/1234",
        "id" => to_string(subscription.id),
        "server_key" => @server_key
      }

      assert expect == res
    end
  end

  describe "deletes the user subscription" do
    test "returns error when user hasn't subscription", %{conn: conn} do
      res =
        conn
        |> delete("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(404)

      assert %{"error" => "Record not found"} == res
    end

    test "returns empty result and delete user subsciption", %{
      conn: conn,
      user: user,
      token: token
    } do
      subscription =
        insert(:push_subscription,
          user: user,
          token: token,
          data: %{"alerts" => %{"mention" => true}}
        )

      res =
        conn
        |> delete("/api/v1/push/subscription", %{})
        |> json_response_and_validate_schema(200)

      assert %{} == res
      refute Pleroma.Repo.get(Subscription, subscription.id)
    end
  end
end