From 03e9a0b840d61f19719d2003c83869f4ef905888 Mon Sep 17 00:00:00 2001 From: robs Date: Mon, 30 Dec 2024 20:14:50 +0100 Subject: [PATCH] add test for secret route validation --- lib/pinchflat_web/router.ex | 1 - .../controllers/podcast_controller_test.exs | 17 +++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index 7af66e3..3e9ca85 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -29,7 +29,6 @@ defmodule PinchflatWeb.Router do scope "/secret/:secret", PinchflatWeb do pipe_through :secret - # has to match before /sources/:id get "/opml/feed", Podcasts.PodcastController, :opml_feed end diff --git a/test/pinchflat_web/controllers/podcast_controller_test.exs b/test/pinchflat_web/controllers/podcast_controller_test.exs index ada823d..f3ade12 100644 --- a/test/pinchflat_web/controllers/podcast_controller_test.exs +++ b/test/pinchflat_web/controllers/podcast_controller_test.exs @@ -5,10 +5,23 @@ defmodule PinchflatWeb.PodcastControllerTest do import Pinchflat.SourcesFixtures describe "opml_feed" do + + test "unauthorized when no secret set", %{conn: conn} do + Application.put_env(:pinchflat, :route_secret, "") + conn = get(conn, ~p"/secret/the-secret/opml/feed" <> ".xml") + assert conn.status == 401 + end + + test "unauthorized when secret incorrect", %{conn: conn} do + Application.put_env(:pinchflat, :route_secret, "test-secret") + conn = get(conn, ~p"/secret/invalid-secret/opml/feed" <> ".xml") + assert conn.status == 401 + end + test "renders the XML document", %{conn: conn} do source = source_fixture() - - conn = get(conn, ~p"/sources/opml" <> ".xml") + Application.put_env(:pinchflat, :route_secret, "test-secret") + conn = get(conn, ~p"/secret/test-secret/opml/feed" <> ".xml") assert conn.status == 200 assert {"content-type", "application/opml+xml; charset=utf-8"} in conn.resp_headers