Skip to content

Commit

Permalink
Log retention time changes (#2858)
Browse files Browse the repository at this point in the history
* Add successful changes to the activity log retention time to the activity log catalog

* Extract request body as activity log metadata for :activity_log_settings_update activity

* Add test for logging changing retention time activity
  • Loading branch information
nelsonkopliku authored Aug 6, 2024
1 parent 98e4387 commit bf13a09
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/trento/activity_logging/activity_catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ defmodule Trento.ActivityLog.ActivityCatalog do
{TrentoWeb.V1.UsersController, :delete} => {:user_deletion, 204},
{TrentoWeb.V1.ProfileController, :update} => {:profile_update, 200},
{TrentoWeb.V1.ClusterController, :request_checks_execution} =>
{:cluster_checks_execution_request, 202}
{:cluster_checks_execution_request, 202},
{TrentoWeb.V1.SettingsController, :update_activity_log_settings} =>
{:activity_log_settings_update, 200}
}
end
end
5 changes: 3 additions & 2 deletions lib/trento/activity_logging/parser/phoenix_conn_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ defmodule Trento.ActivityLog.Logger.Parser.PhoenixConnParser do
end

def get_activity_metadata(
:api_key_generation,
activity,
%Plug.Conn{
body_params: request_body
}
) do
)
when activity in [:api_key_generation, :activity_log_settings_update] do
request_body
end

Expand Down
3 changes: 2 additions & 1 deletion test/trento/activity_logging/activity_catalog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Trento.ActivityLog.ActivityCatalogTest do
:user_modification,
:user_deletion,
:profile_update,
:cluster_checks_execution_request
:cluster_checks_execution_request,
:activity_log_settings_update
]

connection_activity_catalog = ActivityCatalog.connection_activities()
Expand Down
57 changes: 57 additions & 0 deletions test/trento/activity_logging/activity_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,63 @@ defmodule Trento.ActivityLog.ActivityLoggerTest do
end
end

describe "changing activity log retention time detection" do
defp change_retention_time(conn, retention_time) do
conn
|> put_req_header("content-type", "application/json")
|> put("/api/v1/settings/activity_log", retention_time)
end

test "should not log a failing update of the activity log settings",
%{conn: conn, user: %{id: user_id}} do
insert(:activity_log_settings)

conn
|> with_token(user_id)
|> change_retention_time(%{
retention_time: %{
value: 42,
unit: :invalid_unit
}
})
|> json_response(422)

wait_for_tasks_completion()

assert [] = Trento.Repo.all(ActivityLog)
end

test "should log a successful update of the activity log settings",
%{conn: conn, user: %{id: user_id, username: username}} do
insert(:activity_log_settings)

conn
|> with_token(user_id)
|> change_retention_time(%{
retention_time: %{
value: 42,
unit: :day
}
})
|> json_response(200)

wait_for_tasks_completion()

assert [
%ActivityLog{
type: "activity_log_settings_update",
actor: ^username,
metadata: %{
"retention_time" => %{
"unit" => "day",
"value" => 42
}
}
}
] = Trento.Repo.all(ActivityLog)
end
end

test "domain event activity logging" do
heartbeat_succeeded_event = build(:heartbeat_succeded)
heartbeat_failed_event = build(:heartbeat_failed)
Expand Down
14 changes: 14 additions & 0 deletions test/trento/activity_logging/phoenix_conn_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ defmodule Trento.ActivityLog.PhoenixConnParserTest do
end
end

describe "metadata detection" do
test "should extract the request body as metadata for relevant activities", %{conn: conn} do
for activity <- [:api_key_generation, :activity_log_settings_update] do
request_body = %{"foo" => "bar"}

assert request_body ==
PhoenixConnParser.get_activity_metadata(activity, %Plug.Conn{
conn
| body_params: request_body
})
end
end
end

defp assert_for_relevant_activity(assertion_function) do
ActivityCatalog.connection_activities()
|> Enum.filter(&(&1 != :login_attempt))
Expand Down

0 comments on commit bf13a09

Please sign in to comment.