-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
limit the scope of setup_user (#2373)
* limit the scope of setup_user * update CL
- Loading branch information
1 parent
75dcf9c
commit a4b8645
Showing
4 changed files
with
31 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -871,15 +871,21 @@ defmodule Lightning.SetupUtils do | |
|
||
@doc """ | ||
In some (mostly remote-controlled) deployments, it's necessary to create a | ||
user, empty projects that they can access, and credentials (shared with those | ||
projects) that they own so that later `openfn deploy` calls can make use of | ||
these artifacts. | ||
user, and apiToken, and multiple credentials (owned by the user) so that later | ||
`openfn deploy` calls can make use of these artifacts. | ||
When run _before_ `openfn deploy`, this function makes it possible to set up | ||
an entire lightning instance with a working project (including secrets) | ||
without using the web UI. | ||
## Examples | ||
iex> setup_user(%{email: "[email protected]", first_name: "taylor", last_name: "downs", password: "shh12345!"}, "secretToken", [%{name: "openmrs", schema: "raw", body: %{"a" => "secret"}}, %{ name: "dhis2", schema: "raw", body: %{"b" => "safe"}}]) | ||
:ok | ||
""" | ||
def setup_user(user, token, project_names, credentials) do | ||
@spec setup_user(map(), String.t(), list(map())) :: :ok | {:error, any()} | ||
def setup_user(user, token, credentials) do | ||
# create user | ||
{:ok, user} = Accounts.create_user(user) | ||
|
||
|
@@ -890,36 +896,12 @@ defmodule Lightning.SetupUtils do | |
token: token | ||
}) | ||
|
||
# create projects | ||
projects = | ||
project_names | ||
|> Enum.map(fn name -> | ||
{:ok, project} = | ||
Projects.create_project( | ||
%{ | ||
name: name, | ||
history_retention_period: | ||
Application.get_env(:lightning, :default_retention_period), | ||
project_users: [%{user_id: user.id, role: :owner}] | ||
}, | ||
false | ||
) | ||
|
||
project | ||
end) | ||
|
||
# create credentials | ||
Enum.each(credentials, fn credential -> | ||
{:ok, _credential} = | ||
Credentials.create_credential( | ||
credential | ||
|> Map.put(:user_id, user.id) | ||
|> Map.put( | ||
:project_credentials, | ||
Enum.map(projects, fn project -> | ||
%{project_id: project.id} | ||
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
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 |
---|---|---|
|
@@ -4,7 +4,7 @@ defmodule Lightning.SetupUtilsTest do | |
import Swoosh.TestAssertions | ||
|
||
alias Lightning.{Accounts, Projects, Workflows, Jobs, SetupUtils} | ||
alias Lightning.Projects.{Project, ProjectUser, ProjectCredential} | ||
alias Lightning.Projects | ||
alias Lightning.Accounts.{User, UserToken} | ||
alias Lightning.Credentials.{Credential} | ||
|
||
|
@@ -650,8 +650,8 @@ defmodule Lightning.SetupUtilsTest do | |
end | ||
end | ||
|
||
describe "setup_user/4" do | ||
test "creates a user, an api token, projects, and credentials" do | ||
describe "setup_user/3" do | ||
test "creates a user, an api token, and credentials" do | ||
assert :ok == | ||
Lightning.SetupUtils.setup_user( | ||
%{ | ||
|
@@ -660,8 +660,7 @@ defmodule Lightning.SetupUtilsTest do | |
email: "[email protected]", | ||
password: "shh12345678!" | ||
}, | ||
"abc123", | ||
["project-a", "project-b"], | ||
"abc123supersecret", | ||
[ | ||
%{ | ||
name: "openmrs", | ||
|
@@ -676,39 +675,17 @@ defmodule Lightning.SetupUtilsTest do | |
] | ||
) | ||
|
||
# check that the user and the API token has been created | ||
# check that the user has been created | ||
assert %User{id: user_id} = Repo.get_by(User, email: "[email protected]") | ||
assert %UserToken{} = Repo.get_by(UserToken, token: "abc123") | ||
|
||
# check that both projects are there | ||
assert [%Project{id: p1_id}, %Project{id: p2_id}] = Repo.all(Project) | ||
# check that the apiToken has been created | ||
assert %UserToken{} = Repo.get_by(UserToken, token: "abc123supersecret") | ||
|
||
# check that the user has owner access to both | ||
# check that the credentials have been created | ||
assert [ | ||
%ProjectUser{project_id: ^p1_id, user_id: ^user_id, role: :owner}, | ||
%ProjectUser{project_id: ^p2_id, user_id: ^user_id, role: :owner} | ||
] = | ||
Repo.all(ProjectUser) | ||
|
||
credentials = | ||
Repo.all(Credential) |> Repo.preload(:project_credentials) | ||
|
||
assert [ | ||
%Credential{ | ||
name: "openmrs", | ||
project_credentials: [ | ||
%ProjectCredential{project_id: ^p1_id}, | ||
%ProjectCredential{project_id: ^p2_id} | ||
] | ||
}, | ||
%Credential{ | ||
name: "dhis2", | ||
project_credentials: [ | ||
%ProjectCredential{project_id: ^p1_id}, | ||
%ProjectCredential{project_id: ^p2_id} | ||
] | ||
} | ||
] = credentials | ||
%Credential{name: "openmrs", user_id: ^user_id}, | ||
%Credential{name: "dhis2", user_id: ^user_id} | ||
] = Repo.all(Credential) | ||
end | ||
end | ||
|
||
|