Skip to content

Commit

Permalink
create test for list_apikeys_for_person/1 #42
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 26, 2020
1 parent 36f87d4 commit d2842e7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 44 deletions.
22 changes: 11 additions & 11 deletions lib/auth/apikey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Auth.Apikey do
end

def list_apikeys_for_person(person_id) do
IO.inspect(person_id, label: "person_id")
# IO.inspect(person_id, label: "person_id")
query = from(
a in __MODULE__,
where: a.person_id == ^person_id,
Expand All @@ -45,20 +45,20 @@ defmodule Auth.Apikey do
Repo.all(query)
end

@doc """
Gets a single apikey.
@doc """
Gets a single apikey.
Raises `Ecto.NoResultsError` if the Apikey does not exist.
Raises `Ecto.NoResultsError` if the Apikey does not exist.
## Examples
## Examples
iex> get_apikey!(123)
%Apikey{}
iex> get_apikey!(123)
%Apikey{}
iex> get_apikey!(456)
** (Ecto.NoResultsError)
iex> get_apikey!(456)
** (Ecto.NoResultsError)
"""
def get_apikey!(id), do: Repo.get!(__MODULE__, id)
"""
def get_apikey!(id), do: Repo.get!(__MODULE__, id)

end
3 changes: 2 additions & 1 deletion lib/auth_web/controllers/apikey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule AuthWeb.ApikeyController do
end

def create(conn, %{"apikey" => apikey_params}) do
IO.inspect(apikey_params, label: "apikey_params")
# IO.inspect(apikey_params, label: "apikey_params")
person_id = conn.assigns.decoded.id

params = Map.merge(apikey_params, %{
Expand All @@ -52,6 +52,7 @@ defmodule AuthWeb.ApikeyController do

def show(conn, %{"id" => id}) do
apikey = Apikey.get_apikey!(id)
# combined = apikey.client_id <> "/" <> apikey.client_secret
render(conn, "show.html", apikey: apikey)
end

Expand Down
1 change: 1 addition & 0 deletions lib/auth_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule AuthWeb.PageController do
def admin(conn, params) do
# IO.inspect(conn.req_headers, label: "conn.req_headers")
IO.inspect(params, label: "params")
IO.inspect(conn.assigns)

conn
|> put_view(AuthWeb.PageView)
Expand Down
9 changes: 6 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,20 @@ defmodule Auth.Mixfile do
# https://github.com/dwyl/elixir-auth-google
{:elixir_auth_google, "~> 1.2.0"},
# https://github.com/dwyl/auth_plug
{:auth_plug, "~> 0.9.0"},
{:auth_plug, "~> 0.11.0"},

# Field Validation and Encryption: github.com/dwyl/fields
{:fields, "~> 2.4.0"},
{:exbase58, "~> 1.0"}, # pending: github.com/dwyl/base58/pull/17
{:exbase58, "~> 1.0.2"}, # pending: github.com/dwyl/base58/pull/17

# Check test coverage
{:excoveralls, "~> 0.12.3", only: :test},

# Property based tests: github.com/dwyl/learn-property-based-testing
{:stream_data, "~> 0.4.3", only: :test},

# Create Documentation for publishing Hex.docs:
{:ex_doc, "~> 0.21.3", only: :dev}
{:ex_doc, "~> 0.21.3", only: :dev},
]
end

Expand Down
4 changes: 3 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%{
"argon2_elixir": {:hex, :argon2_elixir, "2.3.0", "e251bdafd69308e8c1263e111600e6d68bd44f23d2cccbe43fcb1a417a76bc8e", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "28ccb63bff213aecec1f7f3dde9648418b031f822499973281d8f494b9d5a3b3"},
"auth_plug": {:hex, :auth_plug, "0.9.0", "0c1279201b8393d9ad930e81e1c4ab716b388d24c8b3d687587812a87a06295e", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "2eed448c5227aebf03d9d95ec219bd1d4651a43f716e9f646d1121d46ad50f4d"},
"auth_plug": {:hex, :auth_plug, "0.11.0", "d1a6596ad35de45929432018fa1b0bf4e642fc2f92ef5a1f24bd6117bb01db96", [:mix], [{:joken, "~> 2.2.0", [hex: :joken, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "b0a95785243915ad80b76588de60c3e0dcbca17e19367b6a610d1d84de43d83c"},
"certifi": {:hex, :certifi, "2.5.1", "867ce347f7c7d78563450a18a6a28a8090331e77fa02380b4a21962a65d36ee5", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "805abd97539caf89ec6d4732c91e62ba9da0cda51ac462380bbd28ee697a8c42"},
"comeonin": {:hex, :comeonin, "5.3.1", "7fe612b739c78c9c1a75186ef2d322ce4d25032d119823269d0aa1e2f1e20025", [:mix], [], "hexpm", "d6222483060c17f0977fad1b7401ef0c5863c985a64352755f366aee3799c245"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
Expand All @@ -15,6 +15,7 @@
"elixir_auth_google": {:hex, :elixir_auth_google, "1.2.0", "99672af2cadcf18bad10548a75667dada2f802cf1a8c519d2283346c8b8e99b8", [:mix], [{:httpoison, "~> 1.6.2", [hex: :httpoison, repo: "hexpm", optional: false]}, {:poison, "~> 4.0.1", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "8720fd473f471a9a03d6630fc3c8b90dff5e089047e153dc1f78ed68a68740a9"},
"elixir_make": {:hex, :elixir_make, "0.6.0", "38349f3e29aff4864352084fc736fa7fa0f2995a819a737554f7ebd28b85aaab", [:mix], [], "hexpm", "d522695b93b7f0b4c0fcb2dfe73a6b905b1c301226a5a55cb42e5b14d509e050"},
"ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"},
"exbase58": {:hex, :exbase58, "1.0.2", "2caa5df4d769b5c555cde11b85e93199037ed8b41f1da23e812619c10e3a3424", [:mix], [], "hexpm", "fe6b6b465750bdc1bd01c7b33b265902dabd63061f7db24e663509b45b4bba3c"},
"excoveralls": {:hex, :excoveralls, "0.12.3", "2142be7cb978a3ae78385487edda6d1aff0e482ffc6123877bb7270a8ffbcfe0", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "568a3e616c264283f5dea5b020783ae40eef3f7ee2163f7a67cbd7b35bcadada"},
"fields": {:hex, :fields, "2.4.0", "af1f26a56d2462888c9838de1d7a6afc6deaf624b4aead8901c388d2d2ddfbfc", [:mix], [{:argon2_elixir, "~> 2.3.0", [hex: :argon2_elixir, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:html_sanitize_ex, "~> 1.4", [hex: :html_sanitize_ex, repo: "hexpm", optional: false]}], "hexpm", "48bf472db9d5c221b635a36d01a2a832a933a9d5ad815fab5f0cb6e63a85bca8"},
"file_system": {:hex, :file_system, "0.2.8", "f632bd287927a1eed2b718f22af727c5aeaccc9a98d8c2bd7bff709e851dc986", [:mix], [], "hexpm", "97a3b6f8d63ef53bd0113070102db2ce05352ecf0d25390eb8d747c2bde98bca"},
Expand Down Expand Up @@ -46,6 +47,7 @@
"postgrex": {:hex, :postgrex, "0.15.3", "5806baa8a19a68c4d07c7a624ccdb9b57e89cbc573f1b98099e3741214746ae4", [: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", "4737ce62a31747b4c63c12b20c62307e51bb4fcd730ca0c32c280991e0606c90"},
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm", "13104d7897e38ed7f044c4de953a6c28597d1c952075eb2e328bc6d6f2bfc496"},
"stream_data": {:hex, :stream_data, "0.4.3", "62aafd870caff0849a5057a7ec270fad0eb86889f4d433b937d996de99e3db25", [:mix], [], "hexpm", "7dafd5a801f0bc897f74fcd414651632b77ca367a7ae4568778191fc3bf3a19a"},
"telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm", "1d1848c40487cdb0b30e8ed975e34e025860c02e419cb615d255849f3427439d"},
}
45 changes: 37 additions & 8 deletions test/auth/apikey_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
# defmodule Auth.CtxTest do
# use Auth.DataCase
defmodule Auth.ApikeyTest do
# use Auth.DataCase
use AuthWeb.ConnCase
# alias Auth.Apikey
@email System.get_env("ADMIN_EMAIL")

test "list_apikeys_for_person/1 returns all apikeys person" do
person = Auth.Person.get_person_by_email(@email)
# IO.inspect(person, label: "person")

keys = Auth.Apikey.list_apikeys_for_person(person.id)
# IO.inspect(keys, label: "keys")
assert keys == []

# Insert Two API keys:
params = %{
# "description" => "test key",
"name" => "My Amazing Key",
"url" => "http://localhost:400",
"person_id" => person.id,
"client_secret" => AuthWeb.ApikeyController.encrypt_encode(person.id)
}
Auth.Apikey.create_apikey(params)

Map.merge(params, %{
"client_secret" => AuthWeb.ApikeyController.encrypt_encode(person.id)
}) |> Auth.Apikey.create_apikey()

keys = Auth.Apikey.list_apikeys_for_person(person.id)
assert length(keys) == 2
end



#
# alias Auth.Ctx
#
# describe "apikeys" do
# alias Auth.Ctx.Apikey
# alias Auth.Apikey
#
# @valid_attrs %{client_secret: "some client_secret", description: "some description", key_id: 42, name: "some name", url: "some url"}
# @update_attrs %{client_secret: "some updated client_secret", description: "some updated description", key_id: 43, name: "some updated name", url: "some updated url"}
Expand All @@ -19,10 +51,7 @@
# apikey
# end
#
# test "list_apikeys/0 returns all apikeys" do
# apikey = apikey_fixture()
# assert Ctx.list_apikeys() == [apikey]
# end

#
# test "get_apikey!/1 returns the apikey with given id" do
# apikey = apikey_fixture()
Expand Down Expand Up @@ -69,4 +98,4 @@
# assert %Ecto.Changeset{} = Ctx.change_apikey(apikey)
# end
# end
# end
end
55 changes: 35 additions & 20 deletions test/auth_web/controllers/apikey_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
defmodule AuthWeb.ApikeyControllerTest do
use AuthWeb.ConnCase
use ExUnitProperties

# alias Auth.Apikey
import AuthWeb.ApikeyController
@email System.get_env("ADMIN_EMAIL")

describe "Create a DWYL_API_KEY for a given person_id" do
test "encrypt_encode/1 returns a base58 we can decrypt" do
Expand Down Expand Up @@ -35,18 +37,24 @@ defmodule AuthWeb.ApikeyControllerTest do
end

test "decrypt_api_key/1 decrypts a DWYL_API_KEY" do
person_id = 123456789
key = create_api_key(person_id)
person_id = 1234
key = create_api_key(person_id) |> IO.inspect()
# IO.inspect(String.length(key), label: "String.length(key)")
decrypted = decrypt_api_key(key) # |> IO.inspect()
assert decrypted == person_id
end

property "Check a batch of int values can be decoded decode_decrypt/1" do
check all(int <- integer()) do
assert decode_decrypt(encrypt_encode(int)) == int
end
end
end



# @create_attrs %{client_secret: "some client_secret", description: "some description", key_id: 42, name: "some name", url: "some url"}
# @update_attrs %{client_secret: "some updated client_secret", description: "some updated description", key_id: 43, name: "some updated name", url: "some updated url"}
@create_attrs %{description: "some description", name: "some name", url: "some url"}
# @update_attrs %{client_secret: "some updated client_secret", description: "some updated description", key_id: 43, name: "some updated name", url: "surl"}
# @invalid_attrs %{client_secret: nil, description: nil, key_id: nil, name: nil, url: nil}
#
# def fixture(:apikey) do
Expand All @@ -68,22 +76,29 @@ defmodule AuthWeb.ApikeyControllerTest do
# end
# end
#
# describe "create apikey" do
# test "redirects to show when data is valid", %{conn: conn} do
# conn = post(conn, Routes.apikey_path(conn, :create), apikey: @create_attrs)
#
# assert %{id: id} = redirected_params(conn)
# assert redirected_to(conn) == Routes.apikey_path(conn, :show, id)
#
# conn = get(conn, Routes.apikey_path(conn, :show, id))
# assert html_response(conn, 200) =~ "Show Apikey"
# end
#
# test "renders errors when data is invalid", %{conn: conn} do
# conn = post(conn, Routes.apikey_path(conn, :create), apikey: @invalid_attrs)
# assert html_response(conn, 200) =~ "New Apikey"
# end
# end
describe "create apikey" do

test "redirects to show when data is valid", %{conn: conn} do
person = Auth.Person.get_person_by_email(@email)
conn = AuthPlug.create_jwt_session(conn, %{email: @email, id: person.id})

conn = post(conn, Routes.apikey_path(conn, :create), apikey: @create_attrs)

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.apikey_path(conn, :show, id)

conn = get(conn, Routes.apikey_path(conn, :show, id))
assert html_response(conn, 200) =~ "Your DWYL_API_KEY"
end

# test "renders errors when data is invalid", %{conn: conn} do
# person = Auth.Person.get_person_by_email(@email)
# conn = AuthPlug.create_jwt_session(conn, %{email: @email, id: person.id})
#
# conn = post(conn, Routes.apikey_path(conn, :create), apikey: @invalid_attrs)
# assert html_response(conn, 200) =~ "New Apikey"
# end
end
#
# describe "edit apikey" do
# setup [:create_apikey]
Expand Down

0 comments on commit d2842e7

Please sign in to comment.