1 defmodule Pleroma.Web.TwitterAPI.ControllerTest do
2 use Pleroma.Web.ConnCase
3 alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
4 alias Pleroma.Builders.{ActivityBuilder, UserBuilder}
5 alias Pleroma.{Repo, Activity, User, Object, Notification}
6 alias Pleroma.Web.ActivityPub.ActivityPub
7 alias Pleroma.Web.TwitterAPI.UserView
8 alias Pleroma.Web.TwitterAPI.NotificationView
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Web.TwitterAPI.TwitterAPI
13 import Pleroma.Factory
15 describe "POST /api/account/verify_credentials" do
18 test "without valid credentials", %{conn: conn} do
19 conn = post(conn, "/api/account/verify_credentials.json")
20 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
23 test "with credentials", %{conn: conn, user: user} do
26 |> with_credentials(user.nickname, "test")
27 |> post("/api/account/verify_credentials.json")
29 assert response = json_response(conn, 200)
30 assert response == UserView.render("show.json", %{user: user, token: response["token"]})
34 describe "POST /statuses/update.json" do
37 test "without valid credentials", %{conn: conn} do
38 conn = post(conn, "/api/statuses/update.json")
39 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
42 test "with credentials", %{conn: conn, user: user} do
43 conn_with_creds = conn |> with_credentials(user.nickname, "test")
44 request_path = "/api/statuses/update.json"
47 "request" => request_path,
48 "error" => "Client must provide a 'status' parameter with a value."
51 conn = conn_with_creds |> post(request_path)
52 assert json_response(conn, 400) == error_response
54 conn = conn_with_creds |> post(request_path, %{status: ""})
55 assert json_response(conn, 400) == error_response
57 conn = conn_with_creds |> post(request_path, %{status: " "})
58 assert json_response(conn, 400) == error_response
60 # we post with visibility private in order to avoid triggering relay
61 conn = conn_with_creds |> post(request_path, %{status: "Nice meme.", visibility: "private"})
63 assert json_response(conn, 200) ==
64 ActivityRepresenter.to_map(Repo.one(Activity), %{user: user})
68 describe "GET /statuses/public_timeline.json" do
69 test "returns statuses", %{conn: conn} do
70 {:ok, user} = UserBuilder.insert()
71 activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
72 ActivityBuilder.insert_list(10, %{}, %{user: user})
73 since_id = List.last(activities).id
77 |> get("/api/statuses/public_timeline.json", %{since_id: since_id})
79 response = json_response(conn, 200)
81 assert length(response) == 10
84 test "returns 403 to unauthenticated request when the instance is not public" do
86 Application.get_env(:pleroma, :instance)
87 |> Keyword.put(:public, false)
89 Application.put_env(:pleroma, :instance, instance)
92 |> get("/api/statuses/public_timeline.json")
96 Application.get_env(:pleroma, :instance)
97 |> Keyword.put(:public, true)
99 Application.put_env(:pleroma, :instance, instance)
102 test "returns 200 to unauthenticated request when the instance is public" do
104 |> get("/api/statuses/public_timeline.json")
105 |> json_response(200)
109 describe "GET /statuses/public_and_external_timeline.json" do
110 test "returns 403 to unauthenticated request when the instance is not public" do
112 Application.get_env(:pleroma, :instance)
113 |> Keyword.put(:public, false)
115 Application.put_env(:pleroma, :instance, instance)
118 |> get("/api/statuses/public_and_external_timeline.json")
119 |> json_response(403)
122 Application.get_env(:pleroma, :instance)
123 |> Keyword.put(:public, true)
125 Application.put_env(:pleroma, :instance, instance)
128 test "returns 200 to unauthenticated request when the instance is public" do
130 |> get("/api/statuses/public_and_external_timeline.json")
131 |> json_response(200)
135 describe "GET /statuses/show/:id.json" do
136 test "returns one status", %{conn: conn} do
138 {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"})
139 actor = Repo.get_by!(User, ap_id: activity.data["actor"])
143 |> get("/api/statuses/show/#{activity.id}.json")
145 response = json_response(conn, 200)
147 assert response == ActivityRepresenter.to_map(activity, %{user: actor})
151 describe "GET /users/show.json" do
152 test "gets user with screen_name", %{conn: conn} do
157 |> get("/api/users/show.json", %{"screen_name" => user.nickname})
159 response = json_response(conn, 200)
161 assert response["id"] == user.id
164 test "gets user with user_id", %{conn: conn} do
169 |> get("/api/users/show.json", %{"user_id" => user.id})
171 response = json_response(conn, 200)
173 assert response["id"] == user.id
176 test "gets a user for a logged in user", %{conn: conn} do
178 logged_in = insert(:user)
180 {:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id})
184 |> with_credentials(logged_in.nickname, "test")
185 |> get("/api/users/show.json", %{"user_id" => user.id})
187 response = json_response(conn, 200)
189 assert response["following"] == true
193 describe "GET /statusnet/conversation/:id.json" do
194 test "returns the statuses in the conversation", %{conn: conn} do
195 {:ok, _user} = UserBuilder.insert()
196 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
197 {:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
198 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
202 |> get("/api/statusnet/conversation/#{activity.data["context_id"]}.json")
204 response = json_response(conn, 200)
206 assert length(response) == 2
210 describe "GET /statuses/friends_timeline.json" do
213 test "without valid credentials", %{conn: conn} do
214 conn = get(conn, "/api/statuses/friends_timeline.json")
215 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
218 test "with credentials", %{conn: conn, user: current_user} do
222 ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user})
224 returned_activities =
225 ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user})
227 other_user = insert(:user)
228 ActivityBuilder.insert_list(10, %{}, %{user: other_user})
229 since_id = List.last(activities).id
232 Ecto.Changeset.change(current_user, following: [User.ap_followers(user)])
237 |> with_credentials(current_user.nickname, "test")
238 |> get("/api/statuses/friends_timeline.json", %{since_id: since_id})
240 response = json_response(conn, 200)
242 assert length(response) == 10
245 Enum.map(returned_activities, fn activity ->
246 ActivityRepresenter.to_map(activity, %{
247 user: User.get_cached_by_ap_id(activity.data["actor"]),
254 describe "GET /statuses/dm_timeline.json" do
255 test "it show direct messages", %{conn: conn} do
256 user_one = insert(:user)
257 user_two = insert(:user)
259 {:ok, user_two} = User.follow(user_two, user_one)
262 CommonAPI.post(user_one, %{
263 "status" => "Hi @#{user_two.nickname}!",
264 "visibility" => "direct"
268 CommonAPI.post(user_two, %{
269 "status" => "Hi @#{user_one.nickname}!",
270 "visibility" => "direct"
273 {:ok, _follower_only} =
274 CommonAPI.post(user_one, %{
275 "status" => "Hi @#{user_two.nickname}!",
276 "visibility" => "private"
279 # Only direct should be visible here
282 |> assign(:user, user_two)
283 |> get("/api/statuses/dm_timeline.json")
285 [status, status_two] = json_response(res_conn, 200)
286 assert status["id"] == direct_two.id
287 assert status_two["id"] == direct.id
291 describe "GET /statuses/mentions.json" do
294 test "without valid credentials", %{conn: conn} do
295 conn = get(conn, "/api/statuses/mentions.json")
296 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
299 test "with credentials", %{conn: conn, user: current_user} do
301 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
305 |> with_credentials(current_user.nickname, "test")
306 |> get("/api/statuses/mentions.json")
308 response = json_response(conn, 200)
310 assert length(response) == 1
312 assert Enum.at(response, 0) ==
313 ActivityRepresenter.to_map(activity, %{
315 mentioned: [current_user]
320 describe "GET /api/qvitter/statuses/notifications.json" do
323 test "without valid credentials", %{conn: conn} do
324 conn = get(conn, "/api/qvitter/statuses/notifications.json")
325 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
328 test "with credentials", %{conn: conn, user: current_user} do
329 other_user = insert(:user)
332 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
336 |> with_credentials(current_user.nickname, "test")
337 |> get("/api/qvitter/statuses/notifications.json")
339 response = json_response(conn, 200)
341 assert length(response) == 1
344 NotificationView.render("notification.json", %{
345 notifications: Notification.for_user(current_user),
351 describe "POST /api/qvitter/statuses/notifications/read" do
354 test "without valid credentials", %{conn: conn} do
355 conn = post(conn, "/api/qvitter/statuses/notifications/read", %{"latest_id" => 1_234_567})
356 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
359 test "with credentials, without any params", %{conn: conn, user: current_user} do
362 |> with_credentials(current_user.nickname, "test")
363 |> post("/api/qvitter/statuses/notifications/read")
365 assert json_response(conn, 400) == %{
366 "error" => "You need to specify latest_id",
367 "request" => "/api/qvitter/statuses/notifications/read"
371 test "with credentials, with params", %{conn: conn, user: current_user} do
372 other_user = insert(:user)
375 ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user})
379 |> with_credentials(current_user.nickname, "test")
380 |> get("/api/qvitter/statuses/notifications.json")
382 [notification] = response = json_response(response_conn, 200)
384 assert length(response) == 1
386 assert notification["is_seen"] == 0
390 |> with_credentials(current_user.nickname, "test")
391 |> post("/api/qvitter/statuses/notifications/read", %{"latest_id" => notification["id"]})
393 [notification] = response = json_response(response_conn, 200)
395 assert length(response) == 1
397 assert notification["is_seen"] == 1
401 describe "GET /statuses/user_timeline.json" do
404 test "without any params", %{conn: conn} do
405 conn = get(conn, "/api/statuses/user_timeline.json")
407 assert json_response(conn, 400) == %{
408 "error" => "You need to specify screen_name or user_id",
409 "request" => "/api/statuses/user_timeline.json"
413 test "with user_id", %{conn: conn} do
415 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
417 conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
418 response = json_response(conn, 200)
419 assert length(response) == 1
420 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
423 test "with screen_name", %{conn: conn} do
425 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
427 conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
428 response = json_response(conn, 200)
429 assert length(response) == 1
430 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
433 test "with credentials", %{conn: conn, user: current_user} do
434 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
438 |> with_credentials(current_user.nickname, "test")
439 |> get("/api/statuses/user_timeline.json")
441 response = json_response(conn, 200)
443 assert length(response) == 1
444 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
447 test "with credentials with user_id", %{conn: conn, user: current_user} do
449 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
453 |> with_credentials(current_user.nickname, "test")
454 |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
456 response = json_response(conn, 200)
458 assert length(response) == 1
459 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
462 test "with credentials screen_name", %{conn: conn, user: current_user} do
464 {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
468 |> with_credentials(current_user.nickname, "test")
469 |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
471 response = json_response(conn, 200)
473 assert length(response) == 1
474 assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
478 describe "POST /friendships/create.json" do
481 test "without valid credentials", %{conn: conn} do
482 conn = post(conn, "/api/friendships/create.json")
483 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
486 test "with credentials", %{conn: conn, user: current_user} do
487 followed = insert(:user)
491 |> with_credentials(current_user.nickname, "test")
492 |> post("/api/friendships/create.json", %{user_id: followed.id})
494 current_user = Repo.get(User, current_user.id)
495 assert User.ap_followers(followed) in current_user.following
497 assert json_response(conn, 200) ==
498 UserView.render("show.json", %{user: followed, for: current_user})
502 describe "POST /friendships/destroy.json" do
505 test "without valid credentials", %{conn: conn} do
506 conn = post(conn, "/api/friendships/destroy.json")
507 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
510 test "with credentials", %{conn: conn, user: current_user} do
511 followed = insert(:user)
513 {:ok, current_user} = User.follow(current_user, followed)
514 assert User.ap_followers(followed) in current_user.following
515 ActivityPub.follow(current_user, followed)
519 |> with_credentials(current_user.nickname, "test")
520 |> post("/api/friendships/destroy.json", %{user_id: followed.id})
522 current_user = Repo.get(User, current_user.id)
523 assert current_user.following == [current_user.ap_id]
525 assert json_response(conn, 200) ==
526 UserView.render("show.json", %{user: followed, for: current_user})
530 describe "POST /blocks/create.json" do
533 test "without valid credentials", %{conn: conn} do
534 conn = post(conn, "/api/blocks/create.json")
535 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
538 test "with credentials", %{conn: conn, user: current_user} do
539 blocked = insert(:user)
543 |> with_credentials(current_user.nickname, "test")
544 |> post("/api/blocks/create.json", %{user_id: blocked.id})
546 current_user = Repo.get(User, current_user.id)
547 assert User.blocks?(current_user, blocked)
549 assert json_response(conn, 200) ==
550 UserView.render("show.json", %{user: blocked, for: current_user})
554 describe "POST /blocks/destroy.json" do
557 test "without valid credentials", %{conn: conn} do
558 conn = post(conn, "/api/blocks/destroy.json")
559 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
562 test "with credentials", %{conn: conn, user: current_user} do
563 blocked = insert(:user)
565 {:ok, current_user, blocked} = TwitterAPI.block(current_user, %{"user_id" => blocked.id})
566 assert User.blocks?(current_user, blocked)
570 |> with_credentials(current_user.nickname, "test")
571 |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
573 current_user = Repo.get(User, current_user.id)
574 assert current_user.info["blocks"] == []
576 assert json_response(conn, 200) ==
577 UserView.render("show.json", %{user: blocked, for: current_user})
581 describe "GET /help/test.json" do
582 test "returns \"ok\"", %{conn: conn} do
583 conn = get(conn, "/api/help/test.json")
584 assert json_response(conn, 200) == "ok"
588 describe "POST /api/qvitter/update_avatar.json" do
591 test "without valid credentials", %{conn: conn} do
592 conn = post(conn, "/api/qvitter/update_avatar.json")
593 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
596 test "with credentials", %{conn: conn, user: current_user} do
597 avatar_image = File.read!("test/fixtures/avatar_data_uri")
601 |> with_credentials(current_user.nickname, "test")
602 |> post("/api/qvitter/update_avatar.json", %{img: avatar_image})
604 current_user = Repo.get(User, current_user.id)
605 assert is_map(current_user.avatar)
607 assert json_response(conn, 200) ==
608 UserView.render("show.json", %{user: current_user, for: current_user})
612 describe "GET /api/qvitter/mutes.json" do
615 test "unimplemented mutes without valid credentials", %{conn: conn} do
616 conn = get(conn, "/api/qvitter/mutes.json")
617 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
620 test "unimplemented mutes with credentials", %{conn: conn, user: current_user} do
623 |> with_credentials(current_user.nickname, "test")
624 |> get("/api/qvitter/mutes.json")
626 current_user = Repo.get(User, current_user.id)
628 assert [] = json_response(conn, 200)
632 describe "POST /api/favorites/create/:id" do
635 test "without valid credentials", %{conn: conn} do
636 note_activity = insert(:note_activity)
637 conn = post(conn, "/api/favorites/create/#{note_activity.id}.json")
638 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
641 test "with credentials", %{conn: conn, user: current_user} do
642 note_activity = insert(:note_activity)
646 |> with_credentials(current_user.nickname, "test")
647 |> post("/api/favorites/create/#{note_activity.id}.json")
649 assert json_response(conn, 200)
652 test "with credentials, invalid param", %{conn: conn, user: current_user} do
655 |> with_credentials(current_user.nickname, "test")
656 |> post("/api/favorites/create/wrong.json")
658 assert json_response(conn, 400)
661 test "with credentials, invalid activity", %{conn: conn, user: current_user} do
664 |> with_credentials(current_user.nickname, "test")
665 |> post("/api/favorites/create/1.json")
667 assert json_response(conn, 500)
671 describe "POST /api/favorites/destroy/:id" do
674 test "without valid credentials", %{conn: conn} do
675 note_activity = insert(:note_activity)
676 conn = post(conn, "/api/favorites/destroy/#{note_activity.id}.json")
677 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
680 test "with credentials", %{conn: conn, user: current_user} do
681 note_activity = insert(:note_activity)
682 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
683 ActivityPub.like(current_user, object)
687 |> with_credentials(current_user.nickname, "test")
688 |> post("/api/favorites/destroy/#{note_activity.id}.json")
690 assert json_response(conn, 200)
694 describe "POST /api/statuses/retweet/:id" do
697 test "without valid credentials", %{conn: conn} do
698 note_activity = insert(:note_activity)
699 conn = post(conn, "/api/statuses/retweet/#{note_activity.id}.json")
700 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
703 test "with credentials", %{conn: conn, user: current_user} do
704 note_activity = insert(:note_activity)
706 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
710 |> with_credentials(current_user.nickname, "test")
711 |> post(request_path)
713 activity = Repo.get(Activity, note_activity.id)
714 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
716 assert json_response(response, 200) ==
717 ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
721 describe "POST /api/statuses/unretweet/:id" do
724 test "without valid credentials", %{conn: conn} do
725 note_activity = insert(:note_activity)
726 conn = post(conn, "/api/statuses/unretweet/#{note_activity.id}.json")
727 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
730 test "with credentials", %{conn: conn, user: current_user} do
731 note_activity = insert(:note_activity)
733 request_path = "/api/statuses/retweet/#{note_activity.id}.json"
737 |> with_credentials(current_user.nickname, "test")
738 |> post(request_path)
740 request_path = String.replace(request_path, "retweet", "unretweet")
744 |> with_credentials(current_user.nickname, "test")
745 |> post(request_path)
747 activity = Repo.get(Activity, note_activity.id)
748 activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
750 assert json_response(response, 200) ==
751 ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
755 describe "POST /api/account/register" do
756 test "it creates a new user", %{conn: conn} do
758 "nickname" => "lain",
759 "email" => "lain@wired.jp",
760 "fullname" => "lain iwakura",
761 "bio" => "close the world.",
762 "password" => "bear",
768 |> post("/api/account/register", data)
770 user = json_response(conn, 200)
772 fetched_user = Repo.get_by(User, nickname: "lain")
773 assert user == UserView.render("show.json", %{user: fetched_user})
776 test "it returns errors on a problem", %{conn: conn} do
778 "email" => "lain@wired.jp",
779 "fullname" => "lain iwakura",
780 "bio" => "close the world.",
781 "password" => "bear",
787 |> post("/api/account/register", data)
789 errors = json_response(conn, 400)
791 assert is_binary(errors["error"])
795 describe "GET /api/externalprofile/show" do
796 test "it returns the user", %{conn: conn} do
798 other_user = insert(:user)
802 |> assign(:user, user)
803 |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id})
805 assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user})
809 describe "GET /api/statuses/followers" do
810 test "it returns a user's followers", %{conn: conn} do
812 follower_one = insert(:user)
813 follower_two = insert(:user)
814 _not_follower = insert(:user)
816 {:ok, follower_one} = User.follow(follower_one, user)
817 {:ok, follower_two} = User.follow(follower_two, user)
821 |> assign(:user, user)
822 |> get("/api/statuses/followers")
824 expected = UserView.render("index.json", %{users: [follower_one, follower_two], for: user})
825 result = json_response(conn, 200)
826 assert Enum.sort(expected) == Enum.sort(result)
830 describe "GET /api/statuses/friends" do
831 test "it returns the logged in user's friends", %{conn: conn} do
833 followed_one = insert(:user)
834 followed_two = insert(:user)
835 _not_followed = insert(:user)
837 {:ok, user} = User.follow(user, followed_one)
838 {:ok, user} = User.follow(user, followed_two)
842 |> assign(:user, user)
843 |> get("/api/statuses/friends")
845 expected = UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
846 result = json_response(conn, 200)
847 assert Enum.sort(expected) == Enum.sort(result)
850 test "it returns a given user's friends with user_id", %{conn: conn} do
852 followed_one = insert(:user)
853 followed_two = insert(:user)
854 _not_followed = insert(:user)
856 {:ok, user} = User.follow(user, followed_one)
857 {:ok, user} = User.follow(user, followed_two)
861 |> assign(:user, user)
862 |> get("/api/statuses/friends", %{"user_id" => user.id})
864 assert MapSet.equal?(
865 MapSet.new(json_response(conn, 200)),
867 UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
872 test "it returns a given user's friends with screen_name", %{conn: conn} do
874 followed_one = insert(:user)
875 followed_two = insert(:user)
876 _not_followed = insert(:user)
878 {:ok, user} = User.follow(user, followed_one)
879 {:ok, user} = User.follow(user, followed_two)
883 |> assign(:user, user)
884 |> get("/api/statuses/friends", %{"screen_name" => user.nickname})
886 assert MapSet.equal?(
887 MapSet.new(json_response(conn, 200)),
889 UserView.render("index.json", %{users: [followed_one, followed_two], for: user})
895 describe "GET /friends/ids" do
896 test "it returns a user's friends", %{conn: conn} do
898 followed_one = insert(:user)
899 followed_two = insert(:user)
900 _not_followed = insert(:user)
902 {:ok, user} = User.follow(user, followed_one)
903 {:ok, user} = User.follow(user, followed_two)
907 |> assign(:user, user)
908 |> get("/api/friends/ids")
910 expected = [followed_one.id, followed_two.id]
912 assert MapSet.equal?(
913 MapSet.new(Poison.decode!(json_response(conn, 200))),
919 describe "POST /api/account/update_profile.json" do
920 test "it updates a user's profile", %{conn: conn} do
925 |> assign(:user, user)
926 |> post("/api/account/update_profile.json", %{
927 "name" => "new name",
928 "description" => "new description"
931 user = Repo.get!(User, user.id)
932 assert user.name == "new name"
933 assert user.bio == "new description"
935 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
938 test "it locks an account", %{conn: conn} do
943 |> assign(:user, user)
944 |> post("/api/account/update_profile.json", %{
948 user = Repo.get!(User, user.id)
949 assert user.info["locked"] == true
951 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
954 test "it unlocks an account", %{conn: conn} do
959 |> assign(:user, user)
960 |> post("/api/account/update_profile.json", %{
964 user = Repo.get!(User, user.id)
965 assert user.info["locked"] == false
967 assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
971 defp valid_user(_context) do
976 defp with_credentials(conn, username, password) do
977 header_content = "Basic " <> Base.encode64("#{username}:#{password}")
978 put_req_header(conn, "authorization", header_content)
981 describe "GET /api/search.json" do
982 test "it returns search results", %{conn: conn} do
984 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
986 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
987 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
991 |> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"})
993 assert [status] = json_response(conn, 200)
994 assert status["id"] == activity.id
998 describe "GET /api/statusnet/tags/timeline/:tag.json" do
999 test "it returns the tags timeline", %{conn: conn} do
1000 user = insert(:user)
1001 user_two = insert(:user, %{nickname: "shp@shitposter.club"})
1003 {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about #2hu"})
1004 {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
1008 |> get("/api/statusnet/tags/timeline/2hu.json")
1010 assert [status] = json_response(conn, 200)
1011 assert status["id"] == activity.id
1015 test "Convert newlines to <br> in bio", %{conn: conn} do
1016 user = insert(:user)
1020 |> assign(:user, user)
1021 |> post("/api/account/update_profile.json", %{
1022 "description" => "Hello,\r\nWorld! I\n am a test."
1025 user = Repo.get!(User, user.id)
1026 assert user.bio == "Hello,<br>World! I<br> am a test."
1029 describe "POST /api/pleroma/change_password" do
1032 test "without credentials", %{conn: conn} do
1033 conn = post(conn, "/api/pleroma/change_password")
1034 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
1037 test "with credentials and invalid password", %{conn: conn, user: current_user} do
1040 |> with_credentials(current_user.nickname, "test")
1041 |> post("/api/pleroma/change_password", %{
1043 "new_password" => "newpass",
1044 "new_password_confirmation" => "newpass"
1047 assert json_response(conn, 200) == %{"error" => "Invalid password."}
1050 test "with credentials, valid password and new password and confirmation not matching", %{
1056 |> with_credentials(current_user.nickname, "test")
1057 |> post("/api/pleroma/change_password", %{
1058 "password" => "test",
1059 "new_password" => "newpass",
1060 "new_password_confirmation" => "notnewpass"
1063 assert json_response(conn, 200) == %{
1064 "error" => "New password does not match confirmation."
1068 test "with credentials, valid password and invalid new password", %{
1074 |> with_credentials(current_user.nickname, "test")
1075 |> post("/api/pleroma/change_password", %{
1076 "password" => "test",
1077 "new_password" => "",
1078 "new_password_confirmation" => ""
1081 assert json_response(conn, 200) == %{
1082 "error" => "New password can't be blank."
1086 test "with credentials, valid password and matching new password and confirmation", %{
1092 |> with_credentials(current_user.nickname, "test")
1093 |> post("/api/pleroma/change_password", %{
1094 "password" => "test",
1095 "new_password" => "newpass",
1096 "new_password_confirmation" => "newpass"
1099 assert json_response(conn, 200) == %{"status" => "success"}
1100 fetched_user = Repo.get(User, current_user.id)
1101 assert Pbkdf2.checkpw("newpass", fetched_user.password_hash) == true
1105 describe "POST /api/pleroma/delete_account" do
1108 test "without credentials", %{conn: conn} do
1109 conn = post(conn, "/api/pleroma/delete_account")
1110 assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
1113 test "with credentials and invalid password", %{conn: conn, user: current_user} do
1116 |> with_credentials(current_user.nickname, "test")
1117 |> post("/api/pleroma/delete_account", %{"password" => "hi"})
1119 assert json_response(conn, 200) == %{"error" => "Invalid password."}
1122 test "with credentials and valid password", %{conn: conn, user: current_user} do
1125 |> with_credentials(current_user.nickname, "test")
1126 |> post("/api/pleroma/delete_account", %{"password" => "test"})
1128 assert json_response(conn, 200) == %{"status" => "success"}
1129 # Wait a second for the started task to end
1134 describe "GET /api/pleroma/friend_requests" do
1135 test "it lists friend requests" do
1136 user = insert(:user, %{info: %{"locked" => true}})
1137 other_user = insert(:user)
1139 {:ok, activity} = ActivityPub.follow(other_user, user)
1141 user = Repo.get(User, user.id)
1142 other_user = Repo.get(User, other_user.id)
1144 assert User.following?(other_user, user) == false
1148 |> assign(:user, user)
1149 |> get("/api/pleroma/friend_requests")
1151 assert [relationship] = json_response(conn, 200)
1152 assert other_user.id == relationship["id"]
1156 describe "POST /api/pleroma/friendships/approve" do
1157 test "it approves a friend request" do
1158 user = insert(:user, %{info: %{"locked" => true}})
1159 other_user = insert(:user)
1161 {:ok, activity} = ActivityPub.follow(other_user, user)
1163 user = Repo.get(User, user.id)
1164 other_user = Repo.get(User, other_user.id)
1166 assert User.following?(other_user, user) == false
1170 |> assign(:user, user)
1171 |> post("/api/pleroma/friendships/approve", %{"user_id" => to_string(other_user.id)})
1173 assert relationship = json_response(conn, 200)
1174 assert other_user.id == relationship["id"]
1175 assert relationship["follows_you"] == true
1179 describe "POST /api/pleroma/friendships/deny" do
1180 test "it denies a friend request" do
1181 user = insert(:user, %{info: %{"locked" => true}})
1182 other_user = insert(:user)
1184 {:ok, activity} = ActivityPub.follow(other_user, user)
1186 user = Repo.get(User, user.id)
1187 other_user = Repo.get(User, other_user.id)
1189 assert User.following?(other_user, user) == false
1193 |> assign(:user, user)
1194 |> post("/api/pleroma/friendships/deny", %{"user_id" => to_string(other_user.id)})
1196 assert relationship = json_response(conn, 200)
1197 assert other_user.id == relationship["id"]
1198 assert relationship["follows_you"] == false
1202 describe "GET /api/pleroma/search_user" do
1203 test "it returns users, ordered by similarity", %{conn: conn} do
1204 user = insert(:user, %{name: "eal"})
1205 user_two = insert(:user, %{name: "ean"})
1206 user_three = insert(:user, %{name: "ebn"})
1210 |> get(twitter_api_search__path(conn, :search_user), query: "eal")
1211 |> json_response(200)
1213 assert length(resp) == 3
1214 assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end)