websub_test.exs 6.8 KB
Newer Older
1
2
3
4
5
defmodule Pleroma.Web.WebsubMock do
  def verify(sub) do
    {:ok, sub}
  end
end
6

lain's avatar
lain committed
7
8
9
defmodule Pleroma.Web.WebsubTest do
  use Pleroma.DataCase
  alias Pleroma.Web.Websub
lain's avatar
lain committed
10
  alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
lain's avatar
lain committed
11
  import Pleroma.Factory
12
  alias Pleroma.Web.Router.Helpers
lain's avatar
lain committed
13
14
15
16
17

  test "a verification of a request that is accepted" do
    sub = insert(:websub_subscription)
    topic = sub.topic

lain's avatar
lain committed
18
    getter = fn _path, _headers, options ->
lain's avatar
lain committed
19
20
21
22
23
24
25
      %{
        "hub.challenge": challenge,
        "hub.lease_seconds": seconds,
        "hub.topic": ^topic,
        "hub.mode": "subscribe"
      } = Keyword.get(options, :params)

lain's avatar
lain committed
26
      assert String.to_integer(seconds) > 0
lain's avatar
lain committed
27

lain's avatar
lain committed
28
29
30
31
32
      {:ok,
       %HTTPoison.Response{
         status_code: 200,
         body: challenge
       }}
lain's avatar
lain committed
33
34
35
36
37
38
39
40
41
    end

    {:ok, sub} = Websub.verify(sub, getter)
    assert sub.state == "active"
  end

  test "a verification of a request that doesn't return 200" do
    sub = insert(:websub_subscription)

lain's avatar
lain committed
42
43
44
45
46
47
    getter = fn _path, _headers, _options ->
      {:ok,
       %HTTPoison.Response{
         status_code: 500,
         body: ""
       }}
lain's avatar
lain committed
48
49
50
    end

    {:error, sub} = Websub.verify(sub, getter)
51
52
    # Keep the current state.
    assert sub.state == "requested"
lain's avatar
lain committed
53
  end
54
55
56
57
58
59

  test "an incoming subscription request" do
    user = insert(:user)

    data = %{
      "hub.callback" => "http://example.org/sub",
60
      "hub.mode" => "subscribe",
61
62
63
64
65
      "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
      "hub.secret" => "a random secret",
      "hub.lease_seconds" => "100"
    }

lain's avatar
lain committed
66
    {:ok, subscription} = Websub.incoming_subscription_request(user, data)
67
68
69
70
71
72
73
74
    assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
    assert subscription.state == "requested"
    assert subscription.secret == "a random secret"
    assert subscription.callback == "http://example.org/sub"
  end

  test "an incoming subscription request for an existing subscription" do
    user = insert(:user)
lain's avatar
lain committed
75
76
77

    sub =
      insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user))
78
79
80

    data = %{
      "hub.callback" => sub.callback,
81
      "hub.mode" => "subscribe",
82
83
84
85
86
      "hub.topic" => Pleroma.Web.OStatus.feed_path(user),
      "hub.secret" => "a random secret",
      "hub.lease_seconds" => "100"
    }

lain's avatar
lain committed
87
    {:ok, subscription} = Websub.incoming_subscription_request(user, data)
88
89
90
91
92
93
94
    assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
    assert subscription.state == sub.state
    assert subscription.secret == "a random secret"
    assert subscription.callback == sub.callback
    assert length(Repo.all(WebsubServerSubscription)) == 1
    assert subscription.id == sub.id
  end
95

96
  def accepting_verifier(subscription) do
lain's avatar
lain committed
97
    {:ok, %{subscription | state: "accepted"}}
98
99
  end

100
  test "initiate a subscription for a given user and topic" do
lain's avatar
lain committed
101
    subscriber = insert(:user)
lain's avatar
lain committed
102
    user = insert(:user, %{info: %Pleroma.User.Info{topic: "some_topic", hub: "some_hub"}})
103

lain's avatar
lain committed
104
105
106
107
    {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1)
    assert websub.subscribers == [subscriber.ap_id]
    assert websub.topic == "some_topic"
    assert websub.hub == "some_hub"
108
    assert is_binary(websub.secret)
109
    assert websub.user == user
110
111
    assert websub.state == "accepted"
  end
112
113
114
115

  test "discovers the hub and canonical url" do
    topic = "https://mastodon.social/users/lambadalambda.atom"

lain's avatar
lain committed
116
    getter = fn ^topic ->
117
118
119
120
      doc = File.read!("test/fixtures/lambadalambda.atom")
      {:ok, %{status_code: 200, body: doc}}
    end

lain's avatar
lain committed
121
    {:ok, discovered} = Websub.gather_feed_data(topic, getter)
lain's avatar
lain committed
122

lain's avatar
lain committed
123
    expected = %{
124
125
126
127
128
      "hub" => "https://mastodon.social/api/push",
      "uri" => "https://mastodon.social/users/lambadalambda",
      "nickname" => "lambadalambda",
      "name" => "Critical Value",
      "host" => "mastodon.social",
129
      "bio" => "a cool dude.",
lain's avatar
lain committed
130
131
132
133
134
135
136
137
138
139
140
      "avatar" => %{
        "type" => "Image",
        "url" => [
          %{
            "href" =>
              "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244",
            "mediaType" => "image/gif",
            "type" => "Link"
          }
        ]
      }
lain's avatar
lain committed
141
142
143
    }

    assert expected == discovered
144
145
146
147
148
149
150
  end

  test "calls the hub, requests topic" do
    hub = "https://social.heldscal.la/main/push/hub"
    topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
    websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})

lain's avatar
lain committed
151
    poster = fn ^hub, {:form, data}, _headers ->
152
      assert Keyword.get(data, :"hub.mode") == "subscribe"
lain's avatar
lain committed
153
154
155
156
157
158
159
160

      assert Keyword.get(data, :"hub.callback") ==
               Helpers.websub_url(
                 Pleroma.Web.Endpoint,
                 :websub_subscription_confirmation,
                 websub.id
               )

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
      {:ok, %{status_code: 202}}
    end

    task = Task.async(fn -> Websub.request_subscription(websub, poster) end)

    change = Ecto.Changeset.change(websub, %{state: "accepted"})
    {:ok, _} = Repo.update(change)

    {:ok, websub} = Task.await(task)

    assert websub.state == "accepted"
  end

  test "rejects the subscription if it can't be accepted" do
    hub = "https://social.heldscal.la/main/push/hub"
    topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom"
    websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})

lain's avatar
lain committed
179
    poster = fn ^hub, {:form, _data}, _headers ->
180
181
182
183
184
185
186
      {:ok, %{status_code: 202}}
    end

    {:error, websub} = Websub.request_subscription(websub, poster, 1000)
    assert websub.state == "rejected"

    websub = insert(:websub_client_subscription, %{hub: hub, topic: topic})
lain's avatar
lain committed
187
188

    poster = fn ^hub, {:form, _data}, _headers ->
189
190
191
192
193
194
      {:ok, %{status_code: 400}}
    end

    {:error, websub} = Websub.request_subscription(websub, poster, 1000)
    assert websub.state == "rejected"
  end
lain's avatar
lain committed
195
196
197

  test "sign a text" do
    signed = Websub.sign("secret", "text")
lain's avatar
lain committed
198
    assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase()
lain's avatar
lain committed
199

lain's avatar
lain committed
200
    _signed = Websub.sign("secret", [["て"], ['す']])
lain's avatar
lain committed
201
  end
lain's avatar
lain committed
202
203
204
205

  describe "renewing subscriptions" do
    test "it renews subscriptions that have less than a day of time left" do
      day = 60 * 60 * 24
lain's avatar
lain committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
      now = NaiveDateTime.utc_now()

      still_good =
        insert(:websub_client_subscription, %{
          valid_until: NaiveDateTime.add(now, 2 * day),
          topic: "http://example.org/still_good",
          state: "accepted"
        })

      needs_refresh =
        insert(:websub_client_subscription, %{
          valid_until: NaiveDateTime.add(now, day - 100),
          topic: "http://example.org/needs_refresh",
          state: "accepted"
        })
lain's avatar
lain committed
221

lain's avatar
lain committed
222
      _refresh = Websub.refresh_subscriptions()
lain's avatar
lain committed
223
224
225
226
227

      assert still_good == Repo.get(WebsubClientSubscription, still_good.id)
      refute needs_refresh == Repo.get(WebsubClientSubscription, needs_refresh.id)
    end
  end
lain's avatar
lain committed
228
end