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