Skip to content

Commit

Permalink
suit of couple tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobarboza7 committed Mar 1, 2021
1 parent 6f25794 commit 12bb586
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 11 deletions.
5 changes: 5 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ config :rocketpay, Rocketpay.Repo,
migration_primary_key: [type: :binary_id],
migration_foreign_key: [type: :binary_id]

# env when real api
config :rocketpay, :basic_auth,
username: "banana",
password: "123456"

# Configures Elixir's Logger
config :logger, :console,
format: "$time $metadata[$level] $message\n",
Expand Down
6 changes: 3 additions & 3 deletions lib/rocketpay/accounts/deposit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ defmodule Rocketpay.Accounts.Deposit do

def call(params) do
params
|> Operation.call(:deposit)
|> run_transaction()
|> Operation.call(:deposit)
|> run_transaction()
end

defp run_transaction(multi) do
case Repo.transaction(multi) do
{:error, _opration, reason, _changes} -> {:error, reason}
{:ok, %{account_deposit: account}} -> {:ok, account}
{:ok, %{deposit: account}} -> {:ok, account}
end
end
end
6 changes: 3 additions & 3 deletions lib/rocketpay/accounts/withdraw.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ defmodule Rocketpay.Accounts.Withdraw do

def call(params) do
params
|> Operation.call(:withdraw)
|> run_transaction()
|> Operation.call(:withdraw)
|> run_transaction()
end

defp run_transaction(multi) do
case Repo.transaction(multi) do
{:error, _opration, reason, _changes} -> {:error, reason}
{:ok, %{account_withdraw: account}} -> {:ok, account}
{:ok, %{withdraw: account}} -> {:ok, account}
end
end
end
11 changes: 11 additions & 0 deletions lib/rocketpay_web/router.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
defmodule RocketpayWeb.Router do
use RocketpayWeb, :router

import Plug.BasicAuth

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

pipeline :auth do
plug :basic_auth, Application.compile_env(:rocketpay, :basic_auth)
end

scope "/api", RocketpayWeb do
pipe_through :api

get "/:filename", WelcomeController, :index

post "/users", UsersController, :create
end

scope "/api", RocketpayWeb do
pipe_through [:api, :auth]

post "/accounts/:id/deposit", AccountsController, :deposit
post "/accounts/:id/withdraw", AccountsController, :withdraw
Expand Down
12 changes: 10 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ defmodule Rocketpay.MixProject do
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps()
deps: deps(),
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test
]
]
end

Expand Down Expand Up @@ -45,7 +52,8 @@ defmodule Rocketpay.MixProject do
{:plug_cowboy, "~> 2.0"},
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:bcrypt_elixir, "~> 2.0"},
{:decimal, "~> 2.0"}
{:decimal, "~> 2.0"},
{:excoveralls, "~> 0.10", only: :test}
]
end

Expand Down
9 changes: 9 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%{
"bcrypt_elixir": {:hex, :bcrypt_elixir, "2.3.0", "6cb662d5c1b0a8858801cf20997bd006e7016aa8c52959c9ef80e0f34fb60b7a", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "2c81d61d4f6ed0e5cf7bf27a9109b791ff216a1034b3d541327484f46dd43769"},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.3", "70bdd7e7188c804f3a30ee0e7c99655bc35d8ac41c23e12325f36ab449b70651", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "ed516acb3929b101208a9d700062d520f3953da3b6b918d866106ffa980e1c10"},
"comeonin": {:hex, :comeonin, "5.3.2", "5c2f893d05c56ae3f5e24c1b983c2d5dfb88c6d979c9287a76a7feb1e1d8d646", [:mix], [], "hexpm", "d0993402844c49539aeadb3fe46a3c9bd190f1ecf86b6f9ebd71957534c95f04"},
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
"cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"},
Expand All @@ -12,10 +13,16 @@
"ecto": {:hex, :ecto, "3.5.8", "8ebf12be6016cb99313348ba7bb4612f4114b9a506d6da79a2adc7ef449340bc", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ea0be182ea8922eb7742e3ae8e71b67ee00ae177de1bf76210299a5f16ba4c77"},
"ecto_sql": {:hex, :ecto_sql, "3.5.4", "a9e292c40bd79fff88885f95f1ecd7b2516e09aa99c7dd0201aa84c54d2358e4", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.5.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.3.0 or ~> 0.4.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "1fff1a28a898d7bbef263f1f3ea425b04ba9f33816d843238c84eff883347343"},
"elixir_make": {:hex, :elixir_make, "0.6.2", "7dffacd77dec4c37b39af867cedaabb0b59f6a871f89722c25b28fcd4bd70530", [:mix], [], "hexpm", "03e49eadda22526a7e5279d53321d1cced6552f344ba4e03e619063de75348d9"},
"excoveralls": {:hex, :excoveralls, "0.14.0", "4b562d2acd87def01a3d1621e40037fdbf99f495ed3a8570dfcf1ab24e15f76d", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "94f17478b0cca020bcd85ce7eafea82d2856f7ed022be777734a2f864d36091a"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"hackney": {:hex, :hackney, "1.17.0", "717ea195fd2f898d9fe9f1ce0afcc2621a41ecfe137fae57e7fe6e9484b9aa99", [:rebar3], [{:certifi, "~>2.5", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "64c22225f1ea8855f584720c0e5b3cd14095703af1c9fbc845ba042811dc671c"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"phoenix": {:hex, :phoenix, "1.5.7", "2923bb3af924f184459fe4fa4b100bd25fa6468e69b2803dfae82698269aa5e0", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "774cd64417c5a3788414fdbb2be2eb9bcd0c048d9e6ad11a0c1fd67b7c0d0978"},
"phoenix_ecto": {:hex, :phoenix_ecto, "4.2.1", "13f124cf0a3ce0f1948cf24654c7b9f2347169ff75c1123f44674afee6af3b03", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "478a1bae899cac0a6e02be1deec7e2944b7754c04e7d4107fc5a517f877743c0"},
"phoenix_html": {:hex, :phoenix_html, "2.14.3", "51f720d0d543e4e157ff06b65de38e13303d5778a7919bcc696599e5934271b8", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "efd697a7fff35a13eeeb6b43db884705cba353a1a41d127d118fda5f90c8e80f"},
Expand All @@ -27,7 +34,9 @@
"plug_crypto": {:hex, :plug_crypto, "1.2.1", "5c854427528bf61d159855cedddffc0625e2228b5f30eff76d5a4de42d896ef4", [:mix], [], "hexpm", "6961c0e17febd9d0bfa89632d391d2545d2e0eb73768f5f50305a23961d8782c"},
"postgrex": {:hex, :postgrex, "0.15.8", "f5e782bbe5e8fa178d5e3cd1999c857dc48eda95f0a4d7f7bd92a50e84a0d491", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "698fbfacea34c4cf22c8281abeb5cf68d99628d541874f085520ab3b53d356fe"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.0", "da9d49ee7e6bb1c259d36ce6539cd45ae14d81247a2b0c90edf55e2b50507f7b", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5cfe67ad464b243835512aa44321cee91faed6ea868d7fb761d7016e02915c3d"},
"telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
}
6 changes: 3 additions & 3 deletions test/rocketpay/numbers_test.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Rocketpay.NumbersTest do
use ExUnit.Case
use ExUnit.Case, async: true

alias Rocketpay.Numbers

Expand All @@ -9,15 +9,15 @@ defmodule Rocketpay.NumbersTest do

expected_response = {:ok, %{result: 37}}

assert response == expected_response
assert response == expected_response
end

test "when there is no a file with the given name, returns an error" do
response = Numbers.sum_from_file("banana")

expected_response = {:error, %{message: "fail"}}

assert response == expected_response
assert response == expected_response
end
end
end
41 changes: 41 additions & 0 deletions test/rocketpay/users/create_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
defmodule Rocketpay.Users.CreateTest do
use Rocketpay.DataCase, async: true

alias Rocketpay.User
alias Rocketpay.Users.Create

describe "call/1" do
test "when all params are valid, returns an user" do
params = %{
name: "fabio",
password: "123456",
nickname: "fabiobarboza",
email: "[email protected]",
age: 27
}

{:ok, %User{id: user_id}} = Create.call(params)
user = Repo.get(User, user_id)

assert %User{name: "fabio", age: 27, id: ^user_id} = user
end

test "when there are invalid params, returns an error" do
params = %{
name: "fabio",
nickname: "fabiobarboza",
email: "[email protected]",
age: 15
}

{:error, changeset} = Create.call(params)

expected_response = %{
age: ["must be greater than or equal to 18"],
password: ["can't be blank"]
}

assert errors_on(changeset) == expected_response
end
end
end
50 changes: 50 additions & 0 deletions test/rocketpay_web/controllers/accounts_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule RocketpayWeb.AccountsControllerTest do
use RocketpayWeb.ConnCase, async: true

alias Rocketpay.{Account, User}

describe "deposit/2" do
setup %{conn: conn} do
params = %{
name: "fabio",
password: "123456",
nickname: "fabiobarboza",
email: "[email protected]",
age: 27
}

{:ok, %User{account: %Account{id: account_id}}} = Rocketpay.create_user(params)

conn = put_req_header(conn, "authorization", "Basic YmFuYW5hOjEyMzQ1Ng==")

{:ok, conn: conn, account_id: account_id}
end

test "when all params are valid, make the deposit", %{conn: conn, account_id: account_id} do
params = %{"value" => "50.00"}

response =
conn
|> post(Routes.accounts_path(conn, :deposit, account_id, params))
|> json_response(:ok)

assert %{
"account" => %{"balance" => "50.00", "id" => _id},
"message" => "Ballance changed successfully"
} = response
end

test "when there are invalid params, return an error", %{conn: conn, account_id: account_id} do
params = %{"value" => "invalid"}

response =
conn
|> post(Routes.accounts_path(conn, :deposit, account_id, params))
|> json_response(:bad_request)

expected_response = %{"message" => "Invalid deposit value!"}

assert response == expected_response
end
end
end
38 changes: 38 additions & 0 deletions test/rocketpay_web/views/users_view_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
defmodule RocketpayWeb.UsersViewTest do
use RocketpayWeb.ConnCase, async: true

import Phoenix.View

alias Rocketpay.{Account, User}
alias RocketpayWeb.UsersView

test "renders create.json" do
params = %{
name: "fabio",
password: "123456",
nickname: "fabiobarboza",
email: "[email protected]",
age: 27
}

{:ok, %User{id: user_id, account: %Account{id: account_id}} = user} =
Rocketpay.create_user(params)

response = render(UsersView, "create.json", user: user)

expected_response = %{
message: "User created",
user: %{
account: %{
balance: Decimal.new("0.00"),
id: account_id
},
id: user_id,
name: "fabio",
nickname: "fabiobarboza"
}
}

assert response == expected_response
end
end

0 comments on commit 12bb586

Please sign in to comment.