diff options
Diffstat (limited to 'test/web/twitter_api')
-rw-r--r-- | test/web/twitter_api/representers/activity_representer_test.exs | 5 | ||||
-rw-r--r-- | test/web/twitter_api/twitter_api_controller_test.exs | 180 | ||||
-rw-r--r-- | test/web/twitter_api/twitter_api_test.exs | 79 | ||||
-rw-r--r-- | test/web/twitter_api/views/activity_view_test.exs | 3 | ||||
-rw-r--r-- | test/web/twitter_api/views/user_view_test.exs | 48 |
5 files changed, 304 insertions, 11 deletions
diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 16c6e7b0d..3f85e028b 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -126,7 +126,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do } expected_html = - "<span>2hu</span><br />alert('YAY')Some <img height='32px' width='32px' alt='2hu' title='2hu' src='corndog.png' /> content mentioning <a href=\"#{ + "<p>2hu</p>alert('YAY')Some <img height='32px' width='32px' alt='2hu' title='2hu' src='corndog.png' /> content mentioning <a href=\"#{ mentioned_user.ap_id }\">@shp</a>" @@ -155,7 +155,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "activity_type" => "post", "possibly_sensitive" => true, "uri" => activity.data["object"]["id"], - "visibility" => "direct" + "visibility" => "direct", + "summary" => "2hu" } assert ActivityRepresenter.to_map(activity, %{ diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index c2ea41aa3..3a035e298 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -260,7 +260,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "with credentials", %{conn: conn, user: current_user} do other_user = insert(:user) - {:ok, activity} = + {:ok, _activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: other_user}) conn = @@ -491,6 +491,26 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "GET /api/qvitter/mutes.json" do + setup [:valid_user] + + test "unimplemented mutes without valid credentials", %{conn: conn} do + conn = get(conn, "/api/qvitter/mutes.json") + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "unimplemented mutes with credentials", %{conn: conn, user: current_user} do + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/qvitter/mutes.json") + + current_user = Repo.get(User, current_user.id) + + assert [] = json_response(conn, 200) + end + end + describe "POST /api/favorites/create/:id" do setup [:valid_user] @@ -510,6 +530,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert json_response(conn, 200) end + + test "with credentials, invalid param", %{conn: conn, user: current_user} do + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/favorites/create/wrong.json") + + assert json_response(conn, 400) + end + + test "with credentials, invalid activity", %{conn: conn, user: current_user} do + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/favorites/create/1.json") + + assert json_response(conn, 500) + end end describe "POST /api/favorites/destroy/:id" do @@ -562,6 +600,40 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "POST /api/statuses/unretweet/:id" do + setup [:valid_user] + + test "without valid credentials", %{conn: conn} do + note_activity = insert(:note_activity) + conn = post(conn, "/api/statuses/unretweet/#{note_activity.id}.json") + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "with credentials", %{conn: conn, user: current_user} do + note_activity = insert(:note_activity) + + request_path = "/api/statuses/retweet/#{note_activity.id}.json" + + _response = + conn + |> with_credentials(current_user.nickname, "test") + |> post(request_path) + + request_path = String.replace(request_path, "retweet", "unretweet") + + response = + conn + |> with_credentials(current_user.nickname, "test") + |> post(request_path) + + activity = Repo.get(Activity, note_activity.id) + activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"]) + + assert json_response(response, 200) == + ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user}) + end + end + describe "POST /api/account/register" do test "it creates a new user", %{conn: conn} do data = %{ @@ -668,6 +740,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do conn = conn + |> assign(:user, user) |> get("/api/statuses/friends", %{"user_id" => user.id}) assert MapSet.equal?( @@ -689,6 +762,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do conn = conn + |> assign(:user, user) |> get("/api/statuses/friends", %{"screen_name" => user.nickname}) assert MapSet.equal?( @@ -742,6 +816,38 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end + + test "it locks an account", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "locked" => "true" + }) + + user = Repo.get!(User, user.id) + assert user.info["locked"] == true + + assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) + end + + test "it unlocks an account", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "locked" => "false" + }) + + user = Repo.get!(User, user.id) + assert user.info["locked"] == false + + assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) + end end defp valid_user(_context) do @@ -791,7 +897,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "Convert newlines to <br> in bio", %{conn: conn} do user = insert(:user) - conn = + _conn = conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ @@ -902,6 +1008,76 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do |> post("/api/pleroma/delete_account", %{"password" => "test"}) assert json_response(conn, 200) == %{"status" => "success"} + # Wait a second for the started task to end + :timer.sleep(1000) + end + end + + describe "GET /api/pleroma/friend_requests" do + test "it lists friend requests" do + user = insert(:user, %{info: %{"locked" => true}}) + other_user = insert(:user) + + {:ok, activity} = ActivityPub.follow(other_user, user) + + user = Repo.get(User, user.id) + other_user = Repo.get(User, other_user.id) + + assert User.following?(other_user, user) == false + + conn = + build_conn() + |> assign(:user, user) + |> get("/api/pleroma/friend_requests") + + assert [relationship] = json_response(conn, 200) + assert other_user.id == relationship["id"] + end + end + + describe "POST /api/pleroma/friendships/approve" do + test "it approves a friend request" do + user = insert(:user, %{info: %{"locked" => true}}) + other_user = insert(:user) + + {:ok, activity} = ActivityPub.follow(other_user, user) + + user = Repo.get(User, user.id) + other_user = Repo.get(User, other_user.id) + + assert User.following?(other_user, user) == false + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/pleroma/friendships/approve", %{"user_id" => to_string(other_user.id)}) + + assert relationship = json_response(conn, 200) + assert other_user.id == relationship["id"] + assert relationship["follows_you"] == true + end + end + + describe "POST /api/pleroma/friendships/deny" do + test "it denies a friend request" do + user = insert(:user, %{info: %{"locked" => true}}) + other_user = insert(:user) + + {:ok, activity} = ActivityPub.follow(other_user, user) + + user = Repo.get(User, user.id) + other_user = Repo.get(User, other_user.id) + + assert User.following?(other_user, user) == false + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/pleroma/friendships/deny", %{"user_id" => to_string(other_user.id)}) + + assert relationship = json_response(conn, 200) + assert other_user.id == relationship["id"] + assert relationship["follows_you"] == false end end end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 4716abb84..6486540f8 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -2,7 +2,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do use Pleroma.DataCase alias Pleroma.Builders.UserBuilder alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView} - alias Pleroma.{Activity, User, Object, Repo} + alias Pleroma.{Activity, User, Object, Repo, UserInviteToken} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.ActivityView @@ -35,7 +35,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input) expected_text = - "Hello again, <span><a href='shp'>@<span>shp</span></a></span>.<script></script><br>This is on another :moominmamma: line. <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a> <a href='http://localhost:4001/tag/epic' rel='tag'>#epic</a> <a href='http://localhost:4001/tag/phantasmagoric' rel='tag'>#phantasmagoric</a><br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>" + "Hello again, <span><a class='mention' href='shp'>@<span>shp</span></a></span>.<script></script><br>This is on another :moominmamma: line. <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a> <a href='http://localhost:4001/tag/epic' rel='tag'>#epic</a> <a href='http://localhost:4001/tag/phantasmagoric' rel='tag'>#phantasmagoric</a><br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>" assert get_in(activity.data, ["object", "content"]) == expected_text assert get_in(activity.data, ["object", "type"]) == "Note" @@ -228,6 +228,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert status == updated_activity end + test "it unretweets an already retweeted status" do + user = insert(:user) + note_activity = insert(:note_activity) + + {:ok, _status} = TwitterAPI.repeat(user, note_activity.id) + {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id) + updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) + + assert status == updated_activity + end + test "it registers a new user and returns the user." do data = %{ "nickname" => "lain", @@ -246,6 +257,70 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do UserView.render("show.json", %{user: fetched_user}) end + @moduletag skip: "needs 'registrations_open: false' in config" + test "it registers a new user via invite token and returns the user." do + {:ok, token} = UserInviteToken.create_token() + + data = %{ + "nickname" => "vinny", + "email" => "pasta@pizza.vs", + "fullname" => "Vinny Vinesauce", + "bio" => "streamer", + "password" => "hiptofbees", + "confirm" => "hiptofbees", + "token" => token.token + } + + {:ok, user} = TwitterAPI.register_user(data) + + fetched_user = Repo.get_by(User, nickname: "vinny") + token = Repo.get_by(UserInviteToken, token: token.token) + + assert token.used == true + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) + end + + @moduletag skip: "needs 'registrations_open: false' in config" + test "it returns an error if invalid token submitted" do + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => "DudeLetMeInImAFairy" + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Invalid token" + refute Repo.get_by(User, nickname: "GrimReaper") + end + + @moduletag skip: "needs 'registrations_open: false' in config" + test "it returns an error if expired token submitted" do + {:ok, token} = UserInviteToken.create_token() + UserInviteToken.mark_as_used(token.token) + + data = %{ + "nickname" => "GrimReaper", + "email" => "death@reapers.afterlife", + "fullname" => "Reaper Grim", + "bio" => "Your time has come", + "password" => "scythe", + "confirm" => "scythe", + "token" => token.token + } + + {:error, msg} = TwitterAPI.register_user(data) + + assert msg == "Expired token" + refute Repo.get_by(User, nickname: "GrimReaper") + end + test "it returns the error on registration problems" do data = %{ "nickname" => "lain", diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs index 5b2a7466b..a101e4ae8 100644 --- a/test/web/twitter_api/views/activity_view_test.exs +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -48,7 +48,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do "text" => "Hey @shp!", "uri" => activity.data["object"]["id"], "user" => UserView.render("show.json", %{user: user}), - "visibility" => "direct" + "visibility" => "direct", + "summary" => nil } assert result == expected diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 9f8bf4cdc..fefb6bdcc 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -20,6 +20,30 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do assert represented["profile_image_url"] == image end + test "A user with emoji in username", %{user: user} do + expected = + "<img height='32px' width='32px' alt='karjalanpiirakka' title='karjalanpiirakka' src='/file.png' /> man" + + user = %{ + user + | info: %{ + "source_data" => %{ + "tag" => [ + %{ + "type" => "Emoji", + "icon" => %{"url" => "/file.png"}, + "name" => ":karjalanpiirakka:" + } + ] + } + } + } + + user = %{user | name: ":karjalanpiirakka: man"} + represented = UserView.render("show.json", %{user: user}) + assert represented["name_html"] == expected + end + test "A user" do note_activity = insert(:note_activity) user = User.get_cached_by_ap_id(note_activity.data["actor"]) @@ -40,7 +64,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 1, @@ -59,7 +85,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "statusnet_profile_url" => user.ap_id, "cover_photo" => banner, "background_image" => nil, - "is_local" => true + "is_local" => true, + "locked" => false, + "default_scope" => "public" } assert represented == UserView.render("show.json", %{user: user}) @@ -75,7 +103,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -94,7 +124,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "statusnet_profile_url" => user.ap_id, "cover_photo" => banner, "background_image" => nil, - "is_local" => true + "is_local" => true, + "locked" => false, + "default_scope" => "public" } assert represented == UserView.render("show.json", %{user: user, for: follower}) @@ -111,7 +143,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "id" => follower.id, "name" => follower.name, "screen_name" => follower.nickname, + "name_html" => follower.name, "description" => HtmlSanitizeEx.strip_tags(follower.bio), + "description_html" => HtmlSanitizeEx.strip_tags(follower.bio), "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -130,7 +164,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "statusnet_profile_url" => follower.ap_id, "cover_photo" => banner, "background_image" => nil, - "is_local" => true + "is_local" => true, + "locked" => false, + "default_scope" => "public" } assert represented == UserView.render("show.json", %{user: follower, for: user}) @@ -154,7 +190,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "id" => user.id, "name" => user.name, "screen_name" => user.nickname, + "name_html" => user.name, "description" => HtmlSanitizeEx.strip_tags(user.bio), + "description_html" => HtmlSanitizeEx.strip_tags(user.bio), "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, @@ -173,7 +211,9 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "statusnet_profile_url" => user.ap_id, "cover_photo" => banner, "background_image" => nil, - "is_local" => true + "is_local" => true, + "locked" => false, + "default_scope" => "public" } blocker = Repo.get(User, blocker.id) |