diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/support/factory.ex | 12 | ||||
-rw-r--r-- | test/web/ostatus/activity_representer_test.exs | 43 | ||||
-rw-r--r-- | test/web/ostatus/feed_representer_test.exs | 45 | ||||
-rw-r--r-- | test/web/ostatus/ostatus_controller_test.exs | 15 | ||||
-rw-r--r-- | test/web/ostatus/user_representer_test.exs | 31 | ||||
-rw-r--r-- | test/web/web_finger/web_finger_test.exs | 11 | ||||
-rw-r--r-- | test/web/websub/websub_controller_test.exs | 23 | ||||
-rw-r--r-- | test/web/websub/websub_test.exs | 90 | ||||
-rw-r--r-- | test/xml_builder_test.exs | 59 |
9 files changed, 328 insertions, 1 deletions
diff --git a/test/support/factory.ex b/test/support/factory.ex index 3fc9cf710..d7c16f0e0 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -3,7 +3,7 @@ defmodule Pleroma.Factory do def user_factory do user = %Pleroma.User{ - name: sequence(:name, &"Test User #{&1}"), + name: sequence(:name, &"Test テスト User #{&1}"), email: sequence(:email, &"user#{&1}@example.com"), nickname: sequence(:nickname, &"nick#{&1}"), password_hash: Comeonin.Pbkdf2.hashpwsalt("test"), @@ -64,4 +64,14 @@ defmodule Pleroma.Factory do data: data } end + + def websub_subscription_factory do + %Pleroma.Web.Websub.WebsubServerSubscription{ + topic: "http://example.org", + callback: "http://example/org/callback", + secret: "here's a secret", + valid_until: NaiveDateTime.add(NaiveDateTime.utc_now, 100), + state: "requested" + } + end end diff --git a/test/web/ostatus/activity_representer_test.exs b/test/web/ostatus/activity_representer_test.exs new file mode 100644 index 000000000..61df41a1d --- /dev/null +++ b/test/web/ostatus/activity_representer_test.exs @@ -0,0 +1,43 @@ +defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do + use Pleroma.DataCase + + alias Pleroma.Web.OStatus.ActivityRepresenter + alias Pleroma.{User, Activity} + + import Pleroma.Factory + + test "a note activity" do + note_activity = insert(:note_activity) + updated_at = note_activity.updated_at + |> NaiveDateTime.to_iso8601 + inserted_at = note_activity.inserted_at + |> NaiveDateTime.to_iso8601 + + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + + expected = """ + <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type> + <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb> + <id>#{note_activity.data["object"]["id"]}</id> + <title>New note by #{user.nickname}</title> + <content type="html">#{note_activity.data["object"]["content"]}</content> + <published>#{inserted_at}</published> + <updated>#{updated_at}</updated> + """ + + tuple = ActivityRepresenter.to_simple_form(note_activity, user) + + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + + assert clean(res) == clean(expected) + end + + test "an unknown activity" do + tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil) + assert is_nil(tuple) + end + + defp clean(string) do + String.replace(string, ~r/\s/, "") + end +end diff --git a/test/web/ostatus/feed_representer_test.exs b/test/web/ostatus/feed_representer_test.exs new file mode 100644 index 000000000..9a02d8c16 --- /dev/null +++ b/test/web/ostatus/feed_representer_test.exs @@ -0,0 +1,45 @@ +defmodule Pleroma.Web.OStatus.FeedRepresenterTest do + use Pleroma.DataCase + import Pleroma.Factory + alias Pleroma.User + alias Pleroma.Web.OStatus.{FeedRepresenter, UserRepresenter, ActivityRepresenter} + alias Pleroma.Web.OStatus + + test "returns a feed of the last 20 items of the user" do + note_activity = insert(:note_activity) + user = User.get_cached_by_ap_id(note_activity.data["actor"]) + + tuple = FeedRepresenter.to_simple_form(user, [note_activity], [user]) + + 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) + + expected = """ + <feed xmlns="http://www.w3.org/2005/Atom" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:poco="http://portablecontacts.net/spec/1.0"> + <id>#{OStatus.feed_path(user)}</id> + <title>#{user.nickname}'s timeline</title> + <updated>#{most_recent_update}</updated> + <link rel="hub" href="#{OStatus.pubsub_path(user)}" /> + <link rel="self" href="#{OStatus.feed_path(user)}" /> + <author> + #{user_xml} + </author> + <entry> + #{entry_xml} + </entry> + </feed> + """ + assert clean(res) == clean(expected) + end + + defp clean(string) do + String.replace(string, ~r/\s/, "") + end +end diff --git a/test/web/ostatus/ostatus_controller_test.exs b/test/web/ostatus/ostatus_controller_test.exs new file mode 100644 index 000000000..229cd9b1e --- /dev/null +++ b/test/web/ostatus/ostatus_controller_test.exs @@ -0,0 +1,15 @@ +defmodule Pleroma.Web.OStatus.OStatusControllerTest do + use Pleroma.Web.ConnCase + import Pleroma.Factory + alias Pleroma.User + + test "gets a feed", %{conn: conn} 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") + + assert response(conn, 200) + end +end diff --git a/test/web/ostatus/user_representer_test.exs b/test/web/ostatus/user_representer_test.exs new file mode 100644 index 000000000..a4afc2cf7 --- /dev/null +++ b/test/web/ostatus/user_representer_test.exs @@ -0,0 +1,31 @@ +defmodule Pleroma.Web.OStatus.UserRepresenterTest do + use Pleroma.DataCase + alias Pleroma.Web.OStatus.UserRepresenter + + import Pleroma.Factory + alias Pleroma.User + + test "returns a user with id, uri, name and link" do + user = build(:user, nickname: "レイン") + tuple = UserRepresenter.to_simple_form(user) + + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> to_string + + expected = """ + <id>#{user.ap_id}</id> + <activity:object>http://activitystrea.ms/schema/1.0/person</activity:object> + <uri>#{user.ap_id}</uri> + <poco:preferredUsername>#{user.nickname}</poco:preferredUsername> + <poco:displayName>#{user.name}</poco:displayName> + <poco:note>#{user.bio}</poco:note> + <name>#{user.nickname}</name> + <link rel="avatar" href="#{User.avatar_url(user)}" /> + """ + + assert clean(res) == clean(expected) + end + + defp clean(string) do + String.replace(string, ~r/\s/, "") + end +end diff --git a/test/web/web_finger/web_finger_test.exs b/test/web/web_finger/web_finger_test.exs new file mode 100644 index 000000000..8a3007ff9 --- /dev/null +++ b/test/web/web_finger/web_finger_test.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Web.WebFingerTest do + use Pleroma.DataCase + + describe "host meta" do + test "returns a link to the xml lrdd" do + host_info = Pleroma.Web.WebFinger.host_meta + + assert String.contains?(host_info, Pleroma.Web.base_url) + end + end +end diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs new file mode 100644 index 000000000..8368cafea --- /dev/null +++ b/test/web/websub/websub_controller_test.exs @@ -0,0 +1,23 @@ +defmodule Pleroma.Web.Websub.WebsubControllerTest do + use Pleroma.Web.ConnCase + import Pleroma.Factory + + test "websub subscription request", %{conn: conn} do + user = insert(:user) + + path = Pleroma.Web.OStatus.pubsub_path(user) + + data = %{ + "hub.callback": "http://example.org/sub", + "hub.mode": "subscribe", + "hub.topic": Pleroma.Web.OStatus.feed_path(user), + "hub.secret": "a random secret", + "hub.lease_seconds": "100" + } + + conn = conn + |> post(path, data) + + assert response(conn, 202) == "Accepted" + end +end diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs new file mode 100644 index 000000000..334ba03fc --- /dev/null +++ b/test/web/websub/websub_test.exs @@ -0,0 +1,90 @@ +defmodule Pleroma.Web.WebsubMock do + def verify(sub) do + {:ok, sub} + end +end +defmodule Pleroma.Web.WebsubTest do + use Pleroma.DataCase + alias Pleroma.Web.Websub + alias Pleroma.Web.Websub.WebsubServerSubscription + import Pleroma.Factory + + test "a verification of a request that is accepted" do + sub = insert(:websub_subscription) + topic = sub.topic + + getter = fn (_path, _headers, options) -> + %{ + "hub.challenge": challenge, + "hub.lease_seconds": seconds, + "hub.topic": ^topic, + "hub.mode": "subscribe" + } = Keyword.get(options, :params) + + assert String.to_integer(seconds) > 0 + + {:ok, %HTTPoison.Response{ + status_code: 200, + body: challenge + }} + end + + {:ok, sub} = Websub.verify(sub, getter) + assert sub.state == "active" + end + + 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: "" + }} + end + + {:error, sub} = Websub.verify(sub, getter) + assert sub.state == "rejected" + end + + test "an incoming subscription request" do + user = insert(:user) + + data = %{ + "hub.callback" => "http://example.org/sub", + "hub.mode" => "subscribe", + "hub.topic" => Pleroma.Web.OStatus.feed_path(user), + "hub.secret" => "a random secret", + "hub.lease_seconds" => "100" + } + + + {: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" + assert subscription.callback == "http://example.org/sub" + end + + 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)) + + data = %{ + "hub.callback" => sub.callback, + "hub.mode" => "subscribe", + "hub.topic" => Pleroma.Web.OStatus.feed_path(user), + "hub.secret" => "a random secret", + "hub.lease_seconds" => "100" + } + + + {: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" + assert subscription.callback == sub.callback + assert length(Repo.all(WebsubServerSubscription)) == 1 + assert subscription.id == sub.id + end +end diff --git a/test/xml_builder_test.exs b/test/xml_builder_test.exs new file mode 100644 index 000000000..f502a0f0e --- /dev/null +++ b/test/xml_builder_test.exs @@ -0,0 +1,59 @@ +defmodule Pleroma.XmlBuilderTest do + use Pleroma.DataCase + alias Pleroma.XmlBuilder + + test "Build a basic xml string from a tuple" do + data = { :feed, %{ xmlns: "http://www.w3.org/2005/Atom"}, "Some content" } + + expected_xml = "<feed xmlns=\"http://www.w3.org/2005/Atom\">Some content</feed>" + + assert XmlBuilder.to_xml(data) == expected_xml + end + + test "returns a complete document" do + data = { :feed, %{ xmlns: "http://www.w3.org/2005/Atom"}, "Some content" } + + expected_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><feed xmlns=\"http://www.w3.org/2005/Atom\">Some content</feed>" + + assert XmlBuilder.to_doc(data) == expected_xml + end + + test "Works without attributes" do + data = { + :feed, + "Some content" + } + + expected_xml = "<feed>Some content</feed>" + + assert XmlBuilder.to_xml(data) == expected_xml + end + + test "It works with nested tuples" do + data = { + :feed, + [ + {:guy, "brush"}, + {:lament, %{ configuration: "puzzle" }, "pinhead" } + ] + } + + expected_xml = ~s[<feed><guy>brush</guy><lament configuration="puzzle">pinhead</lament></feed>] + + assert XmlBuilder.to_xml(data) == expected_xml + end + + test "Represents NaiveDateTime as iso8601" do + assert XmlBuilder.to_xml(~N[2000-01-01 13:13:33]) == "2000-01-01T13:13:33" + end + + test "Uses self-closing tags when no content is giving" do + data = { + :link, + %{ rel: "self" } + } + + expected_xml = ~s[<link rel="self" />] + assert XmlBuilder.to_xml(data) == expected_xml + end +end |