Packages
A web application in pursuit of meaning in life.
Current section
Files
Jump to
Current section
Files
test/controllers/auth_test.exs
###############################################################################
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# https://raw.githubusercontent.com/udia-software/udia/master/LICENSE.
# The License is based on the Mozilla Public License Version 1.1, but
# Sections 14 and 15 have been added to cover use of software over a computer
# network and provide for limited attribution for the Original Developer.
# In addition, Exhibit A has been modified to be consistent with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is UDIA.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is Udia Software Incorporated.
#
# All portions of the code written by UDIA are Copyright (c) 2016-2017
# Udia Software Incorporated. All Rights Reserved.
###############################################################################
defmodule Udia.AuthTest do
use Udia.Web.ConnCase
alias Udia.Web.Auth
setup %{conn: conn} do
conn =
conn
|> bypass_through(Udia.Web.Router, :browser)
|> get("/")
{:ok, %{conn: conn}}
end
test "authenticate_user halts when no current_user exists", %{conn: conn} do
conn = Auth.authenticate_user(conn, [])
assert conn.halted
end
test "authenticate_user continues when the current_user exists", %{conn: conn} do
conn =
conn
|> assign(:current_user, %Udia.User{})
|> Auth.authenticate_user([])
refute conn.halted
end
test "login puts the user in the session", %{conn: conn} do
login_conn =
conn
|> Auth.login(%Udia.User{id: 123})
|> send_resp(:ok, "")
next_conn = get(login_conn, "/")
assert get_session(next_conn, :user_id) == 123
end
test "logout drops the session", %{conn: conn} do
logout_conn =
conn
|> put_session(:user_id, 123)
|> Auth.logout()
|> send_resp(:ok, "")
next_conn = get(logout_conn, "/")
refute get_session(next_conn, :user_id)
end
test "call places user from session into assigns", %{conn: conn} do
user = insert_user()
conn =
conn
|> put_session(:user_id, user.id)
|> Auth.call(Repo)
assert conn.assigns.current_user.id == user.id
end
test "call with no session sets current_user assign to nil", %{conn: conn} do
conn = Auth.call(conn, Repo)
assert conn.assigns.current_user == nil
end
test "login with a valid username and pass", %{conn: conn} do
user = insert_user(username: "me", password: "secret")
{:ok, conn} = Auth.login_by_username_and_pass(conn, "me", "secret", repo: Repo)
assert conn.assigns.current_user.id == user.id
end
test "login with a not found user", %{conn: conn} do
assert {:error, :not_found, _conn} = Auth.login_by_username_and_pass(conn, "me", "secret", repo: Repo)
end
test "login with password mismatch", %{conn: conn} do
_ = insert_user(username: "me", password: "secret")
assert {:error, :unauthorized, _conn} = Auth.login_by_username_and_pass(conn, "me", "wrong", repo: Repo)
end
end