summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/formatter_test.exs41
-rw-r--r--test/plugs/http_signature_plug_test.exs44
-rw-r--r--test/web/activity_pub/activity_pub_test.exs27
-rw-r--r--test/web/activity_pub/transmogrifier_test.exs10
-rw-r--r--test/web/mastodon_api/mastodon_api_controller_test.exs18
-rw-r--r--test/web/twitter_api/twitter_api_controller_test.exs52
-rw-r--r--test/web/twitter_api/views/activity_view_test.exs33
-rw-r--r--test/web/twitter_api/views/notification_view_test.exs108
8 files changed, 308 insertions, 25 deletions
diff --git a/test/formatter_test.exs b/test/formatter_test.exs
index b7246252f..2cf1f3f8e 100644
--- a/test/formatter_test.exs
+++ b/test/formatter_test.exs
@@ -23,14 +23,21 @@ defmodule Pleroma.FormatterTest do
text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla."
expected =
- "Hey, check out <a href='https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla'>https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a>."
+ "Hey, check out <a href=\"https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&amp;y=2#blabla\">https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&amp;y=2#blabla</a>."
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
text = "https://mastodon.social/@lambadalambda"
expected =
- "<a href='https://mastodon.social/@lambadalambda'>https://mastodon.social/@lambadalambda</a>"
+ "<a href=\"https://mastodon.social/@lambadalambda\">https://mastodon.social/@lambadalambda</a>"
+
+ assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
+
+ text = "https://mastodon.social:4000/@lambadalambda"
+
+ expected =
+ "<a href=\"https://mastodon.social:4000/@lambadalambda\">https://mastodon.social:4000/@lambadalambda</a>"
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
@@ -40,21 +47,35 @@ defmodule Pleroma.FormatterTest do
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
text = "http://www.cs.vu.nl/~ast/intel/"
- expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>"
+ expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">http://www.cs.vu.nl/~ast/intel/</a>"
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
expected =
- "<a href='https://forum.zdoom.org/viewtopic.php?f=44&t=57087'>https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
+ "<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&amp;t=57087\">https://forum.zdoom.org/viewtopic.php?f=44&amp;t=57087</a>"
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
expected =
- "<a href='https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul'>https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
+ "<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
+
+ assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
+
+ text = "https://www.google.co.jp/search?q=Nasim+Aghdam"
+
+ expected =
+ "<a href=\"https://www.google.co.jp/search?q=Nasim+Aghdam\">https://www.google.co.jp/search?q=Nasim+Aghdam</a>"
+
+ assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
+
+ text = "https://en.wikipedia.org/wiki/Duff's_device"
+
+ expected =
+ "<a href=\"https://en.wikipedia.org/wiki/Duff&#39;s_device\">https://en.wikipedia.org/wiki/Duff&#39;s_device</a>"
assert Formatter.add_links({[], text}) |> Formatter.finalize() == expected
end
@@ -64,7 +85,13 @@ defmodule Pleroma.FormatterTest do
test "gives a replacement for user links" 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 =
+ insert(:user, %{
+ nickname: "archaeme",
+ info: %{"source_data" => %{"url" => "https://archeme/@archaeme"}}
+ })
+
archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
mentions = Pleroma.Formatter.parse_mentions(text)
@@ -76,7 +103,7 @@ defmodule Pleroma.FormatterTest do
expected_text =
"<span><a href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a href='#{
- archaeme.ap_id
+ "https://archeme/@archaeme"
}'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a href='#{
archaeme_remote.ap_id
}'>@<span>archaeme</span></a></span>"
diff --git a/test/plugs/http_signature_plug_test.exs b/test/plugs/http_signature_plug_test.exs
new file mode 100644
index 000000000..a15c5b470
--- /dev/null
+++ b/test/plugs/http_signature_plug_test.exs
@@ -0,0 +1,44 @@
+defmodule Pleroma.Web.Plugs.HTTPSignaturePlugTest do
+ use Pleroma.Web.ConnCase
+ alias Pleroma.Web.HTTPSignatures
+ alias Pleroma.Web.Plugs.HTTPSignaturePlug
+
+ import Plug.Conn
+ import Mock
+
+ test "it call HTTPSignatures to check validity if the actor sighed it" do
+ params = %{"actor" => "http://mastodon.example.org/users/admin"}
+ conn = build_conn(:get, "/doesntmattter", params)
+
+ with_mock HTTPSignatures, validate_conn: fn _ -> true end do
+ conn =
+ conn
+ |> put_req_header(
+ "signature",
+ "keyId=\"http://mastodon.example.org/users/admin#main-key"
+ )
+ |> HTTPSignaturePlug.call(%{})
+
+ assert conn.assigns.valid_signature == true
+ assert called(HTTPSignatures.validate_conn(:_))
+ end
+ end
+
+ test "bails out early if the signature isn't by the activity actor" do
+ params = %{"actor" => "https://mst3k.interlinked.me/users/luciferMysticus"}
+ conn = build_conn(:get, "/doesntmattter", params)
+
+ with_mock HTTPSignatures, validate_conn: fn _ -> false end do
+ conn =
+ conn
+ |> put_req_header(
+ "signature",
+ "keyId=\"http://mastodon.example.org/users/admin#main-key"
+ )
+ |> HTTPSignaturePlug.call(%{})
+
+ assert conn.assigns.valid_signature == false
+ refute called(HTTPSignatures.validate_conn(:_))
+ end
+ end
+end
diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs
index cf25abfc1..6887e8871 100644
--- a/test/web/activity_pub/activity_pub_test.exs
+++ b/test/web/activity_pub/activity_pub_test.exs
@@ -40,12 +40,31 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
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
@@ -64,16 +83,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
describe "create activities" do
test "removes doubled 'to' recipients" do
+ user = insert(:user)
+
{:ok, activity} =
ActivityPub.create(%{
to: ["user1", "user1", "user2"],
- actor: %User{ap_id: "1"},
+ 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
diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs
index 060ebe9f1..eb093262f 100644
--- a/test/web/activity_pub/transmogrifier_test.exs
+++ b/test/web/activity_pub/transmogrifier_test.exs
@@ -89,6 +89,10 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
"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
@@ -102,7 +106,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
user = insert(:user)
data =
- File.read!("test/fixtures/mastodon-follow-activity.json") |> Poison.decode!()
+ 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)
@@ -118,7 +123,8 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
{:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
data =
- File.read!("test/fixtures/mastodon-like.json") |> Poison.decode!()
+ 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)
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
index 2c9cdd194..5293b9364 100644
--- a/test/web/mastodon_api/mastodon_api_controller_test.exs
+++ b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -316,6 +316,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
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"])
@@ -564,6 +575,13 @@ 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 =
diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs
index 5abdc2e0e..406dace1c 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
@@ -160,15 +161,13 @@ 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, 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")
+ |> get("/api/statusnet/conversation/#{activity.data["context_id"]}.json")
response = json_response(conn, 200)
@@ -249,6 +248,35 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
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]
@@ -600,8 +628,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> 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
@@ -620,12 +649,9 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|> 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
diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs
index cb8f60fcf..7f2017d3c 100644
--- a/test/web/twitter_api/views/activity_view_test.exs
+++ b/test/web/twitter_api/views/activity_view_test.exs
@@ -10,7 +10,9 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
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)
@@ -51,6 +53,37 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
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"})
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