Commit ef252f62 authored by Frank's avatar Frank 👽
Browse files

Run mix format

parent efe18235
Pipeline #75831755 passed with stage
in 3 minutes and 41 seconds
......@@ -14,9 +14,7 @@ config :gaja, Gaja.Endpoint,
url: [host: "localhost"],
secret_key_base: "B9ney6bs6p3KIWVSjKJgalmKjXFHmTj+D9+i5L7XEyHmxPapwZw2FBKttY/X3IgM",
render_errors: [view: Gaja.ErrorView, accepts: ~w(json)],
pubsub: [name: Gaja.PubSub,
adapter: Phoenix.PubSub.PG2]
pubsub: [name: Gaja.PubSub, adapter: Phoenix.PubSub.PG2]
# Phauxth authentication configuration
config :phauxth,
......@@ -31,12 +29,11 @@ config :logger, :console,
metadata: [:request_id]
# Configure phoenix generators
config :phoenix, :generators,
binary_id: true
config :phoenix, :generators, binary_id: true
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"
import_config "#{Mix.env()}.exs"
......@@ -26,7 +26,6 @@ config :gaja, Gaja.Endpoint,
check_origin: false,
watchers: []
# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"
......
......@@ -8,8 +8,7 @@ use Mix.Config
# file or create a script for recreating it, since it's
# kept out of version control and might be hard to recover
# or recreate for your teammates (or yourself later on).
config :gaja, Gaja.Endpoint,
secret_key_base: System.get_env("SECRET_KEY_BASE")
config :gaja, Gaja.Endpoint, secret_key_base: System.get_env("SECRET_KEY_BASE")
# Configure your database
config :gaja, Gaja.Repo,
......
......@@ -17,5 +17,4 @@ config :gaja, Gaja.Repo,
hostname: System.get_env("DATABASE_HOST", "localhost"),
pool: Ecto.Adapters.SQL.Sandbox
config :argon2_elixir, t_cost: 1, m_cost: 8
......@@ -13,7 +13,7 @@ defmodule Gaja do
# Start the Ecto repository
supervisor(Gaja.Repo, []),
# Start the endpoint when the application starts
supervisor(Gaja.Endpoint, []),
supervisor(Gaja.Endpoint, [])
# Start your own worker by calling: Gaja.Worker.start_link(arg1, arg2, arg3)
# worker(Gaja.Worker, [arg1, arg2, arg3]),
]
......
......@@ -49,9 +49,11 @@ defmodule Gaja.Accounts do
Updates a user.
"""
def update_user(%User{} = user, attrs) do
changeset = user
|> User.update_changeset(attrs)
changeset
changeset =
user
|> User.update_changeset(attrs)
changeset
|> Repo.update()
end
......
......@@ -7,12 +7,12 @@ defmodule Gaja.Accounts.User do
import Ecto.Changeset
schema "users" do
field :email, :string
field :name, :string
field :password, :string, virtual: true
field :password_hash, :string
field(:email, :string)
field(:name, :string)
field(:password, :string, virtual: true)
field(:password_hash, :string)
has_many :trackers, Gaja.Tracker
has_many(:trackers, Gaja.Tracker)
timestamps()
end
......@@ -60,8 +60,7 @@ defmodule Gaja.Accounts.User do
end)
end
defp put_pass_hash(%Ecto.Changeset{valid?: true, changes:
%{password: password}} = changeset) do
defp put_pass_hash(%Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset) do
change(changeset, Argon2.add_hash(password))
end
......
defmodule Gaja.Endpoint do
use Phoenix.Endpoint, otp_app: :gaja
socket "/socket", Gaja.Web.UserSocket,
websocket: true, # or list of options
socket "/socket", Gaja.Web.UserSocket,
# or list of options
websocket: true,
longpoll: false
# Code reloading can be explicitly enabled under the
......@@ -33,10 +34,10 @@ defmodule Gaja.Endpoint do
plug Gaja.Router
def init(_type, config) do
port = System.get_env("PORT") || 4000
host = System.get_env("HOST") || "localhost"
port = System.get_env("PORT") || 4000
host = System.get_env("HOST") || "localhost"
config = Keyword.put(config, :http, [:inet6, port: port])
config = Keyword.put(config, :url, [host: host, port: port])
config = Keyword.put(config, :url, host: host, port: port)
{:ok, config}
end
end
defmodule Gaja.Repo do
use Ecto.Repo, otp_app: :gaja,
adapter: Ecto.Adapters.Postgres
use Ecto.Repo,
otp_app: :gaja,
adapter: Ecto.Adapters.Postgres
def init(_, opts) do
{:ok, build_opts(opts)}
......
......@@ -7,9 +7,9 @@ defmodule Gaja.Tracker do
import Ecto.Changeset
schema "trackers" do
field :name, :string
has_many :tracker_entries, Gaja.TrackerEntry
belongs_to :user, Gaja.Accounts.User
field(:name, :string)
has_many(:tracker_entries, Gaja.TrackerEntry)
belongs_to(:user, Gaja.Accounts.User)
timestamps()
end
......
......@@ -18,15 +18,15 @@ defmodule Gaja.TrackerEntries do
def list_tracker_entries(%Tracker{id: tracker_id}, start_date, end_date) do
TrackerEntry
|> where([t], t.tracker_id == ^tracker_id)
|> where([t], t.date >= ^start_date and t.date <= ^end_date)
|> Repo.all()
|> where([t], t.tracker_id == ^tracker_id)
|> where([t], t.date >= ^start_date and t.date <= ^end_date)
|> Repo.all()
end
def get_tracker_entry(%Tracker{id: tracker_id}, id) do
TrackerEntry
|> where([t], t.id == ^id and t.tracker_id == ^tracker_id)
|> Repo.one()
|> where([t], t.id == ^id and t.tracker_id == ^tracker_id)
|> Repo.one()
end
def get_by(attrs) do
......
......@@ -6,10 +6,10 @@ defmodule Gaja.TrackerEntry do
import Ecto.Changeset
schema "tracker_entries" do
field :date, :naive_datetime
field :value, :float
field :value_string, :string
belongs_to :tracker, Gaja.Tracker
field(:date, :naive_datetime)
field(:value, :float)
field(:value_string, :string)
belongs_to(:tracker, Gaja.Tracker)
timestamps()
end
......
......@@ -6,7 +6,8 @@ defmodule Gaja.ReleaseTasks do
:ssl,
:postgrex,
:ecto,
:ecto_sql # If using Ecto 3.0 or higher
# If using Ecto 3.0 or higher
:ecto_sql
]
@repos Application.get_env(:gaja, :ecto_repos, [])
......@@ -38,7 +39,7 @@ defmodule Gaja.ReleaseTasks do
# Start the Repo(s) for app
IO.puts("Starting repos..")
# pool_size can be 1 for ecto < 3.0
Enum.each(@repos, & &1.start_link(pool_size: 2))
end
......@@ -55,13 +56,16 @@ defmodule Gaja.ReleaseTasks do
defp run_db_create_for(repo) do
case repo.__adapter__.storage_up(repo.config) do
:ok ->
IO.puts "The database for #{inspect repo} has been created"
IO.puts("The database for #{inspect(repo)} has been created")
{:error, :already_up} ->
IO.puts "The database for #{inspect repo} has already been created"
IO.puts("The database for #{inspect(repo)} has already been created")
{:error, term} when is_binary(term) ->
IO.puts "The database for #{inspect repo} couldn't be created: #{term}"
IO.puts("The database for #{inspect(repo)} couldn't be created: #{term}")
{:error, term} ->
IO.puts "The database for #{inspect repo} couldn't be created: #{inspect term}"
IO.puts("The database for #{inspect(repo)} couldn't be created: #{inspect(term)}")
end
end
......
......@@ -29,5 +29,6 @@
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.15.0", "dd5349161019caeea93efa42f9b22f9d79995c3a86bdffb796427b4c9863b0f0", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
"sobelow": {:hex, :sobelow, "0.8.0", "a3ec73e546dfde19f14818e5000c418e3f305d9edb070e79dd391de0ae1cd1ea", [], [], "hexpm"},
"telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
}
......@@ -11,6 +11,6 @@ defmodule Gaja.Repo.Migrations.CreateUsers do
timestamps()
end
create unique_index :users, [:email]
create(unique_index(:users, [:email]))
end
end
......@@ -4,10 +4,10 @@ defmodule Gaja.Repo.Migrations.AddTrackersTable do
def change do
create table(:trackers, primary_key: false) do
add(:id, :binary_id, primary_key: true)
add :name, :string
add(:name, :string)
timestamps()
end
create unique_index :trackers, [:name]
create(unique_index(:trackers, [:name]))
end
end
......@@ -3,7 +3,7 @@ defmodule Gaja.Repo.Migrations.CleanTrackerRelation do
def change do
alter table(:trackers) do
add :user_id, references(:users, type: :binary_id), null: false
add(:user_id, references(:users, type: :binary_id), null: false)
end
end
end
......@@ -11,6 +11,6 @@ defmodule Gaja.Repo.Migrations.AddTrackerEntryTable do
timestamps()
end
create index(:tracker_entries, [:date])
create(index(:tracker_entries, [:date]))
end
end
......@@ -5,7 +5,7 @@ defmodule Gaja.AccountsTest do
alias Gaja.Accounts.User
@create_attrs %{
email: "fred@example.com",
email: "fred@example.com",
name: "Fred",
password: "reallyHard2gue$$"
}
......@@ -54,10 +54,13 @@ defmodule Gaja.AccountsTest do
test "update_user/2 with invalid data returns error changeset" do
user = fixture(:user)
{:error, %Ecto.Changeset{}} = Accounts.update_user(
user,
@invalid_attrs
)
{:error, %Ecto.Changeset{}} =
Accounts.update_user(
user,
@invalid_attrs
)
assert user == Accounts.get_user(user.id)
end
end
......
defmodule GajaWeb.TrackersChannelTest do
use Gaja.ChannelCase
"""
setup do
{:ok, _, socket} =
GajaWeb.UserSocket
......@@ -10,7 +11,6 @@ defmodule GajaWeb.TrackersChannelTest do
{:ok, socket: socket}
end
"""
test "ping replies with status ok", %{socket: socket} do
ref = push socket, "ping", %{"hello" => "there"}
assert_reply ref, :ok, %{"hello" => "there"}
......
......@@ -4,11 +4,11 @@ defmodule Gaja.Web.SessionControllerTest do
import Gaja.AuthCase
@create_attrs %{
email: "robin@example.com",
email: "robin@example.com",
password: "reallyHard2gue$$"
}
@invalid_attrs %{
email: "robin@example.com",
email: "robin@example.com",
password: "cannotGue$$it"
}
......@@ -19,32 +19,39 @@ defmodule Gaja.Web.SessionControllerTest do
describe "create session" do
test "login succeeds", %{conn: conn} do
conn = post(
conn,
Routes.session_path(conn, :create),
session: @create_attrs
)
conn =
post(
conn,
Routes.session_path(conn, :create),
session: @create_attrs
)
assert json_response(conn, 200)["access_token"]
end
test "login fails when log in twice", %{conn: conn, user: user} do
conn = conn |> add_token_conn(user)
conn = post(
conn,
Routes.session_path(conn, :create),
session: @create_attrs
)
conn =
post(
conn,
Routes.session_path(conn, :create),
session: @create_attrs
)
message = json_response(conn, 401)["message"]
assert message
assert message =~ "Anonymously logged in"
end
test "login fails for invalid password", %{conn: conn} do
conn = post(
conn,
Routes.session_path(conn, :create),
session: @invalid_attrs
)
conn =
post(
conn,
Routes.session_path(conn, :create),
session: @invalid_attrs
)
assert json_response(conn, 401)["errors"]["detail"] =~ "Not authenticated"
end
end
......
......@@ -7,10 +7,12 @@ defmodule Gaja.Web.TrackerControllerTest do
setup %{conn: conn} = config do
email = config[:login] || "default@example.com"
user = add_user(email)
attrs = %{
user: user,
user: user,
name: "First tracker"
}
if config[:login] do
{:ok, tracker} = Trackers.create_tracker(user, attrs)
conn = conn |> add_token_conn(user)
......@@ -28,7 +30,7 @@ defmodule Gaja.Web.TrackerControllerTest do
assert json_response(conn, 200)
end
test "401 for unauthorized user", %{conn: conn} do
test "401 for unauthorized user", %{conn: conn} do
conn = get(conn, Routes.tracker_path(conn, :index))
assert json_response(conn, 401)
end
......@@ -41,7 +43,7 @@ defmodule Gaja.Web.TrackerControllerTest do
assert json_response(conn, 200)["data"]["uuid"] == tracker.id
end
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
conn = get(conn, Routes.tracker_path(conn, :show, tracker))
assert json_response(conn, 401)
end
......@@ -51,34 +53,39 @@ defmodule Gaja.Web.TrackerControllerTest do
@tag login: "bill@example.com"
test "data is valid", %{conn: conn, user: user} do
attrs = %{
user: user,
user: user,
name: "New tracker"
}
conn = post(
conn,
Routes.tracker_path(conn, :create),
attrs
)
conn =
post(
conn,
Routes.tracker_path(conn, :create),
attrs
)
assert json_response(conn, 201)["data"]["uuid"]
assert Trackers.get_by name: "New tracker"
assert Trackers.get_by(name: "New tracker")
assert length(Trackers.list_trackers(user)) == 2
end
@tag login: "bill@example.com"
test "data is invalid", %{conn: conn, user: user} do
attrs = %{
user: user,
name: nil
user: user,
name: nil
}
conn = post(conn, Routes.tracker_path(conn, :create), tracker: attrs)
assert json_response(conn, 422)["errors"] != %{}
end
test "401 for unauthorized user", %{conn: conn, user: user} do
test "401 for unauthorized user", %{conn: conn, user: user} do
attrs = %{
user: user,
name: "New tracker"
}
conn = post(conn, Routes.tracker_path(conn, :create), tracker: attrs)
assert json_response(conn, 401)
end
......@@ -88,8 +95,9 @@ defmodule Gaja.Web.TrackerControllerTest do
@tag login: "reg@example.com"
test "data is valid", %{conn: conn, user: user, tracker: tracker} do
attrs = %{
name: "New name"
name: "New name"
}
conn = put(conn, Routes.tracker_path(conn, :update, tracker.id), tracker: attrs)
assert json_response(conn, 200)
updated_tracker = Trackers.get_tracker(user, tracker.id)
......@@ -99,8 +107,9 @@ defmodule Gaja.Web.TrackerControllerTest do
@tag login: "reg@example.com"
test "data is invalid", %{conn: conn, tracker: tracker} do
invalid_attrs = %{
name: nil
name: nil
}
conn = put(conn, Routes.tracker_path(conn, :update, tracker.id), tracker: invalid_attrs)
assert json_response(conn, 422)["errors"] != %{}
end
......@@ -114,7 +123,7 @@ defmodule Gaja.Web.TrackerControllerTest do
refute Trackers.get_tracker(user, tracker.id)
end
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
conn = delete(conn, Routes.tracker_path(conn, :delete, tracker))
assert json_response(conn, 401)
end
......
......@@ -7,19 +7,23 @@ defmodule Gaja.Web.TrackerEntryControllerTest do
setup %{conn: conn} = config do
email = config[:login] || "default@example.com"
user = add_user(email)
tracker_attrs = %{
name: "My tracker"
}
attrs = %{
value: 8,
date: "2019-08-01T00:00:00"
}
{:ok, tracker} = Trackers.create_tracker(user, tracker_attrs)
{:ok, tracker_entry} = TrackerEntries.create_tracker_entry(tracker, attrs)
if config[:login] do
conn = conn |> add_token_conn(user)
{:ok, %{conn: conn, user: user, tracker: tracker, tracker_entry: tracker_entry}}
else
else
{:ok, %{conn: conn, user: user, tracker: tracker, tracker_entry: tracker_entry}}
end
end
......@@ -36,10 +40,13 @@ defmodule Gaja.Web.TrackerEntryControllerTest do
attrs = %{value: 2, date: "2019-06-01T00:00:00"}
TrackerEntries.create_tracker_entry(tracker, attrs)
attrs = %{value: 5, date: "2019-07-01T00:00:00"}
{:ok, tracker_entry} = TrackerEntries.create_tracker_entry(
tracker,
attrs
)
{:ok, tracker_entry} =
TrackerEntries.create_tracker_entry(
tracker,
attrs
)
response =
conn
|> get(
......@@ -48,16 +55,22 @@ defmodule Gaja.Web.TrackerEntryControllerTest do
end_date: "2019-07-15T00:00:00"
)
|> json_response(200)
expected = %{"data" => [%{
"date" => "2019-07-01T00:00:00",
"uuid" => tracker_entry.id,
"value" => 5.0,
"value_string" => nil
}]}
expected = %{
"data" => [
%{
"date" => "2019-07-01T00:00:00",
"uuid" => tracker_entry.id,
"value" => 5.0,
"value_string" => nil
}
]
}
assert response == expected
end
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
test "401 for unauthorized user", %{conn: conn, tracker: tracker} do
conn = get(conn, Routes.tracker_tracker_entry_path(conn, :index, tracker))
assert json_response(conn, 401)
end
......@@ -70,11 +83,14 @@ defmodule Gaja.Web.TrackerEntryControllerTest do
value: 8,
date: "2019-08-01T00:00:00"
}
conn = post(
conn,
Routes.tracker_tracker_entry_path(conn, :create, tracker),