Merge branch 'hotfix/delete-activities' into 'develop'
[akkoma] / test / web / websub / websub_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Websub.WebsubControllerTest do
6 use Pleroma.Web.ConnCase
7 import Pleroma.Factory
8 alias Pleroma.Activity
9 alias Pleroma.Repo
10 alias Pleroma.Web.Websub
11 alias Pleroma.Web.Websub.WebsubClientSubscription
12
13 test "websub subscription request", %{conn: conn} do
14 user = insert(:user)
15
16 path = Pleroma.Web.OStatus.pubsub_path(user)
17
18 data = %{
19 "hub.callback": "http://example.org/sub",
20 "hub.mode": "subscribe",
21 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
22 "hub.secret": "a random secret",
23 "hub.lease_seconds": "100"
24 }
25
26 conn =
27 conn
28 |> post(path, data)
29
30 assert response(conn, 202) == "Accepted"
31 end
32
33 test "websub subscription confirmation", %{conn: conn} do
34 websub = insert(:websub_client_subscription)
35
36 params = %{
37 "hub.mode" => "subscribe",
38 "hub.topic" => websub.topic,
39 "hub.challenge" => "some challenge",
40 "hub.lease_seconds" => "100"
41 }
42
43 conn =
44 conn
45 |> get("/push/subscriptions/#{websub.id}", params)
46
47 websub = Repo.get(WebsubClientSubscription, websub.id)
48
49 assert response(conn, 200) == "some challenge"
50 assert websub.state == "accepted"
51 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
52 end
53
54 describe "websub_incoming" do
55 test "handles incoming feed updates", %{conn: conn} do
56 websub = insert(:websub_client_subscription)
57 doc = "some stuff"
58 signature = Websub.sign(websub.secret, doc)
59
60 conn =
61 conn
62 |> put_req_header("x-hub-signature", "sha1=" <> signature)
63 |> put_req_header("content-type", "application/atom+xml")
64 |> post("/push/subscriptions/#{websub.id}", doc)
65
66 assert response(conn, 200) == "OK"
67
68 assert length(Repo.all(Activity)) == 1
69 end
70
71 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
72 websub = insert(:websub_client_subscription)
73 doc = "some stuff"
74 signature = Websub.sign("wrong secret", doc)
75
76 conn =
77 conn
78 |> put_req_header("x-hub-signature", "sha1=" <> signature)
79 |> put_req_header("content-type", "application/atom+xml")
80 |> post("/push/subscriptions/#{websub.id}", doc)
81
82 assert response(conn, 500) == "Error"
83
84 assert Enum.empty?(Repo.all(Activity))
85 end
86 end
87 end