Better error handling for OstatusController.
[akkoma] / test / web / ostatus / ostatus_controller_test.exs
1 defmodule Pleroma.Web.OStatus.OStatusControllerTest do
2 use Pleroma.Web.ConnCase
3 import Pleroma.Factory
4 alias Pleroma.{User, Repo}
5 alias Pleroma.Web.OStatus.ActivityRepresenter
6
7 test "decodes a salmon", %{conn: conn} do
8 user = insert(:user)
9 salmon = File.read!("test/fixtures/salmon.xml")
10
11 conn =
12 conn
13 |> put_req_header("content-type", "application/atom+xml")
14 |> post("/users/#{user.nickname}/salmon", salmon)
15
16 assert response(conn, 200)
17 end
18
19 test "decodes a salmon with a changed magic key", %{conn: conn} do
20 user = insert(:user)
21 salmon = File.read!("test/fixtures/salmon.xml")
22
23 conn =
24 conn
25 |> put_req_header("content-type", "application/atom+xml")
26 |> post("/users/#{user.nickname}/salmon", salmon)
27
28 assert response(conn, 200)
29
30 # Set a wrong magic-key for a user so it has to refetch
31 salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1")
32 # Wrong key
33 info =
34 salmon_user.info
35 |> Map.put(
36 "magic_key",
37 "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB"
38 )
39
40 Repo.update(User.info_changeset(salmon_user, %{info: info}))
41
42 conn =
43 build_conn()
44 |> put_req_header("content-type", "application/atom+xml")
45 |> post("/users/#{user.nickname}/salmon", salmon)
46
47 assert response(conn, 200)
48 end
49
50 test "gets a feed", %{conn: conn} do
51 note_activity = insert(:note_activity)
52 user = User.get_cached_by_ap_id(note_activity.data["actor"])
53
54 conn =
55 conn
56 |> put_req_header("content-type", "application/atom+xml")
57 |> get("/users/#{user.nickname}/feed.atom")
58
59 assert response(conn, 200) =~ note_activity.data["object"]["content"]
60 end
61
62 test "returns 404 for a missing feed", %{conn: conn} do
63 conn =
64 conn
65 |> put_req_header("content-type", "application/atom+xml")
66 |> get("/users/nonexisting/feed.atom")
67
68 assert response(conn, 404)
69 end
70
71 test "gets an object", %{conn: conn} do
72 note_activity = insert(:note_activity)
73 user = User.get_by_ap_id(note_activity.data["actor"])
74 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
75 url = "/objects/#{uuid}"
76
77 conn =
78 conn
79 |> get(url)
80
81 expected =
82 ActivityRepresenter.to_simple_form(note_activity, user, true)
83 |> ActivityRepresenter.wrap_with_entry()
84 |> :xmerl.export_simple(:xmerl_xml)
85 |> to_string
86
87 assert response(conn, 200) == expected
88 end
89
90 test "404s on private objects", %{conn: conn} do
91 note_activity = insert(:direct_note_activity)
92 user = User.get_by_ap_id(note_activity.data["actor"])
93 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]))
94 url = "/objects/#{uuid}"
95
96 conn =
97 conn
98 |> get(url)
99
100 assert response(conn, 404)
101 end
102
103 test "404s on nonexisting objects", %{conn: conn} do
104 url = "/objects/123"
105
106 conn =
107 conn
108 |> get(url)
109
110 assert response(conn, 404)
111 end
112
113 test "gets an activity", %{conn: conn} do
114 note_activity = insert(:note_activity)
115 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
116 url = "/activities/#{uuid}"
117
118 conn =
119 conn
120 |> get(url)
121
122 assert response(conn, 200)
123 end
124
125 test "404s on private activities", %{conn: conn} do
126 note_activity = insert(:direct_note_activity)
127 [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]))
128 url = "/activities/#{uuid}"
129
130 conn =
131 conn
132 |> get(url)
133
134 assert response(conn, 404)
135 end
136
137 test "404s on nonexistent activities", %{conn: conn} do
138 url = "/activities/123"
139
140 conn =
141 conn
142 |> get(url)
143
144 assert response(conn, 404)
145 end
146
147 test "gets a notice", %{conn: conn} do
148 note_activity = insert(:note_activity)
149 url = "/notice/#{note_activity.id}"
150
151 conn =
152 conn
153 |> get(url)
154
155 assert response(conn, 200)
156 end
157
158 test "404s a private notice", %{conn: conn} do
159 note_activity = insert(:direct_note_activity)
160 url = "/notice/#{note_activity.id}"
161
162 conn =
163 conn
164 |> get(url)
165
166 assert response(conn, 404)
167 end
168
169 test "404s a nonexisting notice", %{conn: conn} do
170 url = "/notice/123"
171
172 conn =
173 conn
174 |> get(url)
175
176 assert response(conn, 404)
177 end
178 end