Elixir/domain ops (#2837)

Adds a helper method we can call from a live IEX to provision an account
when signups are disabled.
This commit is contained in:
Jamil
2023-12-09 20:35:48 -05:00
committed by GitHub
parent 46a950da84
commit 786e156337
3 changed files with 71 additions and 0 deletions

View File

@@ -284,6 +284,15 @@ Interactive Elixir (1.15.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(web@web-w2f6.us-east1-d.c.firezone-staging.internal)1>
```
### Quickly provisioning an account
Useful for onboarding beta customers. See the `Domain.Ops.provision_account/1`
function:
```elixir
iex> Domain.Ops.provision_account(%{account_name: "Customer Account", slug: "customer_account", account_admin_name: "Test User", account_email: "test@firezone.localhost"})
```
### Creating an account on staging instance using CLI
```elixir

View File

@@ -0,0 +1,28 @@
defmodule Domain.Ops do
def provision_account(%{
account_name: account_name,
account_slug: account_slug,
account_admin_name: account_admin_name,
account_admin_email: account_admin_email
}) do
Domain.Repo.transaction(fn ->
{:ok, account} = Domain.Accounts.create_account(%{name: account_name, slug: account_slug})
{:ok, magic_link_provider} =
Domain.Auth.create_provider(account, %{
name: "Email",
adapter: :email,
adapter_config: %{}
})
{:ok, actor} =
Domain.Actors.create_actor(account, %{type: :account_admin_user, name: account_admin_name})
{:ok, _identity} =
Domain.Auth.upsert_identity(actor, magic_link_provider, %{
provider_identifier: account_admin_email,
provider_identifier_confirmation: account_admin_email
})
end)
end
end

View File

@@ -0,0 +1,34 @@
defmodule Domain.OpsTest do
use Domain.DataCase, async: true
import Domain.Ops
describe "provision_account/1" do
setup do
Domain.Config.put_env_override(:outbound_email_adapter_configured?, true)
end
test "provisions an account when valid input is provider" do
params = %{
account_name: "Test Account",
account_slug: "test_account",
account_admin_name: "Test Admin",
account_admin_email: "test_admin@firezone.local"
}
assert {:ok, _} = provision_account(params)
assert {:ok, _} = Domain.Accounts.fetch_account_by_id_or_slug("test_account")
end
test "returns an error when invalid input is provided" do
params = %{
account_name: "Test Account",
account_slug: "test_account",
account_admin_name: "Test Admin",
account_admin_email: "invalid"
}
# provision_account/1 catches the invalid params and raises MatchError
assert_raise(MatchError, fn -> provision_account(params) end)
end
end
end