Merge branch 'legal-boilerplate' 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.Web.Websub.WebsubClientSubscription
9 alias Pleroma.{Repo, Activity}
10 alias Pleroma.Web.Websub
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 test "handles incoming feed updates", %{conn: conn} do
54 websub = insert(:websub_client_subscription)
55 doc = "some stuff"
56 signature = Websub.sign(websub.secret, doc)
57
58 conn =
59 conn
60 |> put_req_header("x-hub-signature", "sha1=" <> signature)
61 |> put_req_header("content-type", "application/atom+xml")
62 |> post("/push/subscriptions/#{websub.id}", doc)
63
64 assert response(conn, 200) == "OK"
65
66 assert length(Repo.all(Activity)) == 1
67 end
68
69 test "rejects incoming feed updates with the wrong signature", %{conn: conn} do
70 websub = insert(:websub_client_subscription)
71 doc = "some stuff"
72 signature = Websub.sign("wrong secret", doc)
73
74 conn =
75 conn
76 |> put_req_header("x-hub-signature", "sha1=" <> signature)
77 |> put_req_header("content-type", "application/atom+xml")
78 |> post("/push/subscriptions/#{websub.id}", doc)
79
80 assert response(conn, 500) == "Error"
81
82 assert length(Repo.all(Activity)) == 0
83 end
84 end