Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Users + Code coverage & CI #24

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: elixir
elixir:
- 1.4.1
addons:
postgresql: '9.4'
apt:
packages:
- wkhtmltopdf
env:
- MIX_ENV=test
before_script:
- mix do ecto.create, ecto.migrate
script:
- mix do deps.get, compile --warnings-as-errors, coveralls.json
after_success:
- bash <(curl -s https://codecov.io/bash)
notifications:
email: false
28 changes: 28 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
codecov:
codecov:
token: d9616fef-dc91-4fb3-a919-a75b7f0ba079
notify:
require_ci_to_pass: yes

coverage:
precision: 2
round: down
range: "70...100"

status:
project: yes
patch: yes
changes: no

parsers:
gcov:
branch_detection:
conditional: yes
loop: yes
method: no
macro: no

comment:
layout: "header, diff, sunburst"
behavior: default
require_changes: no
2,371 changes: 2,371 additions & 0 deletions cover/excoveralls.html

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"coverage_options": {
"minimum_coverage": 100
},
"skip_files": [
"web/channels",
"lib/feedback.ex",
"test/support/model_case.ex",
"test/support/channel_case.ex",
"web/web.ex",
"web/views/error_helpers.ex"
]
}
11 changes: 9 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ defmodule Feedback.Mixfile do
compilers: [:phoenix, :gettext] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
test_coverage: [tool: ExCoveralls],
preferred_cli_env: ["coveralls": :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test],
aliases: aliases(),
deps: deps()]
end
Expand All @@ -19,7 +24,7 @@ defmodule Feedback.Mixfile do
def application do
[mod: {Feedback, []},
applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
:phoenix_ecto, :postgrex]]
:phoenix_ecto, :postgrex, :comeonin]]
end

# Specifies which paths to compile per environment.
Expand All @@ -37,7 +42,9 @@ defmodule Feedback.Mixfile do
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"}]
{:cowboy, "~> 1.0"},
{:comeonin, "~> 2.0"},
{:excoveralls, "~> 0.6.2"}]
end

# Aliases are shortcuts or tasks specific to the current project.
Expand Down
14 changes: 12 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
%{"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
%{"certifi": {:hex, :certifi, "1.0.0", "1c787a85b1855ba354f0b8920392c19aa1d06b0ee1362f9141279620a5be2039", [:rebar3], []},
"comeonin": {:hex, :comeonin, "2.6.0", "74c288338b33205f9ce97e2117bb9a2aaab103a1811d243443d76fdb62f904ac", [:make, :mix], []},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], []},
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, optional: false]}]},
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], []},
"db_connection": {:hex, :db_connection, "1.1.2", "2865c2a4bae0714e2213a0ce60a1b12d76a6efba0c51fbda59c9ab8d1accc7a8", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, optional: false]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"decimal": {:hex, :decimal, "1.3.1", "157b3cedb2bfcb5359372a7766dd7a41091ad34578296e951f58a946fcab49c6", [:mix], []},
"ecto": {:hex, :ecto, "2.1.4", "d1ba932813ec0e0d9db481ef2c17777f1cefb11fc90fa7c142ff354972dfba7e", [:mix], [{:db_connection, "~> 1.1", [hex: :db_connection, optional: true]}, {:decimal, "~> 1.2", [hex: :decimal, optional: false]}, {:mariaex, "~> 0.8.0", [hex: :mariaex, optional: true]}, {:poison, "~> 2.2 or ~> 3.0", [hex: :poison, optional: true]}, {:poolboy, "~> 1.5", [hex: :poolboy, optional: false]}, {:postgrex, "~> 0.13.0", [hex: :postgrex, optional: true]}, {:sbroker, "~> 1.0", [hex: :sbroker, optional: true]}]},
"excoveralls": {:hex, :excoveralls, "0.6.3", "894bf9254890a4aac1d1165da08145a72700ff42d8cb6ce8195a584cb2a4b374", [:mix], [{:exjsx, "~> 3.0", [hex: :exjsx, optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, optional: false]}]},
"exjsx": {:hex, :exjsx, "3.2.1", "1bc5bf1e4fd249104178f0885030bcd75a4526f4d2a1e976f4b428d347614f0f", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, optional: false]}]},
"fs": {:hex, :fs, "0.9.2", "ed17036c26c3f70ac49781ed9220a50c36775c6ca2cf8182d123b6566e49ec59", [:rebar], []},
"gettext": {:hex, :gettext, "0.13.1", "5e0daf4e7636d771c4c71ad5f3f53ba09a9ae5c250e1ab9c42ba9edccc476263", [:mix], []},
"hackney": {:hex, :hackney, "1.7.1", "e238c52c5df3c3b16ce613d3a51c7220a784d734879b1e231c9babd433ac1cb4", [:rebar3], [{:certifi, "1.0.0", [hex: :certifi, optional: false]}, {:idna, "4.0.0", [hex: :idna, optional: false]}, {:metrics, "1.0.1", [hex: :metrics, optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, optional: false]}, {:ssl_verify_fun, "1.1.1", [hex: :ssl_verify_fun, optional: false]}]},
"idna": {:hex, :idna, "4.0.0", "10aaa9f79d0b12cf0def53038547855b91144f1bfcc0ec73494f38bb7b9c4961", [:rebar3], []},
"jsx": {:hex, :jsx, "2.8.2", "7acc7d785b5abe8a6e9adbde926a24e481f29956dd8b4df49e3e4e7bcc92a018", [:mix, :rebar3], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mime": {:hex, :mime, "1.1.0", "01c1d6f4083d8aa5c7b8c246ade95139620ef8effb009edde934e0ec3b28090a", [:mix], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"phoenix": {:hex, :phoenix, "1.2.3", "b68dd6a7e6ff3eef38ad59771007d2f3f344988ea6e658e9b2c6ffb2ef494810", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, optional: true]}, {:phoenix_pubsub, "~> 1.0", [hex: :phoenix_pubsub, optional: false]}, {:plug, "~> 1.4 or ~> 1.3.3 or ~> 1.2.4 or ~> 1.1.8 or ~> 1.0.5", [hex: :plug, optional: false]}, {:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"phoenix_ecto": {:hex, :phoenix_ecto, "3.2.3", "450c749876ff1de4a78fdb305a142a76817c77a1cd79aeca29e5fc9a6c630b26", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, optional: true]}, {:plug, "~> 1.0", [hex: :plug, optional: false]}]},
"phoenix_html": {:hex, :phoenix_html, "2.9.3", "1b5a2122cbf743aa242f54dced8a4f1cc778b8bd304f4b4c0043a6250c58e258", [:mix], [{:plug, "~> 1.0", [hex: :plug, optional: false]}]},
Expand All @@ -16,4 +25,5 @@
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [:rebar], []},
"postgrex": {:hex, :postgrex, "0.13.2", "2b88168fc6a5456a27bfb54ccf0ba4025d274841a7a3af5e5deb1b755d95154e", [:mix], [{:connection, "~> 1.0", [hex: :connection, optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, optional: false]}]},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []}}
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], []}}
16 changes: 16 additions & 0 deletions priv/repo/migrations/20170405220936_create_users.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Feedback.Repo.Migrations.CreateUsers do
use Ecto.Migration

def change do
create table(:users) do
add :first_name, :string
add :last_name, :string
add :email, :string
add :password_hash, :string

timestamps()
end

create unique_index(:users, [:email])
end
end
48 changes: 48 additions & 0 deletions test/models/user_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule Feedback.UserTest do
use Feedback.ModelCase, async: false
alias Feedback.User
alias Comeonin.Bcrypt

@valid_attrs %{
email: "[email protected]",
password: "secretshhh",
password_hash: Bcrypt.hashpwsalt("secretshhh"),
first_name: "First",
last_name: "Last"
}
@invalid_attrs %{email: "[email protected]"}

test "changeset with valid attributes" do
changeset = User.changeset(%User{}, @valid_attrs)
assert changeset.valid?
end

test "changeset with invalid attributes" do
changeset = User.changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end

test "registration_changeset with valid attributes" do
changeset = User.registration_changeset(%User{}, @valid_attrs)
assert changeset.valid?
end

test "registration_changeset with invalid attributes" do
changeset = User.registration_changeset(%User{}, @invalid_attrs)
refute changeset.valid?
end

test "user schema" do
actual = User.__schema__(:fields)
expected = [
:id,
:first_name,
:last_name,
:email,
:password_hash,
:inserted_at,
:updated_at
]
assert actual == expected
end
end
45 changes: 45 additions & 0 deletions web/models/user.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
defmodule Feedback.User do
use Feedback.Web, :model
alias Ecto.Changeset
alias Comeonin.Bcrypt

schema "users" do
field :first_name, :string
field :last_name, :string
field :email, :string
field :password, :string, virtual: true
field :password_hash, :string

timestamps()
end

def changeset(struct, params \\ :invalid) do
struct
|> cast(params, [:email, :first_name, :last_name, :password])
|> validate_format(:email, ~r/@/)
|> validate_required([:email, :password, :first_name, :last_name])
|> unique_constraint(:email)
end

def registration_changeset(struct, params) do
struct
|> changeset(params)
|> validate_password(params)
|> put_pass_hash()
end

def validate_password(changeset, params) do
changeset
|> cast(params, [:password, :email])
|> validate_length(:password, min: 6, max: 100)
end

def put_pass_hash(changeset) do
case changeset do
%Changeset{valid?: true, changes: %{password: pass}} ->
put_change(changeset, :password_hash, Bcrypt.hashpwsalt(pass))
_ ->
changeset
end
end
end
4 changes: 0 additions & 4 deletions web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ defmodule Feedback.Router do
plug :put_secure_browser_headers
end

pipeline :api do
plug :accepts, ["json"]
end

scope "/", Feedback do
pipe_through :browser # Use the default browser stack

Expand Down