Skip to content

Commit

Permalink
[admin] organization creation in static configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
patatoid committed Oct 22, 2024
1 parent b5abe93 commit 18f3282
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/boruta_admin/lib/boruta_admin/configuration_loader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule BorutaAdmin.ConfigurationLoader do
alias BorutaIdentity.Configuration
alias BorutaIdentity.Configuration.ErrorTemplate
alias BorutaIdentity.IdentityProviders
alias BorutaIdentity.Organizations
alias ExJsonSchema.Validator.Error.BorutaFormatter

@spec node_name() :: node_name :: String.t()
Expand Down Expand Up @@ -119,6 +120,38 @@ defmodule BorutaAdmin.ConfigurationLoader do
load_configuration(Map.delete(configuration, "microgateway"), result)
end

def load_configuration(%{"organization" => organization_configurations} = configuration, result) when is_list(organization_configurations) do
result =
Map.put(
result,
:organization,
Enum.map(organization_configurations, fn organization_configuration ->
with :ok <-
ExJsonSchema.Validator.validate(
Schema.organization(),
organization_configuration,
error_formatter: BorutaFormatter
),
{:ok, organization} <-
Admin.create_organization(organization_configuration) do
{:ok, organization}
else
{:error, %Ecto.Changeset{} = changeset} ->
{:error, [changeset]}

{:error, errors} ->
{:error, errors}
end
end)
|> Enum.flat_map(fn
{:ok, _organization} -> []
{:error, errors} -> errors
end)
)

load_configuration(Map.delete(configuration, "organization"), result)
end

def load_configuration(%{"backend" => backend_configurations} = configuration, result) when is_list(backend_configurations) do
result =
Map.put(
Expand Down
13 changes: 13 additions & 0 deletions apps/boruta_admin/lib/boruta_admin/configuration_loader/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ defmodule BorutaAdmin.ConfigurationLoader.Schema do
|> Schema.resolve()
end

def organization do
%{
"type" => "object",
"properties" => %{
"id" => %{"type" => "string"},
"name" => %{"type" => "string"},
"label" => %{"type" => "string"}
},
"required" => ["name"],
"additionalProperties" => false
}
end

def backend do
%{
"type" => "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
version: "1.0"
configuration:
organization:
- name: ""
label: bad organization
additional: true
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ configuration:
error_template:
- type: "500"
content: test
organization:
- name: test
17 changes: 17 additions & 0 deletions apps/boruta_admin/test/boruta_admin/configuration_loader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule BorutaAdmin.ConfigurationLoaderTest do
alias BorutaIdentity.IdentityProviders.Backend
alias BorutaIdentity.IdentityProviders.IdentityProvider
alias BorutaIdentity.IdentityProviders.Template
alias BorutaIdentity.Organizations.Organization

test "returns an error with a bad configuration file" do
assert BorutaGateway.Repo.all(Upstream) |> Enum.empty?()
Expand Down Expand Up @@ -53,6 +54,20 @@ defmodule BorutaAdmin.ConfigurationLoaderTest do
}}
end

test "returns an error with a bad organization configuration file" do
assert BorutaIdentity.Repo.all(Backend) |> Enum.count() == 1

configuration_file_path =
:code.priv_dir(:boruta_admin)
|> Path.join("/test/configuration_files/bad_organization_configuration.yml")

assert ConfigurationLoader.from_file!(configuration_file_path) ==
{:ok,
%{
organization: ["Schema does not allow additional properties: #/additional."]
}}
end

test "returns an error with a bad identity provider configuration file" do
assert BorutaIdentity.Repo.all(IdentityProvider) |> Enum.empty?()

Expand Down Expand Up @@ -207,6 +222,8 @@ defmodule BorutaAdmin.ConfigurationLoaderTest do

assert %Role{name: "test"} = BorutaIdentity.Repo.all(Role) |> List.last()

assert %Organization{name: "test"} = BorutaIdentity.Repo.all(Organization) |> List.last()

assert %ErrorTemplate{type: "500", content: "test"} =
BorutaIdentity.Repo.all(ErrorTemplate) |> List.last()
end
Expand Down

0 comments on commit 18f3282

Please sign in to comment.