Skip to content

Commit

Permalink
limit the scope of setup_user (#2373)
Browse files Browse the repository at this point in the history
* limit the scope of setup_user

* update CL
  • Loading branch information
taylordowns2000 authored Aug 13, 2024
1 parent 75dcf9c commit a4b8645
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 71 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ and this project adheres to

### Added

- Added an `iex` command to setup a user, apiToken, project, and credential so
that it's possible to get a fully running lightning instance via external
shell script. (This is a tricky requirement for a distributed set of local
deployments) [#2369](https://github.com/OpenFn/lightning/issues/2369)
- Added an `iex` command to setup a user, an apiToken, and credentials so that
it's possible to get a fully running lightning instance via external shell
script. (This is a tricky requirement for a distributed set of local
deployments) [#2369](https://github.com/OpenFn/lightning/issues/2369) and
[#2373](https://github.com/OpenFn/lightning/pull/2373)

### Changed

Expand Down
38 changes: 10 additions & 28 deletions lib/lightning/setup_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions test/lightning/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ defmodule Lightning.AccountsTest do
test "purging user deletes all project credentials that involve this user's credentials" do
user = insert(:user)

CredentialsFixtures.project_credential_fixture(user_id: user.id)
CredentialsFixtures.project_credential_fixture(user_id: user.id)
CredentialsFixtures.project_credential_fixture(user_id: user.id, name: "a")
CredentialsFixtures.project_credential_fixture(user_id: user.id, name: "b")
CredentialsFixtures.project_credential_fixture()

assert count_project_credentials_for_user(user) == 2
Expand All @@ -591,9 +591,9 @@ defmodule Lightning.AccountsTest do
user_1 = insert(:user)
user_2 = insert(:user)

CredentialsFixtures.credential_fixture(user_id: user_1.id)
CredentialsFixtures.credential_fixture(user_id: user_1.id)
CredentialsFixtures.credential_fixture(user_id: user_2.id)
CredentialsFixtures.credential_fixture(user_id: user_1.id, name: "a")
CredentialsFixtures.credential_fixture(user_id: user_1.id, name: "b")
CredentialsFixtures.credential_fixture(user_id: user_2.id, name: "a")

assert count_for(Credentials.Credential) == 3

Expand Down
45 changes: 11 additions & 34 deletions test/lightning/setup_utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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(
%{
Expand All @@ -660,8 +660,7 @@ defmodule Lightning.SetupUtilsTest do
email: "[email protected]",
password: "shh12345678!"
},
"abc123",
["project-a", "project-b"],
"abc123supersecret",
[
%{
name: "openmrs",
Expand All @@ -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

Expand Down

0 comments on commit a4b8645

Please sign in to comment.