defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
use Pleroma.DataCase
alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
- alias Pleroma.Web.TwitterAPI.TwitterAPI
- alias Pleroma.Web.TwitterAPI.Utils
+ alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView}
+ alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.{Activity, User, Object, Repo}
- alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
+ alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
alias Pleroma.Web.ActivityPub.ActivityPub
import Pleroma.Factory
test "create a status" do
- user = UserBuilder.build(%{ap_id: "142344"})
+ user = insert(:user)
_mentioned_user = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
object_data = %{
{ :ok, activity = %Activity{} } = TwitterAPI.create_status(user, input)
- assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>\nThis is on another line. #2hu #epic #phantasmagoric<br>\n<a href='http://example.org/image.jpg' class='attachment'>image.jpg</a>"
+ assert get_in(activity.data, ["object", "content"]) == "Hello again, <a href='shp'>@shp</a>.<br>\nThis is on another line. #2hu #epic #phantasmagoric<br>\n<a href=\"http://example.org/image.jpg\" class='attachment'>image.jpg</a>"
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 is_list(activity.data["object"]["attachment"])
assert activity.data["object"] == Object.get_by_ap_id(activity.data["object"]["id"]).data
+
+ user = User.get_by_ap_id(user.ap_id)
+
+ assert user.info["note_count"] == 1
end
test "create a status that is a reply" do
- user = UserBuilder.build(%{ap_id: "some_cool_id"})
+ user = insert(:user)
input = %{
"status" => "Hello again."
}
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
- assert Enum.member?(get_in(reply.data, ["to"]), "some_cool_id")
+ assert Enum.member?(get_in(reply.data, ["to"]), user.ap_id)
end
test "fetch public statuses, excluding remote ones." do
{:ok, user, followed, _activity } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert user.following == [User.ap_followers(followed)]
+ followed = User.get_by_ap_id(followed.ap_id)
+ assert followed.info["follower_count"] == 1
+
{ :error, msg } = TwitterAPI.follow(user, %{"screen_name" => followed.nickname})
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
end
assert msg == "Not subscribed!"
end
+ test "Block another user using user_id" do
+ user = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, user, blocked} = TwitterAPI.block(user, %{"user_id" => blocked.id})
+ assert User.blocks?(user, blocked)
+ end
+
+ test "Block another user using screen_name" do
+ user = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, user, blocked} = TwitterAPI.block(user, %{"screen_name" => blocked.nickname})
+ assert User.blocks?(user, blocked)
+ end
+
+ test "Unblock another user using user_id" do
+ unblocked = insert(:user)
+ user = insert(:user)
+ User.block(user, unblocked)
+
+ {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"user_id" => unblocked.id})
+ assert user.info["blocks"] == []
+ end
+
+ test "Unblock another user using screen_name" do
+ unblocked = insert(:user)
+ user = insert(:user)
+ User.block(user, unblocked)
+
+ {:ok, user, unblocked} = TwitterAPI.unblock(user, %{"screen_name" => unblocked.nickname})
+ assert user.info["blocks"] == []
+ end
+
test "fetch statuses in a context using the conversation id" do
{:ok, user} = UserBuilder.insert()
- {:ok, activity} = ActivityBuilder.insert(%{"context" => "2hu"})
- {:ok, activity_two} = ActivityBuilder.insert(%{"context" => "2hu"})
- {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
+ {: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, 0)["id"] == activity.id
- assert Enum.at(statuses, 1)["id"] == activity_two.id
+ assert Enum.at(statuses, 1)["id"] == activity.id
+ assert Enum.at(statuses, 0)["id"] == activity_two.id
end
test "upload a file" do
assert is_binary(response)
end
- test "it can parse mentions and return the relevant users" 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"})
-
- expected_result = [
- {"@gsimg", gsimg},
- {"@archaeme", archaeme},
- {"@archaeme@archae.me", archaeme_remote},
- ]
-
- assert TwitterAPI.parse_mentions(text) == expected_result
- end
-
test "it adds user links to an existing text" do
text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
archaeme = insert(:user, %{nickname: "archaeme"})
archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
- mentions = TwitterAPI.parse_mentions(text)
+ mentions = Pleroma.Formatter.parse_mentions(text)
expected_text = "<a href='#{gsimg.ap_id}'>@gsimg</a> According to <a href='#{archaeme.ap_id}'>@archaeme</a>, that is @daggsy. Also hello <a href='#{archaeme_remote.ap_id}'>@archaeme</a>"
assert Utils.add_user_links(text, mentions) == expected_text
note_activity = insert(:note_activity)
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
- {:ok, status} = TwitterAPI.favorite(user, note_activity)
+ {: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})
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.unfavorite(user, note_activity)
+ {:ok, status} = TwitterAPI.unfav(user, note_activity.id)
assert status["fave_num"] == 0
end
note_activity = insert(:note_activity)
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
- {:ok, status} = TwitterAPI.retweet(user, note_activity)
+ {: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})
{:ok, user} = TwitterAPI.register_user(data)
fetched_user = Repo.get_by(User, nickname: "lain")
- assert user == UserRepresenter.to_map(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
describe "fetching a user by uri" do
test "fetches a user by uri" do
+ id = "https://mastodon.social/users/lambadalambda"
user = insert(:user)
+ {:ok, represented} = TwitterAPI.get_external_profile(user, id)
+ remote = User.get_by_ap_id(id)
+
+ assert represented["id"] == UserView.render("show.json", %{user: remote, for: user})["id"]
- {:ok, represented} = TwitterAPI.get_external_profile(user, user.ap_id)
- assert represented = UserRepresenter.to_map(user, %{for: user})
+ # Also fetches the feed.
+ assert Activity.get_create_activity_by_object_ap_id("tag:mastodon.social,2017-04-05:objectId=1641750:objectType=Status")
end
end
end