I’ve added Pow into my project for authentication. Now I’m trying to update the tests Phoenix generated when I created my contexts.
I’ve added all my resource routes to the protected
pipeline.
I used Pow.Plug.assign_current_user/3
to add a current user to my conn
.
test "redirects to show when data is valid", %{conn: conn} do
user = %Golf.Accounts.User{email: "test@example.com", id: 1}
conn = Pow.Plug.assign_current_user(conn, user, [])
conn = post(conn, Routes.course_path(conn, :create), course: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.course_path(conn, :show, id)
conn = get(conn, Routes.course_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Course"
end
My assertions about the create
action all pass.
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.course_path(conn, :show, id)
However, the final show
action
conn = get(conn, Routes.course_path(conn, :show, id))
assert html_response(conn, 200) =~ "Show Course"
fails because it gets redirected to the new_session
path:
** (RuntimeError) expected response with status 200, got: 302, with body:
<html><body>You are being <a href="/session/new?request_path=%2Fcourses">redirected</a>.</body></html>
If I reuse the original conn
after I assign the current user, but before the create action, my tests pass.
test "redirects to show when data is valid", %{conn: conn} do
user = %Golf.Accounts.User{email: "test@example.com", id: 1}
new_conn = Pow.Plug.assign_current_user(conn, user, [])
conn = post(new_conn, Routes.course_path(new_conn, :create), course: @create_attrs)
assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.course_path(conn, :show, id)
conn = get(new_conn, Routes.course_path(new_conn, :show, id))
assert html_response(conn, 200) =~ "Show Course"
end
Can someone help me out with how I need to update my tests?
Thanks!
Axel