Skip to content

Commit

Permalink
run "mix format" to keep SourceLevel happy #85 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 20, 2020
1 parent 1d6a259 commit 812e192
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 108 deletions.
5 changes: 1 addition & 4 deletions lib/auth/people_roles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Auth.PeopleRoles do
timestamps()
end


@doc """
grant_role/3 grants a role to the given person
the conn must have conn.assigns.person to check for admin in order to grant the role.
Expand All @@ -34,7 +33,5 @@ defmodule Auth.PeopleRoles do
else
AuthWeb.AuthController.unauthorized(conn)
end

end

end
end
1 change: 0 additions & 1 deletion lib/auth/permission.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,4 @@ defmodule Auth.Permission do
def change_permission(%Permission{} = permission, attrs \\ %{}) do
Permission.changeset(permission, attrs)
end

end
7 changes: 2 additions & 5 deletions lib/auth/role.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ defmodule Auth.Role do
|> validate_required([:name, :desc])
end


@doc """
@doc """
Returns the list of roles.
## Examples
Expand Down Expand Up @@ -117,12 +116,10 @@ defmodule Auth.Role do
Role.changeset(role, attrs)
end


# @doc """
# grants the default "subscriber" (6) role to the person
# """
# def set_default_role(person) do

# end

# end
end
2 changes: 1 addition & 1 deletion lib/auth_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ defmodule AuthWeb.AuthController do
Enum.filter(apikeys, fn k ->
# if the API Key belongs to Super Admin, don't check URL as it's the "setup key":
if person_id == 1 do
k.client_id == client_id
k.client_id == client_id
else
# check url matches the state for all other keys:
k.client_id == client_id and state =~ k.url
Expand Down
2 changes: 1 addition & 1 deletion lib/auth_web/controllers/ping_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ defmodule AuthWeb.PingController do
def ping(conn, params) do
Ping.render_pixel(conn, params)
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ defmodule Auth.Mixfile do
{:ping, "~> 1.0.1"},

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

#  Property based tests: github.com/dwyl/learn-property-based-testing
{:stream_data, "~> 0.4.3", only: :test},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule Auth.Repo.Migrations.CreateRolePermissions do
add :role_id, references(:roles, on_delete: :nothing)
add :permission_id, references(:permissions, on_delete: :nothing)
add :granter_id, references(:people, on_delete: :nothing)

timestamps()
end

create unique_index(:role_permissions, [:role_id, :permission_id])
end
end
4 changes: 2 additions & 2 deletions priv/repo/migrations/20200723154847_create_people_roles.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule Auth.Repo.Migrations.CreatePeopleRoles do
add :person_id, references(:people, on_delete: :nothing)
add :role_id, references(:roles, on_delete: :nothing)
add :granter_id, references(:people, on_delete: :nothing)

timestamps()
end

create unique_index(:people_roles, [:person_id, :role_id])
end
end
38 changes: 20 additions & 18 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ defmodule Auth.Seeds do
person ->
person
end

if(Mix.env() == :test) do
# don't print noise during tests
else
IO.inspect(person.id, label: "seeds.exs person.id")
IO.puts("- - - - - - - - - - - - - - - - - - - - - - ")
end

person
end

Expand All @@ -59,16 +60,19 @@ defmodule Auth.Seeds do
# write the key:value pair to project .env file
def write_env(key, value) do
# IO.inspect(File.cwd!, label: "cwd")
path = File.cwd! <> "/.env"
path = File.cwd!() <> "/.env"
IO.inspect(path, label: ".env file path")
{:ok, data} = File.read(path)
# IO.inspect(data)

lines = String.split(data, "\n")
|> Enum.filter(fn line ->
not String.contains?(line, key)
end)
str = "export #{key}=#{value}" # |> IO.inspect
lines =
String.split(data, "\n")
|> Enum.filter(fn line ->
not String.contains?(line, key)
end)

# |> IO.inspect
str = "export #{key}=#{value}"
vars = lines ++ [str]
content = Enum.join(vars, "\n")
File.write!(path, content) |> File.close()
Expand All @@ -78,10 +82,11 @@ defmodule Auth.Seeds do
# export all the environment variables during app excution/tests
def env(vars) do
Enum.map(vars, fn line ->
parts = line
|> String.replace("export ", "")
|> String.replace("'", "")
|> String.split("=")
parts =
line
|> String.replace("export ", "")
|> String.replace("'", "")
|> String.split("=")

# IO.inspect(List.last(parts), label: List.first(parts))
System.put_env(List.first(parts), List.last(parts))
Expand All @@ -92,14 +97,13 @@ end
Auth.Seeds.create_admin()
|> Auth.Seeds.create_apikey_for_admin()


# scripts for creating default roles and permissions
defmodule SetupRoles do
alias Auth.Role

def get_json(filepath) do
# IO.inspect(filepath, label: "filepath")
path = File.cwd! <> filepath
path = File.cwd!() <> filepath
# IO.inspect(path, label: "path")
{:ok, data} = File.read(path)
json = Jason.decode!(data)
Expand All @@ -109,17 +113,15 @@ defmodule SetupRoles do

def create_default_roles() do
json = get_json("/priv/repo/default_roles.json")
Enum.each(json, fn role ->

Enum.each(json, fn role ->
Role.create_role(role)
# |> IO.inspect()
end)
end

def assign_superadmin_role() do

end


end

SetupRoles.create_default_roles()
SetupRoles.create_default_roles()
5 changes: 2 additions & 3 deletions test/auth/people_roles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ defmodule AuthWeb.PeopleRolesTest do
test "attempt to grant_role/3 without admin should 401", %{conn: conn} do
alex = %{email: "[email protected]", auth_provider: "email"}
grantee = Auth.Person.create_person(alex)
conn = assign(conn, :person, grantee) #
conn = assign(conn, :person, grantee)
role_id = 4
conn = Auth.PeopleRoles.grant_role(conn, grantee.id, role_id)

assert conn.status == 401
end

end
end
32 changes: 14 additions & 18 deletions test/auth/role_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ defmodule Auth.RoleTest do

test "update_role/2 with invalid data returns error changeset" do
role = role_fixture()
assert {:error, %Ecto.Changeset{}} =
Role.update_role(role, @invalid_attrs)
assert {:error, %Ecto.Changeset{}} = Role.update_role(role, @invalid_attrs)
assert role == Role.get_role!(role.id)
end

Expand Down Expand Up @@ -91,37 +90,40 @@ defmodule Auth.RoleTest do
end

test "create_permission/1 with valid data creates a permission" do
assert {:ok, %Permission{} = permission} =
Permission.create_permission(@valid_attrs)
assert {:ok, %Permission{} = permission} = Permission.create_permission(@valid_attrs)
assert permission.desc == "some desc"
assert permission.name == "some name"
end

test "create_permission/1 with invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} =
Permission.create_permission(@invalid_attrs)
assert {:error, %Ecto.Changeset{}} = Permission.create_permission(@invalid_attrs)
end

test "update_permission/2 with valid data updates the permission" do
permission = permission_fixture()
assert {:ok, %Permission{} = permission} =
Permission.update_permission(permission, @update_attrs)

assert {:ok, %Permission{} = permission} =
Permission.update_permission(permission, @update_attrs)

assert permission.desc == "some updated desc"
assert permission.name == "some updated name"
end

test "update_permission/2 with invalid data returns error changeset" do
permission = permission_fixture()
assert {:error, %Ecto.Changeset{}} =
Permission.update_permission(permission, @invalid_attrs)

assert {:error, %Ecto.Changeset{}} =
Permission.update_permission(permission, @invalid_attrs)

assert permission == Permission.get_permission!(permission.id)
end

test "delete_permission/1 deletes the permission" do
permission = permission_fixture()
assert {:ok, %Permission{}} = Permission.delete_permission(permission)
assert_raise Ecto.NoResultsError, fn ->
Permission.get_permission!(permission.id)

assert_raise Ecto.NoResultsError, fn ->
Permission.get_permission!(permission.id)
end
end

Expand All @@ -131,16 +133,10 @@ defmodule Auth.RoleTest do
end
end


# create a new person and confirm they were asigned a default role of "subscriber"



# describe "grant role" do




# # test "change_permission/1 returns a permission changeset" do
# # permission = permission_fixture()
# # assert %Ecto.Changeset{} = Permission.change_permission(permission)
Expand Down
17 changes: 10 additions & 7 deletions test/auth_web/controllers/apikey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,29 @@ defmodule AuthWeb.ApikeyControllerTest do
#
describe "index" do
test "lists all apikeys", %{conn: conn} do
conn = admin_login(conn)
|> get(Routes.apikey_path(conn, :index))
conn =
admin_login(conn)
|> get(Routes.apikey_path(conn, :index))

assert html_response(conn, 200) =~ "Auth API Keys"
end
end

describe "new apikey" do
test "renders form", %{conn: conn} do
conn = admin_login(conn)
|> get(Routes.apikey_path(conn, :new))
conn =
admin_login(conn)
|> get(Routes.apikey_path(conn, :new))

assert html_response(conn, 200) =~ "New Apikey"
end
end

describe "create apikey" do
test "redirects to show when data is valid", %{conn: conn} do
conn = admin_login(conn)
|> post(Routes.apikey_path(conn, :create), apikey: @create_attrs)
conn =
admin_login(conn)
|> post(Routes.apikey_path(conn, :create), apikey: @create_attrs)

assert %{id: id} = redirected_params(conn)
assert redirected_to(conn) == Routes.apikey_path(conn, :show, id)
Expand Down Expand Up @@ -134,7 +137,7 @@ defmodule AuthWeb.ApikeyControllerTest do
auth_provider: "email"
})

conn = AuthPlug.create_jwt_session(conn, wrong_person)
conn = AuthPlug.create_jwt_session(conn, wrong_person)

{:ok, key} =
%{"name" => "test key", "url" => "http://localhost:4000"}
Expand Down
30 changes: 17 additions & 13 deletions test/auth_web/controllers/auth_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ defmodule AuthWeb.AuthControllerTest do
}

person = Auth.Person.create_person(data)
conn = AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
|> get("/profile", %{})

conn =
AuthPlug.create_jwt_session(conn, Map.merge(data, %{id: person.id}))
|> get("/profile", %{})

assert html_response(conn, 200) =~ "Google account"
end
Expand All @@ -50,19 +52,20 @@ defmodule AuthWeb.AuthControllerTest do
end

test "get_client_secret(client_id, state) gets the secret for the given client_id" do
person =
Auth.Person.create_person(%{
email: "[email protected]",
auth_provider: "email"
})

person = Auth.Person.create_person(%{
email: "[email protected]",
auth_provider: "email"
})

{:ok, key} = %{"name" => "test key", "url" => "example.com"}
|> AuthWeb.ApikeyController.make_apikey(person.id)
|> Auth.Apikey.create_apikey()
{:ok, key} =
%{"name" => "test key", "url" => "example.com"}
|> AuthWeb.ApikeyController.make_apikey(person.id)
|> Auth.Apikey.create_apikey()

state = "https://www.example.com/profile?auth_client_id=#{key.client_id}"
secret = AuthWeb.AuthController.get_client_secret(key.client_id, state)

assert secret == key.client_secret
end

Expand Down Expand Up @@ -126,8 +129,9 @@ defmodule AuthWeb.AuthControllerTest do

person = Auth.Person.upsert_person(data)

conn = AuthPlug.create_jwt_session(conn, person)
|> get("/auth/google/callback", %{"code" => "234", "state" => nil})
conn =
AuthPlug.create_jwt_session(conn, person)
|> get("/auth/google/callback", %{"code" => "234", "state" => nil})

assert html_response(conn, 200) =~ "Google account"
end
Expand Down
Loading

0 comments on commit 812e192

Please sign in to comment.