X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Fpleroma%2Fweb%2Fmastodon_api%2Fcontrollers%2Faccount_controller_test.exs;h=2f5bfda985faed8b07f278025aae4e583df7488b;hb=1c4ca20ff7c7a56e4b28b5c1e4cc21f49853bb9c;hp=58ce76ab88b1e7a449973b537057c63ac6b286e4;hpb=77c239f3092af61d0896ae412e9180503dd98b00;p=akkoma diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index 58ce76ab8..29e34546e 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -1,5 +1,5 @@ # Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors +# Copyright © 2017-2021 Pleroma Authors # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do @@ -11,6 +11,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do alias Pleroma.Web.ActivityPub.InternalFetchActor alias Pleroma.Web.CommonAPI alias Pleroma.Web.OAuth.Token + alias Pleroma.Web.Plugs.SetLocalePlug import Pleroma.Factory @@ -29,6 +30,45 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do |> json_response_and_validate_schema(404) end + test "relationship field" do + %{conn: conn, user: user} = oauth_access(["read"]) + + other_user = insert(:user) + + response = + conn + |> get("/api/v1/accounts/#{other_user.id}") + |> json_response_and_validate_schema(200) + + assert response["id"] == other_user.id + assert response["pleroma"]["relationship"] == %{} + + assert %{"pleroma" => %{"relationship" => %{"following" => false, "followed_by" => false}}} = + conn + |> get("/api/v1/accounts/#{other_user.id}?with_relationships=true") + |> json_response_and_validate_schema(200) + + {:ok, _, %{id: other_id}} = User.follow(user, other_user) + + assert %{ + "id" => ^other_id, + "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => false}} + } = + conn + |> get("/api/v1/accounts/#{other_id}?with_relationships=true") + |> json_response_and_validate_schema(200) + + {:ok, _, _} = User.follow(other_user, user) + + assert %{ + "id" => ^other_id, + "pleroma" => %{"relationship" => %{"following" => true, "followed_by" => true}} + } = + conn + |> get("/api/v1/accounts/#{other_id}?with_relationships=true") + |> json_response_and_validate_schema(200) + end + test "works by nickname" do user = insert(:user) @@ -126,7 +166,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do end test "returns 404 for deactivated user", %{conn: conn} do - user = insert(:user, deactivated: true) + user = insert(:user, is_active: false) assert %{"error" => "Can't find user"} = conn @@ -256,7 +296,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do end test "deactivated user", %{conn: conn} do - user = insert(:user, deactivated: true) + user = insert(:user, is_active: false) assert %{"error" => "Can't find user"} == conn @@ -320,7 +360,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do user_two = insert(:user) user_three = insert(:user) - {:ok, _user_three} = User.follow(user_three, user_one) + {:ok, _user_three, _user_one} = User.follow(user_three, user_one) {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!"}) @@ -374,6 +414,20 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert json_response_and_validate_schema(conn, 200) == [] end + test "gets local-only statuses for authenticated users", %{user: _user, conn: conn} do + user_one = insert(:user) + + {:ok, activity} = CommonAPI.post(user_one, %{status: "HI!!!", visibility: "local"}) + + resp = + conn + |> get("/api/v1/accounts/#{user_one.id}/statuses") + |> json_response_and_validate_schema(200) + + assert [%{"id" => id}] = resp + assert id == to_string(activity.id) + end + test "gets an users media, excludes reblogs", %{conn: conn} do note = insert(:note_activity) user = User.get_cached_by_ap_id(note.data["actor"]) @@ -436,6 +490,54 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?exclude_visibilities[]=direct") assert [%{"id" => ^public_activity_id}] = json_response_and_validate_schema(conn, 200) end + + test "muted reactions", %{user: user, conn: conn} do + user2 = insert(:user) + User.mute(user, user2) + {:ok, activity} = CommonAPI.post(user, %{status: "."}) + {:ok, _} = CommonAPI.react_with_emoji(activity.id, user2, "🎅") + + result = + conn + |> get("/api/v1/accounts/#{user.id}/statuses") + |> json_response_and_validate_schema(200) + + assert [ + %{ + "pleroma" => %{ + "emoji_reactions" => [] + } + } + ] = result + + result = + conn + |> get("/api/v1/accounts/#{user.id}/statuses?with_muted=true") + |> json_response_and_validate_schema(200) + + assert [ + %{ + "pleroma" => %{ + "emoji_reactions" => [%{"count" => 1, "me" => false, "name" => "🎅"}] + } + } + ] = result + end + + test "paginates a user's statuses", %{user: user, conn: conn} do + {:ok, post_1} = CommonAPI.post(user, %{status: "first post"}) + {:ok, post_2} = CommonAPI.post(user, %{status: "second post"}) + + response_1 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1") + assert [res] = json_response_and_validate_schema(response_1, 200) + assert res["id"] == post_2.id + + response_2 = get(conn, "/api/v1/accounts/#{user.id}/statuses?limit=1&max_id=#{res["id"]}") + assert [res] = json_response_and_validate_schema(response_2, 200) + assert res["id"] == post_1.id + + refute response_1 == response_2 + end end defp local_and_remote_activities(%{local: local, remote: remote}) do @@ -535,16 +637,55 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do test "getting followers", %{user: user, conn: conn} do other_user = insert(:user) - {:ok, %{id: user_id}} = User.follow(user, other_user) + {:ok, %{id: user_id}, other_user} = User.follow(user, other_user) conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers") assert [%{"id" => ^user_id}] = json_response_and_validate_schema(conn, 200) end + test "following with relationship", %{conn: conn, user: user} do + other_user = insert(:user) + {:ok, %{id: id}, _} = User.follow(other_user, user) + + assert [ + %{ + "id" => ^id, + "pleroma" => %{ + "relationship" => %{ + "id" => ^id, + "following" => false, + "followed_by" => true + } + } + } + ] = + conn + |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true") + |> json_response_and_validate_schema(200) + + {:ok, _, _} = User.follow(user, other_user) + + assert [ + %{ + "id" => ^id, + "pleroma" => %{ + "relationship" => %{ + "id" => ^id, + "following" => true, + "followed_by" => true + } + } + } + ] = + conn + |> get("/api/v1/accounts/#{user.id}/followers?with_relationships=true") + |> json_response_and_validate_schema(200) + end + test "getting followers, hide_followers", %{user: user, conn: conn} do other_user = insert(:user, hide_followers: true) - {:ok, _user} = User.follow(user, other_user) + {:ok, _user, _other_user} = User.follow(user, other_user) conn = get(conn, "/api/v1/accounts/#{other_user.id}/followers") @@ -554,7 +695,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do test "getting followers, hide_followers, same user requesting" do user = insert(:user) other_user = insert(:user, hide_followers: true) - {:ok, _user} = User.follow(user, other_user) + {:ok, _user, _other_user} = User.follow(user, other_user) conn = build_conn() @@ -566,9 +707,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do end test "getting followers, pagination", %{user: user, conn: conn} do - {:ok, %User{id: follower1_id}} = :user |> insert() |> User.follow(user) - {:ok, %User{id: follower2_id}} = :user |> insert() |> User.follow(user) - {:ok, %User{id: follower3_id}} = :user |> insert() |> User.follow(user) + {:ok, %User{id: follower1_id}, _user} = :user |> insert() |> User.follow(user) + {:ok, %User{id: follower2_id}, _user} = :user |> insert() |> User.follow(user) + {:ok, %User{id: follower3_id}, _user} = :user |> insert() |> User.follow(user) assert [%{"id" => ^follower3_id}, %{"id" => ^follower2_id}] = conn @@ -583,9 +724,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] = conn |> get( - "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{ - follower3_id - }" + "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{follower3_id}" ) |> json_response_and_validate_schema(200) @@ -604,7 +743,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do test "getting following", %{user: user, conn: conn} do other_user = insert(:user) - {:ok, user} = User.follow(user, other_user) + {:ok, user, other_user} = User.follow(user, other_user) conn = get(conn, "/api/v1/accounts/#{user.id}/following") @@ -612,10 +751,28 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert id == to_string(other_user.id) end + test "following with relationship", %{conn: conn, user: user} do + other_user = insert(:user) + {:ok, user, other_user} = User.follow(user, other_user) + + conn = get(conn, "/api/v1/accounts/#{user.id}/following?with_relationships=true") + + id = other_user.id + + assert [ + %{ + "id" => ^id, + "pleroma" => %{ + "relationship" => %{"id" => ^id, "following" => true, "followed_by" => false} + } + } + ] = json_response_and_validate_schema(conn, 200) + end + test "getting following, hide_follows, other user requesting" do user = insert(:user, hide_follows: true) other_user = insert(:user) - {:ok, user} = User.follow(user, other_user) + {:ok, user, other_user} = User.follow(user, other_user) conn = build_conn() @@ -629,7 +786,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do test "getting following, hide_follows, same user requesting" do user = insert(:user, hide_follows: true) other_user = insert(:user) - {:ok, user} = User.follow(user, other_user) + {:ok, user, _other_user} = User.follow(user, other_user) conn = build_conn() @@ -644,9 +801,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do following1 = insert(:user) following2 = insert(:user) following3 = insert(:user) - {:ok, _} = User.follow(user, following1) - {:ok, _} = User.follow(user, following2) - {:ok, _} = User.follow(user, following3) + {:ok, _, _} = User.follow(user, following1) + {:ok, _, _} = User.follow(user, following2) + {:ok, _, _} = User.follow(user, following3) res_conn = get(conn, "/api/v1/accounts/#{user.id}/following?since_id=#{following1.id}") @@ -737,7 +894,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert [] == conn |> get("/api/v1/timelines/home") - |> json_response(200) + |> json_response_and_validate_schema(200) assert %{"showing_reblogs" => true} = conn @@ -745,10 +902,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: true}) |> json_response_and_validate_schema(200) + assert %{"showing_reblogs" => true} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: "1"}) + |> json_response_and_validate_schema(200) + assert [%{"id" => ^reblog_id}] = conn |> get("/api/v1/timelines/home") - |> json_response(200) + |> json_response_and_validate_schema(200) end test "following with reblogs" do @@ -766,7 +929,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert [%{"id" => ^reblog_id}] = conn |> get("/api/v1/timelines/home") - |> json_response(200) + |> json_response_and_validate_schema(200) assert %{"showing_reblogs" => false} = conn @@ -774,10 +937,39 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: false}) |> json_response_and_validate_schema(200) + assert %{"showing_reblogs" => false} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{reblogs: "0"}) + |> json_response_and_validate_schema(200) + assert [] == conn |> get("/api/v1/timelines/home") - |> json_response(200) + |> json_response_and_validate_schema(200) + end + + test "following with subscription and unsubscribing" do + %{conn: conn} = oauth_access(["follow"]) + followed = insert(:user) + + assert %{"subscribing" => true} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: true}) + |> json_response_and_validate_schema(200) + + assert %{"subscribing" => true} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: "1"}) + |> json_response_and_validate_schema(200) + + assert %{"subscribing" => false} = + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{followed.id}/follow", %{notify: false}) + |> json_response_and_validate_schema(200) end test "following / unfollowing errors", %{user: user, conn: conn} do @@ -959,7 +1151,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert %{"error" => "{\"email\":[\"Invalid email\"]}"} = json_response_and_validate_schema(conn, 400) - Pleroma.Config.put([User, :email_blacklist], []) + clear_config([User, :email_blacklist], []) conn = build_conn() @@ -979,8 +1171,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do user = Repo.preload(token_from_db, :user).user assert user - refute user.confirmation_pending - refute user.approval_pending + assert user.is_confirmed + assert user.is_approved end test "registers but does not log in with :account_activation_required", %{conn: conn} do @@ -1040,7 +1232,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do refute response["token_type"] user = Repo.get_by(User, email: "lain@example.org") - assert user.confirmation_pending + refute user.is_confirmed end test "registers but does not log in with :account_approval_required", %{conn: conn} do @@ -1102,7 +1294,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do user = Repo.get_by(User, email: "lain@example.org") - assert user.approval_pending + refute user.is_approved assert user.registration_reason == "I'm a cool dude, bro" end @@ -1378,8 +1570,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do |> json_response_and_validate_schema(:ok) assert Token |> Repo.get_by(token: access_token) |> Repo.preload(:user) |> Map.get(:user) - - Cachex.del(:used_captcha_cache, token) end test "returns 400 if any captcha field is not provided", %{conn: conn} do @@ -1425,6 +1615,75 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do end end + describe "create account with language" do + setup %{conn: conn} do + app_token = insert(:oauth_token, user: nil) + + conn = + conn + |> put_req_header("authorization", "Bearer " <> app_token.token) + |> put_req_header("content-type", "multipart/form-data") + |> put_req_cookie(SetLocalePlug.frontend_language_cookie_name(), "zh-Hans") + |> SetLocalePlug.call([]) + + [conn: conn] + end + + test "creates an account with language parameter", %{conn: conn} do + params = %{ + username: "foo", + email: "foo@example.org", + password: "dupa.8", + agreement: true, + language: "ru" + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 200) + + assert %{language: "ru"} = Pleroma.User.get_by_nickname("foo") + end + + test "language parameter should be normalized", %{conn: conn} do + params = %{ + username: "foo", + email: "foo@example.org", + password: "dupa.8", + agreement: true, + language: "ru-RU" + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 200) + + assert %{language: "ru_RU"} = Pleroma.User.get_by_nickname("foo") + end + + test "createing an account without language parameter should fallback to cookie/header language", + %{conn: conn} do + params = %{ + username: "foo2", + email: "foo2@example.org", + password: "dupa.8", + agreement: true + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 200) + + assert %{language: "zh_Hans"} = Pleroma.User.get_by_nickname("foo2") + end + end + describe "GET /api/v1/accounts/:id/lists - account_lists" do test "returns lists to which the account belongs" do %{user: user, conn: conn} = oauth_access(["read:lists"]) @@ -1452,7 +1711,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do response = json_response_and_validate_schema(conn, 200) assert %{"id" => id, "source" => %{"privacy" => "public"}} = response - assert response["pleroma"]["chat_token"] assert response["pleroma"]["unread_notifications_count"] == 6 assert id == to_string(user.id) end @@ -1487,7 +1745,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do test "returns the relationships for the current user", %{user: user, conn: conn} do %{id: other_user_id} = other_user = insert(:user) - {:ok, _user} = User.follow(user, other_user) + {:ok, _user, _other_user} = User.follow(user, other_user) assert [%{"id" => ^other_user_id}] = conn @@ -1519,7 +1777,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do result = conn - |> assign(:user, user) |> get("/api/v1/mutes") |> json_response_and_validate_schema(200) @@ -1527,7 +1784,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do result = conn - |> assign(:user, user) |> get("/api/v1/mutes?limit=1") |> json_response_and_validate_schema(200) @@ -1535,7 +1791,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do result = conn - |> assign(:user, user) |> get("/api/v1/mutes?since_id=#{id1}") |> json_response_and_validate_schema(200) @@ -1543,7 +1798,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do result = conn - |> assign(:user, user) |> get("/api/v1/mutes?since_id=#{id1}&max_id=#{id3}") |> json_response_and_validate_schema(200) @@ -1551,13 +1805,45 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do result = conn - |> assign(:user, user) |> get("/api/v1/mutes?since_id=#{id1}&limit=1") |> json_response_and_validate_schema(200) assert [%{"id" => ^id2}] = result end + test "list of mutes with with_relationships parameter" do + %{user: user, conn: conn} = oauth_access(["read:mutes"]) + %{id: id1} = other_user1 = insert(:user) + %{id: id2} = other_user2 = insert(:user) + %{id: id3} = other_user3 = insert(:user) + + {:ok, _, _} = User.follow(other_user1, user) + {:ok, _, _} = User.follow(other_user2, user) + {:ok, _, _} = User.follow(other_user3, user) + + {:ok, _} = User.mute(user, other_user1) + {:ok, _} = User.mute(user, other_user2) + {:ok, _} = User.mute(user, other_user3) + + assert [ + %{ + "id" => ^id1, + "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}} + }, + %{ + "id" => ^id2, + "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}} + }, + %{ + "id" => ^id3, + "pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}} + } + ] = + conn + |> get("/api/v1/mutes?with_relationships=true") + |> json_response_and_validate_schema(200) + end + test "getting a list of blocks" do %{user: user, conn: conn} = oauth_access(["read:blocks"]) %{id: id1} = other_user1 = insert(:user) @@ -1608,4 +1894,89 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do assert [%{"id" => ^id2}] = result end + + test "account lookup", %{conn: conn} do + %{nickname: acct} = insert(:user, %{nickname: "nickname"}) + %{nickname: acct_two} = insert(:user, %{nickname: "nickname@notlocaldoma.in"}) + + result = + conn + |> get("/api/v1/accounts/lookup?acct=#{acct}") + |> json_response_and_validate_schema(200) + + assert %{"acct" => ^acct} = result + + result = + conn + |> get("/api/v1/accounts/lookup?acct=#{acct_two}") + |> json_response_and_validate_schema(200) + + assert %{"acct" => ^acct_two} = result + + _result = + conn + |> get("/api/v1/accounts/lookup?acct=unexisting_nickname") + |> json_response_and_validate_schema(404) + end + + test "create a note on a user" do + %{conn: conn} = oauth_access(["write:accounts", "read:follows"]) + other_user = insert(:user) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/accounts/#{other_user.id}/note", %{ + "comment" => "Example note" + }) + + assert [%{"note" => "Example note"}] = + conn + |> put_req_header("content-type", "application/json") + |> get("/api/v1/accounts/relationships?id=#{other_user.id}") + |> json_response_and_validate_schema(200) + end + + describe "remove from followers" do + setup do: oauth_access(["follow"]) + + test "removing user from followers", %{conn: conn, user: user} do + %{id: other_user_id} = other_user = insert(:user) + + CommonAPI.follow(other_user, user) + + assert %{"id" => ^other_user_id, "followed_by" => false} = + conn + |> post("/api/v1/accounts/#{other_user_id}/remove_from_followers") + |> json_response_and_validate_schema(200) + + refute User.following?(other_user, user) + end + + test "removing remote user from followers", %{conn: conn, user: user} do + %{id: other_user_id} = other_user = insert(:user, local: false) + + CommonAPI.follow(other_user, user) + + assert User.following?(other_user, user) + + assert %{"id" => ^other_user_id, "followed_by" => false} = + conn + |> post("/api/v1/accounts/#{other_user_id}/remove_from_followers") + |> json_response_and_validate_schema(200) + + refute User.following?(other_user, user) + end + + test "removing user from followers errors", %{user: user, conn: conn} do + # self remove + conn_res = post(conn, "/api/v1/accounts/#{user.id}/remove_from_followers") + + assert %{"error" => "Can not unfollow yourself"} = + json_response_and_validate_schema(conn_res, 400) + + # remove non existing user + conn_res = post(conn, "/api/v1/accounts/doesntexist/remove_from_followers") + assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn_res, 404) + end + end end