Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Reorganized tests
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun committed Feb 23, 2017
1 parent f3668ca commit 6593859
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 116 deletions.
2 changes: 1 addition & 1 deletion installer/integration_test/openmaize_phoenixauth_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function edit_mix_config {
sed -i 's/username: "postgres"/username: "dev"/g' config/dev.exs config/test.exs
sed -i 's/password: "postgres"/password: System.get_env("POSTGRES_PASS")/g' config/dev.exs config/test.exs
sed -i 's/:postgrex]/:postgrex, :openmaize]/g' mix.exs
sed -i 's/{:postgrex, ">= 0.0.0"},/{:postgrex, ">= 0.0.0"},\n {:openmaize, "~> 2.6"},/g' mix.exs
sed -i 's/{:postgrex, ">= 0.0.0"},/{:postgrex, ">= 0.0.0"},\n {:openmaize, "~> 3.0"},/g' mix.exs
mix deps.get
}

Expand Down
3 changes: 2 additions & 1 deletion lib/openmaize/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ defmodule Openmaize.Log do
def current_user_id(%{current_user: %{id: id}}), do: "#{id}"
def current_user_id(_), do: "nil"

defp format({key, val}) do
defp format({key, val}) when is_binary(val) do
if String.contains?(val, [" ", "="]) do
~s(#{key}="#{val}")
else
~s(#{key}=#{val})
end
end
defp format({key, val}), do: format({key, to_string(val)})
end
27 changes: 16 additions & 11 deletions test/authenticate_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
defmodule Openmaize.AuthenticateTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

alias Openmaize.{Authenticate, SessionHelper, TestRepo, TestUser}
alias Openmaize.{Authenticate, SessionHelper, TestRepo, TestUser, UserHelpers}

setup do
{:ok, user} = UserHelpers.add_confirmed()
{:ok, %{user: user}}
end

def call(id) do
conn(:get, "/")
Expand All @@ -11,29 +16,29 @@ defmodule Openmaize.AuthenticateTest do
|> Authenticate.call({TestRepo, TestUser})
end

test "current user in session" do
conn = call(1)
test "current user in session", %{user: user} do
conn = call(user.id)
%{current_user: user} = conn.assigns
assert user.username == "fred"
assert user.username == "ray"
assert user.role == "user"
end

test "no user found" do
conn = call(1000)
test "no user found", %{user: user} do
conn = call(user.id + 1)
assert conn.assigns == %{current_user: nil}
end

test "user removed from session" do
conn = call(1) |> configure_session(drop: true)
test "user removed from session", %{user: user} do
conn = call(user.id) |> configure_session(drop: true)
newconn = conn(:get, "/")
|> recycle_cookies(conn)
|> SessionHelper.sign_conn
|> Authenticate.call({TestRepo, TestUser})
assert newconn.assigns == %{current_user: nil}
end

test "output to current_user does not contain password_hash or otp_secret" do
conn = call(1)
test "output to current_user does not contain password_hash or otp_secret", %{user: user} do
conn = call(user.id)
%{current_user: user} = conn.assigns
refute Map.has_key?(user, :password_hash)
refute Map.has_key?(user, :otp_secret)
Expand Down
9 changes: 3 additions & 6 deletions test/confirm_email_test.exs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
defmodule Openmaize.ConfirmEmailTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

import Ecto.Changeset
alias Openmaize.{ConfirmEmail, TestRepo, TestUser}
alias Openmaize.{ConfirmEmail, TestRepo, TestUser, UserHelpers}

@valid_link "email=fred%2B1%40mail.com&key=lg8UXGNMpb5LUGEDm62PrwW8c20qZmIw"
@invalid_link "email=wrong%40mail.com&key=lg8UXGNMpb5LUGEDm62PrwW8c20qZmIw"
@incomplete_link "email=wrong%40mail.com"

setup do
{:ok, _user} = TestRepo.get_by(TestUser, email: "[email protected]")
|> change(%{confirmed_at: nil})
|> Openmaize.TestRepo.update
UserHelpers.add_user()
:ok
end

Expand Down
26 changes: 16 additions & 10 deletions test/login_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
defmodule Openmaize.LoginTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

alias Openmaize.{DummyCrypto, TestRepo, TestUser}
alias Openmaize.{DummyCrypto, TestRepo, TestUser, UserHelpers}

setup do
{:ok, _} = UserHelpers.add_user()
{:ok, _} = UserHelpers.add_confirmed()
:ok
end

def login(name, password, uniq \\ :email, user_params \\ "email") do
conn(:post, "/login",
Expand All @@ -20,14 +26,14 @@ defmodule Openmaize.LoginTest do

test "login succeeds with username" do
conn = login("ray", "h4rd2gU3$$", :username, "username")
%{id: id} = conn.private[:openmaize_user]
assert id == 4
%{username: username} = conn.private[:openmaize_user]
assert username == "ray"
end

test "login succeeds with email" do
conn = login("[email protected]", "h4rd2gU3$$")
%{id: id} = conn.private[:openmaize_user]
assert id == 4
%{email: email} = conn.private[:openmaize_user]
assert email == "[email protected]"
end

test "login fails when crypto mod changes" do
Expand Down Expand Up @@ -60,14 +66,14 @@ defmodule Openmaize.LoginTest do

test "function unique_id with email" do
conn = login("[email protected]", "h4rd2gU3$$", &phone_name/1, "email")
%{id: id} = conn.private[:openmaize_user]
assert id == 4
%{email: email} = conn.private[:openmaize_user]
assert email == "[email protected]"
end

test "function unique_id with phone" do
conn = login("081555555", "h4rd2gU3$$", &phone_name/1, "email")
%{id: id} = conn.private[:openmaize_user]
assert id == 4
%{email: email} = conn.private[:openmaize_user]
assert email == "[email protected]"
end

test "output to current_user does not contain password_hash or otp_secret" do
Expand Down
48 changes: 24 additions & 24 deletions test/onetime_pass_test.exs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
defmodule Openmaize.OnetimePassTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

import Ecto.Changeset
alias Comeonin.Otp
alias Openmaize.{OnetimePass, TestRepo, TestUser}
alias Openmaize.{OnetimePass, TestRepo, TestUser, UserHelpers}

setup context do
{:ok, %{id: user_id}} = UserHelpers.add_otp_user()
otp_last = context[:last] || 0
update_repo(5, otp_last)
on_exit fn -> update_repo(5, 0) end
:ok
update_repo(user_id, otp_last)
{:ok, %{user_id: user_id}}
end

def call(user, opts) do
Expand All @@ -28,64 +28,64 @@ defmodule Openmaize.OnetimePassTest do
assert OnetimePass.init([]) == {Openmaize.Repo, Openmaize.User, []}
end

test "check hotp with default options" do
user = %{"hotp" => "816065", "id" => "5"}
test "check hotp with default options", %{user_id: user_id} do
user = %{"hotp" => "816065", "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
%{id: id, otp_last: otp_last} = conn.private[:openmaize_user]
assert id == 5
assert id == user_id
assert otp_last == 2
refute conn.private[:openmaize_error]
fail = %{"hotp" => "816066", "id" => "5"}
fail = %{"hotp" => "816066", "id" => user_id}
conn = call(fail, {TestRepo, TestUser, []})
assert conn.private[:openmaize_error]
end

@tag last: 18
test "check hotp with updated last" do
user = %{"hotp" => "088239", "id" => "5"}
test "check hotp with updated last", %{user_id: user_id} do
user = %{"hotp" => "088239", "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
%{id: id, otp_last: otp_last} = conn.private[:openmaize_user]
assert id == 5
assert id == user_id
assert otp_last == 19
assert conn.private[:openmaize_user]
refute conn.private[:openmaize_error]
fail = %{"hotp" => "088238", "id" => "5"}
fail = %{"hotp" => "088238", "id" => user_id}
conn = call(fail, {TestRepo, TestUser, []})
assert conn.private[:openmaize_error]
end

test "check totp with default options" do
test "check totp with default options", %{user_id: user_id} do
token = Otp.gen_totp("MFRGGZDFMZTWQ2LK")
user = %{"totp" => token, "id" => "5"}
user = %{"totp" => token, "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
assert conn.private[:openmaize_user]
refute conn.private[:openmaize_error]
end

test "disallow totp check with same token" do
test "disallow totp check with same token", %{user_id: user_id} do
token = Otp.gen_totp("MFRGGZDFMZTWQ2LK")
user = %{"totp" => token, "id" => "5"}
user = %{"totp" => token, "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
%{otp_last: otp_last} = conn.private[:openmaize_user]
update_repo(5, otp_last)
update_repo(user_id, otp_last)
conn = call(user, {TestRepo, TestUser, []})
assert conn.private[:openmaize_error]
end

test "disallow totp check with earlier token that is still valid" do
test "disallow totp check with earlier token that is still valid", %{user_id: user_id} do
token = Otp.gen_totp("MFRGGZDFMZTWQ2LK")
user = %{"totp" => token, "id" => "5"}
user = %{"totp" => token, "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
%{otp_last: otp_last} = conn.private[:openmaize_user]
update_repo(5, otp_last)
update_repo(user_id, otp_last)
new_token = Otp.gen_hotp("MFRGGZDFMZTWQ2LK", otp_last - 1)
user = %{"totp" => new_token, "id" => "5"}
user = %{"totp" => new_token, "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
assert conn.private[:openmaize_error]
end

test "output to current_user does not contain password_hash or otp_secret" do
user = %{"hotp" => "816065", "id" => "5"}
test "output to current_user does not contain password_hash or otp_secret", %{user_id: user_id} do
user = %{"hotp" => "816065", "id" => user_id}
conn = call(user, {TestRepo, TestUser, []})
user = conn.private[:openmaize_user]
refute Map.has_key?(user, :password_hash)
Expand Down
22 changes: 11 additions & 11 deletions test/remember_test.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
defmodule Openmaize.RememberTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

alias Openmaize.{Authenticate, Remember, SessionHelper, TestRepo, TestUser}
alias Openmaize.{Authenticate, Remember, SessionHelper, TestRepo, TestUser, UserHelpers}

setup do
{:ok, user} = UserHelpers.add_user()
{:ok, other} = UserHelpers.add_confirmed()
conn = conn(:get, "/")
|> SessionHelper.sign_conn
|> Remember.add_cookie("1")
|> Remember.add_cookie(user.id)

newconn = conn(:get, "/")
|> recycle_cookies(conn)
|> SessionHelper.sign_conn

{:ok, %{conn: conn, newconn: newconn}}
{:ok, %{conn: conn, newconn: newconn, other: other}}
end

test "init function" do
Expand All @@ -23,7 +25,6 @@ defmodule Openmaize.RememberTest do
test "call remember with default options", %{newconn: newconn} do
newconn = Remember.call(newconn, {TestRepo, TestUser})
%{current_user: user} = newconn.assigns
assert user.id == 1
assert user.username == "fred"
assert user.role == "user"
end
Expand All @@ -47,21 +48,20 @@ defmodule Openmaize.RememberTest do
refute conn.assigns[:current_user]
end

test "call remember with current_user already set", %{newconn: newconn} do
test "call remember with current_user already set", %{newconn: newconn, other: other} do
newconn = newconn
|> put_session(:user_id, 2)
|> put_session(:user_id, other.id)
|> Authenticate.call({TestRepo, TestUser})
|> Remember.call({TestRepo, TestUser})
%{current_user: user} = newconn.assigns
assert user.id == 2
assert user.username == "dim"
assert user.role == "user"
assert user.id == other.id
assert user.email == other.email
end

test "add cookie", %{conn: conn} do
remember = conn.resp_cookies["remember_me"]
assert remember.max_age == 604_800
assert remember.value == "SFMyNTY.MQ.yX9edPVZtRiJwMsoARY8QJqXfKnQpicssKlqGPjtoUw"
assert remember.value =~ "SFMyNTY"
end

test "output to current_user does not contain password_hash or otp_secret" , %{newconn: newconn} do
Expand Down
15 changes: 6 additions & 9 deletions test/reset_password_test.exs
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
defmodule Openmaize.ResetPasswordTest do
use ExUnit.Case
use Openmaize.TestCase
use Plug.Test

import Ecto.Changeset
alias Comeonin.Bcrypt
alias Openmaize.{ResetPassword, TestRepo, TestUser}
alias Openmaize.{ResetPassword, TestRepo, TestUser, UserHelpers}

setup do
{:ok, _user} = TestRepo.get_by(TestUser, email: "[email protected]")
|> change(%{reset_token: "lg8UXGNMpb5LUGEDm62PrwW8c20qZmIw",
reset_sent_at: Ecto.DateTime.utc})
|> Openmaize.TestRepo.update
UserHelpers.add_reset_user("lg8UXGNMpb5LUGEDm62PrwW8c20qZmIw")
:ok
end

def call_reset(password, opts) do
conn(:post, "/password_reset",
%{"password_reset" => %{"email" => "dim@mail.com",
%{"password_reset" => %{"email" => "frank@mail.com",
"key" => "lg8UXGNMpb5LUGEDm62PrwW8c20qZmIw",
"password" => password}})
|> ResetPassword.call(opts)
end

def password_changed(password) do
user = TestRepo.get_by(TestUser, email: "dim@mail.com")
user = TestRepo.get_by(TestUser, email: "frank@mail.com")
Bcrypt.checkpw(password, user.password_hash)
end

Expand All @@ -47,7 +44,7 @@ defmodule Openmaize.ResetPasswordTest do
end

test "reset password fails when reset_sent_at is nil" do
user = TestRepo.get_by(TestUser, email: "dim@mail.com")
user = TestRepo.get_by(TestUser, email: "frank@mail.com")
change(user, %{reset_sent_at: nil})
|> Openmaize.TestRepo.update
conn = call_reset("password", {TestRepo, TestUser, {120, &IO.puts/1}})
Expand Down
Loading

0 comments on commit 6593859

Please sign in to comment.