Skip to content

Commit

Permalink
update seeds and tests to create App before API Key #97
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Sep 2, 2020
1 parent a4d5e58 commit b7188d0
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 39 deletions.
6 changes: 3 additions & 3 deletions lib/auth/apikey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ defmodule Auth.Apikey do
@doc false
def changeset(apikey, attrs) do
apikey
|> cast(attrs, [:client_id, :client_secret, :person_id, :app_id])
|> cast(attrs, [:client_id, :client_secret, :person_id])
|> validate_required([:client_secret])
# |> put_assoc(:app, attrs.app_id)
|> put_assoc(:app, Map.get(attrs, "app"))
end

def change_apikey(%Apikey{} = apikey) do
Expand Down Expand Up @@ -67,7 +67,7 @@ defmodule Auth.Apikey do
"""
def get_apikey!(id) do
Repo.get!(__MODULE__, id)
|> Repo.preload(:apps)
|> Repo.preload(:app)
end

@doc """
Expand Down
4 changes: 3 additions & 1 deletion lib/auth/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ defmodule Auth.App do
field :person_id, :id
field :status, :id
# field :apikey_id, :id
# has_many :apikeys, Auth.Status
has_many :apikeys, Auth.Apikey

timestamps()
end
Expand All @@ -27,6 +27,8 @@ defmodule Auth.App do
app
|> cast(attrs, [:name, :description, :url, :end])
|> validate_required([:name, :url])


end

@doc """
Expand Down
6 changes: 0 additions & 6 deletions lib/auth_web/templates/apikey/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
<thead>
<tr class="f3">
<th>AUTH_API_KEY</th>
<th>Name</th>
<th>Description</th>
<th>Url</th>
</tr>
</thead>
<tbody>
Expand All @@ -17,9 +14,6 @@
<td style="width:200px;">
<%= apikey.client_id %>/<%= apikey.client_secret %>
</td>
<td><%= apikey.name %></td>
<td><%= apikey.description %></td>
<td><%= apikey.url %></td>

<td>
<span><%= link "View", to: Routes.apikey_path(@conn, :show, apikey),
Expand Down
21 changes: 0 additions & 21 deletions lib/auth_web/templates/apikey/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@
</p>


<p>
<strong>Key Name:</strong>
<span class="db ba b--black-80 pa2 bg-light-gray w-100 mt2">
<%= @apikey.name %>
</span>
</p>

<p>
<strong>Description:</strong>
<span class="db ba b--black-80 pa2 bg-light-gray w-100 mt2">
<%= @apikey.description %>
</span>
</p>

<p>
<strong>URL:</strong>
<span class="db ba b--black-80 pa2 bg-light-gray w-100 mt2">
<%= @apikey.url %>
</span>
</p>


<span>
<%= link "< Back", to: Routes.apikey_path(@conn, :index),
Expand Down
10 changes: 7 additions & 3 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,19 @@ defmodule Auth.Seeds do
end

def create_apikey_for_admin(person) do
{:ok, key} =
{:ok, app} =
%{
"name" => "system admin key",
"name" => "default system app",
"description" => "Created by /priv/repo/seeds.exs during setup.",
"url" => "localhost:4000"
}
|> AuthWeb.ApikeyController.make_apikey(person.id)
|> Auth.App.create_app()

{:ok, key} = AuthWeb.ApikeyController.make_apikey(%{"app" => app}, person.id)
|> Auth.Apikey.create_apikey()

# IO.inspect(key, label: "key")

api_key = key.client_id <> "/" <> key.client_secret
# set the AUTH_API_KEY environment variable during test run:
if(Mix.env() == :test) do
Expand Down
9 changes: 4 additions & 5 deletions test/auth_web/controllers/apikey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule AuthWeb.ApikeyControllerTest do
# alias Auth.Apikey
# alias AuthWeb.ApikeyController, as: Ctrl
@email System.get_env("ADMIN_EMAIL")
@create_attrs %{description: "some description", name: "some name", url: "some url"}
@create_attrs %{description: "some description", name: "some name", url: "localhost"}
@update_attrs %{
client_secret: "updated client sec",
description: "some updated desc",
Expand Down Expand Up @@ -94,9 +94,11 @@ defmodule AuthWeb.ApikeyControllerTest do

describe "create apikey" do
test "redirects to show when data is valid", %{conn: conn} do
{:ok, app} = Auth.App.create_app(@create_attrs)

conn =
admin_login(conn)
|> post(Routes.apikey_path(conn, :create), apikey: @create_attrs)
|> post(Routes.apikey_path(conn, :create), apikey: %{"app" => app})

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.apikey_path(conn, :show, id)
Expand Down Expand Up @@ -162,9 +164,6 @@ defmodule AuthWeb.ApikeyControllerTest do

conn = put(conn, Routes.apikey_path(conn, :update, key.id), apikey: @update_attrs)
assert redirected_to(conn) == Routes.apikey_path(conn, :show, key)

conn = get(conn, Routes.apikey_path(conn, :show, key))
assert html_response(conn, 200) =~ "some updated desc"
end

test "renders errors when data is invalid", %{conn: conn} do
Expand Down

0 comments on commit b7188d0

Please sign in to comment.