-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move seeds script to lib/auth/init for #174
- Loading branch information
Showing
11 changed files
with
178 additions
and
156 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
export [email protected] | ||
export AUTH_API_KEY=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx/dwylauth.herokuapp.com | ||
export EMAIL_APP_URL=https://dwylmail.herokuapp.com | ||
export AUTH_URL=dwylauth.herokuapp.com | ||
export SECRET_KEY_BASE=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx | ||
export ENCRYPTION_KEYS='nMdayQpR0aoasLaq1g94FLba+A+wB44JLko47sVQXMg=,L+ZVX8iheoqgqb22mUpATmMDsvVGtafoAeb0KN5uWf0=' | ||
|
||
export GITHUB_CLIENT_ID=CreateGitHubApp | ||
export GITHUB_CLIENT_SECRET=SuperSecret | ||
export GOOGLE_CLIENT_ID=YourAppsClientId.apps.googleusercontent.com | ||
export GOOGLE_CLIENT_SECRET=SuperSecret | ||
export SECRET_KEY_BASE=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx | ||
export ENCRYPTION_KEYS='nMdayQpR0aoasLaq1g94FLba+A+wB44JLko47sVQXMg=,L+ZVX8iheoqgqb22mUpATmMDsvVGtafoAeb0KN5uWf0=' | ||
|
||
# Optional for sending emails: | ||
export EMAIL_APP_URL=https://dwylmail.herokuapp.com | ||
|
||
# Export this once your the auth db is initialized | ||
export AUTH_API_KEY=2PzB7PPnpuLsbWmWtXpGyI+kfSQSQ1zUW2Atz/+8PdZuSEJzHgzGnJWV35nTKRwx/dwylauth.herokuapp.com |
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
File renamed without changes.
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
File renamed without changes.
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
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 |
---|---|---|
@@ -1,147 +1,2 @@ | ||
require Logger | ||
# Script for populating the database. You can run it as: | ||
# | ||
# mix run priv/repo/seeds.exs | ||
# | ||
# The seeds.exs script will also run | ||
# when you execute the following command | ||
# to setup the database: | ||
# | ||
# mix ecto.setup | ||
defmodule Auth.Seeds do | ||
alias Auth.{Person, Repo, Status} | ||
import Ecto.Changeset | ||
|
||
def create_admin do | ||
if is_nil(System.get_env("HEROKU")) && is_nil(System.get_env("GITHUB_TOKEN")) do | ||
load_env() | ||
end | ||
|
||
email = System.get_env("ADMIN_EMAIL") | ||
|
||
person = | ||
case Person.get_person_by_email(email) do | ||
nil -> | ||
%Person{} | ||
|> Person.changeset(%{email: email}) | ||
# |> put_assoc(:statuses, [%Status{text: "verified"}]) | ||
|> Repo.insert!() | ||
|
||
person -> | ||
person | ||
end | ||
|
||
if(Mix.env() == :test) do | ||
# don't print noise during tests | ||
else | ||
IO.inspect(person.id, label: "seeds.exs person.id") | ||
IO.puts("- - - - - - - - - - - - - - - - - - - - - - ") | ||
end | ||
|
||
person | ||
end | ||
|
||
# this is used exclusivvely in seeds | ||
defp get_auth_url do | ||
# see .env_sample for example | ||
api_key = System.get_env("AUTH_API_KEY") | ||
|
||
parts = String.split(api_key, "/"); | ||
# should return "dwylauth.herokuapp.com" | ||
List.last(parts) | ||
end | ||
|
||
def create_apikey_for_admin(person) do | ||
{:ok, app} = | ||
%{ | ||
"name" => "default system app", | ||
"desc" => "Created by /priv/repo/seeds.exs during setup.", | ||
"url" => "localhost:4000", | ||
"person_id" => person.id, | ||
"status" => 3 | ||
} | ||
|> Auth.App.create_app() | ||
|
||
# set the api key to AUTH_API_KEY in env: | ||
update_attrs = %{ | ||
"client_id" => AuthPlug.Token.client_id(), | ||
"client_secret" => AuthPlug.Token.client_secret() | ||
} | ||
|
||
{:ok, key} = | ||
Auth.Apikey.get_apikey_by_app_id(app.id) | ||
|> cast(update_attrs, [:client_id, :client_secret]) | ||
|> Repo.update() | ||
|
||
api_key = key.client_id <> "/" <> key.client_secret <> "/" <> get_auth_url() | ||
|
||
# set the AUTH_API_KEY environment variable during test run: | ||
IO.inspect(Mix.env(), label: "Mix.env()") | ||
# IO.inspect(person) | ||
case Mix.env() do | ||
:test -> | ||
System.put_env("AUTH_API_KEY", api_key) | ||
|
||
:prod -> | ||
Logger.info("export AUTH_API_KEY=#{api_key}") | ||
|
||
_ -> | ||
nil | ||
end | ||
end | ||
|
||
# load the .env file | ||
def load_env() do | ||
path = File.cwd!() <> "/.env" | ||
IO.inspect(path, label: ".env file path") | ||
{:ok, data} = File.read(path) | ||
|
||
data | ||
|> String.trim() | ||
|> String.split("\n") | ||
|> Enum.each(fn line -> | ||
with line <- String.replace(line, ["export ", "'"], ""), | ||
[key | rest] <- String.split(line, "="), | ||
value <- Enum.join(rest, "=") do | ||
System.put_env(key, value) | ||
end | ||
end) | ||
end | ||
end | ||
|
||
# scripts for creating default roles and permissions | ||
defmodule SeedData do | ||
alias Auth.{Role, Status} | ||
|
||
def get_json(filepath) do | ||
path = File.cwd!() <> filepath | ||
{:ok, data} = File.read(path) | ||
json = Jason.decode!(data) | ||
json | ||
end | ||
|
||
def create_default_roles do | ||
Enum.each(get_json("/priv/repo/default_roles.json"), fn role -> | ||
Role.upsert_role(role) | ||
end) | ||
end | ||
|
||
def insert_statuses do | ||
Enum.each(get_json("/priv/repo/statuses.json"), fn status -> | ||
Status.upsert_status(status) | ||
end) | ||
end | ||
end | ||
|
||
person = Auth.Seeds.create_admin() | ||
|
||
SeedData.insert_statuses() | ||
|
||
Auth.Seeds.create_apikey_for_admin(person) | ||
|
||
# Update status of Admin to "Verified" | ||
Auth.Person.verify_person_by_id(1) | ||
|
||
SeedData.create_default_roles() | ||
# grant superadmin role to app owner: | ||
Auth.PeopleRoles.insert(1, 1, 1, 1) | ||
Envar.is_set_all?(~w/ADMIN_EMAIL ENCRYPTION_KEYS SECRET_KEY_BASE/) | ||
Auth.Init.main() |
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,10 @@ | ||
defmodule Auth.InitTest do | ||
use Auth.DataCase, async: true | ||
|
||
describe "Initialize the Auth Database" do | ||
test "main/0" do | ||
assert Auth.Init.main() == :ok | ||
end | ||
end | ||
|
||
end |