diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/support/factory.ex | 10 | ||||
-rw-r--r-- | test/web/ostatus/activity_representer_test.exs | 38 | ||||
-rw-r--r-- | test/web/ostatus/feed_representer_test.exs | 44 | ||||
-rw-r--r-- | test/web/ostatus/ostatus_controller_test.exs | 15 | ||||
-rw-r--r-- | test/web/ostatus/user_representer_test.exs | 28 | ||||
-rw-r--r-- | test/web/web_finger/web_finger_test.exs | 11 | ||||
-rw-r--r-- | test/web/websub/websub_controller_test.exs | 30 | ||||
-rw-r--r-- | test/web/websub/websub_test.exs | 44 | ||||
-rw-r--r-- | test/xml_builder_test.exs | 59 |
9 files changed, 279 insertions, 0 deletions
diff --git a/test/support/factory.ex b/test/support/factory.ex index 3fc9cf710..401fdfda3 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -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..de79717b1 --- /dev/null +++ b/test/web/ostatus/activity_representer_test.exs @@ -0,0 +1,38 @@ +defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do + use Pleroma.DataCase + + alias Pleroma.Web.OStatus.ActivityRepresenter + alias Pleroma.User + + 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 + + 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..3d8eaac6e --- /dev/null +++ b/test/web/ostatus/feed_representer_test.exs @@ -0,0 +1,44 @@ +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) |> IO.iodata_to_binary + 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/"> + <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)}" /> + <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..a401a56da --- /dev/null +++ b/test/web/ostatus/user_representer_test.exs @@ -0,0 +1,28 @@ +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) + tuple = UserRepresenter.to_simple_form(user) + + res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary + + expected = """ + <id>#{user.ap_id}</id> + <activity:object>http://activitystrea.ms/schema/1.0/person</activity:object> + <uri>#{user.ap_id}</uri> + <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..4eff598d6 --- /dev/null +++ b/test/web/websub/websub_controller_test.exs @@ -0,0 +1,30 @@ +defmodule Pleroma.Web.Websub.WebsubControllerTest do + use Pleroma.Web.ConnCase + import Pleroma.Factory + alias Pleroma.Repo + alias Pleroma.Web.Websub.WebsubServerSubscription + + 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": "subscription", + "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" + subscription = Repo.one!(WebsubServerSubscription) + assert subscription.topic == Pleroma.Web.OStatus.feed_path(user) + assert subscription.state == "requested" + assert subscription.secret == "a random secret" + assert subscription.valid_until == NaiveDateTime.add(subscription.inserted_at, 100) + end +end diff --git a/test/web/websub/websub_test.exs b/test/web/websub/websub_test.exs new file mode 100644 index 000000000..93a44fe46 --- /dev/null +++ b/test/web/websub/websub_test.exs @@ -0,0 +1,44 @@ +defmodule Pleroma.Web.WebsubTest do + use Pleroma.DataCase + alias Pleroma.Web.Websub + 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 is_number(seconds) + + {: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) + topic = sub.topic + + getter = fn (_path, _headers, _options) -> + {:ok, %HTTPoison.Response{ + status_code: 500, + body: "" + }} + end + + {:error, sub} = Websub.verify(sub, getter) + assert sub.state == "rejected" + 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 |