Move account_register, relationships and verify_credentials to MastodonAPI.AccountCon...
[akkoma] / test / web / mastodon_api / mastodon_api_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Ecto.Changeset
9 alias Pleroma.Config
10 alias Pleroma.Notification
11 alias Pleroma.Object
12 alias Pleroma.Repo
13 alias Pleroma.Tests.ObanHelpers
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.OAuth.App
18 alias Pleroma.Web.Push
19
20 import ExUnit.CaptureLog
21 import Pleroma.Factory
22 import Swoosh.TestAssertions
23 import Tesla.Mock
24
25 setup do
26 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
27 :ok
28 end
29
30 clear_config([:instance, :public])
31 clear_config([:rich_media, :enabled])
32
33 test "apps/verify_credentials", %{conn: conn} do
34 token = insert(:oauth_token)
35
36 conn =
37 conn
38 |> assign(:user, token.user)
39 |> assign(:token, token)
40 |> get("/api/v1/apps/verify_credentials")
41
42 app = Repo.preload(token, :app).app
43
44 expected = %{
45 "name" => app.client_name,
46 "website" => app.website,
47 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
48 }
49
50 assert expected == json_response(conn, 200)
51 end
52
53 test "creates an oauth app", %{conn: conn} do
54 user = insert(:user)
55 app_attrs = build(:oauth_app)
56
57 conn =
58 conn
59 |> assign(:user, user)
60 |> post("/api/v1/apps", %{
61 client_name: app_attrs.client_name,
62 redirect_uris: app_attrs.redirect_uris
63 })
64
65 [app] = Repo.all(App)
66
67 expected = %{
68 "name" => app.client_name,
69 "website" => app.website,
70 "client_id" => app.client_id,
71 "client_secret" => app.client_secret,
72 "id" => app.id |> to_string(),
73 "redirect_uri" => app.redirect_uris,
74 "vapid_key" => Push.vapid_config() |> Keyword.get(:public_key)
75 }
76
77 assert expected == json_response(conn, 200)
78 end
79
80 describe "media upload" do
81 setup do
82 user = insert(:user)
83
84 conn =
85 build_conn()
86 |> assign(:user, user)
87
88 image = %Plug.Upload{
89 content_type: "image/jpg",
90 path: Path.absname("test/fixtures/image.jpg"),
91 filename: "an_image.jpg"
92 }
93
94 [conn: conn, image: image]
95 end
96
97 clear_config([:media_proxy])
98 clear_config([Pleroma.Upload])
99
100 test "returns uploaded image", %{conn: conn, image: image} do
101 desc = "Description of the image"
102
103 media =
104 conn
105 |> post("/api/v1/media", %{"file" => image, "description" => desc})
106 |> json_response(:ok)
107
108 assert media["type"] == "image"
109 assert media["description"] == desc
110 assert media["id"]
111
112 object = Repo.get(Object, media["id"])
113 assert object.data["actor"] == User.ap_id(conn.assigns[:user])
114 end
115 end
116
117 describe "/api/v1/pleroma/mascot" do
118 test "mascot upload", %{conn: conn} do
119 user = insert(:user)
120
121 non_image_file = %Plug.Upload{
122 content_type: "audio/mpeg",
123 path: Path.absname("test/fixtures/sound.mp3"),
124 filename: "sound.mp3"
125 }
126
127 conn =
128 conn
129 |> assign(:user, user)
130 |> put("/api/v1/pleroma/mascot", %{"file" => non_image_file})
131
132 assert json_response(conn, 415)
133
134 file = %Plug.Upload{
135 content_type: "image/jpg",
136 path: Path.absname("test/fixtures/image.jpg"),
137 filename: "an_image.jpg"
138 }
139
140 conn =
141 build_conn()
142 |> assign(:user, user)
143 |> put("/api/v1/pleroma/mascot", %{"file" => file})
144
145 assert %{"id" => _, "type" => image} = json_response(conn, 200)
146 end
147
148 test "mascot retrieving", %{conn: conn} do
149 user = insert(:user)
150 # When user hasn't set a mascot, we should just get pleroma tan back
151 conn =
152 conn
153 |> assign(:user, user)
154 |> get("/api/v1/pleroma/mascot")
155
156 assert %{"url" => url} = json_response(conn, 200)
157 assert url =~ "pleroma-fox-tan-smol"
158
159 # When a user sets their mascot, we should get that back
160 file = %Plug.Upload{
161 content_type: "image/jpg",
162 path: Path.absname("test/fixtures/image.jpg"),
163 filename: "an_image.jpg"
164 }
165
166 conn =
167 build_conn()
168 |> assign(:user, user)
169 |> put("/api/v1/pleroma/mascot", %{"file" => file})
170
171 assert json_response(conn, 200)
172
173 user = User.get_cached_by_id(user.id)
174
175 conn =
176 build_conn()
177 |> assign(:user, user)
178 |> get("/api/v1/pleroma/mascot")
179
180 assert %{"url" => url, "type" => "image"} = json_response(conn, 200)
181 assert url =~ "an_image"
182 end
183 end
184
185 test "getting a list of mutes", %{conn: conn} do
186 user = insert(:user)
187 other_user = insert(:user)
188
189 {:ok, user} = User.mute(user, other_user)
190
191 conn =
192 conn
193 |> assign(:user, user)
194 |> get("/api/v1/mutes")
195
196 other_user_id = to_string(other_user.id)
197 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
198 end
199
200 test "getting a list of blocks", %{conn: conn} do
201 user = insert(:user)
202 other_user = insert(:user)
203
204 {:ok, user} = User.block(user, other_user)
205
206 conn =
207 conn
208 |> assign(:user, user)
209 |> get("/api/v1/blocks")
210
211 other_user_id = to_string(other_user.id)
212 assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
213 end
214
215 test "unimplemented follow_requests, blocks, domain blocks" do
216 user = insert(:user)
217
218 ["blocks", "domain_blocks", "follow_requests"]
219 |> Enum.each(fn endpoint ->
220 conn =
221 build_conn()
222 |> assign(:user, user)
223 |> get("/api/v1/#{endpoint}")
224
225 assert [] = json_response(conn, 200)
226 end)
227 end
228
229 test "returns the favorites of a user", %{conn: conn} do
230 user = insert(:user)
231 other_user = insert(:user)
232
233 {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})
234 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"})
235
236 {:ok, _, _} = CommonAPI.favorite(activity.id, user)
237
238 first_conn =
239 conn
240 |> assign(:user, user)
241 |> get("/api/v1/favourites")
242
243 assert [status] = json_response(first_conn, 200)
244 assert status["id"] == to_string(activity.id)
245
246 assert [{"link", _link_header}] =
247 Enum.filter(first_conn.resp_headers, fn element -> match?({"link", _}, element) end)
248
249 # Honours query params
250 {:ok, second_activity} =
251 CommonAPI.post(other_user, %{
252 "status" =>
253 "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."
254 })
255
256 {:ok, _, _} = CommonAPI.favorite(second_activity.id, user)
257
258 last_like = status["id"]
259
260 second_conn =
261 conn
262 |> assign(:user, user)
263 |> get("/api/v1/favourites?since_id=#{last_like}")
264
265 assert [second_status] = json_response(second_conn, 200)
266 assert second_status["id"] == to_string(second_activity.id)
267
268 third_conn =
269 conn
270 |> assign(:user, user)
271 |> get("/api/v1/favourites?limit=0")
272
273 assert [] = json_response(third_conn, 200)
274 end
275
276 test "get instance information", %{conn: conn} do
277 conn = get(conn, "/api/v1/instance")
278 assert result = json_response(conn, 200)
279
280 email = Config.get([:instance, :email])
281 # Note: not checking for "max_toot_chars" since it's optional
282 assert %{
283 "uri" => _,
284 "title" => _,
285 "description" => _,
286 "version" => _,
287 "email" => from_config_email,
288 "urls" => %{
289 "streaming_api" => _
290 },
291 "stats" => _,
292 "thumbnail" => _,
293 "languages" => _,
294 "registrations" => _,
295 "poll_limits" => _
296 } = result
297
298 assert email == from_config_email
299 end
300
301 test "get instance stats", %{conn: conn} do
302 user = insert(:user, %{local: true})
303
304 user2 = insert(:user, %{local: true})
305 {:ok, _user2} = User.deactivate(user2, !user2.info.deactivated)
306
307 insert(:user, %{local: false, nickname: "u@peer1.com"})
308 insert(:user, %{local: false, nickname: "u@peer2.com"})
309
310 {:ok, _} = CommonAPI.post(user, %{"status" => "cofe"})
311
312 # Stats should count users with missing or nil `info.deactivated` value
313
314 {:ok, _user} =
315 user.id
316 |> User.get_cached_by_id()
317 |> User.update_info(&Changeset.change(&1, %{deactivated: nil}))
318
319 Pleroma.Stats.force_update()
320
321 conn = get(conn, "/api/v1/instance")
322
323 assert result = json_response(conn, 200)
324
325 stats = result["stats"]
326
327 assert stats
328 assert stats["user_count"] == 1
329 assert stats["status_count"] == 1
330 assert stats["domain_count"] == 2
331 end
332
333 test "get peers", %{conn: conn} do
334 insert(:user, %{local: false, nickname: "u@peer1.com"})
335 insert(:user, %{local: false, nickname: "u@peer2.com"})
336
337 Pleroma.Stats.force_update()
338
339 conn = get(conn, "/api/v1/instance/peers")
340
341 assert result = json_response(conn, 200)
342
343 assert ["peer1.com", "peer2.com"] == Enum.sort(result)
344 end
345
346 test "put settings", %{conn: conn} do
347 user = insert(:user)
348
349 conn =
350 conn
351 |> assign(:user, user)
352 |> put("/api/web/settings", %{"data" => %{"programming" => "socks"}})
353
354 assert _result = json_response(conn, 200)
355
356 user = User.get_cached_by_ap_id(user.ap_id)
357 assert user.info.settings == %{"programming" => "socks"}
358 end
359
360 describe "link headers" do
361 test "preserves parameters in link headers", %{conn: conn} do
362 user = insert(:user)
363 other_user = insert(:user)
364
365 {:ok, activity1} =
366 CommonAPI.post(other_user, %{
367 "status" => "hi @#{user.nickname}",
368 "visibility" => "public"
369 })
370
371 {:ok, activity2} =
372 CommonAPI.post(other_user, %{
373 "status" => "hi @#{user.nickname}",
374 "visibility" => "public"
375 })
376
377 notification1 = Repo.get_by(Notification, activity_id: activity1.id)
378 notification2 = Repo.get_by(Notification, activity_id: activity2.id)
379
380 conn =
381 conn
382 |> assign(:user, user)
383 |> get("/api/v1/notifications", %{media_only: true})
384
385 assert [link_header] = get_resp_header(conn, "link")
386 assert link_header =~ ~r/media_only=true/
387 assert link_header =~ ~r/min_id=#{notification2.id}/
388 assert link_header =~ ~r/max_id=#{notification1.id}/
389 end
390 end
391
392 describe "custom emoji" do
393 test "with tags", %{conn: conn} do
394 [emoji | _body] =
395 conn
396 |> get("/api/v1/custom_emojis")
397 |> json_response(200)
398
399 assert Map.has_key?(emoji, "shortcode")
400 assert Map.has_key?(emoji, "static_url")
401 assert Map.has_key?(emoji, "tags")
402 assert is_list(emoji["tags"])
403 assert Map.has_key?(emoji, "category")
404 assert Map.has_key?(emoji, "url")
405 assert Map.has_key?(emoji, "visible_in_picker")
406 end
407 end
408
409 describe "index/2 redirections" do
410 setup %{conn: conn} do
411 session_opts = [
412 store: :cookie,
413 key: "_test",
414 signing_salt: "cooldude"
415 ]
416
417 conn =
418 conn
419 |> Plug.Session.call(Plug.Session.init(session_opts))
420 |> fetch_session()
421
422 test_path = "/web/statuses/test"
423 %{conn: conn, path: test_path}
424 end
425
426 test "redirects not logged-in users to the login page", %{conn: conn, path: path} do
427 conn = get(conn, path)
428
429 assert conn.status == 302
430 assert redirected_to(conn) == "/web/login"
431 end
432
433 test "redirects not logged-in users to the login page on private instances", %{
434 conn: conn,
435 path: path
436 } do
437 Config.put([:instance, :public], false)
438
439 conn = get(conn, path)
440
441 assert conn.status == 302
442 assert redirected_to(conn) == "/web/login"
443 end
444
445 test "does not redirect logged in users to the login page", %{conn: conn, path: path} do
446 token = insert(:oauth_token)
447
448 conn =
449 conn
450 |> assign(:user, token.user)
451 |> put_session(:oauth_token, token.token)
452 |> get(path)
453
454 assert conn.status == 200
455 end
456
457 test "saves referer path to session", %{conn: conn, path: path} do
458 conn = get(conn, path)
459 return_to = Plug.Conn.get_session(conn, :return_to)
460
461 assert return_to == path
462 end
463
464 test "redirects to the saved path after log in", %{conn: conn, path: path} do
465 app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
466 auth = insert(:oauth_authorization, app: app)
467
468 conn =
469 conn
470 |> put_session(:return_to, path)
471 |> get("/web/login", %{code: auth.token})
472
473 assert conn.status == 302
474 assert redirected_to(conn) == path
475 end
476
477 test "redirects to the getting-started page when referer is not present", %{conn: conn} do
478 app = insert(:oauth_app, client_name: "Mastodon-Local", redirect_uris: ".")
479 auth = insert(:oauth_authorization, app: app)
480
481 conn = get(conn, "/web/login", %{code: auth.token})
482
483 assert conn.status == 302
484 assert redirected_to(conn) == "/web/getting-started"
485 end
486 end
487
488 describe "GET /api/v1/polls/:id" do
489 test "returns poll entity for object id", %{conn: conn} do
490 user = insert(:user)
491
492 {:ok, activity} =
493 CommonAPI.post(user, %{
494 "status" => "Pleroma does",
495 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20}
496 })
497
498 object = Object.normalize(activity)
499
500 conn =
501 conn
502 |> assign(:user, user)
503 |> get("/api/v1/polls/#{object.id}")
504
505 response = json_response(conn, 200)
506 id = to_string(object.id)
507 assert %{"id" => ^id, "expired" => false, "multiple" => false} = response
508 end
509
510 test "does not expose polls for private statuses", %{conn: conn} do
511 user = insert(:user)
512 other_user = insert(:user)
513
514 {:ok, activity} =
515 CommonAPI.post(user, %{
516 "status" => "Pleroma does",
517 "poll" => %{"options" => ["what Mastodon't", "n't what Mastodoes"], "expires_in" => 20},
518 "visibility" => "private"
519 })
520
521 object = Object.normalize(activity)
522
523 conn =
524 conn
525 |> assign(:user, other_user)
526 |> get("/api/v1/polls/#{object.id}")
527
528 assert json_response(conn, 404)
529 end
530 end
531
532 describe "POST /api/v1/polls/:id/votes" do
533 test "votes are added to the poll", %{conn: conn} do
534 user = insert(:user)
535 other_user = insert(:user)
536
537 {:ok, activity} =
538 CommonAPI.post(user, %{
539 "status" => "A very delicious sandwich",
540 "poll" => %{
541 "options" => ["Lettuce", "Grilled Bacon", "Tomato"],
542 "expires_in" => 20,
543 "multiple" => true
544 }
545 })
546
547 object = Object.normalize(activity)
548
549 conn =
550 conn
551 |> assign(:user, other_user)
552 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
553
554 assert json_response(conn, 200)
555 object = Object.get_by_id(object.id)
556
557 assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
558 total_items == 1
559 end)
560 end
561
562 test "author can't vote", %{conn: conn} do
563 user = insert(:user)
564
565 {:ok, activity} =
566 CommonAPI.post(user, %{
567 "status" => "Am I cute?",
568 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
569 })
570
571 object = Object.normalize(activity)
572
573 assert conn
574 |> assign(:user, user)
575 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [1]})
576 |> json_response(422) == %{"error" => "Poll's author can't vote"}
577
578 object = Object.get_by_id(object.id)
579
580 refute Enum.at(object.data["oneOf"], 1)["replies"]["totalItems"] == 1
581 end
582
583 test "does not allow multiple choices on a single-choice question", %{conn: conn} do
584 user = insert(:user)
585 other_user = insert(:user)
586
587 {:ok, activity} =
588 CommonAPI.post(user, %{
589 "status" => "The glass is",
590 "poll" => %{"options" => ["half empty", "half full"], "expires_in" => 20}
591 })
592
593 object = Object.normalize(activity)
594
595 assert conn
596 |> assign(:user, other_user)
597 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1]})
598 |> json_response(422) == %{"error" => "Too many choices"}
599
600 object = Object.get_by_id(object.id)
601
602 refute Enum.any?(object.data["oneOf"], fn %{"replies" => %{"totalItems" => total_items}} ->
603 total_items == 1
604 end)
605 end
606
607 test "does not allow choice index to be greater than options count", %{conn: conn} do
608 user = insert(:user)
609 other_user = insert(:user)
610
611 {:ok, activity} =
612 CommonAPI.post(user, %{
613 "status" => "Am I cute?",
614 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20}
615 })
616
617 object = Object.normalize(activity)
618
619 conn =
620 conn
621 |> assign(:user, other_user)
622 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [2]})
623
624 assert json_response(conn, 422) == %{"error" => "Invalid indices"}
625 end
626
627 test "returns 404 error when object is not exist", %{conn: conn} do
628 user = insert(:user)
629
630 conn =
631 conn
632 |> assign(:user, user)
633 |> post("/api/v1/polls/1/votes", %{"choices" => [0]})
634
635 assert json_response(conn, 404) == %{"error" => "Record not found"}
636 end
637
638 test "returns 404 when poll is private and not available for user", %{conn: conn} do
639 user = insert(:user)
640 other_user = insert(:user)
641
642 {:ok, activity} =
643 CommonAPI.post(user, %{
644 "status" => "Am I cute?",
645 "poll" => %{"options" => ["Yes", "No"], "expires_in" => 20},
646 "visibility" => "private"
647 })
648
649 object = Object.normalize(activity)
650
651 conn =
652 conn
653 |> assign(:user, other_user)
654 |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0]})
655
656 assert json_response(conn, 404) == %{"error" => "Record not found"}
657 end
658 end
659
660 describe "POST /auth/password, with valid parameters" do
661 setup %{conn: conn} do
662 user = insert(:user)
663 conn = post(conn, "/auth/password?email=#{user.email}")
664 %{conn: conn, user: user}
665 end
666
667 test "it returns 204", %{conn: conn} do
668 assert json_response(conn, :no_content)
669 end
670
671 test "it creates a PasswordResetToken record for user", %{user: user} do
672 token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
673 assert token_record
674 end
675
676 test "it sends an email to user", %{user: user} do
677 ObanHelpers.perform_all()
678 token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
679
680 email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token)
681 notify_email = Config.get([:instance, :notify_email])
682 instance_name = Config.get([:instance, :name])
683
684 assert_email_sent(
685 from: {instance_name, notify_email},
686 to: {user.name, user.email},
687 html_body: email.html_body
688 )
689 end
690 end
691
692 describe "POST /auth/password, with invalid parameters" do
693 setup do
694 user = insert(:user)
695 {:ok, user: user}
696 end
697
698 test "it returns 404 when user is not found", %{conn: conn, user: user} do
699 conn = post(conn, "/auth/password?email=nonexisting_#{user.email}")
700 assert conn.status == 404
701 assert conn.resp_body == ""
702 end
703
704 test "it returns 400 when user is not local", %{conn: conn, user: user} do
705 {:ok, user} = Repo.update(Changeset.change(user, local: false))
706 conn = post(conn, "/auth/password?email=#{user.email}")
707 assert conn.status == 400
708 assert conn.resp_body == ""
709 end
710 end
711
712 describe "GET /api/v1/suggestions" do
713 setup do
714 user = insert(:user)
715 other_user = insert(:user)
716 host = Config.get([Pleroma.Web.Endpoint, :url, :host])
717 url500 = "http://test500?#{host}&#{user.nickname}"
718 url200 = "http://test200?#{host}&#{user.nickname}"
719
720 mock(fn
721 %{method: :get, url: ^url500} ->
722 %Tesla.Env{status: 500, body: "bad request"}
723
724 %{method: :get, url: ^url200} ->
725 %Tesla.Env{
726 status: 200,
727 body:
728 ~s([{"acct":"yj455","avatar":"https://social.heldscal.la/avatar/201.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/201.jpeg"}, {"acct":"#{
729 other_user.ap_id
730 }","avatar":"https://social.heldscal.la/avatar/202.jpeg","avatar_static":"https://social.heldscal.la/avatar/s/202.jpeg"}])
731 }
732 end)
733
734 [user: user, other_user: other_user]
735 end
736
737 clear_config(:suggestions)
738
739 test "returns empty result when suggestions disabled", %{conn: conn, user: user} do
740 Config.put([:suggestions, :enabled], false)
741
742 res =
743 conn
744 |> assign(:user, user)
745 |> get("/api/v1/suggestions")
746 |> json_response(200)
747
748 assert res == []
749 end
750
751 test "returns error", %{conn: conn, user: user} do
752 Config.put([:suggestions, :enabled], true)
753 Config.put([:suggestions, :third_party_engine], "http://test500?{{host}}&{{user}}")
754
755 assert capture_log(fn ->
756 res =
757 conn
758 |> assign(:user, user)
759 |> get("/api/v1/suggestions")
760 |> json_response(500)
761
762 assert res == "Something went wrong"
763 end) =~ "Could not retrieve suggestions"
764 end
765
766 test "returns suggestions", %{conn: conn, user: user, other_user: other_user} do
767 Config.put([:suggestions, :enabled], true)
768 Config.put([:suggestions, :third_party_engine], "http://test200?{{host}}&{{user}}")
769
770 res =
771 conn
772 |> assign(:user, user)
773 |> get("/api/v1/suggestions")
774 |> json_response(200)
775
776 assert res == [
777 %{
778 "acct" => "yj455",
779 "avatar" => "https://social.heldscal.la/avatar/201.jpeg",
780 "avatar_static" => "https://social.heldscal.la/avatar/s/201.jpeg",
781 "id" => 0
782 },
783 %{
784 "acct" => other_user.ap_id,
785 "avatar" => "https://social.heldscal.la/avatar/202.jpeg",
786 "avatar_static" => "https://social.heldscal.la/avatar/s/202.jpeg",
787 "id" => other_user.id
788 }
789 ]
790 end
791 end
792
793 describe "PUT /api/v1/media/:id" do
794 setup do
795 actor = insert(:user)
796
797 file = %Plug.Upload{
798 content_type: "image/jpg",
799 path: Path.absname("test/fixtures/image.jpg"),
800 filename: "an_image.jpg"
801 }
802
803 {:ok, %Object{} = object} =
804 ActivityPub.upload(
805 file,
806 actor: User.ap_id(actor),
807 description: "test-m"
808 )
809
810 [actor: actor, object: object]
811 end
812
813 test "updates name of media", %{conn: conn, actor: actor, object: object} do
814 media =
815 conn
816 |> assign(:user, actor)
817 |> put("/api/v1/media/#{object.id}", %{"description" => "test-media"})
818 |> json_response(:ok)
819
820 assert media["description"] == "test-media"
821 assert refresh_record(object).data["name"] == "test-media"
822 end
823
824 test "returns error wheb request is bad", %{conn: conn, actor: actor, object: object} do
825 media =
826 conn
827 |> assign(:user, actor)
828 |> put("/api/v1/media/#{object.id}", %{})
829 |> json_response(400)
830
831 assert media == %{"error" => "bad_request"}
832 end
833 end
834
835 describe "DELETE /auth/sign_out" do
836 test "redirect to root page", %{conn: conn} do
837 user = insert(:user)
838
839 conn =
840 conn
841 |> assign(:user, user)
842 |> delete("/auth/sign_out")
843
844 assert conn.status == 302
845 assert redirected_to(conn) == "/"
846 end
847 end
848
849 describe "empty_array, stubs for mastodon api" do
850 test "GET /api/v1/accounts/:id/identity_proofs", %{conn: conn} do
851 user = insert(:user)
852
853 res =
854 conn
855 |> assign(:user, user)
856 |> get("/api/v1/accounts/#{user.id}/identity_proofs")
857 |> json_response(200)
858
859 assert res == []
860 end
861
862 test "GET /api/v1/endorsements", %{conn: conn} do
863 user = insert(:user)
864
865 res =
866 conn
867 |> assign(:user, user)
868 |> get("/api/v1/endorsements")
869 |> json_response(200)
870
871 assert res == []
872 end
873
874 test "GET /api/v1/trends", %{conn: conn} do
875 user = insert(:user)
876
877 res =
878 conn
879 |> assign(:user, user)
880 |> get("/api/v1/trends")
881 |> json_response(200)
882
883 assert res == []
884 end
885 end
886 end