Skip to content

Commit

Permalink
add tests #176
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Feb 6, 2022
1 parent f90ecb6 commit fd1aad1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/auth/init/init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,11 @@ defmodule Auth.Init do

case Person.get_person_by_email(email) do
# Ignore if the Super Admin already exists:
# coveralls-ignore-start
nil ->
%Person{}
|> Person.changeset(%{email: email})
|> Repo.insert!()

# coveralls-ignore-stop

person ->
person
end
Expand Down Expand Up @@ -116,8 +113,11 @@ defmodule Auth.Init do
|> Repo.update()

key

# coveralls-ignore-start
else
List.last(app.apikeys)
# coveralls-ignore-stop
end

key.client_id <> "/" <> key.client_secret <> "/" <> get_auth_url()
Expand Down
16 changes: 13 additions & 3 deletions lib/auth/init/roles.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Auth.InitRoles do

def create_default_roles do
roles = [
def roles do
[
%{
name: "superadmin",
desc: "With great power comes great responsibility",
Expand Down Expand Up @@ -59,8 +59,18 @@ defmodule Auth.InitRoles do
permissions: "manage_own_apps, create_content, upload_images, edit_own_content, delete_own_content, invite_people"
}
]
Enum.each(roles, fn role ->
end

def create_default_roles do
Enum.each(roles(), fn role ->
Auth.Role.upsert_role(role)
end)
end

# def delete_default_roles do
# Enum.each(roles(), fn role ->
# role = Auth.Role.get_role!(role.id)
# Auth.Role.delete_role(role)
# end)
# end
end
15 changes: 12 additions & 3 deletions lib/auth/init/statuses.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Auth.InitStatuses do

def insert_statuses do
statuses = [
def statuses do
[
%{
text: "verified",
id: "1",
Expand Down Expand Up @@ -103,8 +103,17 @@ defmodule Auth.InitStatuses do
desc: "an unexpected condition was encountered"
}
]
Enum.each(statuses, fn status ->
end

def insert_statuses do
Enum.each(statuses(), fn status ->
Auth.Status.upsert_status(status)
end)
end

# def delete_statuses do
# Enum.each(statuses(), fn status ->
# Auth.Status.delete_status(status)
# end)
# end
end
16 changes: 16 additions & 0 deletions lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,20 @@ defmodule Auth.Person do
end
end
end

@doc """
Deletes a person.
## Examples
iex> delete(person)
{:ok, %Role{}}
iex> delete(person)
{:error, %Ecto.Changeset{}}
"""
def delete(person) do
Repo.delete(person)
end
end
4 changes: 4 additions & 0 deletions lib/auth/status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ defmodule Auth.Status do
def list_statuses do
Repo.all(__MODULE__)
end

# def delete_status(status) do
# Repo.delete(status)
# end
end
2 changes: 2 additions & 0 deletions lib/auth_web/controllers/init_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ defmodule AuthWeb.InitController do
defp api_key_set?() do
IO.puts("AuthPlug.Token.api_key() #{AuthPlug.Token.api_key()}")
case AuthPlug.Token.api_key() do
# coveralls-ignore-start
nil ->
IO.puts("AuthPlug.Token.api_key() #{AuthPlug.Token.api_key()}")
false
# coveralls-ignore-stop

key ->
String.length(key) > 1
Expand Down
7 changes: 5 additions & 2 deletions lib/auth_web/templates/init/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
<table class="w-80 mt3 mw5 mw7-ns center">
<thead>
<tr class="bb b--silver">
<th class="tl pv3 f3 code">AUTH_API_KEY</th>
<th class="tl pv3 code">
<span class="f3">AUTH_API_KEY</span> <br />
<small>(AuthPlug.Token.api_key/1)</small>
</th>
<th class="center pl6">
<img width=30px" src="/images/<%=@api_key_set%>.png" class="center pl4">
<img width=30px" src="/images/<%=@api_key_set%>.png" class="center pr3">
</th>
</tr>
</thead>
Expand Down
12 changes: 12 additions & 0 deletions test/auth/init/init_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ defmodule Auth.InitTest do

describe "Initialize the Auth Database" do
test "main/0" do
Envar.set("MIX_ENV", "test")
assert Auth.Init.main() == :ok
end

test "Delete Everything and init again!" do
Envar.set("MIX_ENV", "test")
app = Auth.App.get_app!(1)
Auth.App.delete_app(app)
email = Envar.get("ADMIN_EMAIL")
person = Auth.Init.create_admin()
Auth.Person.delete(person)

assert Auth.Init.main() == :ok
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/auth_web/controllers/init_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule AuthWeb.InitControllerTest do
use AuthWeb.ConnCase, async: true

test "GET /init init (status) page", %{conn: conn} do
conn = get(conn, Routes.init_path(conn, :index))
assert conn.status == 200
assert conn.state == :sent
assert conn.resp_body =~ "REQUIRED Environment Variable"
end
end

0 comments on commit fd1aad1

Please sign in to comment.