Skip to content

Commit

Permalink
Add controller allow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arbulu89 committed Jul 3, 2024
1 parent beb7e7d commit 06fc806
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/trento_web/controllers/v1/database_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,41 @@ defmodule TrentoWeb.V1.DatabaseControllerTest do
|> json_response(404)
|> assert_schema("NotFound", api_spec)
end

test "should allow the request when the user has cleanup:database_instance ability", %{
conn: conn
} do
%{id: database_id} = build(:database)

%{host_id: host_id, instance_number: instance_number} =
build(:database_instance, database_id: database_id)

%{id: user_id} = insert(:user)

%{id: ability_id} = insert(:ability, name: "cleanup", resource: "database_instance")
insert(:users_abilities, user_id: user_id, ability_id: ability_id)

conn =
conn
|> Pow.Plug.assign_current_user(%{"user_id" => user_id}, Pow.Plug.fetch_config(conn))
|> put_req_header("content-type", "application/json")

expect(
Trento.Commanded.Mock,
:dispatch,
fn %DeregisterDatabaseInstance{
database_id: ^database_id,
host_id: ^host_id,
instance_number: ^instance_number
} ->
:ok
end
)

conn
|> delete("/api/v1/databases/#{database_id}/hosts/#{host_id}/instances/#{instance_number}")
|> response(204)
end
end

describe "forbidden response" do
Expand Down
28 changes: 28 additions & 0 deletions test/trento_web/controllers/v1/host_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,34 @@ defmodule TrentoWeb.V1.HostControllerTest do
|> json_response(404)
|> assert_schema("NotFound", api_spec)
end

test "should allow the request when the user has cleanup:host ability", %{
conn: conn
} do
%{id: host_id} = insert(:host)

%{id: user_id} = insert(:user)

%{id: ability_id} = insert(:ability, name: "cleanup", resource: "host")
insert(:users_abilities, user_id: user_id, ability_id: ability_id)

conn =
conn
|> Pow.Plug.assign_current_user(%{"user_id" => user_id}, Pow.Plug.fetch_config(conn))
|> put_req_header("content-type", "application/json")

expect(
Trento.Commanded.Mock,
:dispatch,
fn %RequestHostDeregistration{host_id: ^host_id} ->
:ok
end
)

conn
|> delete("/api/v1/hosts/#{host_id}")
|> response(204)
end
end

describe "forbidden response" do
Expand Down
37 changes: 37 additions & 0 deletions test/trento_web/controllers/v1/sap_system_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,43 @@ defmodule TrentoWeb.V1.SapSystemControllerTest do
|> json_response(404)
|> assert_schema("NotFound", api_spec)
end

test "should allow the request when the user has cleanup:application_instance ability", %{
conn: conn
} do
%{id: sap_system_id} = build(:sap_system)

%{host_id: host_id, instance_number: instance_number} =
build(:application_instance, sap_system_id: sap_system_id)

%{id: user_id} = insert(:user)

%{id: ability_id} = insert(:ability, name: "cleanup", resource: "application_instance")
insert(:users_abilities, user_id: user_id, ability_id: ability_id)

conn =
conn
|> Pow.Plug.assign_current_user(%{"user_id" => user_id}, Pow.Plug.fetch_config(conn))
|> put_req_header("content-type", "application/json")

expect(
Trento.Commanded.Mock,
:dispatch,
fn %DeregisterApplicationInstance{
sap_system_id: ^sap_system_id,
host_id: ^host_id,
instance_number: ^instance_number
} ->
:ok
end
)

conn
|> delete(
"/api/v1/sap_systems/#{sap_system_id}/hosts/#{host_id}/instances/#{instance_number}"
)
|> response(204)
end
end

describe "forbidden response" do
Expand Down

0 comments on commit 06fc806

Please sign in to comment.