From b7188d0f152f86be5339d0f66721e99b01d0be47 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Wed, 2 Sep 2020 10:37:59 +0100 Subject: [PATCH] update seeds and tests to create App before API Key #97 --- lib/auth/apikey.ex | 6 +++--- lib/auth/app.ex | 4 +++- lib/auth_web/templates/apikey/index.html.eex | 6 ------ lib/auth_web/templates/apikey/show.html.eex | 21 ------------------- priv/repo/seeds.exs | 10 ++++++--- .../controllers/apikey_controller_test.exs | 9 ++++---- 6 files changed, 17 insertions(+), 39 deletions(-) diff --git a/lib/auth/apikey.ex b/lib/auth/apikey.ex index 307ab315..b4692648 100644 --- a/lib/auth/apikey.ex +++ b/lib/auth/apikey.ex @@ -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 @@ -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 """ diff --git a/lib/auth/app.ex b/lib/auth/app.ex index b9ab7b86..c5ffb36d 100644 --- a/lib/auth/app.ex +++ b/lib/auth/app.ex @@ -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 @@ -27,6 +27,8 @@ defmodule Auth.App do app |> cast(attrs, [:name, :description, :url, :end]) |> validate_required([:name, :url]) + + end @doc """ diff --git a/lib/auth_web/templates/apikey/index.html.eex b/lib/auth_web/templates/apikey/index.html.eex index 5b9450b8..0fd7724b 100644 --- a/lib/auth_web/templates/apikey/index.html.eex +++ b/lib/auth_web/templates/apikey/index.html.eex @@ -6,9 +6,6 @@ AUTH_API_KEY - Name - Description - Url @@ -17,9 +14,6 @@ <%= apikey.client_id %>/<%= apikey.client_secret %> - <%= apikey.name %> - <%= apikey.description %> - <%= apikey.url %> <%= link "View", to: Routes.apikey_path(@conn, :show, apikey), diff --git a/lib/auth_web/templates/apikey/show.html.eex b/lib/auth_web/templates/apikey/show.html.eex index 96740c0d..b8c1377d 100644 --- a/lib/auth_web/templates/apikey/show.html.eex +++ b/lib/auth_web/templates/apikey/show.html.eex @@ -25,27 +25,6 @@

-

- Key Name: - - <%= @apikey.name %> - -

- -

- Description: - - <%= @apikey.description %> - -

- -

- URL: - - <%= @apikey.url %> - -

- <%= link "< Back", to: Routes.apikey_path(@conn, :index), diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index 41152667..51a89c3a 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -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 diff --git a/test/auth_web/controllers/apikey_controller_test.exs b/test/auth_web/controllers/apikey_controller_test.exs index 1d717704..64464346 100644 --- a/test/auth_web/controllers/apikey_controller_test.exs +++ b/test/auth_web/controllers/apikey_controller_test.exs @@ -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", @@ -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) @@ -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