From 5fcf3d52673f28e468778b8a82fcc74fe2e65b51 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 1 Dec 2023 12:06:18 +0100 Subject: [PATCH] test: fix tests using verified routes Signed-off-by: Thomas Citharel --- .sobelow-skips | 2 +- lib/service/metadata/actor.ex | 6 ++-- lib/web/controllers/application_controller.ex | 4 +-- test/federation/activity_pub/utils_test.exs | 3 +- test/service/metadata/metadata_test.exs | 7 ++--- test/support/conn_case.ex | 2 -- .../activity_pub_controller_test.exs | 6 ++-- .../application_controller_test.exs | 10 +++++-- test/web/controllers/feed_controller_test.exs | 29 +++++++------------ .../controllers/nodeinfo_controller_test.exs | 13 ++++----- test/web/controllers/page_controller_test.exs | 3 +- 11 files changed, 37 insertions(+), 48 deletions(-) diff --git a/.sobelow-skips b/.sobelow-skips index 0d409653..b0d6ee10 100644 --- a/.sobelow-skips +++ b/.sobelow-skips @@ -1,6 +1,7 @@ 155A1FB53DE39EC8EFCFD7FB94EA823D 1C29EE70E90ECED01AF28EC58D2575B5 +26ED12A8E03D044BEDC08749BAA5E357 2BB1D36656B423758A470021718FCB09 31CE26BC979C57B9E3CC97B40C290CE5 3529E7A4CECC24D02678820E6F521162 @@ -9,7 +10,6 @@ 53CBBEB6243FAF5C37249CBA17DE6F4C 5BCE3651A03711295046DE48BDFE007E 5C4CED447689F00D9D1ACEB9B895ED29 -90EB7E986B2A3A0C3851B6BF158ECD73 94ACF7B17C3FF42F64E57DD1DA936BD8 A32E125003F1EDFAD95C487C6A969725 ACF6272A1DBB3A2ABD96C0C120B5CA69 diff --git a/lib/service/metadata/actor.ex b/lib/service/metadata/actor.ex index a8f801d1..e71cb6cc 100644 --- a/lib/service/metadata/actor.ex +++ b/lib/service/metadata/actor.ex @@ -84,13 +84,15 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do rel: "alternate", type: "application/atom+xml", title: gettext("%{name}'s feed", name: actor_display_name_escaped(group)) |> HTML.raw(), - href: url(~p"/@#{Actor.preferred_username_and_domain(group)}/feed/atom") + href: + ~p"/@#{Actor.preferred_username_and_domain(group)}/feed/atom" |> url() |> URI.decode() ), Tag.tag(:link, rel: "alternate", type: "text/calendar", title: gettext("%{name}'s feed", name: actor_display_name_escaped(group)) |> HTML.raw(), - href: url(~p"/@#{Actor.preferred_username_and_domain(group)}/feed/ics") + href: + ~p"/@#{Actor.preferred_username_and_domain(group)}/feed/ics" |> url() |> URI.decode() ), Tag.tag(:link, rel: "alternate", diff --git a/lib/web/controllers/application_controller.ex b/lib/web/controllers/application_controller.ex index 02ecd1f0..cdfc669e 100644 --- a/lib/web/controllers/application_controller.ex +++ b/lib/web/controllers/application_controller.ex @@ -111,9 +111,7 @@ defmodule Mobilizon.Web.ApplicationController do is_binary(state) and is_binary(scope) do redirect(conn, to: - url( - ~p"/oauth/authorize?#{[client_id: client_id, redirect_uri: redirect_uri, scope: scope, state: state]}" - ) + ~p"/oauth/autorize_approve?#{[client_id: client_id, redirect_uri: redirect_uri, scope: scope, state: state]}" ) else if is_binary(redirect_uri) and valid_uri?(redirect_uri) do diff --git a/test/federation/activity_pub/utils_test.exs b/test/federation/activity_pub/utils_test.exs index 8dc6ef0f..8e73c95b 100644 --- a/test/federation/activity_pub/utils_test.exs +++ b/test/federation/activity_pub/utils_test.exs @@ -6,8 +6,7 @@ defmodule Mobilizon.Federation.ActivityPub.UtilsTest do alias Mobilizon.Federation.ActivityPub.Utils alias Mobilizon.Federation.ActivityStream.Converter - alias Mobilizon.Web.Endpoint - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes describe "make" do test "comment data from struct" do diff --git a/test/service/metadata/metadata_test.exs b/test/service/metadata/metadata_test.exs index a891910c..656111b0 100644 --- a/test/service/metadata/metadata_test.exs +++ b/test/service/metadata/metadata_test.exs @@ -5,9 +5,8 @@ defmodule Mobilizon.Service.MetadataTest do alias Mobilizon.Posts.Post alias Mobilizon.Service.Metadata alias Mobilizon.Tombstone - alias Mobilizon.Web.Endpoint alias Mobilizon.Web.JsonLD.ObjectView - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes use Mobilizon.DataCase import Mobilizon.Factory @@ -18,7 +17,7 @@ defmodule Mobilizon.Service.MetadataTest do assert group |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() == String.trim(""" - + url() |> URI.decode()}" property="og:url"> url() |> URI.decode()}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"> url() |> URI.decode()}" rel="alternate" title="#{group.name}'s feed" type="text/calendar"> """) assert group @@ -26,7 +25,7 @@ defmodule Mobilizon.Service.MetadataTest do |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() == String.trim(""" - + url() |> URI.decode()}" property="og:url"> url() |> URI.decode()}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"> url() |> URI.decode()}" rel="alternate" title="#{group.name}'s feed" type="text/calendar"> """) end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 3fe243a0..3bb187cf 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -27,8 +27,6 @@ defmodule Mobilizon.Web.ConnCase do import Plug.Conn import Phoenix.ConnTest - import Mobilizon.Web.Router.Helpers - use Mobilizon.Web, :verified_routes # The default endpoint for testing diff --git a/test/web/controllers/activity_pub_controller_test.exs b/test/web/controllers/activity_pub_controller_test.exs index e74bd05a..1fd496b7 100644 --- a/test/web/controllers/activity_pub_controller_test.exs +++ b/test/web/controllers/activity_pub_controller_test.exs @@ -18,7 +18,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do alias Mobilizon.Service.HTTP.ActivityPub.Mock alias Mobilizon.Web.ActivityPub.ActorView alias Mobilizon.Web.{Endpoint, PageView} - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes setup_all do Mobilizon.Config.put([:instance, :federating], true) @@ -412,7 +412,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do test "with the relay active, it returns the relay user", %{conn: conn} do res = conn - |> get(activity_pub_path(conn, :relay)) + |> get(url(~p"/relay")) |> json_response(200) assert res["id"] =~ "/relay" @@ -422,7 +422,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do Config.put([:instance, :allow_relay], false) conn - |> get(activity_pub_path(conn, :relay)) + |> get(url(~p"/relay")) |> json_response(404) |> assert diff --git a/test/web/controllers/application_controller_test.exs b/test/web/controllers/application_controller_test.exs index 2d515fcb..f69f859d 100644 --- a/test/web/controllers/application_controller_test.exs +++ b/test/web/controllers/application_controller_test.exs @@ -1,7 +1,7 @@ defmodule Mobilizon.Web.ApplicationControllerTest do use Mobilizon.Web.ConnCase alias Mobilizon.Service.Auth.Applications - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes import Mobilizon.Factory describe "create application" do @@ -74,7 +74,9 @@ defmodule Mobilizon.Web.ApplicationControllerTest do conn = get( conn, - "/oauth/authorize?client_id=hello&redirect_uri=#{URI.encode("https://somewhere.org/callback")}" + url( + ~p"/oauth/authorize?client_id=hello&redirect_uri=#{URI.encode("https://somewhere.org/callback")}" + ) ) assert redirected_to(conn) =~ @@ -85,7 +87,9 @@ defmodule Mobilizon.Web.ApplicationControllerTest do conn = get( conn, - "/oauth/authorize?client_id=hello&redirect_uri=#{URI.encode("https://somewhere.org/callback&state=something&scope=everything")}" + url( + ~p"/oauth/authorize?client_id=hello&scope=everything&state=something&redirect_uri=#{URI.encode("https://somewhere.org/callback")}" + ) ) assert redirected_to(conn) =~ "/oauth/autorize_approve" diff --git a/test/web/controllers/feed_controller_test.exs b/test/web/controllers/feed_controller_test.exs index 4d043dc5..0cf3155d 100644 --- a/test/web/controllers/feed_controller_test.exs +++ b/test/web/controllers/feed_controller_test.exs @@ -4,8 +4,7 @@ defmodule Mobilizon.Web.FeedControllerTest do import Mobilizon.Factory alias Mobilizon.Config - alias Mobilizon.Web.Endpoint - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_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", @@ -24,11 +23,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn - |> get( - Endpoint - |> ~p"/@#{actor.preferred_username}/feed/atom" - |> URI.decode() - ) + |> get(URI.decode(~p"/@#{actor.preferred_username}/feed/atom")) assert response(conn, 200) =~ "" assert response_content_type(conn, :xml) =~ "charset=utf-8" @@ -62,8 +57,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> get( - Endpoint - |> ~p"/@#{actor.preferred_username}/feed/atom" + ~p"/@#{actor.preferred_username}/feed/atom" |> URI.decode() ) @@ -78,8 +72,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn |> put_req_header("accept", "application/atom+xml") |> get( - Endpoint - |> ~p"/@#{actor.preferred_username}/feed/atom" + ~p"/@#{actor.preferred_username}/feed/atom" |> URI.decode() ) @@ -196,8 +189,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> get( - Endpoint - |> ~p"/events/#{event1.uuid}/export/ics" + ~p"/events/#{event1.uuid}/export/ics" |> URI.decode() ) @@ -229,7 +221,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn - |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}", "atom")) + |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}/atom")) assert response(conn, 200) =~ "" assert response_content_type(conn, :xml) =~ "charset=utf-8" @@ -263,7 +255,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> put_req_header("accept", "application/atom+xml") - |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}", "atom")) + |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}/atom")) assert response(conn, 200) =~ "" assert response_content_type(conn, :xml) =~ "charset=utf-8" @@ -305,7 +297,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> put_req_header("accept", "text/calendar") - |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}", "ics")) + |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}/ics")) assert response(conn, 200) =~ "BEGIN:VCALENDAR" assert response_content_type(conn, :calendar) =~ "charset=utf-8" @@ -335,7 +327,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> put_req_header("accept", "text/calendar") - |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}", "ics")) + |> get(URI.decode(~p"/events/going/#{ShortUUID.encode!(feed_token.token)}/ics")) assert response(conn, 200) =~ "BEGIN:VCALENDAR" assert response_content_type(conn, :calendar) =~ "charset=utf-8" @@ -350,8 +342,7 @@ defmodule Mobilizon.Web.FeedControllerTest do conn = conn |> get( - Endpoint - |> ~p"/events/going/not_existing/ics" + ~p"/events/going/not_existing/ics" |> URI.decode() ) diff --git a/test/web/controllers/nodeinfo_controller_test.exs b/test/web/controllers/nodeinfo_controller_test.exs index 0b0287ae..08eca37a 100644 --- a/test/web/controllers/nodeinfo_controller_test.exs +++ b/test/web/controllers/nodeinfo_controller_test.exs @@ -3,20 +3,19 @@ defmodule Mobilizon.Web.NodeInfoControllerTest do alias Mobilizon.Config - alias Mobilizon.Web.Endpoint - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes test "Get node info schemas", %{conn: conn} do - conn = get(conn, node_info_path(conn, :schemas)) + conn = get(conn, url(~p"/.well-known/nodeinfo")) assert json_response(conn, 200) == %{ "links" => [ %{ - "href" => ~p"/.well-known/nodeinfo/2.0Ã", + "href" => url(~p"/.well-known/nodeinfo/2.0"), "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.0" }, %{ - "href" => ~p"/.well-known/nodeinfo/2.1", + "href" => url(~p"/.well-known/nodeinfo/2.1"), "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.1" } ] @@ -26,7 +25,7 @@ defmodule Mobilizon.Web.NodeInfoControllerTest do 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")) + conn = get(conn, url(~p"/.well-known/nodeinfo/2.1")) resp = json_response(conn, 200) assert resp == %{ @@ -48,7 +47,7 @@ defmodule Mobilizon.Web.NodeInfoControllerTest do end test "Get node info with non supported version (1.0)", %{conn: conn} do - conn = get(conn, node_info_path(conn, :nodeinfo, "1.0")) + conn = get(conn, url(~p"/.well-known/nodeinfo/1.0")) assert json_response(conn, 404) == %{"error" => "Nodeinfo schema version not handled"} end diff --git a/test/web/controllers/page_controller_test.exs b/test/web/controllers/page_controller_test.exs index 432bec2c..8f09443e 100644 --- a/test/web/controllers/page_controller_test.exs +++ b/test/web/controllers/page_controller_test.exs @@ -5,8 +5,7 @@ defmodule Mobilizon.Web.PageControllerTest do alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActorSuspension - alias Mobilizon.Web.Endpoint - alias Mobilizon.Web.Router.Helpers, as: Routes + use Mobilizon.Web, :verified_routes setup do conn = build_conn() |> put_req_header("accept", "text/html")