Skip to content

Commit af7ff2d

Browse files
committed
add %User{role} attribute
1 parent b90bf45 commit af7ff2d

16 files changed

+159
-33
lines changed

lib/mix/tasks/client/roles.ex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule Mix.Tasks.Cli.Roles do
2+
use Mix.Task.Point.Client
3+
@shortdoc "Show roles. Param: token"
4+
defrun fn(token)-> points(base_url, token) |> roles end
5+
end

lib/point/client/client.ex

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ defmodule Point.Client do
4444
end
4545
def users(client), do: request(method: :get, url: url(client, "users"), body: %{}, headers: %{token: client.token})
4646

47+
48+
def roles(client), do: request(method: :get, url: url(client, "roles"), body: %{}, headers: %{token: client.token})
49+
50+
4751
def currencies(client, create: %Point.Client.Dto.Currency{code: code, name: name}) do
4852
request(
4953
method: :post,

mix.exs

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ defmodule Point.Mixfile do
1919
# Type `mix help compile.app` for more information.
2020
def application do
2121
[mod: {Point, []},
22-
applications: [:phoenix, :phoenix_pubsub, :cowboy, :logger, :gettext, :phoenix_ecto, :mariaex, :ecto_ttl, :timex,
23-
:httpotion]]
22+
applications: [:phoenix, :phoenix_pubsub, :cowboy, :logger, :gettext, :phoenix_ecto, :mariaex,
23+
:ecto_ttl, :timex, :httpotion]]
2424
end
2525

2626
# Specifies which paths to compile per environment.
@@ -35,7 +35,6 @@ defmodule Point.Mixfile do
3535
{:phoenix, "~> 1.2.1"},
3636
{:phoenix_pubsub, "~> 1.0"},
3737
{:phoenix_ecto, "~> 3.0"},
38-
{:postgrex, ">= 0.0.0"},
3938
{:gettext, "~> 0.11"},
4039
{:cowboy, "~> 1.0"},
4140
{:espec_phoenix, "~> 0.6.4", only: :test},
@@ -51,7 +50,8 @@ defmodule Point.Mixfile do
5150
{:timex, "~> 3.0"},
5251
{:timex_ecto, "~> 3.0"},
5352
{:exjsx, "~> 3.2.1", git: "https://github.com/talentdeficit/exjsx"},
54-
{:httpotion, "~> 3.0.2"}
53+
{:httpotion, "~> 3.0.2"},
54+
{:ecto_enum, "~> 1.0"}
5555
]
5656
end
5757

mix.lock

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"db_connection": {:hex, :db_connection, "1.1.0", "b2b88db6d7d12f99997b584d09fad98e560b817a20dab6a526830e339f54cdb3", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
1111
"decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []},
1212
"ecto": {:hex, :ecto, "2.0.6", "9dcbf819c2a77f67a66b83739b7fcc00b71aaf6c100016db4f798930fa4cfd47", [:mix], [{:db_connection, "~> 1.0", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.1.2 or ~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.12.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0-beta", [hex: :sbroker, optional: true]}]},
13+
"ecto_enum": {:hex, :ecto_enum, "1.0.1", "7853cd36b2063c1e2ba2a2e2987dd4745afa1ab1e24b16f6dba3b6bdbd4c907b", [:mix], [{:ecto, "~> 2.1.0", [hex: :ecto, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}]},
1314
"ecto_migrate": {:hex, :ecto_migrate, "0.6.3", "70b37ac2111bfe16b6076bb8815e0207e24331eceefb3c5b96f725d0296cf65d", [:mix], [{:ecto, "~> 1.0.0", [hex: :ecto, optional: false]}, {:ecto_it, "~> 0.2.0", [hex: :ecto_it, optional: true]}, {:mariaex, ">= 0.0.0", [hex: :mariaex, optional: true]}, {:postgrex, ">= 0.0.0", [hex: :postgrex, optional: true]}]},
1415
"ecto_ttl": {:git, "https://github.com/xerions/ecto_ttl.git", "2a2e2bcd1443e5a0a5ed594f807ddcec46a1a840", []},
1516
"espec": {:hex, :espec, "1.2.0", "93a057d9b200619c6a64676f27b5e7d3d6d80097763232a76bb88d8b0c3db28d", [:mix], [{:meck, "~> 0.8.4", [hex: :meck, optional: false]}]},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Point.Repo.Migrations.AddRoleToUsers do
2+
use Ecto.Migration
3+
4+
def up do
5+
alter table(:users) do
6+
add :role, :integer
7+
end
8+
end
9+
10+
def down do
11+
alter table(:users) do
12+
remove :role
13+
end
14+
end
15+
end

priv/repo/seeds.exs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ alias Point.{User, Entity, Account, Currency, ExchangeRate}
77
# Is the root user
88
chewbacca = insert! User.insert_changeset(%User{}, %{
99
email: "[email protected]", password: "12345678910",
10-
first_name: "Chewbacca", last_name: "Chewbacca"
10+
first_name: "Chewbacca", last_name: "Chewbacca", role: :system_admin
1111
})
1212
obiwan_kenoby = insert! User.insert_changeset(%User{}, %{
1313
email: "[email protected]", password: "12345678910",
14-
first_name: "Obi-Wan", last_name: "Kenoby"
14+
first_name: "Obi-Wan", last_name: "Kenoby", role: :normal_user
1515
})
1616
anakin_skywalker = insert! User.insert_changeset(%User{}, %{
1717
email: "[email protected]", password: "12345678910",
18-
first_name: "Anakin", last_name: "Skywalker"
18+
first_name: "Anakin", last_name: "Skywalker", role: :normal_user
1919
})
2020
#
2121
#

spec/factories/user_factory.ex

+46-6
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,67 @@ defmodule Point.UserFactory do
22
use ExMachina.Ecto, repo: Point.Repo
33
alias Point.User
44

5+
def session_test_user_factory do
6+
%User{
7+
8+
password: "Whatever10",
9+
first_name: "TestFirstName",
10+
last_name: "TestLastName",
11+
role: :system_admin
12+
}
13+
end
14+
515
def chewbacca_factory do
6-
%User{email: sequence(:email, &"chewbacca#{&1}@gmail.com"), first_name: "chewbacca", last_name: "chewbacca"}
16+
%User{
17+
email: sequence(:email, &"chewbacca#{&1}@gmail.com"),
18+
first_name: "chewbacca",
19+
last_name: "chewbacca",
20+
role: :system_admin
21+
}
722
end
823

924
def luke_skywalker_factory do
10-
%User{email: sequence(:email, &"lukeskywalker#{&1}@gmail.com"), first_name: "Luke", last_name: "Skywalker"}
25+
%User{
26+
email: sequence(:email, &"lukeskywalker#{&1}@gmail.com"),
27+
first_name: "Luke",
28+
last_name: "Skywalker",
29+
role: :normal_user
30+
}
1131
end
1232

1333
def obiwan_kenoby_factory do
14-
%User{email: sequence(:email, &"obiwankenoby#{&1}@gmail.com"), first_name: "Obi-Wan", last_name: "Kenoby"}
34+
%User{
35+
email: sequence(:email, &"obiwankenoby#{&1}@gmail.com"),
36+
first_name: "Obi-Wan",
37+
last_name: "Kenoby",
38+
role: :normal_user
39+
}
1540
end
1641

1742
def han_solo_factory do
18-
%User{email: sequence(:email, &"hansolo#{&1}@gmail.com"), first_name: "Han", last_name: "Solo"}
43+
%User{
44+
email: sequence(:email, &"hansolo#{&1}@gmail.com"),
45+
first_name: "Han",
46+
last_name: "Solo",
47+
role: :normal_user
48+
}
1949
end
2050

2151
def anakin_skywalker_factory do
22-
%User{email: sequence(:email, &"anakinskywalker#{&1}@gmail.com"), first_name: "Anakin", last_name: "Skywalker"}
52+
%User{
53+
email: sequence(:email, &"anakinskywalker#{&1}@gmail.com"),
54+
first_name: "Anakin",
55+
last_name: "Skywalker",
56+
role: :normal_user
57+
}
2358
end
2459

2560
def jango_fett_factory do
26-
%User{email: sequence(:email, &"jangofett#{&1}@gmail.com"), first_name: "Jango", last_name: "Fett"}
61+
%User{
62+
email: sequence(:email, &"jangofett#{&1}@gmail.com"),
63+
first_name: "Jango",
64+
last_name: "Fett",
65+
role: :normal_user
66+
}
2767
end
2868
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
defmodule Point.RoleControllerSpec do
2+
use ESpec.Phoenix, controller: Point.RoleController
3+
use ESpec.Phoenix.Helper
4+
5+
describe "index" do
6+
let response: sec_conn |> get(role_path(sec_conn, :index))
7+
let roles: json_response(response, 200)
8+
9+
it "returns ok status", do: expect response.status |> to(eq 200)
10+
it "returns a list with three roles", do: expect roles |> to(have_size 3)
11+
it "returns a list that contains a system_admin role", do: expect roles |> to(have "system_admin")
12+
it "returns a list that contains a entity_admin role", do: expect roles |> to(have "entity_admin")
13+
it "returns a list that contains a normal_user role", do: expect roles |> to(have "normal_user")
14+
end
15+
end

spec/point/controllers/session_controller_spec.exs

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ defmodule Point.SessionControllerSpec do
55
alias Point.{Session, UserService, SessionService}
66

77
let ip: "127.0.0.1"
8-
let valid_user_attrs: %{email: "[email protected]", password: "Whatever1123", first_name: "adrian",
9-
last_name: "marino"}
8+
let valid_user_attrs: %{
9+
10+
password: "Whatever1123",
11+
first_name: "adrian",
12+
last_name: "marino",
13+
role: "normal_user"
14+
}
1015
let valid_attrs: sub_map(valid_user_attrs, [:email, :password])
1116

1217
describe "sign_in" do

spec/point/controllers/user_controller_spec.exs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ defmodule Point.UserControllerSpec do
22
use ESpec.Phoenix, controller: Point.UserController
33
use ESpec.Phoenix.Helper
44

5-
let valid_attrs: %{email: "[email protected]", password: "Whatever10", first_name: "2222", last_name: "222"}
5+
let valid_attrs: %{
6+
7+
password: "Whatever10",
8+
first_name: "2222",
9+
last_name: "222",
10+
role: "normal_user"
11+
}
612

713
describe "create" do
814
let response: sec_conn |> post(user_path(sec_conn, :create), attrs)

spec/point/helper/espec_phoenix_helper.exs

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ defmodule ESpec.Phoenix.Helper do
66

77
alias Point.{Repo, Session, User}
88

9-
let :current_user do
10-
Repo.insert! User.insert_changeset(%User{}, %{
11-
12-
password: "Whatever10",
13-
first_name: "Test",
14-
last_name: "User"
15-
})
16-
end
9+
let current_user: Point.UserFactory.insert(:session_test_user)
1710

1811
let current_session: Repo.insert!(%Session{token: "token", remote_ip: "127.0.0.1", user_id: current_user.id})
1912

spec/point/models/user_spec.exs

+30-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ defmodule Point.UserSpec do
2020
end
2121
end
2222

23+
context "role_changeset" do
24+
let changeset: User.role_changeset(%User{}, attrs)
25+
26+
context "when has a valid role" do
27+
let attrs: %{role: :normal_user}
28+
it do: expect changeset.valid? |> to(be_truthy)
29+
end
30+
context "when an unknow role" do
31+
let attrs: %{role: :any}
32+
it do: expect changeset.valid? |> to(be_falsy)
33+
end
34+
context "when hasn't role" do
35+
let attrs: %{}
36+
it do: expect changeset.valid? |> to(be_falsy)
37+
end
38+
end
39+
2340
context "password_changeset" do
2441
let changeset: User.password_changeset(%User{}, attrs)
2542

@@ -60,14 +77,25 @@ defmodule Point.UserSpec do
6077
end
6178

6279
context "insert_changeset" do
63-
let attrs: %{email: "[email protected]", password: "Whatever10", first_name: "Adrian", last_name: "Marino"}
80+
let attrs: %{
81+
82+
password: "Whatever10",
83+
first_name: "Adrian",
84+
last_name: "Marino",
85+
role: "normal_user"
86+
}
6487
let changeset: User.insert_changeset(%User{}, attrs)
6588

6689
it do: expect changeset.valid? |> to(be_truthy)
6790
end
6891

6992
context "update_changeset" do
70-
let attrs: %{password: "Whatever10", first_name: "Adrian", last_name: "Marino"}
93+
let attrs: %{
94+
password: "Whatever10",
95+
first_name: "Adrian",
96+
last_name: "Marino",
97+
role: "normal_user"
98+
}
7199
let changeset: User.update_changeset(%User{}, attrs)
72100

73101
it do: expect changeset.valid? |> to(be_truthy)

web/controllers/role_controller.ex

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Point.RoleController do
2+
use Point.Web, :controller
3+
import Point.JSON
4+
def index(conn, _) do
5+
conn
6+
|> put_resp_header("content-type", "application/json")
7+
|> send_resp(:ok, to_json ~w[system_admin entity_admin normal_user])
8+
end
9+
end

web/models/user.ex

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
defmodule Point.User do
2-
@moduledoc """
3-
Represent a platform user. A user:
4-
- Could be a issuer or a normal user.
5-
- Belong to entities.
6-
- Could issue currency.
7-
- Has accounts.
8-
"""
92
use Point.Web, :model
103
use Timex.Ecto.Timestamps, usec: true
114

@@ -15,6 +8,7 @@ defmodule Point.User do
158
field :password, :string, virtual: true
169
field :first_name, :string
1710
field :last_name, :string
11+
field :role, UserRole
1812

1913
many_to_many :entities, Point.Entity, join_through: "users_entities"
2014
has_many :currencies, Point.Currency
@@ -26,6 +20,10 @@ defmodule Point.User do
2620
model |> cast_and_validate_required(params, [:first_name, :last_name])
2721
end
2822

23+
def role_changeset(model, params \\ %{}) do
24+
model |> cast_and_validate_required(params, [:role])
25+
end
26+
2927
defp put_password_hash(changeset) do
3028
case changeset do
3129
%Ecto.Changeset{valid?: true, changes: %{password: value}} ->
@@ -53,11 +51,13 @@ defmodule Point.User do
5351
|> email_changeset(params)
5452
|> password_changeset(params)
5553
|> first_last_name_changeset(params)
54+
|> role_changeset(params)
5655
end
5756

5857
def update_changeset(model, params \\ %{}) do
5958
model
6059
|> first_last_name_changeset(params)
60+
|> role_changeset(params)
6161
|> password_changeset(params)
6262
end
6363
end

web/models/user_role.ex

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import EctoEnum
2+
defenum UserRole, system_admin: 0, entity_admin: 1, normal_user: 2

web/router.ex

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ defmodule Point.Router do
1818
get "/sessions", SessionController, :index
1919

2020
resources "/users", UserController, except: [:new, :edit], param: "email"
21+
22+
resources "/roles", RoleController, only: [:index]
23+
2124
resources "/currencies", CurrencyController, except: [:new, :edit], param: "code"
2225

2326
scope "/accounts" do

0 commit comments

Comments
 (0)