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

Commit

Permalink
Upgrading to version 3
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun committed Feb 13, 2017
1 parent 5fdeee0 commit f271544
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v3.0.0

* Changes
* Changed default `unique_id` for Openmaize.Login and the installer to `:email`

## v2.9.0

* Enhancements
Expand Down
Binary file added installer/archives/openmaize_phx-3.0.0-dev.ez
Binary file not shown.
2 changes: 1 addition & 1 deletion installer/phoenix/lib/openmaize_phx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ defmodule Mix.Tasks.Openmaize.Phx do
We are almost ready!
Now edit the `mix.exs` file, adding `:openmaize` to the list of
`applications` and `{:openmaize, "~> 2.7"},` to the deps.
`applications` and `{:openmaize, "~> 3.0"},` to the deps.
Then run `mix deps.get`.
You will probably need to edit the database username and password
Expand Down
5 changes: 1 addition & 4 deletions installer/phoenix/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ defmodule Openmaize.Phx.Mixfile do

def project do
[app: :openmaize_phx,
version: "2.8.0",
version: "3.0.0-dev",
elixir: "~> 1.3"]
end

# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: []]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule <%= base %>.SessionControllerTest do

import <%= base %>.TestHelpers

@valid_attrs %{username: "robin", password: "mangoes&g0oseberries"}
@invalid_attrs %{username: "robin", password: "maaaangoes&g00zeberries"}
@valid_attrs %{email: "robin@mail.com", password: "mangoes&g0oseberries"}
@invalid_attrs %{email: "robin@mail.com", password: "maaaangoes&g00zeberries"}

setup %{conn: conn} do<%= if confirm do %>
add_user("arthur")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule <%= base %>.SessionControllerTest do

import <%= base %>.TestHelpers

@valid_attrs %{username: "robin", password: "mangoes&g0oseberries"}
@invalid_attrs %{username: "robin", password: "maaaangoes&g00zeberries"}
@valid_attrs %{email: "robin@mail.com", password: "mangoes&g0oseberries"}
@invalid_attrs %{email: "robin@mail.com", password: "maaaangoes&g00zeberries"}

setup %{conn: conn} do
conn = conn |> bypass_through(<%= base %>.Router, :browser) |> get("/")<%= if confirm do %>
Expand Down
6 changes: 3 additions & 3 deletions installer/phoenix/templates/phx_html/session_new.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<% end %>

<div class="form-group">
<%= label f, :username, "username", class: "control-label" %>
<%= text_input f, :username, class: "form-control" %>
<%= error_tag f, :username %>
<%= label f, :email, "email", class: "control-label" %>
<%= text_input f, :email, class: "form-control" %>
<%= error_tag f, :email %>
</div>

<div class="form-group">
Expand Down
4 changes: 2 additions & 2 deletions installer/phoenix/templates/user_migration.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ defmodule <%= base %>.Repo.Migrations.CreateUser do

def change do
create table(:users) do
add :username, :string
add :email, :string
add :username, :string
add :password_hash, :string<%= if confirm do %>
add :confirmed_at, :utc_datetime
add :confirmation_token, :string
Expand All @@ -15,6 +15,6 @@ defmodule <%= base %>.Repo.Migrations.CreateUser do
timestamps()
end

create unique_index :users, [:username]
create unique_index :users, [:email]
end
end
4 changes: 2 additions & 2 deletions installer/phoenix/templates/user_model.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ defmodule <%= base %>.User do
alias Openmaize.Database, as: DB

schema "users" do
field :username, :string
field :email, :string
field :username, :string
field :password, :string, virtual: true
field :password_hash, :string<%= if confirm do %>
field :confirmed_at, Ecto.DateTime
Expand All @@ -24,7 +24,7 @@ defmodule <%= base %>.User do
struct
|> cast(params, [:username, :email])
|> validate_required([:username, :email])
|> unique_constraint(:username)
|> unique_constraint(:email)
end<%= if confirm do %>

def auth_changeset(struct, params, key) do<% else %>
Expand Down
2 changes: 1 addition & 1 deletion installer/phoenix/templates/user_model_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule <%= base %>.UserTest do

alias <%= base %>.User

@valid_attrs %{username: "fred", email: "fred@mail.com", password: "mangoes&gooseberries"}
@valid_attrs %{email: "fred@mail.com", username: "fred", password: "mangoes&gooseberries"}
@invalid_attrs %{email: "[email protected]", password: "mangoes&gooseberries"}

test "changeset with valid attributes" do
Expand Down
2 changes: 1 addition & 1 deletion installer/phoenix/test/openmaize_phx_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Mix.Tasks.Openmaize.PhxTest do
end

assert_received {:mix_shell, :info, ["\nWe are almost ready!" <> _ = message]}
assert message =~ ~s({:openmaize, "~> 2.7"})
assert message =~ ~s({:openmaize, "~> 3.0"})
assert message =~ ~s(And to start the server)
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/openmaize/login.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Openmaize.Login do
There are three options:
* unique_id - the name which is used to identify the user (in the database)
* the default is `:username`
* the default is `:email`
* this can also be a function - see below for an example
* repo - the name of the repo
* the default is MyApp.Repo - using the name of the project
Expand All @@ -31,10 +31,10 @@ defmodule Openmaize.Login do
The following example is a function that takes the user parameters as
input and searches for the user by phone number if the input is all digits,
but username otherwise.
but email otherwise.
def phone_name(%{"username" => username, "password" => password}) do
{Regex.match?(~r/^[0-9]+$/, username) and :phone || :username, username, password}
def phone_name(%{"email" => email, "password" => password}) do
{Regex.match?(~r/^[0-9]+$/, email) and :phone || :email, email, password}
end
To use this function, add the following to the session controller:
Expand All @@ -50,8 +50,8 @@ defmodule Openmaize.Login do

@doc false
def init(opts) do
uniq = Keyword.get(opts, :unique_id, :username)
user_params = if is_atom(uniq), do: to_string(uniq), else: "username"
uniq = Keyword.get(opts, :unique_id, :email)
user_params = if is_atom(uniq), do: to_string(uniq), else: "email"
{uniq, user_params,
Keyword.get(opts, :repo, Openmaize.Utils.default_repo),
Keyword.get(opts, :user_model, Openmaize.Utils.default_user_model)}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Openmaize.Mixfile do
use Mix.Project

@version "2.9.0"
@version "3.0.0-dev"

@description """
Authentication library for Elixir using Plug
Expand Down
26 changes: 13 additions & 13 deletions test/login_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ defmodule Openmaize.LoginTest do

alias Openmaize.{DummyCrypto, TestRepo, TestUser}

def login(name, password, uniq \\ :username, user_params \\ "username") do
def login(name, password, uniq \\ :email, user_params \\ "email") do
conn(:post, "/login",
%{"session" => %{user_params => name, "password" => password}})
|> Openmaize.Login.call({uniq, user_params, TestRepo, TestUser})
end

def phone_name(%{"username" => username, "password" => password}) do
{Regex.match?(~r/^[0-9]+$/, username) and :phone || :username, username, password}
def phone_name(%{"email" => email, "password" => password}) do
{Regex.match?(~r/^[0-9]+$/, email) and :phone || :email, email, password}
end

test "init function" do
assert Openmaize.Login.init([]) == {:username, "username", Openmaize.Repo, Openmaize.User}
assert Openmaize.Login.init([]) == {:email, "email", Openmaize.Repo, Openmaize.User}
end

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

test "login succeeds with email" do
conn = login("[email protected]", "h4rd2gU3$$", :email, "email")
conn = login("[email protected]", "h4rd2gU3$$")
%{id: id} = conn.private[:openmaize_user]
assert id == 4
end
Expand All @@ -44,34 +44,34 @@ defmodule Openmaize.LoginTest do
end

test "login fails when account is not yet confirmed" do
conn = login("fred", "mangoes&g0oseberries")
conn = login("fred", "mangoes&g0oseberries", :username, "username")
assert conn.private[:openmaize_error] =~ "have to confirm your account"
end

test "login fails for invalid username" do
conn = login("dick", "h4rd2gU3$$")
conn = login("dick", "h4rd2gU3$$", :username, "username")
assert conn.private[:openmaize_error] =~ "Invalid credentials"
end

test "login fails for invalid email" do
conn = login("[email protected]", "h4rd2gU3$$", :email, "email")
conn = login("[email protected]", "h4rd2gU3$$")
assert conn.private[:openmaize_error] =~ "Invalid credentials"
end

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

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

test "output to current_user does not contain password_hash or otp_secret" do
conn = login("ray", "h4rd2gU3$$")
conn = login("ray", "h4rd2gU3$$", :username, "username")
user = conn.private[:openmaize_user]
refute Map.has_key?(user, :password_hash)
refute Map.has_key?(user, :otp_secret)
Expand Down

0 comments on commit f271544

Please sign in to comment.