Ho un test che deve impostare user_id alla sessione prima del test, perché questa azione deve conoscere l'utente corrente.Come posso impostare la sessione in configurazione quando eseguo l'azione phoenix che richiede user_id in sessione?
setup do
%User{
id: 123456,
username: "lcp",
email: "[email protected]",
password: Comeonin.Bcrypt.hashpwsalt("password")
} |> Repo.insert
{:ok, user: Repo.get(User, 123456) }
end
test "POST /posts", context do
# conn = conn()
# |> put_session(:user_id, context[:user].id)
# |> post("/posts", %{ post: %{ title: "title", body: "body" } })
# assert get_flash(conn, :info) == "Post created successfully."
# updated to =>
conn = conn()
|> Map.put(:secret_key_base, String.duplicate("abcdefgh", 8))
|> Plug.Session.call(@session)
|> Plug.Conn.fetch_session
|> put_session(:user_id, context[:user].id)
|> post("/posts", %{ post: %{ title: "title", body: "body" } })
assert get_flash(conn, :info) == "Post created successfully."
end
Ho provato questo codice, ma si dice che session not fetched, call fetch_session/2
.
web/controller/controller_helper.ex
defmodule SimpleBlog.ControllerHelpers do
alias Phoenix.Controller
alias Plug.Conn
alias SimpleBlog.Router.Helpers
def authenticate(conn, _) do
case Conn.get_session(conn, :user_id) do
nil ->
unauthorized(conn)
user_id ->
case SimpleBlog.Repo.get(SimpleBlog.User, user_id) do
{:ok, user} ->
Conn.assign(conn, :current_user, user)
nil ->
unauthorized(conn)
end
end
end
def unauthorized(conn) do
conn
|> Controller.put_flash(:error, "You must be logged in")
|> Controller.redirect(to: Helpers.session_path(conn, :new))
|> Conn.halt
end
end
Aggiornato
ottengo nulla quando ottengo user_id dalla sessione attraverso Conn.get_session(conn, :user_id)
.
Ecco regolatore postale web/controller/post_controller.ex
defmodule SimpleBlog.PostController do
use SimpleBlog.Web, :controller
import SimpleBlog.ControllerHelpers
alias SimpleBlog.Post
plug :authenticate when not action in [:new]
def create(conn, %{ "post" => post_params }) do
changeset = Post.changeset(%Post{}, post_params)
case Repo.insert(changeset) do
{:ok, _post} ->
conn
|> put_flash(:info, "Post created successfully.")
|> redirect(to: post_path(conn, :new))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
end
E 'il mio file di prova.
defmodule SimpleBlog.PostControllerTest do
use SimpleBlog.ConnCase
alias SimpleBlog.Repo
alias SimpleBlog.User
@session Plug.Session.init(
store: :cookie,
key: "_app",
encryption_salt: "yadayada",
signing_salt: "yadayada"
)
setup do
%User{
id: 123456,
username: "lcp",
email: "[email protected]",
password: Comeonin.Bcrypt.hashpwsalt("password")
} |> Repo.insert
{:ok, user: Repo.get(User, 123456) }
end
@tag timeout: 900000
test "POST /posts", context do
conn = conn()
|> Map.put(:secret_key_base, String.duplicate("abcdefgh", 8))
|> Plug.Session.call(@session)
|> Plug.Conn.fetch_session
|> put_session(:user_id, context[:user].id)
|> post("/posts", %{ post: %{ title: "title", body: "body" } })
assert get_flash(conn, :info) == "Post created successfully."
end
end
aggiornamento ..
lib/simple_blog/candele/authenticated.ex
definisco una spina autenticato
defmodule SimpleBlog.Plugs.Authenticated do
import Plug.Conn
alias Phoenix.Controller
alias SimpleBlog.Router.Helpers
alias SimpleBlog.User
def init(options) do
options
end
def call(conn, _) do
case conn |> current_user_id do
nil ->
conn
|> Controller.put_flash(:error, "You must be logged in")
|> Controller.redirect(to: Helpers.session_path(conn, :new))
|> halt
current_user_id ->
conn |> assign(:current_user, SimpleBlog.Repo.get(User, current_user_id))
end
end
defp current_user_id(conn) do
case Mix.env do
:test ->
conn.private[:authenticated_current_user_id]
_ ->
conn |> fetch_session |> get_session(:current_user_id)
end
end
end
nella mia prova
conn = conn()
|> put_private(:authenticated_current_user_id, context[:user].id)
|> post("/posts", %{ post: %{ title: "title", body: "body" } })
assert get_flash(conn, :info) == "Post created successfully."
ora, il test è superato.
Ho provato, ma non posso ottenere user_id dalla sessione. – LcpMarvel
'Plug.Conn.get_session (conn,: user_id)' – LcpMarvel
http://hexdocs.pm/plug/Plug.Conn.html#get_session/2 - il codice è corretto. Potresti pubblicare più del tuo codice per vedere qual è il problema? – Gazler