Merge branch 'develop' into feature/digest-email
[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.Repo
9 alias Pleroma.Web.Websub
10 alias Pleroma.Web.Websub.WebsubClientSubscription
11
12 test "websub subscription request", %{conn: conn} do
13 user = insert(:user)
14
15 path = Pleroma.Web.OStatus.pubsub_path(user)
16
17 data = %{
18 "hub.callback": "http://example.org/sub",
19 "hub.mode": "subscribe",
20 "hub.topic": Pleroma.Web.OStatus.feed_path(user),
21 "hub.secret": "a random secret",
22 "hub.lease_seconds": "100"
23 }
24
25 conn =
26 conn
27 |> post(path, data)
28
29 assert response(conn, 202) == "Accepted"
30 end
31
32 test "websub subscription confirmation", %{conn: conn} do
33 websub = insert(:websub_client_subscription)
34
35 params = %{
36 "hub.mode" => "subscribe",
37 "hub.topic" => websub.topic,
38 "hub.challenge" => "some challenge",
39 "hub.lease_seconds" => "100"
40 }
41
42 conn =
43 conn
44 |> get("/push/subscriptions/#{websub.id}", params)
45
46 websub = Repo.get(WebsubClientSubscription, websub.id)
47
48 assert response(conn, 200) == "some challenge"
49 assert websub.state == "accepted"
50 assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5
51 end
52
53 describe "websub_incoming" do
54 test "accepts incoming feed updates", %{conn: conn} do
55 websub = insert(:websub_client_subscription)
56 doc = "some stuff"
57 signature = Websub.sign(websub.secret, doc)
58
59 conn =
60 conn
61 |> put_req_header("x-hub-signature", "sha1=" <> signature)
62 |> put_req_header("content-type", "application/atom+xml")
63 |> post("/push/subscriptions/#{websub.id}", doc)
64
65 assert response(conn, 200) == "OK"
66 end
67
68 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
69 websub = insert(:websub_client_subscription)
70 doc = "some stuff"
71 signature = Websub.sign("wrong secret", doc)
72
73 conn =
74 conn
75 |> put_req_header("x-hub-signature", "sha1=" <> signature)
76 |> put_req_header("content-type", "application/atom+xml")
77 |> post("/push/subscriptions/#{websub.id}", doc)
78
79 assert response(conn, 500) == "Error"
80 end
81 end
82 end