Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Authentication 通过Guardian.Plug.EnsureAuthenticated的测试使用LDAP进行身份验证_Authentication_Elixir_Phoenix Framework_Ex Unit - Fatal编程技术网

Authentication 通过Guardian.Plug.EnsureAuthenticated的测试使用LDAP进行身份验证

Authentication 通过Guardian.Plug.EnsureAuthenticated的测试使用LDAP进行身份验证,authentication,elixir,phoenix-framework,ex-unit,Authentication,Elixir,Phoenix Framework,Ex Unit,我按照指南在Phoenix与Guardian一起设置LDAP身份验证。我对菲尼克斯和长生不老药还比较陌生,所以我正在经历设置和测试的过程 我的所有工作都按照指南进行,但是,在编写控制器测试时,我不知道如何通过Guardian.Plug.ensureAuthentication。我跟随了几位在这里找到的向导,但似乎没有任何效果 是否有人使用exLDAP和Guardian设置LDAP身份验证,并为测试提供适当的用户登录,以提供一些指导?任何帮助都将不胜感激 以下是我的设置: lib/ldap\u示例

我按照指南在Phoenix与Guardian一起设置LDAP身份验证。我对菲尼克斯和长生不老药还比较陌生,所以我正在经历设置和测试的过程

我的所有工作都按照指南进行,但是,在编写控制器测试时,我不知道如何通过Guardian.Plug.ensureAuthentication。我跟随了几位在这里找到的向导,但似乎没有任何效果

是否有人使用exLDAP和Guardian设置LDAP身份验证,并为测试提供适当的用户登录,以提供一些指导?任何帮助都将不胜感激

以下是我的设置:

lib/ldap\u示例/guardian\u serializer.ex

defmodule LdapExample.GuardianSerializer do
 @behaviour Guardian.Serializer
 alias LdapExample.User
 alias LdapExample.Repo

 def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
 def for_token(_), do: { :error, "Unknown resource type" }

 def from_token("User:" <> id), do: { :ok, Repo.get(User, id) }
 def from_token(_), do: { :error, "Unknown resource type" }
end
web/controllers/session_controller.ex

defmodule LdapExample.SessionController do
  use LdapExample.Web, :controller
  alias LdapExample.{User, Repo, Ldap}

  def new(conn, _params) do
    render conn, "new.html", changeset: User.login_changeset
  end



def create(conn, %{"user" => params}) do
    username = params["username"]
    password = params["password"]
    case Ldap.authenticate(username, password) do
      :ok -> handle_sign_in(conn, username)
      _   -> handle_error(conn)
    end
  end

defp handle_sign_in(conn, username) do
  {:ok, user} = insert_or_update_user(username)
  conn
  |> put_flash(:info, "Logged in.")
  |> Guardian.Plug.sign_in(user)
  |> redirect(to: page_path(conn, :index))
end

defp insert_or_update_user(username) do
  {:ok, ldap_entry} = Ldap.get_by_uid(username)
  user_attributes = Ldap.to_map(ldap_entry)
  user = Repo.get_by(User, username: username)
  changeset =
    case user do
      nil -> User.changeset(%User{}, user_attributes)
      _ -> User.changeset(user, user_attributes)
    end
  Repo.insert_or_update changeset
end

defp handle_error(conn) do
  conn
  |> put_flash(:error, "Wrong username or password")
  |> redirect(to: session_path(conn, :new))
end

def delete(conn, _params) do
  Guardian.Plug.sign_out(conn)
  |> put_flash(:info, "Logged out successfully.")
  |> redirect(to: "/")
end
结束

web/model/user.ex

defmodule LdapExample.User do
 use LdapExample.Web, :model

 schema "users" do
  field :username, :string
  field :name, :string
  field :email, :string
  field :password, :string, virtual: true
  timestamps()
end



def changeset(struct, params \\ %{}) do
    struct
    |> cast(params, [:username, :name, :email])
    |> validate_required([:username, :name, :email])
  end



def login_changeset do
    %__MODULE__{} |> cast(%{}, ~w(username password), ~w())
  end
end
我创建了一个session_controller_text.ex,如下所示:

defmodule LdapExample.SessionControllerTest do
  use LdapExample.ConnCase
  alias LdapExample.User



test "Get to Login page", %{conn: conn} do
    conn = get conn, session_path(conn, :new)
    assert html_response(conn, 200) =~ "Sign In"
  end

test "shows page only when logged in", %{conn: conn} do
    conn = get conn, page_path(conn, :index)
    assert html_response(conn, 200) =~ "Hello"
  end
end

最后一次测试失败,因为它将我重定向到登录页面。

当然,出于测试目的,您应该使用
绕过\u通过
跳过身份验证过程。阅读更多信息。

谢谢,但是,我更想了解如何模拟实际登录以进行测试。尝试模拟负责与LDAP连接的模块,执行类似于此处介绍的操作:以一种迂回的方式回答我的问题。非常感谢。
defmodule LdapExample.SessionControllerTest do
  use LdapExample.ConnCase
  alias LdapExample.User



test "Get to Login page", %{conn: conn} do
    conn = get conn, session_path(conn, :new)
    assert html_response(conn, 200) =~ "Sign In"
  end

test "shows page only when logged in", %{conn: conn} do
    conn = get conn, page_path(conn, :index)
    assert html_response(conn, 200) =~ "Hello"
  end
end