Skip to content

Commit

Permalink
Improve user login test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rjsamson committed Jun 28, 2016
1 parent 853ce15 commit 6d68917
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions test/controllers/console_controller_test.exs
@@ -1,29 +1,43 @@
defmodule Stackfooter.ConsoleControllerTest do
use Stackfooter.ConnCase
alias Stackfooter.UserAuth

@apikey "4cy7uf63Lw2Sx6652YmLwBKy662weU4q"

@session Plug.Session.init(
store: :cookie,
key: "_app",
encryption_salt: "yadayada",
signing_salt: "yadayada"
)

setup config do
reset_api_keys()

if username = config[:login_as] do
user = insert_user(username: username, api_keys: [@apikey])
conn = assign(build_conn(), :current_user, user)
cond do
config[:no_login] ->
{:ok, conn: build_conn()}
%{username: username, password: password} = config[:login_as] ->
user = insert_user(username: username, api_keys: [@apikey], password: password)

conn = Plug.Session.call(build_conn(), @session) |> fetch_session()
{:ok, conn} = UserAuth.login_by_username_and_pass(conn, username, password, repo: Repo)

{:ok, conn: conn, user: user}
else
{:ok, conn: build_conn()}
{:ok, conn: conn, user: user}
true ->
{:ok, conn: build_conn()}
end
end

@tag login_as: "rjsamson1234"
test "Trade path is authenticated", %{conn: conn, user: _user} do
@tag no_login: true
test "Redirects if not authenticated", %{conn: conn} do
conn = get(conn, "/console")
assert html_response(conn, 200) =~ "rjsamson1234"
assert redirected_to(conn) == session_path(conn, :new)
end

test "Redirects if not authenticated", %{conn: conn} do
@tag login_as: %{username: "rjsamson1234", password: "password"}
test "Trade path is authenticated", %{conn: conn, user: _user} do
conn = get(conn, "/console")
assert redirected_to(conn) == session_path(conn, :new)
assert html_response(conn, 200) =~ "rjsamson1234"
end
end

0 comments on commit 6d68917

Please sign in to comment.