diff --git a/test/music_library_web/controllers/last_fm_controller_test.exs b/test/music_library_web/controllers/last_fm_controller_test.exs new file mode 100644 index 00000000..5cc109e9 --- /dev/null +++ b/test/music_library_web/controllers/last_fm_controller_test.exs @@ -0,0 +1,46 @@ +defmodule MusicLibraryWeb.LastFmControllerTest do + use MusicLibraryWeb.ConnCase + + describe "GET /auth/last_fm/callback" do + @tag :logged_out + test "on success, stores session key and redirects with flash", %{conn: conn} do + session_xml = """ + + + + testuser + test-session-key + 0 + + + """ + + Req.Test.stub(LastFm.API, fn conn -> + Req.Test.text(conn, session_xml) + end) + + conn = get(conn, ~p"/auth/last_fm/callback", %{"token" => "test-token"}) + + assert redirected_to(conn) == "/" + assert conn.assigns.flash["info"] =~ "Successfully connected" + end + + @tag :logged_out + @tag :capture_log + test "on error, redirects with error flash", %{conn: conn} do + error_json = %{ + "error" => 4, + "message" => "Invalid authentication token" + } + + Req.Test.stub(LastFm.API, fn conn -> + Req.Test.json(conn, error_json) + end) + + conn = get(conn, ~p"/auth/last_fm/callback", %{"token" => "bad-token"}) + + assert redirected_to(conn) == "/" + assert conn.assigns.flash["error"] =~ "Failed to connect" + end + end +end