-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implements context function * Implements the view * Implements the controller * Adds an entry to the router * Adds tests * Fixes field value in view * Fixes type signature * Make credo happy Small fixes about parentheses of zero arity functions and ordering of aliases in alphabetical order. * Fixes tests; renames openapi spec tags * Addresses PR comments - Single view file, instead of two - Adds title field in spec * Renames field in schema According to PR comment * Fixes spelling * Fixes more spellings
- Loading branch information
1 parent
c75617d
commit bc874f1
Showing
8 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule TrentoWeb.V1.ActivityLogController do | ||
use TrentoWeb, :controller | ||
use OpenApiSpex.ControllerSpecs | ||
|
||
alias Trento.ActivityLog | ||
alias TrentoWeb.OpenApi.V1.Schema | ||
|
||
plug OpenApiSpex.Plug.CastAndValidate, json_render_error_v2: true | ||
action_fallback TrentoWeb.FallbackController | ||
|
||
operation :get_activity_log, | ||
summary: "Fetches the Activity Log entries.", | ||
tags: ["Platform"], | ||
responses: [ | ||
ok: {"Activity Log settings fetched successfully", "application/json", Schema.ActivityLog} | ||
] | ||
|
||
def get_activity_log(conn, _) do | ||
with activity_log_entries <- ActivityLog.list_activity_log() do | ||
render(conn, "activity_log.json", %{ | ||
activity_log: activity_log_entries | ||
}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule TrentoWeb.OpenApi.V1.Schema.ActivityLog do | ||
@moduledoc false | ||
|
||
require OpenApiSpex | ||
alias OpenApiSpex.Schema | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "ActivityLog", | ||
description: "Activity Log for the current installation.", | ||
type: :array, | ||
items: %Schema{ | ||
title: "ActivityLogEntry", | ||
type: :object, | ||
additionalProperties: false, | ||
properties: %{ | ||
type: %Schema{ | ||
type: :string, | ||
description: "Type of Activity Log entry." | ||
}, | ||
actor: %Schema{ | ||
type: :string, | ||
description: "Actor causing an Activity Log entry. E.g. System or a specific user." | ||
}, | ||
metadata: %Schema{ | ||
type: :object | ||
}, | ||
occurred_on: %Schema{ | ||
type: :string, | ||
description: "Timestamp upon Activity Log entry insertion." | ||
} | ||
}, | ||
required: [:type, :actor, :metadata, :occurred_on] | ||
} | ||
}) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule TrentoWeb.V1.ActivityLogView do | ||
use TrentoWeb, :view | ||
|
||
def render("activity_log.json", %{activity_log: entries}) do | ||
render_many(entries, __MODULE__, "activity_log_entry.json", as: :entries) | ||
end | ||
|
||
def render("activity_log_entry.json", %{entries: entry}) do | ||
%{ | ||
type: entry.type, | ||
actor: entry.actor, | ||
metadata: entry.metadata, | ||
# Time of occurrence approximated by time of insertion in DB. | ||
occurred_on: entry.inserted_at | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/trento_web/controllers/v1/activity_log_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule TrentoWeb.V1.ActivityLogControllerTest do | ||
use TrentoWeb.ConnCase, async: true | ||
|
||
import Trento.Factory | ||
import OpenApiSpex.TestAssertions | ||
|
||
alias TrentoWeb.OpenApi.V1.ApiSpec | ||
|
||
describe "ActivityLogController" do | ||
setup do | ||
%{api_spec: ApiSpec.spec()} | ||
end | ||
|
||
test "should return activity logs after inserting a few entries.", %{ | ||
conn: conn, | ||
api_spec: api_spec | ||
} do | ||
insert(:activity_log_entry) | ||
insert(:activity_log_entry) | ||
|
||
resp = | ||
conn | ||
|> get("/api/v1/activity_log") | ||
|> json_response(200) | ||
|
||
assert length(resp) == 2 | ||
assert_schema(resp, "ActivityLog", api_spec) | ||
end | ||
|
||
test "should return valid response (empty list) if no activity logs entries exist", %{ | ||
conn: conn, | ||
api_spec: api_spec | ||
} do | ||
resp = | ||
conn | ||
|> get("/api/v1/activity_log") | ||
|> json_response(200) | ||
|
||
assert resp == [] | ||
assert_schema(resp, "ActivityLog", api_spec) | ||
end | ||
end | ||
end |