Rename MobilizonWeb to Mobilizon.Web

This commit is contained in:
rustra
2020-01-26 21:36:50 +01:00
parent b3f8d52bc9
commit 8856cc2f55
143 changed files with 490 additions and 490 deletions

View File

@@ -0,0 +1,359 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/web_finger/web_finger_controller_test.exs
defmodule Mobilizon.Web.ActivityPubControllerTest do
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
use Mobilizon.Web.ConnCase
import Mobilizon.Factory
alias Mobilizon.{Actors, Config}
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Web.ActivityPub.ActorView
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.PageView
alias Mobilizon.Web.Router.Helpers, as: Routes
setup_all do
Mobilizon.Config.put([:instance, :federating], true)
:ok
end
setup do
conn = build_conn() |> put_req_header("accept", "application/activity+json")
{:ok, conn: conn}
end
describe "/@:preferred_username" do
test "it returns a json representation of the actor", %{conn: conn} do
actor = insert(:actor)
conn =
conn
|> get(Actor.build_url(actor.preferred_username, :page))
actor = Actors.get_actor!(actor.id)
assert json_response(conn, 200) ==
ActorView.render("actor.json", %{actor: actor})
|> Jason.encode!()
|> Jason.decode!()
end
end
describe "/events/:uuid" do
test "it returns a json representation of the event", %{conn: conn} do
event = insert(:event)
conn =
conn
|> get(Routes.page_url(Endpoint, :event, event.uuid))
assert json_response(conn, 200) ==
PageView.render("event.activity-json", %{conn: %{assigns: %{object: event}}})
end
test "it returns 404 for non-public events", %{conn: conn} do
event = insert(:event, visibility: :private)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :event, event.uuid))
assert json_response(conn, 404)
end
end
describe "/comments/:uuid" do
test "it returns a json representation of the comment", %{conn: conn} do
comment = insert(:comment)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :comment, comment.uuid))
assert json_response(conn, 200) ==
PageView.render("comment.activity-json", %{conn: %{assigns: %{object: comment}}})
end
test "it returns 404 for non-public comments", %{conn: conn} do
comment = insert(:comment, visibility: :private)
conn =
conn
|> put_req_header("accept", "application/activity+json")
|> get(Routes.page_url(Endpoint, :comment, comment.uuid))
assert json_response(conn, 404)
end
end
describe "/@:preferred_username/inbox" do
test "it inserts an incoming event into the database", %{conn: conn} do
use_cassette "activity_pub_controller/mastodon-post-activity_actor_call" do
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Jason.decode!()
conn =
conn
|> assign(:valid_signature, true)
|> post("#{Mobilizon.Web.Endpoint.url()}/inbox", data)
assert "ok" == json_response(conn, 200)
:timer.sleep(500)
assert ActivityPub.fetch_object_from_url(data["object"]["id"])
end
end
end
describe "/@:preferred_username/outbox" do
test "it returns a note activity in a collection", %{conn: conn} do
actor = insert(:actor, visibility: :public)
comment = insert(:comment, actor: actor)
conn =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox))
assert json_response(conn, 200)["totalItems"] == 1
assert json_response(conn, 200)["first"]["orderedItems"] == [comment.url]
end
test "it returns an event activity in a collection", %{conn: conn} do
actor = insert(:actor, visibility: :public)
event = insert(:event, organizer_actor: actor)
conn =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox))
assert json_response(conn, 200)["totalItems"] == 1
assert json_response(conn, 200)["first"]["orderedItems"] == [event.url]
end
test "it works for more than 10 events", %{conn: conn} do
actor = insert(:actor, visibility: :public)
Enum.each(1..15, fn _ ->
insert(:event, organizer_actor: actor)
end)
result =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox))
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
assert result["totalItems"] == 15
result =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox, page: 2))
|> json_response(200)
assert length(result["orderedItems"]) == 5
end
test "it returns an empty collection if the actor has private visibility", %{conn: conn} do
actor = insert(:actor, visibility: :private)
insert(:event, organizer_actor: actor)
conn =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox))
assert json_response(conn, 200)["totalItems"] == 0
assert json_response(conn, 200)["first"]["orderedItems"] == []
end
test "it doesn't returns an event activity in a collection if actor has private visibility",
%{conn: conn} do
actor = insert(:actor, visibility: :private)
insert(:event, organizer_actor: actor)
conn =
conn
|> get(Actor.build_url(actor.preferred_username, :outbox))
assert json_response(conn, 200)["totalItems"] == 0
end
end
describe "/@actor/followers" do
test "it returns the followers in a collection", %{conn: conn} do
actor = insert(:actor, visibility: :public)
actor2 = insert(:actor)
Actors.follow(actor, actor2)
result =
conn
|> get(Actor.build_url(actor.preferred_username, :followers))
|> json_response(200)
assert result["first"]["orderedItems"] == [actor2.url]
end
test "it returns no followers for a private actor", %{conn: conn} do
actor = insert(:actor, visibility: :private)
actor2 = insert(:actor)
Actors.follow(actor, actor2)
result =
conn
|> get(Actor.build_url(actor.preferred_username, :followers))
|> json_response(200)
assert result["first"]["orderedItems"] == []
end
test "it works for more than 10 actors", %{conn: conn} do
actor = insert(:actor, visibility: :public)
Enum.each(1..15, fn _ ->
other_actor = insert(:actor)
Actors.follow(actor, other_actor)
end)
result =
conn
|> get(Actor.build_url(actor.preferred_username, :followers))
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
assert result["totalItems"] == 15
result =
conn
|> get(Actor.build_url(actor.preferred_username, :followers, page: 2))
|> json_response(200)
assert length(result["orderedItems"]) == 5
end
end
describe "/@actor/following" do
test "it returns the followings in a collection", %{conn: conn} do
actor = insert(:actor)
actor2 = insert(:actor, visibility: :public)
Actors.follow(actor, actor2)
result =
conn
|> get(Actor.build_url(actor2.preferred_username, :following))
|> json_response(200)
assert result["first"]["orderedItems"] == [actor.url]
end
test "it returns no followings for a private actor", %{conn: conn} do
actor = insert(:actor)
actor2 = insert(:actor, visibility: :private)
Actors.follow(actor, actor2)
result =
conn
|> get(Actor.build_url(actor2.preferred_username, :following))
|> json_response(200)
assert result["first"]["orderedItems"] == []
end
test "it works for more than 10 actors", %{conn: conn} do
actor = insert(:actor, visibility: :public)
Enum.each(1..15, fn _ ->
other_actor = insert(:actor)
Actors.follow(other_actor, actor)
end)
result =
conn
|> get(Actor.build_url(actor.preferred_username, :following))
|> json_response(200)
assert length(result["first"]["orderedItems"]) == 10
# assert result["first"]["totalItems"] == 15
# assert result["totalItems"] == 15
result =
conn
|> get(Actor.build_url(actor.preferred_username, :following, page: 2))
|> json_response(200)
assert length(result["orderedItems"]) == 5
# assert result["totalItems"] == 15
end
end
describe "/relay" do
test "with the relay active, it returns the relay user", %{conn: conn} do
res =
conn
|> get(activity_pub_path(conn, :relay))
|> json_response(200)
assert res["id"] =~ "/relay"
end
test "with the relay disabled, it returns 404", %{conn: conn} do
Config.put([:instance, :allow_relay], false)
conn
|> get(activity_pub_path(conn, :relay))
|> json_response(404)
|> assert
Config.put([:instance, :allow_relay], true)
end
end
#
# describe "/@:preferred_username/following" do
# test "it returns the following in a collection", %{conn: conn} do
# actor = insert(:actor)
# actor2 = insert(:actor)
# Mobilizon.Federation.ActivityPub.follow(actor, actor2)
# result =
# conn
# |> get("/@#{actor.preferred_username}/following")
# |> json_response(200)
# assert result["first"]["orderedItems"] == [actor2.url]
# end
# test "it works for more than 10 actors", %{conn: conn} do
# actor = insert(:actor)
# Enum.each(1..15, fn _ ->
# actor = Repo.get(Actor, actor.id)
# other_actor = insert(:actor)
# Actors.follow(actor, other_actor)
# end)
# result =
# conn
# |> get("/@#{actor.preferred_username}/following")
# |> json_response(200)
# assert length(result["first"]["orderedItems"]) == 10
# assert result["first"]["totalItems"] == 15
# assert result["totalItems"] == 15
# result =
# conn
# |> get("/@#{actor.preferred_username}/following?page=2")
# |> json_response(200)
# assert length(result["orderedItems"]) == 5
# assert result["totalItems"] == 15
# end
# end
end

View File

@@ -0,0 +1,345 @@
defmodule Mobilizon.Web.FeedControllerTest do
use Mobilizon.Web.ConnCase
import Mobilizon.Factory
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes
describe "/@:preferred_username/feed/atom" do
test "it returns an RSS representation of the actor's public events if the actor is publicly visible",
%{conn: conn} do
actor = insert(:actor, visibility: :public)
tag1 = insert(:tag, title: "RSS", slug: "rss")
tag2 = insert(:tag, title: "ATOM", slug: "atom")
event1 = insert(:event, organizer_actor: actor, tags: [tag1])
event2 = insert(:event, organizer_actor: actor, tags: [tag1, tag2])
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "atom")
|> URI.decode()
)
assert response(conn, 200) =~ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
assert response_content_type(conn, :xml) =~ "charset=utf-8"
{:ok, feed} = ElixirFeedParser.parse(conn.resp_body)
assert feed.title == actor.preferred_username <> "'s public events feed on Mobilizon"
[entry1, entry2] = entries = feed.entries
Enum.each(entries, fn entry ->
assert entry.title in [event1.title, event2.title]
end)
# It seems categories takes term instead of Label
# <category label=\"RSS\" term=\"rss\"/>
assert entry1.categories == [tag2.title, tag1.title] |> Enum.map(&String.downcase/1)
assert entry2.categories == [tag1.title] |> Enum.map(&String.downcase/1)
end
test "it returns a 404 for the actor's public events Atom feed if the actor is not publicly visible",
%{conn: conn} do
actor = insert(:actor)
tag1 = insert(:tag, title: "RSS", slug: "rss")
tag2 = insert(:tag, title: "ATOM", slug: "atom")
insert(:event, organizer_actor: actor, tags: [tag1])
insert(:event, organizer_actor: actor, tags: [tag1, tag2])
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "atom")
|> URI.decode()
)
assert response(conn, 404)
end
test "it returns an RSS representation of the actor's public events with the proper accept header",
%{conn: conn} do
actor = insert(:actor, visibility: :unlisted)
conn =
conn
|> put_req_header("accept", "application/atom+xml")
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "atom")
|> URI.decode()
)
assert response(conn, 200) =~ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
assert response_content_type(conn, :xml) =~ "charset=utf-8"
end
test "it doesn't return anything for an not existing actor", %{conn: conn} do
conn =
conn
|> put_req_header("accept", "application/atom+xml")
|> get("/@notexistent/feed/atom")
assert response(conn, 404)
end
end
describe "/@:preferred_username/feed/ics" do
test "it returns an iCalendar representation of the actor's public events with an actor publicly visible",
%{conn: conn} do
actor = insert(:actor, visibility: :public)
tag1 = insert(:tag, title: "iCalendar", slug: "icalendar")
tag2 = insert(:tag, title: "Apple", slug: "apple")
event1 = insert(:event, organizer_actor: actor, tags: [tag1])
event2 = insert(:event, organizer_actor: actor, tags: [tag1, tag2])
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
[entry1, entry2] = entries = ExIcal.parse(conn.resp_body)
Enum.each(entries, fn entry ->
assert entry.summary in [event1.title, event2.title]
end)
assert entry1.categories == [tag1.title]
assert entry2.categories == [tag1.title, tag2.title]
end
test "it returns a 404 page for the actor's public events iCal feed with an actor not publicly visible",
%{conn: conn} do
actor = insert(:actor, visibility: :private)
tag1 = insert(:tag, title: "iCalendar", slug: "icalendar")
tag2 = insert(:tag, title: "Apple", slug: "apple")
insert(:event, organizer_actor: actor, tags: [tag1])
insert(:event, organizer_actor: actor, tags: [tag1, tag2])
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "ics")
|> URI.decode()
)
assert response(conn, 404)
end
test "it returns an iCalendar representation of the actor's public events with the proper accept header",
%{conn: conn} do
actor = insert(:actor, visibility: :unlisted)
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get(
Endpoint
|> Routes.feed_url(:actor, actor.preferred_username, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
end
test "it doesn't return anything for an not existing actor", %{conn: conn} do
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get("/@notexistent/feed/ics")
assert response(conn, 404)
end
end
describe "/events/:uuid/export/ics" do
test "it returns an iCalendar representation of the event", %{conn: conn} do
tag1 = insert(:tag, title: "iCalendar", slug: "icalendar")
tag2 = insert(:tag, title: "Apple", slug: "apple")
event1 = insert(:event, tags: [tag1, tag2])
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:event, event1.uuid, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
[entry1] = ExIcal.parse(conn.resp_body)
assert entry1.summary == event1.title
assert entry1.categories == [tag1.title, tag2.title]
end
end
describe "/events/going/:token/atom" do
test "it returns an atom feed of all events for all identities for an user token", %{
conn: conn
} do
user = insert(:user)
actor1 = insert(:actor, user: user)
actor2 = insert(:actor, user: user)
event1 = insert(:event)
event2 = insert(:event)
insert(:participant, event: event1, actor: actor1)
insert(:participant, event: event2, actor: actor2)
feed_token = insert(:feed_token, user: user, actor: nil)
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:going, feed_token.token, "atom")
|> URI.decode()
)
assert response(conn, 200) =~ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
assert response_content_type(conn, :xml) =~ "charset=utf-8"
{:ok, feed} = ElixirFeedParser.parse(conn.resp_body)
assert feed.title == "Feed for #{user.email} on Mobilizon"
entries = feed.entries
Enum.each(entries, fn entry ->
assert entry.title in [event1.title, event2.title]
end)
end
test "it returns an atom feed of all events a single identity for an actor token", %{
conn: conn
} do
user = insert(:user)
actor1 = insert(:actor, user: user)
actor2 = insert(:actor, user: user)
event1 = insert(:event)
event2 = insert(:event)
insert(:participant, event: event1, actor: actor1)
insert(:participant, event: event2, actor: actor2)
feed_token = insert(:feed_token, user: user, actor: actor1)
conn =
conn
|> put_req_header("accept", "application/atom+xml")
|> get(
Endpoint
|> Routes.feed_url(:going, feed_token.token, "atom")
|> URI.decode()
)
assert response(conn, 200) =~ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
assert response_content_type(conn, :xml) =~ "charset=utf-8"
{:ok, feed} = ElixirFeedParser.parse(conn.resp_body)
assert feed.title == "#{actor1.preferred_username}'s private events feed on Mobilizon"
[entry] = feed.entries
assert entry.title == event1.title
end
test "it returns 404 for an not existing feed", %{conn: conn} do
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:going, "not existing", "atom")
|> URI.decode()
)
assert response(conn, 404)
end
end
describe "/events/going/:token/ics" do
test "it returns an ical feed of all events for all identities for an user token", %{
conn: conn
} do
user = insert(:user)
actor1 = insert(:actor, user: user)
actor2 = insert(:actor, user: user)
event1 = insert(:event)
event2 = insert(:event)
insert(:participant, event: event1, actor: actor1)
insert(:participant, event: event2, actor: actor2)
feed_token = insert(:feed_token, user: user, actor: nil)
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get(
Endpoint
|> Routes.feed_url(:going, feed_token.token, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
entries = ExIcal.parse(conn.resp_body)
Enum.each(entries, fn entry ->
assert entry.summary in [event1.title, event2.title]
end)
end
test "it returns an ical feed of all events a single identity for an actor token", %{
conn: conn
} do
user = insert(:user)
actor1 = insert(:actor, user: user)
actor2 = insert(:actor, user: user)
event1 = insert(:event)
event2 = insert(:event)
insert(:participant, event: event1, actor: actor1)
insert(:participant, event: event2, actor: actor2)
feed_token = insert(:feed_token, user: user, actor: actor1)
conn =
conn
|> put_req_header("accept", "text/calendar")
|> get(
Endpoint
|> Routes.feed_url(:going, feed_token.token, "ics")
|> URI.decode()
)
assert response(conn, 200) =~ "BEGIN:VCALENDAR"
assert response_content_type(conn, :calendar) =~ "charset=utf-8"
[entry1] = ExIcal.parse(conn.resp_body)
assert entry1.summary == event1.title
assert entry1.categories == event1.tags |> Enum.map(& &1.title)
end
test "it returns 404 for an not existing feed", %{conn: conn} do
conn =
conn
|> get(
Endpoint
|> Routes.feed_url(:going, "not existing", "ics")
|> URI.decode()
)
assert response(conn, 404)
end
end
end

View File

@@ -0,0 +1,55 @@
defmodule Mobilizon.Web.NodeInfoControllerTest do
use Mobilizon.Web.ConnCase
alias Mobilizon.Config
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes
test "Get node info schemas", %{conn: conn} do
conn = get(conn, node_info_path(conn, :schemas))
assert json_response(conn, 200) == %{
"links" => [
%{
"href" => Routes.node_info_url(Endpoint, :nodeinfo, "2.0"),
"rel" => "http://nodeinfo.diaspora.software/ns/schema/2.0"
},
%{
"href" => Routes.node_info_url(Endpoint, :nodeinfo, "2.1"),
"rel" => "http://nodeinfo.diaspora.software/ns/schema/2.1"
}
]
}
end
test "Get node info", %{conn: conn} do
# We clear the cache because it might have been initialized by other tests
Cachex.clear(:statistics)
conn = get(conn, node_info_path(conn, :nodeinfo, "2.1"))
resp = json_response(conn, 200)
assert resp == %{
"metadata" => %{
"nodeName" => Config.instance_name(),
"nodeDescription" => Config.instance_description()
},
"openRegistrations" => Config.instance_registrations_open?(),
"protocols" => ["activitypub"],
"services" => %{"inbound" => [], "outbound" => ["atom1.0"]},
"software" => %{
"name" => "Mobilizon",
"version" => Config.instance_version(),
"repository" => Config.instance_repository()
},
"usage" => %{"localComments" => 0, "localPosts" => 0, "users" => %{"total" => 0}},
"version" => "2.1"
}
end
test "Get node info with non supported version (1.0)", %{conn: conn} do
conn = get(conn, node_info_path(conn, :nodeinfo, "1.0"))
assert json_response(conn, 404) == %{"error" => "Nodeinfo schema version not handled"}
end
end

View File

@@ -0,0 +1,65 @@
defmodule Mobilizon.Web.PageControllerTest do
use Mobilizon.Web.ConnCase
import Mobilizon.Factory
alias Mobilizon.Actors.Actor
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes
setup do
conn = build_conn() |> put_req_header("accept", "text/html")
{:ok, conn: conn}
end
test "GET /", %{conn: conn} do
conn = get(conn, "/")
assert html_response(conn, 200)
end
test "GET /@actor with existing actor", %{conn: conn} do
actor = insert(:actor)
conn = get(conn, Actor.build_url(actor.preferred_username, :page))
assert html_response(conn, 200) =~ actor.preferred_username
end
test "GET /@actor with not existing actor", %{conn: conn} do
conn = get(conn, Actor.build_url("not_existing", :page))
assert html_response(conn, 404)
end
test "GET /events/:uuid", %{conn: conn} do
event = insert(:event)
conn = get(conn, Routes.page_url(Endpoint, :event, event.uuid))
assert html_response(conn, 200) =~ event.title
end
test "GET /events/:uuid with not existing event", %{conn: conn} do
conn = get(conn, Routes.page_url(Endpoint, :event, "not_existing_event"))
assert html_response(conn, 404)
end
test "GET /events/:uuid with event not public", %{conn: conn} do
event = insert(:event, visibility: :restricted)
conn = get(conn, Routes.page_url(Endpoint, :event, event.uuid))
assert html_response(conn, 404)
end
test "GET /comments/:uuid", %{conn: conn} do
comment = insert(:comment)
conn = get(conn, Routes.page_url(Endpoint, :comment, comment.uuid))
assert html_response(conn, 200) =~ comment.text
end
test "GET /comments/:uuid with not existing comment", %{conn: conn} do
conn = get(conn, Routes.page_url(Endpoint, :comment, "not_existing_comment"))
assert html_response(conn, 404)
end
test "GET /comments/:uuid with comment not public", %{conn: conn} do
comment = insert(:comment, visibility: :private)
conn = get(conn, Routes.page_url(Endpoint, :comment, comment.uuid))
assert html_response(conn, 404)
end
end

View File

@@ -0,0 +1,46 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/web_finger/web_finger_test.exs
defmodule Mobilizon.Web.WebFingerControllerTest do
use Mobilizon.Web.ConnCase
import Mobilizon.Factory
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.WebFinger
setup_all do
Mobilizon.Config.put([:instance, :federating], true)
:ok
end
test "GET /.well-known/host-meta", %{conn: conn} do
conn = get(conn, "/.well-known/host-meta")
assert response(conn, 200) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><XRD xmlns=\"http://docs.oasis-open.org/ns/xri/xrd-1.0\"><Link rel=\"lrdd\" template=\"#{
Mobilizon.Web.Endpoint.url()
}/.well-known/webfinger?resource={uri}\" type=\"application/xrd+xml\" /></XRD>"
assert {"content-type", "application/xrd+xml; charset=utf-8"} in conn.resp_headers
end
test "GET /.well-known/webfinger with local actor", %{conn: conn} do
%Actor{preferred_username: username} = actor = insert(:actor)
conn = get(conn, "/.well-known/webfinger?resource=acct:#{username}@mobilizon.test")
assert json_response(conn, 200) == WebFinger.represent_actor(actor)
end
test "GET /.well-known/webfinger with non existent actor", %{conn: conn} do
conn = get(conn, "/.well-known/webfinger?resource=acct:notme@mobilizon.test")
assert response(conn, 404) == "Couldn't find user"
end
test "GET /.well-known/webfinger with no query", %{conn: conn} do
conn = get(conn, "/.well-known/webfinger")
assert response(conn, 400) == "No query provided"
end
end

View File

@@ -0,0 +1,28 @@
# Portions of this file are derived from Pleroma:
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mobilizon.Web.Plug.FederatingTest do
use Mobilizon.Web.ConnCase
alias Mobilizon.Web.Plugs.Federating
test "returns and halt the conn when federating is disabled" do
Mobilizon.Config.put([:instance, :federating], false)
conn = Federating.call(build_conn(), %{})
assert conn.status == 404
assert conn.halted
end
test "does nothing when federating is enabled" do
Mobilizon.Config.put([:instance, :federating], true)
conn = Federating.call(build_conn(), %{})
refute conn.status
refute conn.halted
end
end

View File

@@ -0,0 +1,61 @@
# Portions of this file are derived from Pleroma:
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Mobilizon.Web.Plugs.MappedSignatureToIdentityTest do
use Mobilizon.Web.ConnCase
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
alias Mobilizon.Web.Plugs.MappedSignatureToIdentity
defp set_signature(conn, key_id) do
conn
|> put_req_header("signature", "keyId=\"#{key_id}\"")
|> assign(:valid_signature, true)
end
test "it successfully maps a valid identity with a valid signature" do
use_cassette "activity_pub/signature/valid" do
conn =
build_conn(:get, "/doesntmattter")
|> set_signature("https://framapiaf.org/users/admin")
|> MappedSignatureToIdentity.call(%{})
refute is_nil(conn.assigns.actor)
end
end
test "it successfully maps a valid identity with a valid signature with payload" do
use_cassette "activity_pub/signature/valid_payload" do
conn =
build_conn(:post, "/doesntmattter", %{"actor" => "https://framapiaf.org/users/admin"})
|> set_signature("https://framapiaf.org/users/admin")
|> MappedSignatureToIdentity.call(%{})
refute is_nil(conn.assigns.actor)
end
end
test "it considers a mapped identity to be invalid when it mismatches a payload" do
use_cassette "activity_pub/signature/invalid_payload" do
conn =
build_conn(:post, "/doesntmattter", %{"actor" => "https://framapiaf.org/users/admin"})
|> set_signature("https://niu.moe/users/rye")
|> MappedSignatureToIdentity.call(%{})
assert %{valid_signature: false} == conn.assigns
end
end
test "it considers a mapped identity to be invalid when the identity cannot be found" do
use_cassette "activity_pub/signature/invalid_not_found" do
conn =
build_conn(:post, "/doesntmattter", %{"actor" => "https://framapiaf.org/users/admin"})
|> set_signature("http://niu.moe/users/rye")
|> MappedSignatureToIdentity.call(%{})
assert %{valid_signature: false} == conn.assigns
end
end
end

View File

@@ -0,0 +1,43 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/plugs/uploaded_media_plug_test.ex
defmodule Mobilizon.Web.Plugs.UploadedMediaPlugTest do
use Mobilizon.Web.ConnCase
alias Mobilizon.Web.Upload
defp upload_file(context) do
Mobilizon.DataCase.ensure_local_uploader(context)
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "nice_tf.jpg"
}
{:ok, data} = Upload.store(file)
[attachment_url: data.url]
end
setup_all :upload_file
test "does not send Content-Disposition header when name param is not set", %{
attachment_url: attachment_url
} do
conn = get(build_conn(), attachment_url)
refute Enum.any?(conn.resp_headers, &(elem(&1, 0) == "content-disposition"))
end
test "sends Content-Disposition header when name param is set", %{
attachment_url: attachment_url
} do
conn = get(build_conn(), attachment_url <> "?name=\"cofe\".gif")
assert Enum.any?(
conn.resp_headers,
&(&1 == {"content-disposition", "filename=\"\\\"cofe\\\".gif\""})
)
end
end

View File

@@ -0,0 +1,185 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/media_proxy_test.ex
defmodule Mobilizon.Web.MediaProxyTest do
use ExUnit.Case
import Mobilizon.Web.MediaProxy
alias Mobilizon.Config
alias Mobilizon.Web.MediaProxyController
setup do
enabled = Config.get([:media_proxy, :enabled])
on_exit(fn -> Config.put([:media_proxy, :enabled], enabled) end)
:ok
end
describe "when enabled" do
setup do
Config.put([:media_proxy, :enabled], true)
:ok
end
test "ignores invalid url" do
assert url(nil) == nil
assert url("") == nil
end
test "ignores relative url" do
assert url("/local") == "/local"
assert url("/") == "/"
end
test "ignores local url" do
local_url = Mobilizon.Web.Endpoint.url() <> "/hello"
local_root = Mobilizon.Web.Endpoint.url()
assert url(local_url) == local_url
assert url(local_root) == local_root
end
test "encodes and decodes URL" do
url = "https://pleroma.soykaf.com/static/logo.png"
encoded = url(url)
assert String.starts_with?(
encoded,
Config.get([:media_proxy, :base_url], Mobilizon.Web.Endpoint.url())
)
assert String.ends_with?(encoded, "/logo.png")
assert decode_result(encoded) == url
end
test "encodes and decodes URL without a path" do
url = "https://pleroma.soykaf.com"
encoded = url(url)
assert decode_result(encoded) == url
end
test "encodes and decodes URL without an extension" do
url = "https://pleroma.soykaf.com/path/"
encoded = url(url)
assert String.ends_with?(encoded, "/path")
assert decode_result(encoded) == url
end
test "encodes and decodes URL and ignores query params for the path" do
url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true"
encoded = url(url)
assert String.ends_with?(encoded, "/logo.png")
assert decode_result(encoded) == url
end
test "ensures urls are url-encoded" do
assert decode_result(url("https://pleroma.social/Hello world.jpg")) ==
"https://pleroma.social/Hello%20world.jpg"
assert decode_result(url("https://pleroma.social/Hello%20world.jpg")) ==
"https://pleroma.social/Hello%20world.jpg"
end
test "validates signature" do
secret_key_base = Config.get([Mobilizon.Web.Endpoint, :secret_key_base])
on_exit(fn ->
Config.put([Mobilizon.Web.Endpoint, :secret_key_base], secret_key_base)
end)
encoded = url("https://pleroma.social")
Config.put(
[Mobilizon.Web.Endpoint, :secret_key_base],
"00000000000000000000000000000000000000000000000"
)
[_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
assert decode_url(sig, base64) == {:error, :invalid_signature}
end
test "filename_matches matches url encoded paths" do
assert MediaProxyController.filename_matches(
true,
"/Hello%20world.jpg",
"http://pleroma.social/Hello world.jpg"
) == :ok
assert MediaProxyController.filename_matches(
true,
"/Hello%20world.jpg",
"http://pleroma.social/Hello%20world.jpg"
) == :ok
end
test "filename_matches matches non-url encoded paths" do
assert MediaProxyController.filename_matches(
true,
"/Hello world.jpg",
"http://pleroma.social/Hello%20world.jpg"
) == :ok
assert MediaProxyController.filename_matches(
true,
"/Hello world.jpg",
"http://pleroma.social/Hello world.jpg"
) == :ok
end
test "uses the configured base_url" do
base_url = Config.get([:media_proxy, :base_url])
if base_url do
on_exit(fn ->
Config.put([:media_proxy, :base_url], base_url)
end)
end
Config.put([:media_proxy, :base_url], "https://cache.pleroma.social")
url = "https://pleroma.soykaf.com/static/logo.png"
encoded = url(url)
assert String.starts_with?(encoded, Config.get([:media_proxy, :base_url]))
end
# https://git.pleroma.social/pleroma/pleroma/issues/580
test "encoding S3 links (must preserve `%2F`)" do
url =
"https://s3.amazonaws.com/example/test.png?X-Amz-Credential=your-access-key-id%2F20130721%2Fus-east-1%2Fs3%2Faws4_request"
encoded = url(url)
assert decode_result(encoded) == url
end
end
describe "when disabled" do
setup do
enabled = Config.get([:media_proxy, :enabled])
if enabled do
Config.put([:media_proxy, :enabled], false)
on_exit(fn ->
Config.put([:media_proxy, :enabled], enabled)
:ok
end)
end
:ok
end
test "does not encode remote urls" do
assert url("https://google.fr") == "https://google.fr"
end
end
defp decode_result(encoded) do
[_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/")
{:ok, decoded} = decode_url(sig, base64)
decoded
end
end

View File

@@ -0,0 +1,220 @@
# Portions of this file are derived from Pleroma:
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/upload_test.ex
defmodule Mobilizon.UploadTest do
use Mobilizon.DataCase
alias Mobilizon.Config
alias Mobilizon.Web.Upload
alias Mobilizon.Web.Upload.Uploader
describe "Storing a file with the Local uploader" do
setup [:ensure_local_uploader]
test "returns a media url" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "image.jpg"
}
{:ok, data} = Upload.store(file)
assert %{
url: url,
content_type: "image/jpeg",
size: 13_227
} = data
assert String.starts_with?(url, Mobilizon.Web.Endpoint.url() <> "/media/")
end
test "returns a media url with configured base_url" do
base_url = "https://cache.mobilizon.social"
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "image.jpg"
}
{:ok, data} = Upload.store(file, base_url: base_url)
assert String.starts_with?(data.url, base_url <> "/media/")
end
test "copies the file to the configured folder with deduping" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.Dedupe])
assert data.url ==
Mobilizon.Web.Endpoint.url() <>
"/media/590523d60d3831ec92d05cdd871078409d5780903910efec5cd35ab1b0f19d11.jpg"
end
test "copies the file to the configured folder without deduping" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file)
assert data.name == "an [image.jpg"
end
test "fixes incorrect content type" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "application/octet-stream",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.Dedupe])
assert data.content_type == "image/jpeg"
end
test "adds missing extension" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image"
}
{:ok, data} = Upload.store(file)
assert data.name == "an [image.jpg"
end
test "fixes incorrect file extension" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image.blah"
}
{:ok, data} = Upload.store(file)
assert data.name == "an [image.jpg"
end
test "don't modify filename of an unknown type" do
File.cp("test/fixtures/test.txt", "test/fixtures/test_tmp.txt")
file = %Plug.Upload{
content_type: "text/plain",
path: Path.absname("test/fixtures/test_tmp.txt"),
filename: "test.txt"
}
{:ok, data} = Upload.store(file)
assert data.name == "test.txt"
end
test "copies the file to the configured folder with anonymizing filename" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.AnonymizeFilename])
refute data.name == "an [image.jpg"
end
test "escapes invalid characters in url" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "an… image.jpg"
}
{:ok, data} = Upload.store(file)
assert Path.basename(data.url) == "an%E2%80%A6%20image.jpg"
end
test "escapes reserved uri characters" do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: ":?#[]@!$&\\'()*+,;=.jpg"
}
{:ok, data} = Upload.store(file)
assert Path.basename(data.url) ==
"%3A%3F%23%5B%5D%40%21%24%26%5C%27%28%29%2A%2B%2C%3B%3D.jpg"
end
test "upload and delete successfully a file" do
{path, url} = upload()
assert File.exists?(path)
assert {:ok, _} = Upload.remove(url)
refute File.exists?(path)
path = path |> Path.split() |> Enum.reverse() |> tl |> Enum.reverse() |> Path.join()
refute File.exists?(path)
end
test "delete a not existing file" do
file =
Config.get!([Uploader.Local, :uploads]) <>
"/not_existing/definitely.jpg"
refute File.exists?(file)
assert {:error, "File not_existing/definitely.jpg doesn't exist"} =
Upload.remove("https://mobilizon.test/media/not_existing/definitely.jpg")
end
end
defp upload do
File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg")
file = %Plug.Upload{
content_type: "image/jpg",
path: Path.absname("test/fixtures/image_tmp.jpg"),
filename: "image.jpg"
}
{:ok, data} = Upload.store(file)
assert %{
url: url,
size: 13_227,
content_type: "image/jpeg"
} = data
assert String.starts_with?(url, Mobilizon.Web.Endpoint.url() <> "/media/")
%URI{path: "/media/" <> path} = URI.parse(url)
{Config.get!([Uploader.Local, :uploads]) <> "/" <> path, url}
end
end

View File

@@ -0,0 +1,19 @@
defmodule Mobilizon.Web.ErrorViewTest do
use Mobilizon.Web.ConnCase, async: true
# Bring render/3 and render_to_string/3 for testing custom views
import Phoenix.View
test "renders 404.html" do
assert render_to_string(Mobilizon.Web.ErrorView, "404.html", []) =~
"We're sorry but mobilizon doesn't work properly without JavaScript enabled. Please enable it to continue."
end
test "render 500.html" do
assert render_to_string(Mobilizon.Web.ErrorView, "500.html", []) == "Internal server error"
end
test "render any other" do
assert render_to_string(Mobilizon.Web.ErrorView, "505.html", []) == "Internal server error"
end
end

View File

@@ -0,0 +1,3 @@
defmodule Mobilizon.Web.LayoutViewTest do
use Mobilizon.Web.ConnCase, async: true
end

View File

@@ -0,0 +1,3 @@
defmodule Mobilizon.Web.PageViewTest do
use Mobilizon.Web.ConnCase, async: true
end