diff options
Diffstat (limited to 'test/web')
27 files changed, 1779 insertions, 685 deletions
diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index c8082cd03..25b47ee31 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -9,9 +9,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do test "it returns a json representation of the user", %{conn: conn} do user = insert(:user) - conn = conn - |> put_req_header("accept", "application/activity+json") - |> get("/users/#{user.nickname}") + conn = + conn + |> put_req_header("accept", "application/activity+json") + |> get("/users/#{user.nickname}") user = Repo.get(User, user.id) @@ -22,11 +23,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do describe "/object/:uuid" do test "it returns a json representation of the object", %{conn: conn} do note = insert(:note) - uuid = String.split(note.data["id"], "/") |> List.last + uuid = String.split(note.data["id"], "/") |> List.last() - conn = conn - |> put_req_header("accept", "application/activity+json") - |> get("/objects/#{uuid}") + conn = + conn + |> put_req_header("accept", "application/activity+json") + |> get("/objects/#{uuid}") assert json_response(conn, 200) == ObjectView.render("object.json", %{object: note}) end @@ -34,16 +36,100 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do describe "/users/:nickname/inbox" do test "it inserts an incoming activity into the database", %{conn: conn} do - data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode! + data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() - conn = conn - |> assign(:valid_signature, true) - |> put_req_header("content-type", "application/activity+json") - |> post("/inbox", data) + conn = + conn + |> assign(:valid_signature, true) + |> put_req_header("content-type", "application/activity+json") + |> post("/inbox", data) assert "ok" == json_response(conn, 200) :timer.sleep(500) assert Activity.get_by_ap_id(data["id"]) end end + + describe "/users/:nickname/followers" do + test "it returns the followers in a collection", %{conn: conn} do + user = insert(:user) + user_two = insert(:user) + User.follow(user, user_two) + + result = + conn + |> get("/users/#{user_two.nickname}/followers") + |> json_response(200) + + assert result["first"]["orderedItems"] == [user.ap_id] + end + + test "it works for more than 10 users", %{conn: conn} do + user = insert(:user) + + Enum.each(1..15, fn _ -> + other_user = insert(:user) + User.follow(other_user, user) + end) + + result = + conn + |> get("/users/#{user.nickname}/followers") + |> json_response(200) + + assert length(result["first"]["orderedItems"]) == 10 + assert result["first"]["totalItems"] == 15 + assert result["totalItems"] == 15 + + result = + conn + |> get("/users/#{user.nickname}/followers?page=2") + |> json_response(200) + + assert length(result["orderedItems"]) == 5 + assert result["totalItems"] == 15 + end + end + + describe "/users/:nickname/following" do + test "it returns the following in a collection", %{conn: conn} do + user = insert(:user) + user_two = insert(:user) + User.follow(user, user_two) + + result = + conn + |> get("/users/#{user.nickname}/following") + |> json_response(200) + + assert result["first"]["orderedItems"] == [user_two.ap_id] + end + + test "it works for more than 10 users", %{conn: conn} do + user = insert(:user) + + Enum.each(1..15, fn _ -> + user = Repo.get(User, user.id) + other_user = insert(:user) + User.follow(user, other_user) + end) + + result = + conn + |> get("/users/#{user.nickname}/following") + |> json_response(200) + + assert length(result["first"]["orderedItems"]) == 10 + assert result["first"]["totalItems"] == 15 + assert result["totalItems"] == 15 + + result = + conn + |> get("/users/#{user.nickname}/following?page=2") + |> json_response(200) + + assert length(result["orderedItems"]) == 5 + assert result["totalItems"] == 15 + end + end end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 96792bca5..6d23adfcd 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do use Pleroma.DataCase alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.Web.CommonAPI alias Pleroma.{Activity, Object, User} alias Pleroma.Builders.ActivityBuilder @@ -22,7 +23,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do describe "insertion" do test "returns the activity if one with the same id is already in" do activity = insert(:note_activity) - {:ok, new_activity}= ActivityPub.insert(activity.data) + {:ok, new_activity} = ActivityPub.insert(activity.data) assert activity == new_activity end @@ -37,14 +38,34 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert is_binary(activity.data["id"]) given_id = "bla" + data = %{ "ok" => true, - "id" => given_id + "id" => given_id, + "context" => "blabla" } {:ok, %Activity{} = activity} = ActivityPub.insert(data) assert activity.data["ok"] == data["ok"] assert activity.data["id"] == given_id + assert activity.data["context"] == "blabla" + assert activity.data["context_id"] + end + + test "adds a context when none is there" do + data = %{ + "id" => "some_id", + "object" => %{ + "id" => "object_id" + } + } + + {:ok, %Activity{} = activity} = ActivityPub.insert(data) + + assert is_binary(activity.data["context"]) + assert is_binary(activity.data["object"]["context"]) + assert activity.data["context_id"] + assert activity.data["object"]["context_id"] end test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do @@ -63,9 +84,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do describe "create activities" do test "removes doubled 'to' recipients" do - {:ok, activity} = ActivityPub.create(%{to: ["user1", "user1", "user2"], actor: %User{ap_id: "1"}, context: "", object: %{}}) + user = insert(:user) + + {:ok, activity} = + ActivityPub.create(%{ + to: ["user1", "user1", "user2"], + actor: user, + context: "", + object: %{} + }) + assert activity.data["to"] == ["user1", "user2"] - assert activity.actor == "1" + assert activity.actor == user.ap_id assert activity.recipients == ["user1", "user2"] end end @@ -101,12 +131,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do test "doesn't return blocked activities" do activity_one = insert(:note_activity) activity_two = insert(:note_activity) + activity_three = insert(:note_activity) user = insert(:user) + booster = insert(:user) {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]}) activities = ActivityPub.fetch_activities([], %{"blocking_user" => user}) assert Enum.member?(activities, activity_two) + assert Enum.member?(activities, activity_three) refute Enum.member?(activities, activity_one) {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]}) @@ -114,21 +147,36 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do activities = ActivityPub.fetch_activities([], %{"blocking_user" => user}) assert Enum.member?(activities, activity_two) + assert Enum.member?(activities, activity_three) + assert Enum.member?(activities, activity_one) + + {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]}) + {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster) + %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id) + activity_three = Repo.get(Activity, activity_three.id) + + activities = ActivityPub.fetch_activities([], %{"blocking_user" => user}) + + assert Enum.member?(activities, activity_two) + refute Enum.member?(activities, activity_three) + refute Enum.member?(activities, boost_activity) assert Enum.member?(activities, activity_one) activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil}) assert Enum.member?(activities, activity_two) + assert Enum.member?(activities, activity_three) + assert Enum.member?(activities, boost_activity) assert Enum.member?(activities, activity_one) end describe "public fetch activities" do test "retrieves public activities" do - _activities = ActivityPub.fetch_public_activities + _activities = ActivityPub.fetch_public_activities() - %{public: public} = ActivityBuilder.public_and_non_public + %{public: public} = ActivityBuilder.public_and_non_public() - activities = ActivityPub.fetch_public_activities + activities = ActivityPub.fetch_public_activities() assert length(activities) == 1 assert Enum.at(activities, 0) == public end @@ -137,7 +185,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do activities = ActivityBuilder.insert_list(30) last_expected = List.last(activities) - activities = ActivityPub.fetch_public_activities + activities = ActivityPub.fetch_public_activities() last = List.last(activities) assert length(activities) == 20 @@ -232,7 +280,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do {:ok, announce_activity, object} = ActivityPub.announce(user, object) assert object.data["announcement_count"] == 1 assert object.data["announcements"] == [user.ap_id] - assert announce_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]] + + assert announce_activity.data["to"] == [ + User.ap_followers(user), + note_activity.data["actor"] + ] + assert announce_activity.data["object"] == object.data["id"] assert announce_activity.data["actor"] == user.ap_id assert announce_activity.data["context"] == object.data["context"] @@ -241,7 +294,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do describe "uploading files" do test "copies the file to the configured folder" do - file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } {:ok, %Object{} = object} = ActivityPub.upload(file) assert object.data["name"] == "an_image.jpg" @@ -268,11 +325,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do describe "fetching an object" do test "it fetches an object" do - {:ok, object} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367") + {:ok, object} = + ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367") + assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"]) assert activity.data["id"] - {:ok, object_again} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367") + {:ok, object_again} = + ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367") assert [attachment] = object.data["attachment"] assert is_list(attachment["url"]) @@ -285,7 +345,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"]) assert activity.data["id"] - {:ok, object_again} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873") + {:ok, object_again} = + ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873") assert object == object_again end @@ -344,7 +405,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do user = insert(:user) {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user) user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user}) - {:ok, update} = ActivityPub.update(%{actor: user_data["id"], to: [user.follower_address], cc: [], object: user_data}) + + {:ok, update} = + ActivityPub.update(%{ + actor: user_data["id"], + to: [user.follower_address], + cc: [], + object: user_data + }) assert update.data["actor"] == user.ap_id assert update.data["to"] == [user.follower_address] diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 51b09b166..eb093262f 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -16,9 +16,10 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do test "it ignores an incoming notice if we already have it" do activity = insert(:note_activity) - data = File.read!("test/fixtures/mastodon-post-activity.json") - |> Poison.decode! - |> Map.put("object", activity.data["object"]) + data = + File.read!("test/fixtures/mastodon-post-activity.json") + |> Poison.decode!() + |> Map.put("object", activity.data["object"]) {:ok, returned_activity} = Transmogrifier.handle_incoming(data) @@ -26,53 +27,88 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end test "it fetches replied-to activities if we don't have them" do - data = File.read!("test/fixtures/mastodon-post-activity.json") - |> Poison.decode! + data = + File.read!("test/fixtures/mastodon-post-activity.json") + |> Poison.decode!() - object = data["object"] - |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873") + object = + data["object"] + |> Map.put("inReplyTo", "https://shitposter.club/notice/2827873") - data = data - |> Map.put("object", object) + data = + data + |> Map.put("object", object) {:ok, returned_activity} = Transmogrifier.handle_incoming(data) - assert activity = Activity.get_create_activity_by_object_ap_id("tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment") - assert returned_activity.data["object"]["inReplyToAtomUri"] == "https://shitposter.club/notice/2827873" + assert activity = + Activity.get_create_activity_by_object_ap_id( + "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" + ) + + assert returned_activity.data["object"]["inReplyToAtomUri"] == + "https://shitposter.club/notice/2827873" + assert returned_activity.data["object"]["inReplyToStatusId"] == activity.id end test "it works for incoming notices" do - data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode! + data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) - assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity" - assert data["context"] == "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation" + + assert data["id"] == + "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity" + + assert data["context"] == + "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation" + assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"] + assert data["cc"] == [ - "http://mastodon.example.org/users/admin/followers", - "http://localtesting.pleroma.lol/users/lain" - ] + "http://mastodon.example.org/users/admin/followers", + "http://localtesting.pleroma.lol/users/lain" + ] + assert data["actor"] == "http://mastodon.example.org/users/admin" object = data["object"] assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822" assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"] + assert object["cc"] == [ - "http://mastodon.example.org/users/admin/followers", - "http://localtesting.pleroma.lol/users/lain" - ] + "http://mastodon.example.org/users/admin/followers", + "http://localtesting.pleroma.lol/users/lain" + ] + assert object["actor"] == "http://mastodon.example.org/users/admin" assert object["attributedTo"] == "http://mastodon.example.org/users/admin" - assert object["context"] == "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation" + + assert object["context"] == + "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation" + assert object["sensitive"] == true + + user = User.get_by_ap_id(object["actor"]) + + assert user.info["note_count"] == 1 + end + + test "it works for incoming notices with hashtags" do + data = File.read!("test/fixtures/mastodon-post-activity-hashtag.json") |> Poison.decode!() + + {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) + assert Enum.at(data["object"]["tag"], 2) == "moo" end test "it works for incoming follow requests" do user = insert(:user) - data = File.read!("test/fixtures/mastodon-follow-activity.json") |> Poison.decode! - |> Map.put("object", user.ap_id) + + data = + File.read!("test/fixtures/mastodon-follow-activity.json") + |> Poison.decode!() + |> Map.put("object", user.ap_id) {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) @@ -86,8 +122,10 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "hello"}) - data = File.read!("test/fixtures/mastodon-like.json") |> Poison.decode! - |> Map.put("object", activity.data["object"]["id"]) + data = + File.read!("test/fixtures/mastodon-like.json") + |> Poison.decode!() + |> Map.put("object", activity.data["object"]["id"]) {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) @@ -98,14 +136,18 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do end test "it works for incoming announces" do - data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode! + data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!() {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) assert data["actor"] == "http://mastodon.example.org/users/admin" assert data["type"] == "Announce" - assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" - assert data["object"] == "http://mastodon.example.org/users/admin/statuses/99541947525187367" + + assert data["id"] == + "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" + + assert data["object"] == + "http://mastodon.example.org/users/admin/statuses/99541947525187367" assert Activity.get_create_activity_by_object_ap_id(data["object"]) end @@ -114,53 +156,77 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do user = insert(:user) {:ok, activity} = CommonAPI.post(user, %{"status" => "hey"}) - data = File.read!("test/fixtures/mastodon-announce.json") - |> Poison.decode! - |> Map.put("object", activity.data["object"]["id"]) + data = + File.read!("test/fixtures/mastodon-announce.json") + |> Poison.decode!() + |> Map.put("object", activity.data["object"]["id"]) {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) assert data["actor"] == "http://mastodon.example.org/users/admin" assert data["type"] == "Announce" - assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" + + assert data["id"] == + "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity" + assert data["object"] == activity.data["object"]["id"] assert Activity.get_create_activity_by_object_ap_id(data["object"]).id == activity.id end test "it works for incoming update activities" do - data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode! + data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) - update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode! - object = update_data["object"] - |> Map.put("actor", data["actor"]) - |> Map.put("id", data["actor"]) + update_data = File.read!("test/fixtures/mastodon-update.json") |> Poison.decode!() - update_data = update_data - |> Map.put("actor", data["actor"]) - |> Map.put("object", object) + object = + update_data["object"] + |> Map.put("actor", data["actor"]) + |> Map.put("id", data["actor"]) + + update_data = + update_data + |> Map.put("actor", data["actor"]) + |> Map.put("object", object) {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(update_data) user = User.get_cached_by_ap_id(data["actor"]) assert user.name == "gargle" - assert user.avatar["url"] == [%{"href" => "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"}] - assert user.info["banner"]["url"] == [%{"href" => "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"}] + + assert user.avatar["url"] == [ + %{ + "href" => + "https://cd.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg" + } + ] + + assert user.info["banner"]["url"] == [ + %{ + "href" => + "https://cd.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png" + } + ] + assert user.bio == "<p>Some bio</p>" end test "it works for incoming deletes" do activity = insert(:note_activity) - data = File.read!("test/fixtures/mastodon-delete.json") - |> Poison.decode! - object = data["object"] - |> Map.put("id", activity.data["object"]["id"]) + data = + File.read!("test/fixtures/mastodon-delete.json") + |> Poison.decode!() + + object = + data["object"] + |> Map.put("id", activity.data["object"]["id"]) - data = data - |> Map.put("object", object) - |> Map.put("actor", activity.data["actor"]) + data = + data + |> Map.put("object", object) + |> Map.put("actor", activity.data["actor"]) {:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data) @@ -173,7 +239,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do user = insert(:user) other_user = insert(:user) - {:ok, activity} = CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"}) + {:ok, activity} = + CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"}) {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) object = modified["object"] @@ -185,7 +252,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do } expected_tag = %{ - "href" => Pleroma.Web.Endpoint.url <> "/tags/2hu", + "href" => Pleroma.Web.Endpoint.url() <> "/tags/2hu", "type" => "Hashtag", "name" => "#2hu" } @@ -233,11 +300,32 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29" end + + test "it translates ostatus reply_to IDs to external URLs" do + incoming = File.read!("test/fixtures/incoming_note_activity.xml") + {:ok, [referred_activity]} = OStatus.handle_incoming(incoming) + + user = insert(:user) + + {:ok, activity} = + CommonAPI.post(user, %{"status" => "HI!", "in_reply_to_status_id" => referred_activity.id}) + + {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) + + assert modified["object"]["inReplyTo"] == "http://gs.example.org:4040/index.php/notice/29" + end end describe "user upgrade" do test "it upgrades a user to activitypub" do - user = insert(:user, %{nickname: "rye@niu.moe", local: false, ap_id: "https://niu.moe/users/rye", follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"})}) + user = + insert(:user, %{ + nickname: "rye@niu.moe", + local: false, + ap_id: "https://niu.moe/users/rye", + follower_address: User.ap_followers(%User{nickname: "rye@niu.moe"}) + }) + user_two = insert(:user, %{following: [user.follower_address]}) {:ok, activity} = CommonAPI.post(user, %{"status" => "test"}) @@ -260,8 +348,25 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do activity = Repo.get(Activity, activity.id) assert user.follower_address in activity.recipients - assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"}]} = user.avatar - assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"}]} = user.info["banner"] + + assert %{ + "url" => [ + %{ + "href" => + "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg" + } + ] + } = user.avatar + + assert %{ + "url" => [ + %{ + "href" => + "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png" + } + ] + } = user.info["banner"] + refute "..." in activity.recipients unrelated_activity = Repo.get(Activity, unrelated_activity.id) diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index f6a7da9ed..689bdd61e 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -3,7 +3,8 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do use Pleroma.DataCase test "it adds attachment links to a given text and attachment set" do - name = "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png" + name = + "Sakura%20Mana%20%E2%80%93%20Turned%20on%20by%20a%20Senior%20OL%20with%20a%20Temptating%20Tight%20Skirt-s%20Full%20Hipline%20and%20Panty%20Shot-%20Beautiful%20Thick%20Thighs-%20and%20Erotic%20Ass-%20-2015-%20--%20Oppaitime%208-28-2017%206-50-33%20PM.png" attachment = %{ "url" => [%{"href" => name}] @@ -11,6 +12,7 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do res = Utils.add_attachments("", [attachment]) - assert res == "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>" + assert res == + "<br><a href=\"#{name}\" class='attachment'>Sakura Mana – Turned on by a Se…</a>" end end diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index 5eefa61e1..597690bf7 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -5,7 +5,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do alias Pleroma.User test "Represent a user account" do - user = insert(:user, %{info: %{"note_count" => 5, "follower_count" => 3}, nickname: "shp@shitposter.club", inserted_at: ~N[2017-08-15 15:47:06.597036]}) + user = + insert(:user, %{ + info: %{"note_count" => 5, "follower_count" => 3}, + nickname: "shp@shitposter.club", + inserted_at: ~N[2017-08-15 15:47:06.597036] + }) expected = %{ id: to_string(user.id), diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index a655fa74b..883ebc61e 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -14,17 +14,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"}) - conn = conn - |> assign(:user, user) - |> get("/api/v1/timelines/home") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/timelines/home") assert length(json_response(conn, 200)) == 0 {:ok, user} = User.follow(user, following) - conn = build_conn() - |> assign(:user, user) - |> get("/api/v1/timelines/home") + conn = + build_conn() + |> assign(:user, user) + |> get("/api/v1/timelines/home") assert [%{"content" => "test"}] = json_response(conn, 200) end @@ -32,44 +34,91 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "the public timeline", %{conn: conn} do following = insert(:user) - capture_log fn -> + capture_log(fn -> {:ok, _activity} = TwitterAPI.create_status(following, %{"status" => "test"}) - {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873") - conn = conn - |> get("/api/v1/timelines/public", %{"local" => "False"}) + {:ok, [_activity]} = + OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873") + + conn = + conn + |> get("/api/v1/timelines/public", %{"local" => "False"}) assert length(json_response(conn, 200)) == 2 - conn = build_conn() - |> get("/api/v1/timelines/public", %{"local" => "True"}) + conn = + build_conn() + |> get("/api/v1/timelines/public", %{"local" => "True"}) assert [%{"content" => "test"}] = json_response(conn, 200) - conn = build_conn() - |> get("/api/v1/timelines/public", %{"local" => "1"}) + conn = + build_conn() + |> get("/api/v1/timelines/public", %{"local" => "1"}) assert [%{"content" => "test"}] = json_response(conn, 200) - end + end) end test "posting a status", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses", %{"status" => "cofe", "spoiler_text" => "2hu", "sensitive" => "false"}) + idempotency_key = "Pikachu rocks!" + + conn_one = + conn + |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key) + # Six hours + assert ttl > :timer.seconds(6 * 60 * 60 - 1) + + assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = + json_response(conn_one, 200) - assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn, 200) assert Repo.get(Activity, id) + + conn_two = + conn + |> assign(:user, user) + |> put_req_header("idempotency-key", idempotency_key) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => second_id} = json_response(conn_two, 200) + + assert id == second_id + + conn_three = + conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => third_id} = json_response(conn_three, 200) + + refute id == third_id end test "posting a sensitive status", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true}) + conn = + conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{"status" => "cofe", "sensitive" => true}) assert %{"content" => "cofe", "id" => id, "sensitive" => true} = json_response(conn, 200) assert Repo.get(Activity, id) @@ -80,9 +129,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, replied_to} = TwitterAPI.create_status(user, %{"status" => "cofe"}) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id}) + conn = + conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{"status" => "xD", "in_reply_to_id" => replied_to.id}) assert %{"content" => "xD", "id" => id} = json_response(conn, 200) @@ -95,9 +145,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "verify_credentials", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> get("/api/v1/accounts/verify_credentials") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/verify_credentials") assert %{"id" => id} = json_response(conn, 200) assert id == to_string(user.id) @@ -106,8 +157,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "get a status", %{conn: conn} do activity = insert(:note_activity) - conn = conn - |> get("/api/v1/statuses/#{activity.id}") + conn = + conn + |> get("/api/v1/statuses/#{activity.id}") assert %{"id" => id} = json_response(conn, 200) assert id == to_string(activity.id) @@ -118,9 +170,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do activity = insert(:note_activity) author = User.get_by_ap_id(activity.data["actor"]) - conn = conn - |> assign(:user, author) - |> delete("/api/v1/statuses/#{activity.id}") + conn = + conn + |> assign(:user, author) + |> delete("/api/v1/statuses/#{activity.id}") assert %{} = json_response(conn, 200) @@ -131,9 +184,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do activity = insert(:note_activity) user = insert(:user) - conn = conn - |> assign(:user, user) - |> delete("/api/v1/statuses/#{activity.id}") + conn = + conn + |> assign(:user, user) + |> delete("/api/v1/statuses/#{activity.id}") assert %{"error" => _} = json_response(conn, 403) @@ -146,14 +200,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, activity} = + TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, [_notification]} = Notification.create_notifications(activity) - conn = conn - |> assign(:user, user) - |> get("/api/v1/notifications") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/notifications") + + expected_response = + "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>" - expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>" assert [%{"status" => %{"content" => response}} | _rest] = json_response(conn, 200) assert response == expected_response end @@ -162,14 +221,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, activity} = + TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, [notification]} = Notification.create_notifications(activity) - conn = conn - |> assign(:user, user) - |> get("/api/v1/notifications/#{notification.id}") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/notifications/#{notification.id}") + + expected_response = + "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>" - expected_response = "hi <span><a href=\"#{user.ap_id}\">@<span>#{user.nickname}</span></a></span>" assert %{"status" => %{"content" => response}} = json_response(conn, 200) assert response == expected_response end @@ -178,12 +242,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, activity} = + TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, [notification]} = Notification.create_notifications(activity) - conn = conn - |> assign(:user, user) - |> post("/api/v1/notifications/dismiss", %{"id" => notification.id}) + conn = + conn + |> assign(:user, user) + |> post("/api/v1/notifications/dismiss", %{"id" => notification.id}) assert %{} = json_response(conn, 200) end @@ -192,18 +259,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - {:ok, activity} = TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, activity} = + TwitterAPI.create_status(other_user, %{"status" => "hi @#{user.nickname}"}) + {:ok, [_notification]} = Notification.create_notifications(activity) - conn = conn - |> assign(:user, user) - |> post("/api/v1/notifications/clear") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/notifications/clear") assert %{} = json_response(conn, 200) - conn = build_conn() - |> assign(:user, user) - |> get("/api/v1/notifications") + conn = + build_conn() + |> assign(:user, user) + |> get("/api/v1/notifications") assert all = json_response(conn, 200) assert all == [] @@ -215,11 +286,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do activity = insert(:note_activity) user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses/#{activity.id}/reblog") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/reblog") + + assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = + json_response(conn, 200) - assert %{"reblog" => %{"id" => id, "reblogged" => true, "reblogs_count" => 1}} = json_response(conn, 200) assert to_string(activity.id) == id end end @@ -229,11 +303,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do activity = insert(:note_activity) user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses/#{activity.id}/favourite") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/favourite") + + assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = + json_response(conn, 200) - assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200) assert to_string(activity.id) == id end end @@ -245,11 +322,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, _, _} = CommonAPI.favorite(activity.id, user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/statuses/#{activity.id}/unfavourite") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/statuses/#{activity.id}/unfavourite") + + assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = + json_response(conn, 200) - assert %{"id" => id, "favourites_count" => 0, "favourited" => false} = json_response(conn, 200) assert to_string(activity.id) == id end end @@ -261,32 +341,53 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = User.get_by_ap_id(note_two.data["actor"]) - conn = conn - |> get("/api/v1/accounts/#{user.id}/statuses") + conn = + conn + |> get("/api/v1/accounts/#{user.id}/statuses") assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(note_two.id) end + test "unimplemented pinned statuses feature", %{conn: conn} do + note = insert(:note_activity) + user = User.get_by_ap_id(note.data["actor"]) + + conn = + conn + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + + assert json_response(conn, 200) == [] + end + test "gets an users media", %{conn: conn} do note = insert(:note_activity) user = User.get_by_ap_id(note.data["actor"]) - file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} - media = TwitterAPI.upload(file, "json") - |> Poison.decode! + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } - {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]}) + media = + TwitterAPI.upload(file, "json") + |> Poison.decode!() - conn = conn - |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"}) + {:ok, image_post} = + TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]}) + + conn = + conn + |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"}) assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(image_post.id) - conn = build_conn() - |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"}) + conn = + build_conn() + |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"}) assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(image_post.id) @@ -299,9 +400,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do other_user = insert(:user) {:ok, user} = User.follow(user, other_user) - conn = conn - |> assign(:user, user) - |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]}) + conn = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/relationships", %{"id" => [other_user.id]}) assert [relationship] = json_response(conn, 200) @@ -312,26 +414,33 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "account fetching", %{conn: conn} do user = insert(:user) - conn = conn - |> get("/api/v1/accounts/#{user.id}") + conn = + conn + |> get("/api/v1/accounts/#{user.id}") assert %{"id" => id} = json_response(conn, 200) assert id == to_string(user.id) - conn = build_conn() - |> get("/api/v1/accounts/-1") + conn = + build_conn() + |> get("/api/v1/accounts/-1") assert %{"error" => "Can't find user"} = json_response(conn, 404) end test "media upload", %{conn: conn} do - file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/media", %{"file" => file}) + conn = + conn + |> assign(:user, user) + |> post("/api/v1/media", %{"file" => file}) assert media = json_response(conn, 200) @@ -341,16 +450,20 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "hashtag timeline", %{conn: conn} do following = insert(:user) - capture_log fn -> + capture_log(fn -> {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"}) - {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873") - conn = conn - |> get("/api/v1/timelines/tag/2hu") + + {:ok, [_activity]} = + OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873") + + conn = + conn + |> get("/api/v1/timelines/tag/2hu") assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(activity.id) - end + end) end test "getting followers", %{conn: conn} do @@ -358,8 +471,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do other_user = insert(:user) {:ok, user} = User.follow(user, other_user) - conn = conn - |> get("/api/v1/accounts/#{other_user.id}/followers") + conn = + conn + |> get("/api/v1/accounts/#{other_user.id}/followers") assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(user.id) @@ -370,8 +484,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do other_user = insert(:user) {:ok, user} = User.follow(user, other_user) - conn = conn - |> get("/api/v1/accounts/#{user.id}/following") + conn = + conn + |> get("/api/v1/accounts/#{user.id}/following") assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(other_user.id) @@ -381,23 +496,28 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/accounts/#{other_user.id}/follow") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/accounts/#{other_user.id}/follow") assert %{"id" => _id, "following" => true} = json_response(conn, 200) user = Repo.get(User, user.id) - conn = build_conn() - |> assign(:user, user) - |> post("/api/v1/accounts/#{other_user.id}/unfollow") + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/accounts/#{other_user.id}/unfollow") assert %{"id" => _id, "following" => false} = json_response(conn, 200) user = Repo.get(User, user.id) - conn = build_conn() - |> assign(:user, user) - |> post("/api/v1/follows", %{"uri" => other_user.nickname}) + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/follows", %{"uri" => other_user.nickname}) assert %{"id" => id} = json_response(conn, 200) assert id == to_string(other_user.id) @@ -407,16 +527,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) other_user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/v1/accounts/#{other_user.id}/block") + conn = + conn + |> assign(:user, user) + |> post("/api/v1/accounts/#{other_user.id}/block") assert %{"id" => _id, "blocking" => true} = json_response(conn, 200) user = Repo.get(User, user.id) - conn = build_conn() - |> assign(:user, user) - |> post("/api/v1/accounts/#{other_user.id}/unblock") + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/accounts/#{other_user.id}/unblock") assert %{"id" => _id, "blocking" => false} = json_response(conn, 200) end @@ -427,9 +550,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, user} = User.block(user, other_user) - conn = conn - |> assign(:user, user) - |> get("/api/v1/blocks") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/blocks") other_user_id = to_string(other_user.id) assert [%{"id" => ^other_user_id}] = json_response(conn, 200) @@ -440,10 +564,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do other_user = insert(:user) ["mute", "unmute"] - |> Enum.each(fn(endpoint) -> - conn = build_conn() - |> assign(:user, user) - |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}") + |> Enum.each(fn endpoint -> + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/accounts/#{other_user.id}/#{endpoint}") assert %{"id" => id} = json_response(conn, 200) assert id == to_string(other_user.id) @@ -454,10 +579,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user = insert(:user) ["blocks", "domain_blocks", "mutes", "follow_requests"] - |> Enum.each(fn(endpoint) -> - conn = build_conn() - |> assign(:user, user) - |> get("/api/v1/#{endpoint}") + |> Enum.each(fn endpoint -> + conn = + build_conn() + |> assign(:user, user) + |> get("/api/v1/#{endpoint}") assert [] = json_response(conn, 200) end) @@ -468,9 +594,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do _user_two = insert(:user, %{nickname: "shp@shitposter.club"}) user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) - conn = conn - |> assign(:user, user) - |> get("/api/v1/accounts/search", %{"q" => "2hu"}) + conn = + conn + |> assign(:user, user) + |> get("/api/v1/accounts/search", %{"q" => "2hu"}) assert [account] = json_response(conn, 200) assert account["id"] == to_string(user_three.id) @@ -482,10 +609,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"}) {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"}) + + {:ok, _activity} = + CommonAPI.post(user, %{ + "status" => "This is about 2hu, but private", + "visibility" => "private" + }) + {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"}) - conn = conn - |> get("/api/v1/search", %{"q" => "2hu"}) + conn = + conn + |> get("/api/v1/search", %{"q" => "2hu"}) assert results = json_response(conn, 200) @@ -499,19 +634,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do end test "search fetches remote statuses", %{conn: conn} do - capture_log fn -> - conn = conn - |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"}) + capture_log(fn -> + conn = + conn + |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"}) + assert results = json_response(conn, 200) [status] = results["statuses"] assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" - end + end) end test "search fetches remote accounts", %{conn: conn} do - conn = conn - |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"}) + conn = + conn + |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"}) assert results = json_response(conn, 200) [account] = results["accounts"] @@ -527,9 +665,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do {:ok, _, _} = CommonAPI.favorite(activity.id, user) - conn = conn - |> assign(:user, user) - |> get("/api/v1/favourites") + conn = + conn + |> assign(:user, user) + |> get("/api/v1/favourites") assert [status] = json_response(conn, 200) assert status["id"] == to_string(activity.id) @@ -539,9 +678,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "updates the user's bio", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"}) + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{"note" => "I drink #cofe"}) assert user = json_response(conn, 200) assert user["note"] == "I drink #cofe" @@ -550,9 +690,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "updates the user's name", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"}) + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{"display_name" => "markorepairs"}) assert user = json_response(conn, 200) assert user["display_name"] == "markorepairs" @@ -561,11 +702,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "updates the user's avatar", %{conn: conn} do user = insert(:user) - new_avatar = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + new_avatar = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } - conn = conn - |> assign(:user, user) - |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar}) + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{"avatar" => new_avatar}) assert user = json_response(conn, 200) assert user["avatar"] != "https://placehold.it/48x48" @@ -574,11 +720,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do test "updates the user's banner", %{conn: conn} do user = insert(:user) - new_header = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + new_header = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } - conn = conn - |> assign(:user, user) - |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header}) + conn = + conn + |> assign(:user, user) + |> patch("/api/v1/accounts/update_credentials", %{"header" => new_header}) assert user = json_response(conn, 200) assert user["header"] != "https://placehold.it/700x335" @@ -594,8 +745,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do Pleroma.Stats.update_stats() - conn = conn - |> get("/api/v1/instance") + conn = + conn + |> get("/api/v1/instance") assert result = json_response(conn, 200) diff --git a/test/web/mastodon_api/status_view_test.exs b/test/web/mastodon_api/status_view_test.exs index 0d396f3b8..d9a0a8a95 100644 --- a/test/web/mastodon_api/status_view_test.exs +++ b/test/web/mastodon_api/status_view_test.exs @@ -13,8 +13,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do status = StatusView.render("status.json", %{activity: note}) - created_at = (note.data["object"]["published"] || "") - |> String.replace(~r/\.\d+Z/, ".000Z") + created_at = + (note.data["object"]["published"] || "") + |> String.replace(~r/\.\d+Z/, ".000Z") expected = %{ id: to_string(note.id), @@ -54,11 +55,27 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do assert status == expected end + test "a reply" do + note = insert(:note_activity) + user = insert(:user) + + {:ok, activity} = + CommonAPI.post(user, %{"status" => "he", "in_reply_to_status_id" => note.id}) + + status = StatusView.render("status.json", %{activity: activity}) + + assert status.in_reply_to_id == note.id + + [status] = StatusView.render("index.json", %{activities: [activity], as: :activity}) + + assert status.in_reply_to_id == note.id + end + test "contains mentions" do incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml") # a user with this ap id might be in the cache. recipient = "https://pleroma.soykaf.com/users/lain" - user = User.get_cached_by_ap_id(recipient) || insert(:user, %{ap_id: recipient}) + user = insert(:user, %{ap_id: recipient}) {:ok, [activity]} = OStatus.handle_incoming(incoming) diff --git a/test/web/oauth/authorization_test.exs b/test/web/oauth/authorization_test.exs index 52441fa7d..4a9e2a3ac 100644 --- a/test/web/oauth/authorization_test.exs +++ b/test/web/oauth/authorization_test.exs @@ -4,7 +4,15 @@ defmodule Pleroma.Web.OAuth.AuthorizationTest do import Pleroma.Factory test "create an authorization token for a valid app" do - {:ok, app} = Repo.insert(App.register_changeset(%App{}, %{client_name: "client", scopes: "scope", redirect_uris: "url"})) + {:ok, app} = + Repo.insert( + App.register_changeset(%App{}, %{ + client_name: "client", + scopes: "scope", + redirect_uris: "url" + }) + ) + user = insert(:user) {:ok, auth} = Authorization.create_authorization(app, user) @@ -16,7 +24,15 @@ defmodule Pleroma.Web.OAuth.AuthorizationTest do end test "use up a token" do - {:ok, app} = Repo.insert(App.register_changeset(%App{}, %{client_name: "client", scopes: "scope", redirect_uris: "url"})) + {:ok, app} = + Repo.insert( + App.register_changeset(%App{}, %{ + client_name: "client", + scopes: "scope", + redirect_uris: "url" + }) + ) + user = insert(:user) {:ok, auth} = Authorization.create_authorization(app, user) @@ -30,7 +46,7 @@ defmodule Pleroma.Web.OAuth.AuthorizationTest do expired_auth = %Authorization{ user_id: user.id, app_id: app.id, - valid_until: NaiveDateTime.add(NaiveDateTime.utc_now, -10), + valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), -10), token: "mytoken", used: false } diff --git a/test/web/oauth/token_test.exs b/test/web/oauth/token_test.exs index 3bd763989..58448949c 100644 --- a/test/web/oauth/token_test.exs +++ b/test/web/oauth/token_test.exs @@ -6,7 +6,15 @@ defmodule Pleroma.Web.OAuth.TokenTest do import Pleroma.Factory test "exchanges a auth token for an access token" do - {:ok, app} = Repo.insert(App.register_changeset(%App{}, %{client_name: "client", scopes: "scope", redirect_uris: "url"})) + {:ok, app} = + Repo.insert( + App.register_changeset(%App{}, %{ + client_name: "client", + scopes: "scope", + redirect_uris: "url" + }) + ) + user = insert(:user) {:ok, auth} = Authorization.create_authorization(app, user) diff --git a/test/web/ostatus/activity_representer_test.exs b/test/web/ostatus/activity_representer_test.exs index 3ee9034a7..8bf3bc775 100644 --- a/test/web/ostatus/activity_representer_test.exs +++ b/test/web/ostatus/activity_representer_test.exs @@ -16,9 +16,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do tuple = ActivityRepresenter.to_simple_form(activity, user) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() - assert String.contains?(res, ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>}) + assert String.contains?( + res, + ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>} + ) end test "a note activity" do @@ -46,7 +49,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do tuple = ActivityRepresenter.to_simple_form(note_activity, user) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() assert clean(res) == clean(expected) end @@ -61,7 +64,10 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do answer = %{answer | data: data} note_object = Object.get_by_ap_id(note.data["object"]["id"]) - Repo.update!(Object.change(note_object, %{ data: Map.put(note_object.data, "external_url", "someurl") })) + + Repo.update!( + Object.change(note_object, %{data: Map.put(note_object.data, "external_url", "someurl")}) + ) user = User.get_cached_by_ap_id(answer.data["actor"]) @@ -86,7 +92,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do tuple = ActivityRepresenter.to_simple_form(answer, user) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() assert clean(res) == clean(expected) end @@ -102,9 +108,11 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do note_user = User.get_cached_by_ap_id(note.data["actor"]) note = Repo.get(Activity, note.id) - note_xml = ActivityRepresenter.to_simple_form(note, note_user, true) - |> :xmerl.export_simple_content(:xmerl_xml) - |> to_string + + note_xml = + ActivityRepresenter.to_simple_form(note, note_user, true) + |> :xmerl.export_simple_content(:xmerl_xml) + |> to_string expected = """ <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type> @@ -120,13 +128,16 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do <activity:object> #{note_xml} </activity:object> - <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/> + <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{ + note.data["actor"] + }"/> <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/> """ - announce_xml = ActivityRepresenter.to_simple_form(announce, user) - |> :xmerl.export_simple_content(:xmerl_xml) - |> to_string + announce_xml = + ActivityRepresenter.to_simple_form(announce, user) + |> :xmerl.export_simple_content(:xmerl_xml) + |> to_string assert clean(expected) == clean(announce_xml) end @@ -139,7 +150,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do tuple = ActivityRepresenter.to_simple_form(like, user) refute is_nil(tuple) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() expected = """ <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb> @@ -156,7 +167,9 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do <link ref="#{like.data["context"]}" rel="ostatus:conversation" /> <link rel="self" type="application/atom+xml" href="#{like.data["id"]}"/> <thr:in-reply-to ref="#{note.data["id"]}" /> - <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/> + <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{ + note.data["actor"] + }"/> <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/collection" href="http://activityschema.org/collection/public"/> """ @@ -166,18 +179,20 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do test "a follow activity" do follower = insert(:user) followed = insert(:user) - {:ok, activity} = ActivityPub.insert(%{ - "type" => "Follow", - "actor" => follower.ap_id, - "object" => followed.ap_id, - "to" => [followed.ap_id] - }) + + {:ok, activity} = + ActivityPub.insert(%{ + "type" => "Follow", + "actor" => follower.ap_id, + "object" => followed.ap_id, + "to" => [followed.ap_id] + }) tuple = ActivityRepresenter.to_simple_form(activity, follower) refute is_nil(tuple) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() expected = """ <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type> @@ -193,7 +208,9 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do <uri>#{activity.data["object"]}</uri> </activity:object> <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/> - <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{activity.data["object"]}"/> + <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{ + activity.data["object"] + }"/> """ assert clean(res) == clean(expected) @@ -209,7 +226,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do refute is_nil(tuple) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() expected = """ <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type> @@ -225,7 +242,9 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do <uri>#{followed.ap_id}</uri> </activity:object> <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/> - <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{followed.ap_id}"/> + <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{ + followed.ap_id + }"/> """ assert clean(res) == clean(expected) @@ -233,13 +252,22 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do test "a delete" do user = insert(:user) - activity = %Activity{data: %{ "id" => "ap_id", "type" => "Delete", "actor" => user.ap_id, "object" => "some_id", "published" => "2017-06-18T12:00:18+00:00" }} + + activity = %Activity{ + data: %{ + "id" => "ap_id", + "type" => "Delete", + "actor" => user.ap_id, + "object" => "some_id", + "published" => "2017-06-18T12:00:18+00:00" + } + } tuple = ActivityRepresenter.to_simple_form(activity, nil) refute is_nil(tuple) - res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary() expected = """ <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type> diff --git a/test/web/ostatus/feed_representer_test.exs b/test/web/ostatus/feed_representer_test.exs index 5b8eabcb9..bf3feb14e 100644 --- a/test/web/ostatus/feed_representer_test.exs +++ b/test/web/ostatus/feed_representer_test.exs @@ -11,15 +11,19 @@ defmodule Pleroma.Web.OStatus.FeedRepresenterTest do tuple = FeedRepresenter.to_simple_form(user, [note_activity], [user]) - most_recent_update = note_activity.updated_at - |> NaiveDateTime.to_iso8601 + most_recent_update = + note_activity.updated_at + |> NaiveDateTime.to_iso8601() res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> to_string - user_xml = UserRepresenter.to_simple_form(user) - |> :xmerl.export_simple_content(:xmerl_xml) - entry_xml = ActivityRepresenter.to_simple_form(note_activity, user) - |> :xmerl.export_simple_content(:xmerl_xml) + user_xml = + UserRepresenter.to_simple_form(user) + |> :xmerl.export_simple_content(:xmerl_xml) + + entry_xml = + ActivityRepresenter.to_simple_form(note_activity, user) + |> :xmerl.export_simple_content(:xmerl_xml) expected = """ <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0"> @@ -39,6 +43,7 @@ defmodule Pleroma.Web.OStatus.FeedRepresenterTest do </entry> </feed> """ + assert clean(res) == clean(expected) end diff --git a/test/web/ostatus/incoming_documents/delete_handling_test.exs b/test/web/ostatus/incoming_documents/delete_handling_test.exs index 43d04dea2..1e041e5b0 100644 --- a/test/web/ostatus/incoming_documents/delete_handling_test.exs +++ b/test/web/ostatus/incoming_documents/delete_handling_test.exs @@ -14,8 +14,13 @@ defmodule Pleroma.Web.OStatus.DeleteHandlingTest do {:ok, like, _object} = Pleroma.Web.ActivityPub.ActivityPub.like(user, object) - incoming = File.read!("test/fixtures/delete.xml") - |> String.replace("tag:mastodon.sdf.org,2017-06-10:objectId=310513:objectType=Status", note.data["object"]["id"]) + incoming = + File.read!("test/fixtures/delete.xml") + |> String.replace( + "tag:mastodon.sdf.org,2017-06-10:objectId=310513:objectType=Status", + note.data["object"]["id"] + ) + {:ok, [delete]} = OStatus.handle_incoming(incoming) refute Repo.get(Activity, note.id) diff --git a/test/web/ostatus/ostatus_controller_test.exs b/test/web/ostatus/ostatus_controller_test.exs index 5b8eb8156..ba8855093 100644 --- a/test/web/ostatus/ostatus_controller_test.exs +++ b/test/web/ostatus/ostatus_controller_test.exs @@ -7,9 +7,11 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do test "decodes a salmon", %{conn: conn} do user = insert(:user) salmon = File.read!("test/fixtures/salmon.xml") - conn = conn - |> put_req_header("content-type", "application/atom+xml") - |> post("/users/#{user.nickname}/salmon", salmon) + + conn = + conn + |> put_req_header("content-type", "application/atom+xml") + |> post("/users/#{user.nickname}/salmon", salmon) assert response(conn, 200) end @@ -17,21 +19,30 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do test "decodes a salmon with a changed magic key", %{conn: conn} do user = insert(:user) salmon = File.read!("test/fixtures/salmon.xml") - conn = conn - |> put_req_header("content-type", "application/atom+xml") - |> post("/users/#{user.nickname}/salmon", salmon) + + conn = + conn + |> put_req_header("content-type", "application/atom+xml") + |> post("/users/#{user.nickname}/salmon", salmon) assert response(conn, 200) # Set a wrong magic-key for a user so it has to refetch salmon_user = User.get_by_ap_id("http://gs.example.org:4040/index.php/user/1") - info = salmon_user.info - |> Map.put("magic_key", "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB") # Wrong key + # Wrong key + info = + salmon_user.info + |> Map.put( + "magic_key", + "RSA.pu0s-halox4tu7wmES1FVSx6u-4wc0YrUFXcqWXZG4-27UmbCOpMQftRCldNRfyA-qLbz-eqiwrong1EwUvjsD4cYbAHNGHwTvDOyx5AKthQUP44ykPv7kjKGh3DWKySJvcs9tlUG87hlo7AvnMo9pwRS_Zz2CacQ-MKaXyDepk=.AQAB" + ) + Repo.update(User.info_changeset(salmon_user, %{info: info})) - conn = build_conn() - |> put_req_header("content-type", "application/atom+xml") - |> post("/users/#{user.nickname}/salmon", salmon) + conn = + build_conn() + |> put_req_header("content-type", "application/atom+xml") + |> post("/users/#{user.nickname}/salmon", salmon) assert response(conn, 200) end @@ -40,8 +51,9 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do note_activity = insert(:note_activity) user = User.get_cached_by_ap_id(note_activity.data["actor"]) - conn = conn - |> get("/users/#{user.nickname}/feed.atom") + conn = + conn + |> get("/users/#{user.nickname}/feed.atom") assert response(conn, 200) =~ note_activity.data["object"]["content"] end @@ -49,27 +61,30 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do test "gets an object", %{conn: conn} do note_activity = insert(:note_activity) user = User.get_by_ap_id(note_activity.data["actor"]) - [_, uuid] = hd Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"]) + [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["object"]["id"])) url = "/objects/#{uuid}" - conn = conn - |> get(url) + conn = + conn + |> get(url) - expected = ActivityRepresenter.to_simple_form(note_activity, user, true) - |> ActivityRepresenter.wrap_with_entry - |> :xmerl.export_simple(:xmerl_xml) - |> to_string + expected = + ActivityRepresenter.to_simple_form(note_activity, user, true) + |> ActivityRepresenter.wrap_with_entry() + |> :xmerl.export_simple(:xmerl_xml) + |> to_string assert response(conn, 200) == expected end test "gets an activity", %{conn: conn} do note_activity = insert(:note_activity) - [_, uuid] = hd Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"]) + [_, uuid] = hd(Regex.scan(~r/.+\/([\w-]+)$/, note_activity.data["id"])) url = "/activities/#{uuid}" - conn = conn - |> get(url) + conn = + conn + |> get(url) assert response(conn, 200) end @@ -78,8 +93,9 @@ defmodule Pleroma.Web.OStatus.OStatusControllerTest do note_activity = insert(:note_activity) url = "/notice/#{note_activity.id}" - conn = conn - |> get(url) + conn = + conn + |> get(url) assert response(conn, 200) end diff --git a/test/web/ostatus/ostatus_test.exs b/test/web/ostatus/ostatus_test.exs index 7f67b9ec0..3e7cf0155 100644 --- a/test/web/ostatus/ostatus_test.exs +++ b/test/web/ostatus/ostatus_test.exs @@ -20,12 +20,18 @@ defmodule Pleroma.Web.OStatusTest do assert user.info["note_count"] == 1 assert activity.data["type"] == "Create" assert activity.data["object"]["type"] == "Note" - assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note" + + assert activity.data["object"]["id"] == + "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note" + assert activity.data["published"] == "2017-04-23T14:51:03+00:00" assert activity.data["object"]["published"] == "2017-04-23T14:51:03+00:00" - assert activity.data["context"] == "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b" + + assert activity.data["context"] == + "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b" + assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"] - assert activity.data["object"]["emoji"] == %{ "marko" => "marko.png", "reimu" => "reimu.png" } + assert activity.data["object"]["emoji"] == %{"marko" => "marko.png", "reimu" => "reimu.png"} assert activity.local == false end @@ -65,10 +71,12 @@ defmodule Pleroma.Web.OStatusTest do test "handle incoming notes - Mastodon, salmon, reply" do # It uses the context of the replied to object Repo.insert!(%Object{ - data: %{ - "id" => "https://pleroma.soykaf.com/objects/c237d966-ac75-4fe3-a87a-d89d71a3a7a4", - "context" => "2hu" - }}) + data: %{ + "id" => "https://pleroma.soykaf.com/objects/c237d966-ac75-4fe3-a87a-d89d71a3a7a4", + "context" => "2hu" + } + }) + incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml") {:ok, [activity]} = OStatus.handle_incoming(incoming) @@ -113,8 +121,13 @@ defmodule Pleroma.Web.OStatusTest do assert activity.data["type"] == "Create" assert activity.data["object"]["type"] == "Note" assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211" - assert activity.data["object"]["content"] == "@<a href=\"https://gs.archae.me/user/4687\" class=\"h-card u-url p-nickname mention\" title=\"shpbot\">shpbot</a> why not indeed." - assert activity.data["object"]["inReplyTo"] == "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note" + + assert activity.data["object"]["content"] == + "@<a href=\"https://gs.archae.me/user/4687\" class=\"h-card u-url p-nickname mention\" title=\"shpbot\">shpbot</a> why not indeed." + + assert activity.data["object"]["inReplyTo"] == + "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note" + assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"] end @@ -141,9 +154,11 @@ defmodule Pleroma.Web.OStatusTest do incoming = File.read!("test/fixtures/share-gs-local.xml") note_activity = insert(:note_activity) user = User.get_cached_by_ap_id(note_activity.data["actor"]) - incoming = incoming - |> String.replace("LOCAL_ID", note_activity.data["object"]["id"]) - |> String.replace("LOCAL_USER", user.ap_id) + + incoming = + incoming + |> String.replace("LOCAL_ID", note_activity.data["object"]["id"]) + |> String.replace("LOCAL_USER", user.ap_id) {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming) @@ -168,7 +183,9 @@ defmodule Pleroma.Web.OStatusTest do assert activity.data["type"] == "Announce" assert activity.data["actor"] == "https://mastodon.social/users/lambadalambda" assert activity.data["object"] == retweeted_activity.data["object"]["id"] - assert activity.data["id"] == "tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status" + + assert activity.data["id"] == + "tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status" refute activity.local assert retweeted_activity.data["type"] == "Create" @@ -178,35 +195,42 @@ defmodule Pleroma.Web.OStatusTest do end test "handle incoming favorites - GS, websub" do - capture_log fn -> + capture_log(fn -> incoming = File.read!("test/fixtures/favorite.xml") {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming) assert activity.data["type"] == "Like" assert activity.data["actor"] == "https://social.heldscal.la/user/23211" assert activity.data["object"] == favorited_activity.data["object"]["id"] - assert activity.data["id"] == "tag:social.heldscal.la,2017-05-05:fave:23211:comment:2061643:2017-05-05T09:12:50+00:00" + + assert activity.data["id"] == + "tag:social.heldscal.la,2017-05-05:fave:23211:comment:2061643:2017-05-05T09:12:50+00:00" refute activity.local assert favorited_activity.data["type"] == "Create" assert favorited_activity.data["actor"] == "https://shitposter.club/user/1" - assert favorited_activity.data["object"]["id"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" + + assert favorited_activity.data["object"]["id"] == + "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" + refute favorited_activity.local - end + end) end test "handle conversation references" do incoming = File.read!("test/fixtures/mastodon_conversation.xml") {:ok, [activity]} = OStatus.handle_incoming(incoming) - assert activity.data["context"] == "tag:mastodon.social,2017-08-28:objectId=7876885:objectType=Conversation" + assert activity.data["context"] == + "tag:mastodon.social,2017-08-28:objectId=7876885:objectType=Conversation" end test "handle incoming favorites with locally available object - GS, websub" do note_activity = insert(:note_activity) - incoming = File.read!("test/fixtures/favorite_with_local_note.xml") - |> String.replace("localid", note_activity.data["object"]["id"]) + incoming = + File.read!("test/fixtures/favorite_with_local_note.xml") + |> String.replace("localid", note_activity.data["object"]["id"]) {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming) @@ -224,9 +248,15 @@ defmodule Pleroma.Web.OStatusTest do assert activity.data["type"] == "Create" assert activity.data["object"]["type"] == "Note" - assert activity.data["object"]["inReplyTo"] == "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc" + + assert activity.data["object"]["inReplyTo"] == + "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc" + assert "http://pleroma.example.org:4000/users/lain5" in activity.data["to"] - assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note" + + assert activity.data["object"]["id"] == + "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note" + assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"] end @@ -234,7 +264,10 @@ defmodule Pleroma.Web.OStatusTest do incoming = File.read!("test/fixtures/follow.xml") {:ok, [activity]} = OStatus.handle_incoming(incoming) assert activity.data["type"] == "Follow" - assert activity.data["id"] == "tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00" + + assert activity.data["id"] == + "tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00" + assert activity.data["actor"] == "https://social.heldscal.la/user/23211" assert activity.data["object"] == "https://pawoo.net/users/pekorino" refute activity.local @@ -304,7 +337,8 @@ defmodule Pleroma.Web.OStatusTest do expected = %{ "hub" => "https://social.heldscal.la/main/push/hub", - "magic_key" => "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB", + "magic_key" => + "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB", "name" => "shp", "nickname" => "shp", "salmon" => "https://social.heldscal.la/main/salmon/user/29191", @@ -314,10 +348,20 @@ defmodule Pleroma.Web.OStatusTest do "host" => "social.heldscal.la", "fqn" => user, "bio" => "cofe", - "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}, + "avatar" => %{ + "type" => "Image", + "url" => [ + %{ + "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", + "mediaType" => "image/jpeg", + "type" => "Link" + } + ] + }, "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}", "ap_id" => nil } + assert data == expected end @@ -329,7 +373,8 @@ defmodule Pleroma.Web.OStatusTest do expected = %{ "hub" => "https://social.heldscal.la/main/push/hub", - "magic_key" => "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB", + "magic_key" => + "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB", "name" => "shp", "nickname" => "shp", "salmon" => "https://social.heldscal.la/main/salmon/user/29191", @@ -339,28 +384,40 @@ defmodule Pleroma.Web.OStatusTest do "host" => "social.heldscal.la", "fqn" => user, "bio" => "cofe", - "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}, + "avatar" => %{ + "type" => "Image", + "url" => [ + %{ + "href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", + "mediaType" => "image/jpeg", + "type" => "Link" + } + ] + }, "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}", "ap_id" => nil } + assert data == expected end end describe "fetching a status by it's HTML url" do test "it builds a missing status from an html url" do - capture_log fn -> + capture_log(fn -> url = "https://shitposter.club/notice/2827873" - {:ok, [activity] } = OStatus.fetch_activity_from_url(url) + {:ok, [activity]} = OStatus.fetch_activity_from_url(url) assert activity.data["actor"] == "https://shitposter.club/user/1" - assert activity.data["object"]["id"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" - end + + assert activity.data["object"]["id"] == + "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" + end) end test "it works for atom notes, too" do url = "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056" - {:ok, [activity] } = OStatus.fetch_activity_from_url(url) + {:ok, [activity]} = OStatus.fetch_activity_from_url(url) assert activity.data["actor"] == "https://social.sakamoto.gq/users/eal" assert activity.data["object"]["id"] == url end @@ -370,6 +427,9 @@ defmodule Pleroma.Web.OStatusTest do incoming = File.read!("test/fixtures/nil_mention_entry.xml") {:ok, [activity]} = OStatus.handle_incoming(incoming) - assert activity.data["to"] == ["http://localhost:4001/users/atarifrosch@social.stopwatchingus-heidelberg.de/followers", "https://www.w3.org/ns/activitystreams#Public"] + assert activity.data["to"] == [ + "http://localhost:4001/users/atarifrosch@social.stopwatchingus-heidelberg.de/followers", + "https://www.w3.org/ns/activitystreams#Public" + ] end end diff --git a/test/web/salmon/salmon_test.exs b/test/web/salmon/salmon_test.exs index cf70c908f..1b39b4b2d 100644 --- a/test/web/salmon/salmon_test.exs +++ b/test/web/salmon/salmon_test.exs @@ -22,7 +22,7 @@ defmodule Pleroma.Web.Salmon.SalmonTest do end test "generates an RSA private key pem" do - {:ok, key} = Salmon.generate_rsa_pem + {:ok, key} = Salmon.generate_rsa_pem() assert is_binary(key) assert Regex.match?(~r/RSA/, key) end @@ -62,7 +62,8 @@ defmodule Pleroma.Web.Salmon.SalmonTest do salmon = File.read!("test/fixtures/salmon2.xml") {:ok, key} = Salmon.fetch_magic_key(salmon) - assert key == "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB" + assert key == + "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB" end test "it pushes an activity to remote accounts it's addressed to" do @@ -75,13 +76,14 @@ defmodule Pleroma.Web.Salmon.SalmonTest do mentioned_user = insert(:user, user_data) note = insert(:note) + activity_data = %{ - "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id, + "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(), "type" => "Create", "actor" => note.data["actor"], "to" => note.data["to"] ++ [mentioned_user.ap_id], "object" => note.data, - "published_at" => DateTime.utc_now() |> DateTime.to_iso8601, + "published_at" => DateTime.utc_now() |> DateTime.to_iso8601(), "context" => note.data["context"] } @@ -89,9 +91,10 @@ defmodule Pleroma.Web.Salmon.SalmonTest do user = Repo.get_by(User, ap_id: activity.data["actor"]) {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user) - poster = fn (url, _data, _headers, _options) -> + poster = fn url, _data, _headers, _options -> assert url == "http://example.org/salmon" end + Salmon.publish(user, activity, poster) end end diff --git a/test/web/streamer_test.exs b/test/web/streamer_test.exs new file mode 100644 index 000000000..47d491d1b --- /dev/null +++ b/test/web/streamer_test.exs @@ -0,0 +1,63 @@ +defmodule Pleroma.Web.StreamerTest do + use Pleroma.DataCase + + alias Pleroma.Web.Streamer + alias Pleroma.User + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + + test "it sends to public" do + user = insert(:user) + other_user = insert(:user) + + task = + Task.async(fn -> + assert_receive {:text, _}, 4_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end + + test "it doesn't send to blocked users" do + user = insert(:user) + blocked_user = insert(:user) + {:ok, user} = User.block(user, blocked_user) + + task = + Task.async(fn -> + refute_receive {:text, _}, 1_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end +end diff --git a/test/web/twitter_api/representers/activity_representer_test.exs b/test/web/twitter_api/representers/activity_representer_test.exs index 98a1705b0..bb47d4409 100644 --- a/test/web/twitter_api/representers/activity_representer_test.exs +++ b/test/web/twitter_api/representers/activity_representer_test.exs @@ -16,12 +16,19 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do {:ok, announce_activity, _object} = ActivityPub.announce(user, object) note_activity = Activity.get_by_ap_id(note_activity.data["id"]) - status = ActivityRepresenter.to_map(announce_activity, %{users: [user, activity_actor], announced_activity: note_activity, for: user}) + status = + ActivityRepresenter.to_map(announce_activity, %{ + users: [user, activity_actor], + announced_activity: note_activity, + for: user + }) assert status["id"] == announce_activity.id assert status["user"] == UserView.render("show.json", %{user: user, for: user}) - retweeted_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user}) + retweeted_status = + ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user}) + assert retweeted_status["repeated"] == true assert retweeted_status["id"] == note_activity.id assert status["statusnet_conversation_id"] == retweeted_status["statusnet_conversation_id"] @@ -36,7 +43,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do object = Object.get_by_ap_id(note_activity.data["object"]["id"]) {:ok, like_activity, _object} = ActivityPub.like(user, object) - status = ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity}) + + status = + ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity}) assert status["id"] == like_activity.id assert status["in_reply_to_status_id"] == note_activity.id @@ -49,7 +58,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do end test "an activity" do - {:ok, user} = UserBuilder.insert + {:ok, user} = UserBuilder.insert() # {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"}) mentioned_user = insert(:user, %{nickname: "shp"}) @@ -70,16 +79,20 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do } } - content_html = "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>" + content_html = + "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>" + content = HtmlSanitizeEx.strip_tags(content_html) - date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601 + date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601() + + {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert() - {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert to = [ User.ap_followers(user), "https://www.w3.org/ns/activitystreams#Public", mentioned_user.ap_id ] + activity = %Activity{ id: 1, data: %{ @@ -92,7 +105,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "type" => "Note", "content" => content_html, "summary" => "2hu", - "inReplyToStatusId" => 213123, + "inReplyToStatusId" => 213_123, "attachment" => [ object ], @@ -112,7 +125,10 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do recipients: to } - 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=\"#{mentioned_user.ap_id}\">@shp</a>" + 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=\"#{ + mentioned_user.ap_id + }\">@shp</a>" expected_status = %{ "id" => activity.id, @@ -122,7 +138,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "text" => "2hu" <> content, "is_post_verb" => true, "created_at" => "Tue May 24 13:26:08 +0000 2016", - "in_reply_to_status_id" => 213123, + "in_reply_to_status_id" => 213_123, "statusnet_conversation_id" => convo_object.id, "attachments" => [ ObjectRepresenter.to_map(object) @@ -141,7 +157,11 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do "uri" => activity.data["object"]["id"] } - assert ActivityRepresenter.to_map(activity, %{user: user, for: follower, mentioned: [mentioned_user]}) == expected_status + assert ActivityRepresenter.to_map(activity, %{ + user: user, + for: follower, + mentioned: [mentioned_user] + }) == expected_status end test "an undo for a follow" do diff --git a/test/web/twitter_api/representers/object_representer_test.exs b/test/web/twitter_api/representers/object_representer_test.exs index ac8184407..ebac051dc 100644 --- a/test/web/twitter_api/representers/object_representer_test.exs +++ b/test/web/twitter_api/representers/object_representer_test.exs @@ -34,13 +34,16 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ObjectReprenterTest do id: nil, data: %{ "mediaType" => "image/png", - "name" => "blabla", "type" => "Document", - "url" => "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png" + "name" => "blabla", + "type" => "Document", + "url" => + "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png" } } expected_object = %{ - url: "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png", + url: + "http://mastodon.example.org/system/media_attachments/files/000/000/001/original/8619f31c6edec470.png", mimetype: "image/png", oembed: false, id: nil diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index d3040f0dc..d7113979a 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -2,9 +2,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do use Pleroma.Web.ConnCase alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Builders.{ActivityBuilder, UserBuilder} - alias Pleroma.{Repo, Activity, User, Object} + alias Pleroma.{Repo, Activity, User, Object, Notification} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.UserView + alias Pleroma.Web.TwitterAPI.NotificationView alias Pleroma.Web.CommonAPI alias Pleroma.Web.TwitterAPI.TwitterAPI @@ -12,13 +13,15 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /api/account/verify_credentials" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/account/verify_credentials.json" + conn = post(conn, "/api/account/verify_credentials.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: user} do - conn = conn + conn = + conn |> with_credentials(user.nickname, "test") |> post("/api/account/verify_credentials.json") @@ -29,13 +32,15 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /api/account/most_recent_notification" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/account/most_recent_notification.json" + conn = post(conn, "/api/account/most_recent_notification.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: user} do - conn = conn + conn = + conn |> with_credentials(user.nickname, "test") |> post("/api/account/most_recent_notification.json", %{id: "200"}) @@ -47,8 +52,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /statuses/update.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/statuses/update.json" + conn = post(conn, "/api/statuses/update.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end @@ -56,30 +62,36 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do conn_with_creds = conn |> with_credentials(user.nickname, "test") request_path = "/api/statuses/update.json" - error_response = %{"request" => request_path, - "error" => "Client must provide a 'status' parameter with a value."} + error_response = %{ + "request" => request_path, + "error" => "Client must provide a 'status' parameter with a value." + } + conn = conn_with_creds |> post(request_path) assert json_response(conn, 400) == error_response - conn = conn_with_creds |> post(request_path, %{ status: "" }) + conn = conn_with_creds |> post(request_path, %{status: ""}) assert json_response(conn, 400) == error_response - conn = conn_with_creds |> post(request_path, %{ status: " " }) + conn = conn_with_creds |> post(request_path, %{status: " "}) assert json_response(conn, 400) == error_response - conn = conn_with_creds |> post(request_path, %{ status: "Nice meme." }) - assert json_response(conn, 200) == ActivityRepresenter.to_map(Repo.one(Activity), %{user: user}) + conn = conn_with_creds |> post(request_path, %{status: "Nice meme."}) + + assert json_response(conn, 200) == + ActivityRepresenter.to_map(Repo.one(Activity), %{user: user}) end end describe "GET /statuses/public_timeline.json" do test "returns statuses", %{conn: conn} do - {:ok, user} = UserBuilder.insert + {:ok, user} = UserBuilder.insert() activities = ActivityBuilder.insert_list(30, %{}, %{user: user}) ActivityBuilder.insert_list(10, %{}, %{user: user}) since_id = List.last(activities).id - conn = conn + conn = + conn |> get("/api/statuses/public_timeline.json", %{since_id: since_id}) response = json_response(conn, 200) @@ -90,12 +102,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "GET /statuses/show/:id.json" do test "returns one status", %{conn: conn} do - {:ok, user} = UserBuilder.insert - {:ok, activity} = ActivityBuilder.insert(%{}, %{user: user}) + user = insert(:user) + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey!"}) actor = Repo.get_by!(User, ap_id: activity.data["actor"]) - conn = conn - |> get("/api/statuses/show/#{activity.id}.json") + conn = + conn + |> get("/api/statuses/show/#{activity.id}.json") response = json_response(conn, 200) @@ -107,8 +120,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "gets user with screen_name", %{conn: conn} do user = insert(:user) - conn = conn - |> get("/api/users/show.json", %{"screen_name" => user.nickname}) + conn = + conn + |> get("/api/users/show.json", %{"screen_name" => user.nickname}) response = json_response(conn, 200) @@ -118,8 +132,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "gets user with user_id", %{conn: conn} do user = insert(:user) - conn = conn - |> get("/api/users/show.json", %{"user_id" => user.id}) + conn = + conn + |> get("/api/users/show.json", %{"user_id" => user.id}) response = json_response(conn, 200) @@ -132,9 +147,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, logged_in, user, _activity} = TwitterAPI.follow(logged_in, %{"user_id" => user.id}) - conn = conn - |> with_credentials(logged_in.nickname, "test") - |> get("/api/users/show.json", %{"user_id" => user.id}) + conn = + conn + |> with_credentials(logged_in.nickname, "test") + |> get("/api/users/show.json", %{"user_id" => user.id}) response = json_response(conn, 200) @@ -144,14 +160,14 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "GET /statusnet/conversation/:id.json" do test "returns the statuses in the conversation", %{conn: conn} do - {:ok, _user} = UserBuilder.insert - {:ok, _activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) + {:ok, _user} = UserBuilder.insert() + {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) {:ok, _activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"}) - {:ok, object} = Object.context_mapping("2hu") |> Repo.insert - conn = conn - |> get("/api/statusnet/conversation/#{object.id}.json") + conn = + conn + |> get("/api/statusnet/conversation/#{activity.data["context_id"]}.json") response = json_response(conn, 200) @@ -161,58 +177,116 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "GET /statuses/friends_timeline.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = get conn, "/api/statuses/friends_timeline.json" + conn = get(conn, "/api/statuses/friends_timeline.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: current_user} do user = insert(:user) - activities = ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user}) - returned_activities = ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user}) + + activities = + ActivityBuilder.insert_list(30, %{"to" => [User.ap_followers(user)]}, %{user: user}) + + returned_activities = + ActivityBuilder.insert_list(10, %{"to" => [User.ap_followers(user)]}, %{user: user}) + other_user = insert(:user) ActivityBuilder.insert_list(10, %{}, %{user: other_user}) since_id = List.last(activities).id - current_user = Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) |> Repo.update! + current_user = + Ecto.Changeset.change(current_user, following: [User.ap_followers(user)]) + |> Repo.update!() - conn = conn + conn = + conn |> with_credentials(current_user.nickname, "test") |> get("/api/statuses/friends_timeline.json", %{since_id: since_id}) response = json_response(conn, 200) assert length(response) == 10 - assert response == Enum.map(returned_activities, fn (activity) -> ActivityRepresenter.to_map(activity, %{user: User.get_cached_by_ap_id(activity.data["actor"]), for: current_user}) end) + + assert response == + Enum.map(returned_activities, fn activity -> + ActivityRepresenter.to_map(activity, %{ + user: User.get_cached_by_ap_id(activity.data["actor"]), + for: current_user + }) + end) end end describe "GET /statuses/mentions.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = get conn, "/api/statuses/mentions.json" + conn = get(conn, "/api/statuses/mentions.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: current_user} do - {:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user}) + {:ok, activity} = + ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user}) - conn = conn + conn = + conn |> with_credentials(current_user.nickname, "test") |> get("/api/statuses/mentions.json") response = json_response(conn, 200) assert length(response) == 1 - assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]}) + + assert Enum.at(response, 0) == + ActivityRepresenter.to_map(activity, %{ + user: current_user, + mentioned: [current_user] + }) + end + end + + describe "GET /api/qvitter/statuses/notifications.json" do + setup [:valid_user] + + test "without valid credentials", %{conn: conn} do + conn = get(conn, "/api/qvitter/statuses/notifications.json") + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "with credentials", %{conn: conn, user: current_user} do + {:ok, activity} = + ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user}) + + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/qvitter/statuses/notifications.json") + + response = json_response(conn, 200) + + assert length(response) == 1 + + assert response == + NotificationView.render("notification.json", %{ + notifications: Notification.for_user(current_user), + for: current_user + }) end end describe "GET /statuses/user_timeline.json" do setup [:valid_user] + test "without any params", %{conn: conn} do conn = get(conn, "/api/statuses/user_timeline.json") - assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id", "request" => "/api/statuses/user_timeline.json"} + + assert json_response(conn, 400) == %{ + "error" => "You need to specify screen_name or user_id", + "request" => "/api/statuses/user_timeline.json" + } end test "with user_id", %{conn: conn} do @@ -237,9 +311,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "with credentials", %{conn: conn, user: current_user} do {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user}) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> get("/api/statuses/user_timeline.json") + + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json") response = json_response(conn, 200) @@ -250,9 +326,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "with credentials with user_id", %{conn: conn, user: current_user} do user = insert(:user) {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id}) + + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json", %{"user_id" => user.id}) response = json_response(conn, 200) @@ -263,9 +341,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "with credentials screen_name", %{conn: conn, user: current_user} do user = insert(:user) {:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user}) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname}) + + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname}) response = json_response(conn, 200) @@ -276,28 +356,33 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /friendships/create.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/friendships/create.json" + conn = post(conn, "/api/friendships/create.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: current_user} do followed = insert(:user) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/friendships/create.json", %{user_id: followed.id}) + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/friendships/create.json", %{user_id: followed.id}) current_user = Repo.get(User, current_user.id) assert User.ap_followers(followed) in current_user.following - assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user}) + + assert json_response(conn, 200) == + UserView.render("show.json", %{user: followed, for: current_user}) end end describe "POST /friendships/destroy.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/friendships/destroy.json" + conn = post(conn, "/api/friendships/destroy.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end @@ -308,40 +393,48 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert User.ap_followers(followed) in current_user.following ActivityPub.follow(current_user, followed) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/friendships/destroy.json", %{user_id: followed.id}) + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/friendships/destroy.json", %{user_id: followed.id}) current_user = Repo.get(User, current_user.id) assert current_user.following == [current_user.ap_id] - assert json_response(conn, 200) == UserView.render("show.json", %{user: followed, for: current_user}) + + assert json_response(conn, 200) == + UserView.render("show.json", %{user: followed, for: current_user}) end end describe "POST /blocks/create.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/blocks/create.json" + conn = post(conn, "/api/blocks/create.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: current_user} do blocked = insert(:user) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/blocks/create.json", %{user_id: blocked.id}) + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/blocks/create.json", %{user_id: blocked.id}) current_user = Repo.get(User, current_user.id) assert User.blocks?(current_user, blocked) - assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user}) + + assert json_response(conn, 200) == + UserView.render("show.json", %{user: blocked, for: current_user}) end end describe "POST /blocks/destroy.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/blocks/destroy.json" + conn = post(conn, "/api/blocks/destroy.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end @@ -351,56 +444,66 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, current_user} = User.block(current_user, blocked) assert User.blocks?(current_user, blocked) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/blocks/destroy.json", %{user_id: blocked.id}) + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/blocks/destroy.json", %{user_id: blocked.id}) current_user = Repo.get(User, current_user.id) assert current_user.info["blocks"] == [] - assert json_response(conn, 200) == UserView.render("show.json", %{user: blocked, for: current_user}) + + assert json_response(conn, 200) == + UserView.render("show.json", %{user: blocked, for: current_user}) end end describe "GET /help/test.json" do test "returns \"ok\"", %{conn: conn} do - conn = get conn, "/api/help/test.json" + conn = get(conn, "/api/help/test.json") assert json_response(conn, 200) == "ok" end end describe "POST /api/qvitter/update_avatar.json" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do - conn = post conn, "/api/qvitter/update_avatar.json" + conn = post(conn, "/api/qvitter/update_avatar.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end test "with credentials", %{conn: conn, user: current_user} do avatar_image = File.read!("test/fixtures/avatar_data_uri") - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/qvitter/update_avatar.json", %{img: avatar_image}) + + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/qvitter/update_avatar.json", %{img: avatar_image}) current_user = Repo.get(User, current_user.id) assert is_map(current_user.avatar) - assert json_response(conn, 200) == UserView.render("show.json", %{user: current_user, for: current_user}) + + assert json_response(conn, 200) == + UserView.render("show.json", %{user: current_user, for: current_user}) end end describe "POST /api/favorites/create/:id" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do note_activity = insert(:note_activity) - conn = post conn, "/api/favorites/create/#{note_activity.id}.json" + conn = post(conn, "/api/favorites/create/#{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) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/favorites/create/#{note_activity.id}.json") + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/favorites/create/#{note_activity.id}.json") assert json_response(conn, 200) end @@ -408,9 +511,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /api/favorites/destroy/:id" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do note_activity = insert(:note_activity) - conn = post conn, "/api/favorites/destroy/#{note_activity.id}.json" + conn = post(conn, "/api/favorites/destroy/#{note_activity.id}.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end @@ -419,9 +523,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do object = Object.get_by_ap_id(note_activity.data["object"]["id"]) ActivityPub.like(current_user, object) - conn = conn - |> with_credentials(current_user.nickname, "test") - |> post("/api/favorites/destroy/#{note_activity.id}.json") + conn = + conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/favorites/destroy/#{note_activity.id}.json") assert json_response(conn, 200) end @@ -429,9 +534,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do describe "POST /api/statuses/retweet/:id" do setup [:valid_user] + test "without valid credentials", %{conn: conn} do note_activity = insert(:note_activity) - conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json" + conn = post(conn, "/api/statuses/retweet/#{note_activity.id}.json") assert json_response(conn, 403) == %{"error" => "Invalid credentials."} end @@ -440,12 +546,16 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do request_path = "/api/statuses/retweet/#{note_activity.id}.json" - response = conn - |> with_credentials(current_user.nickname, "test") - |> post(request_path) + 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}) + + assert json_response(response, 200) == + ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user}) end end @@ -460,8 +570,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do "confirm" => "bear" } - conn = conn - |> post("/api/account/register", data) + conn = + conn + |> post("/api/account/register", data) user = json_response(conn, 200) @@ -478,8 +589,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do "confirm" => "bear" } - conn = conn - |> post("/api/account/register", data) + conn = + conn + |> post("/api/account/register", data) errors = json_response(conn, 400) @@ -492,9 +604,10 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do user = insert(:user) other_user = insert(:user) - conn = conn - |> assign(:user, user) - |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id}) + conn = + conn + |> assign(:user, user) + |> get("/api/externalprofile/show", %{profileurl: other_user.ap_id}) assert json_response(conn, 200) == UserView.render("show.json", %{user: other_user}) end @@ -510,11 +623,14 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, follower_one} = User.follow(follower_one, user) {:ok, follower_two} = User.follow(follower_two, user) - conn = conn - |> assign(:user, user) - |> get("/api/statuses/followers") + conn = + conn + |> assign(:user, user) + |> get("/api/statuses/followers") - assert json_response(conn, 200) == UserView.render("index.json", %{users: [follower_one, follower_two], for: user}) + expected = UserView.render("index.json", %{users: [follower_one, follower_two], for: user}) + result = json_response(conn, 200) + assert Enum.sort(expected) == Enum.sort(result) end end @@ -528,11 +644,14 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, user} = User.follow(user, followed_one) {:ok, user} = User.follow(user, followed_two) - conn = conn - |> assign(:user, user) - |> get("/api/statuses/friends") + conn = + conn + |> assign(:user, user) + |> get("/api/statuses/friends") - assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) + expected = UserView.render("index.json", %{users: [followed_one, followed_two], for: user}) + result = json_response(conn, 200) + assert Enum.sort(expected) == Enum.sort(result) end test "it returns a given user's friends with user_id", %{conn: conn} do @@ -544,10 +663,16 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, user} = User.follow(user, followed_one) {:ok, user} = User.follow(user, followed_two) - conn = conn - |> get("/api/statuses/friends", %{"user_id" => user.id}) + conn = + conn + |> get("/api/statuses/friends", %{"user_id" => user.id}) - assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) + assert MapSet.equal?( + MapSet.new(json_response(conn, 200)), + MapSet.new( + UserView.render("index.json", %{users: [followed_one, followed_two], for: user}) + ) + ) end test "it returns a given user's friends with screen_name", %{conn: conn} do @@ -559,10 +684,16 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, user} = User.follow(user, followed_one) {:ok, user} = User.follow(user, followed_two) - conn = conn - |> get("/api/statuses/friends", %{"screen_name" => user.nickname}) + conn = + conn + |> get("/api/statuses/friends", %{"screen_name" => user.nickname}) - assert MapSet.equal?(MapSet.new(json_response(conn, 200)), MapSet.new(UserView.render("index.json", %{users: [followed_one, followed_two], for: user}))) + assert MapSet.equal?( + MapSet.new(json_response(conn, 200)), + MapSet.new( + UserView.render("index.json", %{users: [followed_one, followed_two], for: user}) + ) + ) end end @@ -576,12 +707,17 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, user} = User.follow(user, followed_one) {:ok, user} = User.follow(user, followed_two) - conn = conn - |> assign(:user, user) - |> get("/api/friends/ids") + conn = + conn + |> assign(:user, user) + |> get("/api/friends/ids") expected = [followed_one.id, followed_two.id] - assert MapSet.equal?(MapSet.new(Poison.decode!(json_response(conn, 200))), MapSet.new(expected)) + + assert MapSet.equal?( + MapSet.new(Poison.decode!(json_response(conn, 200))), + MapSet.new(expected) + ) end end @@ -589,9 +725,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do test "it updates a user's profile", %{conn: conn} do user = insert(:user) - conn = conn - |> assign(:user, user) - |> post("/api/account/update_profile.json", %{"name" => "new name", "description" => "new description"}) + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "name" => "new name", + "description" => "new description" + }) user = Repo.get!(User, user.id) assert user.name == "new name" @@ -619,8 +759,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"}) {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"}) - conn = conn - |> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"}) + conn = + conn + |> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"}) assert [status] = json_response(conn, 200) assert status["id"] == activity.id @@ -635,11 +776,26 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do {:ok, activity} = CommonAPI.post(user, %{"status" => "This is about #2hu"}) {:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"}) - conn = conn - |> get("/api/statusnet/tags/timeline/2hu.json") + conn = + conn + |> get("/api/statusnet/tags/timeline/2hu.json") assert [status] = json_response(conn, 200) assert status["id"] == activity.id end end + + test "Convert newlines to <br> in bio", %{conn: conn} do + user = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "description" => "Hello,\r\nWorld! I\n am a test." + }) + + user = Repo.get!(User, user.id) + assert user.bio == "Hello,<br>World! I<br> am a test." + end end diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 6b0b182a3..af565c0e5 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -1,11 +1,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do use Pleroma.DataCase - alias Pleroma.Builders.{UserBuilder, ActivityBuilder} + alias Pleroma.Builders.UserBuilder alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView} - alias Pleroma.Web.CommonAPI.Utils alias Pleroma.{Activity, User, Object, Repo} - alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.TwitterAPI.ActivityView import Pleroma.Factory @@ -28,22 +27,32 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do object = Repo.insert!(%Object{data: object_data}) input = %{ - "status" => "Hello again, @shp.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric", + "status" => + "Hello again, @shp.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric", "media_ids" => [object.id] } - { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input) + {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input) - assert get_in(activity.data, ["object", "content"]) == "Hello again, <span><a href='shp'>@<span>shp</span></a></span>.<script></script><br>This is on another :moominmamma: line. #2hu #epic #phantasmagoric<br><a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>" + 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>" + + assert get_in(activity.data, ["object", "content"]) == expected_text assert get_in(activity.data, ["object", "type"]) == "Note" assert get_in(activity.data, ["object", "actor"]) == user.ap_id assert get_in(activity.data, ["actor"]) == user.ap_id assert Enum.member?(get_in(activity.data, ["cc"]), User.ap_followers(user)) - assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public") + + assert Enum.member?( + get_in(activity.data, ["to"]), + "https://www.w3.org/ns/activitystreams#Public" + ) + assert Enum.member?(get_in(activity.data, ["cc"]), "shp") assert activity.local == true - assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} = activity.data["object"]["emoji"] + assert %{"moominmamma" => "http://localhost:4001/finmoji/128px/moominmamma-128.png"} = + activity.data["object"]["emoji"] # hashtags assert activity.data["object"]["tag"] == ["2hu", "epic", "phantasmagoric"] @@ -63,126 +72,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "create a status that is a reply" do user = insert(:user) + input = %{ "status" => "Hello again." } - { :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input) + {:ok, activity = %Activity{}} = TwitterAPI.create_status(user, input) input = %{ "status" => "Here's your (you).", "in_reply_to_status_id" => activity.id } - { :ok, reply = %Activity{} } = TwitterAPI.create_status(user, input) + {:ok, reply = %Activity{}} = TwitterAPI.create_status(user, input) assert get_in(reply.data, ["context"]) == get_in(activity.data, ["context"]) - assert get_in(reply.data, ["object", "context"]) == get_in(activity.data, ["object", "context"]) - assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"]) - assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id - end - - test "fetch public statuses, excluding remote ones." do - %{ public: activity, user: user } = ActivityBuilder.public_and_non_public - insert(:note_activity, %{local: false}) - - follower = insert(:user, following: [User.ap_followers(user)]) - - statuses = TwitterAPI.fetch_public_statuses(follower) - - assert length(statuses) == 1 - assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower}) - end - - test "fetch whole known network statuses" do - %{ public: activity, user: user } = ActivityBuilder.public_and_non_public - insert(:note_activity, %{local: false}) - - follower = insert(:user, following: [user.follower_address]) - statuses = TwitterAPI.fetch_public_and_external_statuses(follower) + assert get_in(reply.data, ["object", "context"]) == + get_in(activity.data, ["object", "context"]) - assert length(statuses) == 2 - assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user, for: follower}) - end - - test "fetch friends' statuses" do - user = insert(:user, %{following: ["someguy/followers"]}) - {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]}) - {:ok, direct_activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]}) - - statuses = TwitterAPI.fetch_friend_statuses(user) - - activity_user = Repo.get_by(User, ap_id: activity.data["actor"]) - direct_activity_user = Repo.get_by(User, ap_id: direct_activity.data["actor"]) - - assert length(statuses) == 2 - assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user}) - assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]}) - end - - test "fetch user's mentions" do - user = insert(:user) - {:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]}) - activity_user = Repo.get_by(User, ap_id: activity.data["actor"]) - - statuses = TwitterAPI.fetch_mentions(user) - - assert length(statuses) == 1 - assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]}) - end - - test "get a user by params" do - user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"}) - {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"}) - - assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil) - assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id}) - assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.nickname}) - assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname}) - assert user1_result == TwitterAPI.get_user(user1, nil) - assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id}) - assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname}) - assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"}) - assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666}) - end - - test "fetch user's statuses" do - {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"}) - {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"}) - - {:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1}) - {:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2}) - - user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id}) - - assert length(user1_statuses) == 1 - assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1}) - - user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id}) - - assert length(user2_statuses) == 1 - assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2}) - end - - test "fetch a single status" do - {:ok, activity} = ActivityBuilder.insert() - {:ok, user} = UserBuilder.insert() - actor = Repo.get_by!(User, ap_id: activity.data["actor"]) - - status = TwitterAPI.fetch_status(user, activity.id) - - assert status == ActivityRepresenter.to_map(activity, %{for: user, user: actor}) + assert get_in(reply.data, ["object", "inReplyTo"]) == get_in(activity.data, ["object", "id"]) + assert get_in(reply.data, ["object", "inReplyToStatusId"]) == activity.id end test "Follow another user using user_id" do user = insert(:user) followed = insert(:user) - {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"user_id" => followed.id}) + {:ok, user, followed, _activity} = TwitterAPI.follow(user, %{"user_id" => followed.id}) assert User.ap_followers(followed) in user.following - { :error, msg } = TwitterAPI.follow(user, %{"user_id" => followed.id}) + {:error, msg} = TwitterAPI.follow(user, %{"user_id" => followed.id}) assert msg == "Could not follow user: #{followed.nickname} is already on your list." end @@ -190,13 +110,15 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do user = insert(:user) followed = insert(:user) - {:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) + {:ok, user, followed, _activity} = + TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) + assert User.ap_followers(followed) in user.following followed = User.get_by_ap_id(followed.ap_id) assert followed.info["follower_count"] == 1 - { :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) + {:error, msg} = TwitterAPI.follow(user, %{"screen_name" => followed.nickname}) assert msg == "Could not follow user: #{followed.nickname} is already on your list." end @@ -205,10 +127,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do user = insert(:user, %{following: [User.ap_followers(unfollowed)]}) ActivityPub.follow(user, unfollowed) - {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id}) + {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id}) assert user.following == [] - { :error, msg } = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id}) + {:error, msg} = TwitterAPI.unfollow(user, %{"user_id" => unfollowed.id}) assert msg == "Not subscribed!" end @@ -218,10 +140,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do ActivityPub.follow(user, unfollowed) - {:ok, user, unfollowed } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname}) + {:ok, user, unfollowed} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname}) assert user.following == [] - { :error, msg } = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname}) + {:error, msg} = TwitterAPI.unfollow(user, %{"screen_name" => unfollowed.nickname}) assert msg == "Not subscribed!" end @@ -259,77 +181,51 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do assert user.info["blocks"] == [] end - test "fetch statuses in a context using the conversation id" do - {:ok, user} = UserBuilder.insert() - {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) - {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"}) - {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"}) - - {:ok, object} = Object.context_mapping("2hu") |> Repo.insert - - statuses = TwitterAPI.fetch_conversation(user, object.id) - - assert length(statuses) == 2 - assert Enum.at(statuses, 1)["id"] == activity.id - assert Enum.at(statuses, 0)["id"] == activity_two.id - end - test "upload a file" do - file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } response = TwitterAPI.upload(file) assert is_binary(response) end - test "it adds user links to an existing text" do - text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me" - - gsimg = insert(:user, %{nickname: "gsimg"}) - archaeme = insert(:user, %{nickname: "archaeme"}) - archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"}) - - mentions = Pleroma.Formatter.parse_mentions(text) - expected_text = "<span><a href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a href='#{archaeme.ap_id}'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>" - - assert Utils.add_user_links(text, mentions) == expected_text - end - - test "it favorites a status, returns the updated status" do + test "it favorites a status, returns the updated activity" do user = insert(:user) note_activity = insert(:note_activity) - activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) {:ok, status} = TwitterAPI.fav(user, note_activity.id) updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) - assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user}) + assert status == updated_activity end - test "it unfavorites a status, returns the updated status" do + test "it unfavorites a status, returns the updated activity" do user = insert(:user) note_activity = insert(:note_activity) - activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) object = Object.get_by_ap_id(note_activity.data["object"]["id"]) - {:ok, _like_activity, _object } = ActivityPub.like(user, object) + {:ok, _like_activity, _object} = ActivityPub.like(user, object) updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) - assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1 - {:ok, status} = TwitterAPI.unfav(user, note_activity.id) + assert ActivityView.render("activity.json", activity: updated_activity)["fave_num"] == 1 + + {:ok, activity} = TwitterAPI.unfav(user, note_activity.id) - assert status["fave_num"] == 0 + assert ActivityView.render("activity.json", activity: activity)["fave_num"] == 0 end test "it retweets a status and returns the retweet" do user = insert(:user) note_activity = insert(:note_activity) - activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) {:ok, status} = TwitterAPI.repeat(user, note_activity.id) updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) - assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user}) + assert status == updated_activity end test "it registers a new user and returns the user." do @@ -345,7 +241,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do {:ok, user} = TwitterAPI.register_user(data) fetched_user = Repo.get_by(User, nickname: "lain") - assert UserView.render("show.json", %{user: user}) == UserView.render("show.json", %{user: fetched_user}) + + assert UserView.render("show.json", %{user: user}) == + UserView.render("show.json", %{user: fetched_user}) end test "it returns the error on registration problems" do @@ -365,8 +263,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do test "it assigns an integer conversation_id" do note_activity = insert(:note_activity) - user = User.get_cached_by_ap_id(note_activity.data["actor"]) - status = ActivityRepresenter.to_map(note_activity, %{user: user}) + status = ActivityView.render("activity.json", activity: note_activity) assert is_number(status["statusnet_conversation_id"]) end @@ -386,7 +283,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do end test "returns an existing mapping for an existing object" do - {:ok, object} = Object.context_mapping("random context") |> Repo.insert + {:ok, object} = Object.context_mapping("random context") |> Repo.insert() conversation_id = TwitterAPI.context_to_conversation_id("random context") assert conversation_id == object.id diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs new file mode 100644 index 000000000..7f2017d3c --- /dev/null +++ b/test/web/twitter_api/views/activity_view_test.exs @@ -0,0 +1,209 @@ +defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do + use Pleroma.DataCase + + alias Pleroma.Web.CommonAPI + alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.TwitterAPI.ActivityView + alias Pleroma.Web.TwitterAPI.UserView + alias Pleroma.Web.TwitterAPI.TwitterAPI + alias Pleroma.Repo + alias Pleroma.Activity + alias Pleroma.User + alias Pleroma.Web.ActivityPub.ActivityPub + + import Pleroma.Factory + import Mock + + test "a create activity with a note" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + + result = ActivityView.render("activity.json", activity: activity) + + convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"]) + + expected = %{ + "activity_type" => "post", + "attachments" => [], + "attentions" => [ + UserView.render("show.json", %{user: other_user}) + ], + "created_at" => activity.data["object"]["published"] |> Utils.date_to_asctime(), + "external_url" => activity.data["object"]["id"], + "fave_num" => 0, + "favorited" => false, + "id" => activity.id, + "in_reply_to_status_id" => nil, + "is_local" => true, + "is_post_verb" => true, + "possibly_sensitive" => false, + "repeat_num" => 0, + "repeated" => false, + "statusnet_conversation_id" => convo_id, + "statusnet_html" => + "Hey <span><a href=\"#{other_user.ap_id}\">@<span>shp</span></a></span>!", + "tags" => [], + "text" => "Hey @shp!", + "uri" => activity.data["object"]["id"], + "user" => UserView.render("show.json", %{user: user}) + } + + assert result == expected + end + + test "a list of activities" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + + convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"]) + + mocks = [ + { + TwitterAPI, + [], + [context_to_conversation_id: fn _ -> false end] + }, + { + User, + [:passthrough], + [get_cached_by_ap_id: fn _ -> nil end] + } + ] + + with_mocks mocks do + [result] = ActivityView.render("index.json", activities: [activity]) + + assert result["statusnet_conversation_id"] == convo_id + assert result["user"] + refute called(TwitterAPI.context_to_conversation_id(:_)) + refute called(User.get_cached_by_ap_id(user.ap_id)) + refute called(User.get_cached_by_ap_id(other_user.ap_id)) + end + end + + test "an activity that is a reply" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + + {:ok, answer} = + CommonAPI.post(other_user, %{"status" => "Hi!", "in_reply_to_status_id" => activity.id}) + + result = ActivityView.render("activity.json", %{activity: answer}) + + assert result["in_reply_to_status_id"] == activity.id + end + + test "a like activity" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + {:ok, like, _object} = CommonAPI.favorite(activity.id, other_user) + + result = ActivityView.render("activity.json", activity: like) + + expected = %{ + "activity_type" => "like", + "created_at" => like.data["published"] |> Utils.date_to_asctime(), + "external_url" => like.data["id"], + "id" => like.id, + "in_reply_to_status_id" => activity.id, + "is_local" => true, + "is_post_verb" => false, + "statusnet_html" => "shp favorited a status.", + "text" => "shp favorited a status.", + "uri" => "tag:#{like.data["id"]}:objectType=Favourite", + "user" => UserView.render("show.json", user: other_user) + } + + assert result == expected + end + + test "an announce activity" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + {:ok, announce, _object} = CommonAPI.repeat(activity.id, other_user) + + convo_id = TwitterAPI.context_to_conversation_id(activity.data["object"]["context"]) + + activity = Repo.get(Activity, activity.id) + + result = ActivityView.render("activity.json", activity: announce) + + expected = %{ + "activity_type" => "repeat", + "created_at" => announce.data["published"] |> Utils.date_to_asctime(), + "external_url" => announce.data["id"], + "id" => announce.id, + "is_local" => true, + "is_post_verb" => false, + "statusnet_html" => "shp retweeted a status.", + "text" => "shp retweeted a status.", + "uri" => "tag:#{announce.data["id"]}:objectType=note", + "user" => UserView.render("show.json", user: other_user), + "retweeted_status" => ActivityView.render("activity.json", activity: activity), + "statusnet_conversation_id" => convo_id + } + + assert result == expected + end + + test "A follow activity" do + user = insert(:user) + other_user = insert(:user, %{nickname: "shp"}) + + {:ok, follower} = User.follow(user, other_user) + {:ok, follow} = ActivityPub.follow(follower, other_user) + + result = ActivityView.render("activity.json", activity: follow) + + expected = %{ + "activity_type" => "follow", + "attentions" => [], + "created_at" => follow.data["published"] |> Utils.date_to_asctime(), + "external_url" => follow.data["id"], + "id" => follow.id, + "in_reply_to_status_id" => nil, + "is_local" => true, + "is_post_verb" => false, + "statusnet_html" => "#{user.nickname} started following shp", + "text" => "#{user.nickname} started following shp", + "user" => UserView.render("show.json", user: user) + } + + assert result == expected + end + + test "a delete activity" do + user = insert(:user) + + {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!"}) + {:ok, delete} = CommonAPI.delete(activity.id, user) + + result = ActivityView.render("activity.json", activity: delete) + + expected = %{ + "activity_type" => "delete", + "attentions" => [], + "created_at" => delete.data["published"] |> Utils.date_to_asctime(), + "external_url" => delete.data["id"], + "id" => delete.id, + "in_reply_to_status_id" => nil, + "is_local" => true, + "is_post_verb" => false, + "statusnet_html" => "deleted notice {{tag", + "text" => "deleted notice {{tag", + "uri" => delete.data["object"], + "user" => UserView.render("show.json", user: user) + } + + assert result == expected + end +end diff --git a/test/web/twitter_api/views/notification_view_test.exs b/test/web/twitter_api/views/notification_view_test.exs new file mode 100644 index 000000000..e3b140657 --- /dev/null +++ b/test/web/twitter_api/views/notification_view_test.exs @@ -0,0 +1,108 @@ +defmodule Pleroma.Web.TwitterAPI.NotificationViewTest do + use Pleroma.DataCase + + alias Pleroma.{User, Notification} + alias Pleroma.Web.TwitterAPI.TwitterAPI + alias Pleroma.Web.TwitterAPI.NotificationView + alias Pleroma.Web.TwitterAPI.UserView + alias Pleroma.Web.TwitterAPI.ActivityView + alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Builders.UserBuilder + + import Pleroma.Factory + + setup do + user = insert(:user, bio: "<span>Here's some html</span>") + [user: user] + end + + test "A follow notification" do + note_activity = insert(:note_activity) + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + follower = insert(:user) + + {:ok, follower} = User.follow(follower, user) + {:ok, activity} = ActivityPub.follow(follower, user) + Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id))) + [follow_notif] = Notification.for_user(user) + + represented = %{ + "created_at" => follow_notif.inserted_at |> Utils.format_naive_asctime(), + "from_profile" => UserView.render("show.json", %{user: follower, for: user}), + "id" => follow_notif.id, + "is_seen" => 0, + "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}), + "ntype" => "follow" + } + + assert represented == + NotificationView.render("notification.json", %{notification: follow_notif, for: user}) + end + + test "A mention notification" do + user = insert(:user) + other_user = insert(:user) + + {:ok, activity} = + TwitterAPI.create_status(other_user, %{"status" => "Päivää, @#{user.nickname}"}) + + [notification] = Notification.for_user(user) + + represented = %{ + "created_at" => notification.inserted_at |> Utils.format_naive_asctime(), + "from_profile" => UserView.render("show.json", %{user: other_user, for: user}), + "id" => notification.id, + "is_seen" => 0, + "notice" => ActivityView.render("activity.json", %{activity: activity, for: user}), + "ntype" => "mention" + } + + assert represented == + NotificationView.render("notification.json", %{notification: notification, for: user}) + end + + test "A retweet notification" do + note_activity = insert(:note_activity) + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + repeater = insert(:user) + + {:ok, activity} = TwitterAPI.repeat(repeater, note_activity.id) + [notification] = Notification.for_user(user) + + represented = %{ + "created_at" => notification.inserted_at |> Utils.format_naive_asctime(), + "from_profile" => UserView.render("show.json", %{user: repeater, for: user}), + "id" => notification.id, + "is_seen" => 0, + "notice" => + ActivityView.render("activity.json", %{activity: notification.activity, for: user}), + "ntype" => "repeat" + } + + assert represented == + NotificationView.render("notification.json", %{notification: notification, for: user}) + end + + test "A like notification" do + note_activity = insert(:note_activity) + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + liker = insert(:user) + + {:ok, activity} = TwitterAPI.fav(liker, note_activity.id) + [notification] = Notification.for_user(user) + + represented = %{ + "created_at" => notification.inserted_at |> Utils.format_naive_asctime(), + "from_profile" => UserView.render("show.json", %{user: liker, for: user}), + "id" => notification.id, + "is_seen" => 0, + "notice" => + ActivityView.render("activity.json", %{activity: notification.activity, for: user}), + "ntype" => "like" + } + + assert represented == + NotificationView.render("notification.json", %{notification: notification, for: user}) + end +end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 9e0a8a532..dd55c0b7e 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -15,7 +15,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do test "A user with an avatar object", %{user: user} do image = "image" - user = %{ user | avatar: %{ "url" => [%{"href" => image}] }} + user = %{user | avatar: %{"url" => [%{"href" => image}]}} represented = UserView.render("show.json", %{user: user}) assert represented["profile_image_url"] == image end @@ -41,7 +41,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), - "created_at" => user.inserted_at |> Utils.format_naive_asctime, + "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 1, "friends_count" => 1, @@ -76,7 +76,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), - "created_at" => user.inserted_at |> Utils.format_naive_asctime, + "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, "friends_count" => 0, @@ -112,7 +112,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => follower.name, "screen_name" => follower.nickname, "description" => HtmlSanitizeEx.strip_tags(follower.bio), - "created_at" => follower.inserted_at |> Utils.format_naive_asctime, + "created_at" => follower.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, "friends_count" => 1, @@ -155,7 +155,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "name" => user.name, "screen_name" => user.nickname, "description" => HtmlSanitizeEx.strip_tags(user.bio), - "created_at" => user.inserted_at |> Utils.format_naive_asctime, + "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "favourites_count" => 0, "statuses_count" => 0, "friends_count" => 0, diff --git a/test/web/views/error_view_test.exs b/test/web/views/error_view_test.exs index 48cdc5159..1d443b187 100644 --- a/test/web/views/error_view_test.exs +++ b/test/web/views/error_view_test.exs @@ -5,17 +5,16 @@ defmodule Pleroma.Web.ErrorViewTest do import Phoenix.View test "renders 404.json" do - assert render(Pleroma.Web.ErrorView, "404.json", []) == - %{errors: %{detail: "Page not found"}} + assert render(Pleroma.Web.ErrorView, "404.json", []) == %{errors: %{detail: "Page not found"}} end test "render 500.json" do assert render(Pleroma.Web.ErrorView, "500.json", []) == - %{errors: %{detail: "Internal server error"}} + %{errors: %{detail: "Internal server error"}} end test "render any other" do assert render(Pleroma.Web.ErrorView, "505.json", []) == - %{errors: %{detail: "Internal server error"}} + %{errors: %{detail: "Internal server error"}} end end diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs index ccca737f0..69216f393 100644 --- a/test/web/web_finger/web_finger_test.exs +++ b/test/web/web_finger/web_finger_test.exs @@ -7,7 +7,7 @@ defmodule Pleroma.Web.WebFingerTest do test "returns a link to the xml lrdd" do host_info = WebFinger.host_meta() - assert String.contains?(host_info, Pleroma.Web.base_url) + assert String.contains?(host_info, Pleroma.Web.base_url()) end end @@ -15,30 +15,54 @@ defmodule Pleroma.Web.WebFingerTest do test "works for fqns" do user = insert(:user) - {:ok, result} = WebFinger.webfinger("#{user.nickname}@#{Pleroma.Web.Endpoint.host}") + {:ok, result} = + WebFinger.webfinger("#{user.nickname}@#{Pleroma.Web.Endpoint.host()}", "XML") + assert is_binary(result) end test "works for ap_ids" do user = insert(:user) - {:ok, result} = WebFinger.webfinger(user.ap_id) + {:ok, result} = WebFinger.webfinger(user.ap_id, "XML") assert is_binary(result) end end describe "fingering" do - test "returns the info for a user" do + test "returns the info for an OStatus user" do user = "shp@social.heldscal.la" {:ok, data} = WebFinger.finger(user) - assert data["magic_key"] == "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB" + assert data["magic_key"] == + "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB" + assert data["topic"] == "https://social.heldscal.la/api/statuses/user_timeline/29191.atom" assert data["subject"] == "acct:shp@social.heldscal.la" assert data["salmon"] == "https://social.heldscal.la/main/salmon/user/29191" end + test "returns the ActivityPub actor URI for an ActivityPub user" do + user = "framasoft@framatube.org" + + {:ok, _data} = WebFinger.finger(user) + end + + test "returns the correctly for json ostatus users" do + user = "winterdienst@gnusocial.de" + + {:ok, data} = WebFinger.finger(user) + + assert data["magic_key"] == + "RSA.qfYaxztz7ZELrE4v5WpJrPM99SKI3iv9Y3Tw6nfLGk-4CRljNYqV8IYX2FXjeucC_DKhPNnlF6fXyASpcSmA_qupX9WC66eVhFhZ5OuyBOeLvJ1C4x7Hi7Di8MNBxY3VdQuQR0tTaS_YAZCwASKp7H6XEid3EJpGt0EQZoNzRd8=.AQAB" + + assert data["topic"] == "https://gnusocial.de/api/statuses/user_timeline/249296.atom" + assert data["subject"] == "acct:winterdienst@gnusocial.de" + assert data["salmon"] == "https://gnusocial.de/main/salmon/user/249296" + assert data["subscribe_address"] == "https://gnusocial.de/main/ostatussub?profile={uri}" + end + test "it works for friendica" do user = "lain@squeet.me" diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs index f6b86eca0..d861c241f 100644 --- a/test/web/websub/websub_controller_test.exs +++ b/test/web/websub/websub_controller_test.exs @@ -18,8 +18,9 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do "hub.lease_seconds": "100" } - conn = conn - |> post(path, data) + conn = + conn + |> post(path, data) assert response(conn, 202) == "Accepted" end @@ -34,14 +35,15 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do "hub.lease_seconds" => "100" } - conn = conn - |> get("/push/subscriptions/#{websub.id}", params) + conn = + conn + |> get("/push/subscriptions/#{websub.id}", params) websub = Repo.get(WebsubClientSubscription, websub.id) assert response(conn, 200) == "some challenge" assert websub.state == "accepted" - assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now), 100, 5 + assert_in_delta NaiveDateTime.diff(websub.valid_until, NaiveDateTime.utc_now()), 100, 5 end test "handles incoming feed updates", %{conn: conn} do @@ -49,10 +51,11 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do doc = "some stuff" signature = Websub.sign(websub.secret, doc) - conn = conn - |> put_req_header("x-hub-signature", "sha1=" <> signature) - |> put_req_header("content-type", "application/atom+xml") - |> post("/push/subscriptions/#{websub.id}", doc) + conn = + conn + |> put_req_header("x-hub-signature", "sha1=" <> signature) + |> put_req_header("content-type", "application/atom+xml") + |> post("/push/subscriptions/#{websub.id}", doc) assert response(conn, 200) == "OK" @@ -64,10 +67,11 @@ defmodule Pleroma.Web.Websub.WebsubControllerTest do doc = "some stuff" signature = Websub.sign("wrong secret", doc) - conn = conn - |> put_req_header("x-hub-signature", "sha1=" <> signature) - |> put_req_header("content-type", "application/atom+xml") - |> post("/push/subscriptions/#{websub.id}", doc) + conn = + conn + |> put_req_header("x-hub-signature", "sha1=" <> signature) + |> put_req_header("content-type", "application/atom+xml") + |> post("/push/subscriptions/#{websub.id}", doc) assert response(conn, 500) == "Error" diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs index 566ce7fa5..5914a37fc 100644 --- a/test/web/websub/websub_test.exs +++ b/test/web/websub/websub_test.exs @@ -15,7 +15,7 @@ defmodule Pleroma.Web.WebsubTest do sub = insert(:websub_subscription) topic = sub.topic - getter = fn (_path, _headers, options) -> + getter = fn _path, _headers, options -> %{ "hub.challenge": challenge, "hub.lease_seconds": seconds, @@ -25,10 +25,11 @@ defmodule Pleroma.Web.WebsubTest do assert String.to_integer(seconds) > 0 - {:ok, %HTTPoison.Response{ - status_code: 200, - body: challenge - }} + {:ok, + %HTTPoison.Response{ + status_code: 200, + body: challenge + }} end {:ok, sub} = Websub.verify(sub, getter) @@ -38,11 +39,12 @@ defmodule Pleroma.Web.WebsubTest do test "a verification of a request that doesn't return 200" do sub = insert(:websub_subscription) - getter = fn (_path, _headers, _options) -> - {:ok, %HTTPoison.Response{ - status_code: 500, - body: "" - }} + getter = fn _path, _headers, _options -> + {:ok, + %HTTPoison.Response{ + status_code: 500, + body: "" + }} end {:error, sub} = Websub.verify(sub, getter) @@ -61,7 +63,7 @@ defmodule Pleroma.Web.WebsubTest do "hub.lease_seconds" => "100" } - {:ok, subscription } = Websub.incoming_subscription_request(user, data) + {:ok, subscription} = Websub.incoming_subscription_request(user, data) assert subscription.topic == Pleroma.Web.OStatus.feed_path(user) assert subscription.state == "requested" assert subscription.secret == "a random secret" @@ -70,7 +72,9 @@ defmodule Pleroma.Web.WebsubTest do test "an incoming subscription request for an existing subscription" do user = insert(:user) - sub = insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user)) + + sub = + insert(:websub_subscription, state: "accepted", topic: Pleroma.Web.OStatus.feed_path(user)) data = %{ "hub.callback" => sub.callback, @@ -80,7 +84,7 @@ defmodule Pleroma.Web.WebsubTest do "hub.lease_seconds" => "100" } - {:ok, subscription } = Websub.incoming_subscription_request(user, data) + {:ok, subscription} = Websub.incoming_subscription_request(user, data) assert subscription.topic == Pleroma.Web.OStatus.feed_path(user) assert subscription.state == sub.state assert subscription.secret == "a random secret" @@ -90,12 +94,12 @@ defmodule Pleroma.Web.WebsubTest do end def accepting_verifier(subscription) do - {:ok, %{ subscription | state: "accepted" }} + {:ok, %{subscription | state: "accepted"}} end test "initiate a subscription for a given user and topic" do subscriber = insert(:user) - user = insert(:user, %{info: %{ "topic" => "some_topic", "hub" => "some_hub"}}) + user = insert(:user, %{info: %{"topic" => "some_topic", "hub" => "some_hub"}}) {:ok, websub} = Websub.subscribe(subscriber, user, &accepting_verifier/1) assert websub.subscribers == [subscriber.ap_id] @@ -109,12 +113,13 @@ defmodule Pleroma.Web.WebsubTest do test "discovers the hub and canonical url" do topic = "https://mastodon.social/users/lambadalambda.atom" - getter = fn(^topic) -> + getter = fn ^topic -> doc = File.read!("test/fixtures/lambadalambda.atom") {:ok, %{status_code: 200, body: doc}} end {:ok, discovered} = Websub.gather_feed_data(topic, getter) + expected = %{ "hub" => "https://mastodon.social/api/push", "uri" => "https://mastodon.social/users/lambadalambda", @@ -122,7 +127,17 @@ defmodule Pleroma.Web.WebsubTest do "name" => "Critical Value", "host" => "mastodon.social", "bio" => "a cool dude.", - "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", "mediaType" => "image/gif", "type" => "Link"}]} + "avatar" => %{ + "type" => "Image", + "url" => [ + %{ + "href" => + "https://files.mastodon.social/accounts/avatars/000/000/264/original/1429214160519.gif?1492379244", + "mediaType" => "image/gif", + "type" => "Link" + } + ] + } } assert expected == discovered @@ -133,9 +148,16 @@ defmodule Pleroma.Web.WebsubTest do topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom" websub = insert(:websub_client_subscription, %{hub: hub, topic: topic}) - poster = fn (^hub, {:form, data}, _headers) -> + poster = fn ^hub, {:form, data}, _headers -> assert Keyword.get(data, :"hub.mode") == "subscribe" - assert Keyword.get(data, :"hub.callback") == Helpers.websub_url(Pleroma.Web.Endpoint, :websub_subscription_confirmation, websub.id) + + assert Keyword.get(data, :"hub.callback") == + Helpers.websub_url( + Pleroma.Web.Endpoint, + :websub_subscription_confirmation, + websub.id + ) + {:ok, %{status_code: 202}} end @@ -154,7 +176,7 @@ defmodule Pleroma.Web.WebsubTest do topic = "https://social.heldscal.la/api/statuses/user_timeline/23211.atom" websub = insert(:websub_client_subscription, %{hub: hub, topic: topic}) - poster = fn (^hub, {:form, _data}, _headers) -> + poster = fn ^hub, {:form, _data}, _headers -> {:ok, %{status_code: 202}} end @@ -162,7 +184,8 @@ defmodule Pleroma.Web.WebsubTest do assert websub.state == "rejected" websub = insert(:websub_client_subscription, %{hub: hub, topic: topic}) - poster = fn (^hub, {:form, _data}, _headers) -> + + poster = fn ^hub, {:form, _data}, _headers -> {:ok, %{status_code: 400}} end @@ -172,7 +195,7 @@ defmodule Pleroma.Web.WebsubTest do test "sign a text" do signed = Websub.sign("secret", "text") - assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase + assert signed == "B8392C23690CCF871F37EC270BE1582DEC57A503" |> String.downcase() _signed = Websub.sign("secret", [["て"], ['す']]) end @@ -180,9 +203,21 @@ defmodule Pleroma.Web.WebsubTest do describe "renewing subscriptions" do test "it renews subscriptions that have less than a day of time left" do day = 60 * 60 * 24 - now = NaiveDateTime.utc_now - still_good = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, 2 * day), topic: "http://example.org/still_good", state: "accepted"}) - needs_refresh = insert(:websub_client_subscription, %{valid_until: NaiveDateTime.add(now, day - 100), topic: "http://example.org/needs_refresh", state: "accepted"}) + now = NaiveDateTime.utc_now() + + still_good = + insert(:websub_client_subscription, %{ + valid_until: NaiveDateTime.add(now, 2 * day), + topic: "http://example.org/still_good", + state: "accepted" + }) + + needs_refresh = + insert(:websub_client_subscription, %{ + valid_until: NaiveDateTime.add(now, day - 100), + topic: "http://example.org/needs_refresh", + state: "accepted" + }) _refresh = Websub.refresh_subscriptions() |