+
+ 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 = "<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
+ end
+
+ test "it favorites a status, returns the updated status" do
+ user = insert(:user)
+ note_activity = insert(:note_activity)
+ activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
+
+ {:ok, status} = TwitterAPI.favorite(user, note_activity)
+ updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+ assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
+ end
+
+ test "it unfavorites a status, returns the updated status" 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)
+ 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)
+
+ assert status["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.retweet(user, note_activity)
+ updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+ assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
+ end
+
+ test "it registers a new user and returns the user." do
+ data = %{
+ "nickname" => "lain",
+ "email" => "lain@wired.jp",
+ "fullname" => "lain iwakura",
+ "bio" => "close the world.",
+ "password" => "bear",
+ "confirm" => "bear"
+ }
+
+ {:ok, user} = TwitterAPI.register_user(data)
+
+ fetched_user = Repo.get_by(User, nickname: "lain")
+ assert user == UserRepresenter.to_map(fetched_user)
+ end
+
+ test "it returns the error on registration problems" do
+ data = %{
+ "nickname" => "lain",
+ "email" => "lain@wired.jp",
+ "fullname" => "lain iwakura",
+ "bio" => "close the world.",
+ "password" => "bear"
+ }
+
+ {:error, error_object} = TwitterAPI.register_user(data)
+
+ assert is_binary(error_object[:error])
+ refute Repo.get_by(User, nickname: "lain")
+ end
+
+ 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})
+
+ assert is_number(status["statusnet_conversation_id"])
+ end
+
+ setup do
+ Supervisor.terminate_child(Pleroma.Supervisor, Cachex)
+ Supervisor.restart_child(Pleroma.Supervisor, Cachex)
+ :ok
+ end
+
+ describe "context_to_conversation_id" do
+ test "creates a mapping object" do
+ conversation_id = TwitterAPI.context_to_conversation_id("random context")
+ object = Object.get_by_ap_id("random context")
+
+ assert conversation_id == object.id
+ end
+
+ test "returns an existing mapping for an existing object" do
+ {:ok, object} = Object.context_mapping("random context") |> Repo.insert
+ conversation_id = TwitterAPI.context_to_conversation_id("random context")
+
+ assert conversation_id == object.id
+ end
+ end
+
+ 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 == UserRepresenter.to_map(remote, %{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