diff --git a/charts/uffizzi-app/Chart.yaml b/charts/uffizzi-app/Chart.yaml index 3aeabacb..43d552b2 100644 --- a/charts/uffizzi-app/Chart.yaml +++ b/charts/uffizzi-app/Chart.yaml @@ -1,6 +1,6 @@ apiVersion: v2 name: uffizzi-app -version: 1.1.3 +version: 1.2.0 kubeVersion: ">= 1.21.0-0" # https://issuetracker.google.com/issues/77503699 description: "Uffizzi is an open-source engine for creating lightweight, ephemeral test environments for APIs and full-stack applications. Uffizzi enables teams to preview new features before merging." type: application diff --git a/charts/uffizzi-app/README.md b/charts/uffizzi-app/README.md index 9344d95f..f3a77196 100644 --- a/charts/uffizzi-app/README.md +++ b/charts/uffizzi-app/README.md @@ -55,6 +55,9 @@ global: redis: password: ChangeMeNow uffizzi: + firstUser: + email: user@example.com + password: ChangeMeNow controller: password: ChangeMeNow app_url: https://uffizzi.example.com @@ -103,9 +106,11 @@ kubectl get ingress --namespace uffizzi Be sure to add a "wildcard" record for the domain specified in `managed_dns_zone_dns_name`. In the above example, that's `*.uffizzi.example.com`. -### Creating the first user +### Provisioning users + +You'll need to create at least one User Account to access your Uffizzi installation. The easiest way to do this is specify values for `global.uffizzi.firstUser` as shown in the example values file above. Uffizzi will attempt to provision this User each time it starts. -After installation, you'll need to create at least one User to access your Uffizzi installation. For now, the best way to do this is executing an interactive `rake` task within the application server container: +If you did not specify a `firstUser`, or if you want to provision additional Users, you may execute an interactive `rake` task within the application server container: ``` kubectl exec -it deploy/my-uffizzi-app-web --namespace uffizzi -- rake uffizzi_core:create_user diff --git a/charts/uffizzi-app/templates/configmap-common.yaml b/charts/uffizzi-app/templates/configmap-common.yaml index ec6b7d5e..7dc46ee2 100644 --- a/charts/uffizzi-app/templates/configmap-common.yaml +++ b/charts/uffizzi-app/templates/configmap-common.yaml @@ -11,3 +11,6 @@ data: CONTROLLER_URL: {{ default (print "http://" .Release.Name "-controller:8080") .Values.controller_url }} EMAIL_DELIVERY_ENABLED: {{ .Values.feature_email_delivery | quote }} MANAGED_DNS_ZONE_DNS_NAME: {{ .Values.managed_dns_zone_dns_name | quote }} + UFFIZZI_USER_EMAIL: {{ .Values.global.uffizzi.firstUser.email }} + UFFIZZI_USER_PASSWORD: {{ .Values.global.uffizzi.firstUser.password | quote }} + UFFIZZI_PROJECT_NAME: {{ .Values.global.uffizzi.firstUser.projectName | quote }} diff --git a/charts/uffizzi-app/templates/web-deployment.yaml b/charts/uffizzi-app/templates/web-deployment.yaml index e097b748..e8ccc05b 100644 --- a/charts/uffizzi-app/templates/web-deployment.yaml +++ b/charts/uffizzi-app/templates/web-deployment.yaml @@ -24,7 +24,7 @@ spec: - /bin/bash - -c args: - - bundle exec rails db:create db:migrate && bundle exec puma -C config/puma.rb + - bundle exec rails db:create db:migrate && bundle exec rake uffizzi_core:create_user && bundle exec puma -C config/puma.rb envFrom: - secretRef: name: uffizzi-web-secret-envs diff --git a/charts/uffizzi-app/values.yaml b/charts/uffizzi-app/values.yaml index 1a09508a..8508ab78 100644 --- a/charts/uffizzi-app/values.yaml +++ b/charts/uffizzi-app/values.yaml @@ -11,6 +11,10 @@ global: redis: password: ChangeMeNow uffizzi: + firstUser: + email: "" + password: "" + projectName: default controller: username: username password: ChangeMeNow diff --git a/core/app/services/uffizzi_core/user_generator_service.rb b/core/app/services/uffizzi_core/user_generator_service.rb index b91c521b..157a028c 100644 --- a/core/app/services/uffizzi_core/user_generator_service.rb +++ b/core/app/services/uffizzi_core/user_generator_service.rb @@ -6,6 +6,12 @@ class UffizziCore::UserGeneratorService DEFAULT_ACCOUNT_NAME = 'default' class << self + def safe_generate(email, password, project_name) + generate(email, password, project_name) + rescue ActiveRecord::RecordInvalid => e + puts e.message + end + def generate(email, password, project_name) user_attributes = build_user_attributes(email, password) project_attributes = build_project_attributes(project_name) @@ -31,19 +37,17 @@ def build_user_attributes(email, password) if email.present? user_attributes[:email] = email - else + elsif IO::console.present? IO::console.write("Enter User Email (default: #{DEFAULT_USER_EMAIL}): ") user_attributes[:email] = IO::console.gets.strip.presence || DEFAULT_USER_EMAIL end user_attributes[:password] = if password.present? password - else + elsif IO::console.present? IO::console.getpass('Enter Password: ') end - abort('password can\'t be blank') if user_attributes[:password].blank? - user_attributes end @@ -53,9 +57,11 @@ def build_project_attributes(project_name) } if project_name.present? project_attributes[:name] = project_name - else + elsif IO::console.present? IO::console.write("Enter Project Name (default: #{DEFAULT_PROJECT_NAME}): ") project_attributes[:name] = IO::console.gets.strip.presence || DEFAULT_PROJECT_NAME + else + project_attributes[:name] = DEFAULT_PROJECT_NAME end project_attributes[:slug] = prepare_project_slug(project_attributes[:name]) diff --git a/core/lib/tasks/uffizzi_core_tasks.rake b/core/lib/tasks/uffizzi_core_tasks.rake index 44cc8e54..d899fca3 100644 --- a/core/lib/tasks/uffizzi_core_tasks.rake +++ b/core/lib/tasks/uffizzi_core_tasks.rake @@ -14,6 +14,6 @@ namespace :uffizzi_core do desc 'Create a new user' task create_user: :environment do - UffizziCore::UserGeneratorService.generate(ENV['UFFIZZI_USER_EMAIL'], ENV['UFFIZZI_USER_PASSWORD'], ENV['UFFIZZI_PROJECT_NAME']) + UffizziCore::UserGeneratorService.safe_generate(ENV['UFFIZZI_USER_EMAIL'], ENV['UFFIZZI_USER_PASSWORD'], ENV['UFFIZZI_PROJECT_NAME']) end end diff --git a/core/test/services/user_generator_service_test.rb b/core/test/services/user_generator_service_test.rb index 10cd99d4..aebd9987 100644 --- a/core/test/services/user_generator_service_test.rb +++ b/core/test/services/user_generator_service_test.rb @@ -100,7 +100,7 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase console_mock.stubs(:getpass).returns('') IO.stubs(:console).returns(console_mock) - assert_raises SystemExit do + assert_raises StandardError do UffizziCore::UserGeneratorService.generate(email, password, project_name) end end @@ -150,4 +150,95 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase UffizziCore::UserGeneratorService.generate(email, password, project_name) end end + + test '#generate if a user already exists' do + email = generate(:email) + password = generate(:password) + project_name = generate(:string) + + create(:user, :with_organizational_account, email: email) + + assert_raises ActiveRecord::RecordInvalid do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end + + test '#generate if a user already exists and email gets by user' do + email = generate(:email) + password = generate(:password) + project_name = generate(:string) + + create(:user, :with_organizational_account, email: email) + + console_mock = mock('console_mock') + console_mock.stubs(:write) + console_mock.stubs(:gets).returns(email) + IO.stubs(:console).returns(console_mock) + + assert_raises ActiveRecord::RecordInvalid do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end + + test '#generate if have data but no console' do + email = generate(:email) + password = generate(:password) + project_name = generate(:string) + + IO.stubs(:console).returns(nil) + + differences = { + -> { UffizziCore::User.count } => 1, + -> { UffizziCore::Account.count } => 1, + -> { UffizziCore::Membership.count } => 1, + -> { UffizziCore::Project.count } => 1, + } + + assert_difference differences do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end + + test '#generate if no email and no console' do + email = nil + password = generate(:password) + project_name = generate(:string) + + IO.stubs(:console).returns(nil) + + assert_raises ActiveRecord::RecordInvalid do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end + + test '#generate if no password and no console' do + email = generate(:email) + password = nil + project_name = generate(:string) + + IO.stubs(:console).returns(nil) + + assert_raises ActiveRecord::RecordInvalid do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end + + test '#generate if no project_name and no console' do + email = generate(:email) + password = generate(:password) + project_name = nil + + IO.stubs(:console).returns(nil) + + differences = { + -> { UffizziCore::User.count } => 1, + -> { UffizziCore::Account.count } => 1, + -> { UffizziCore::Membership.count } => 1, + -> { UffizziCore::Project.count } => 1, + } + + assert_difference differences do + UffizziCore::UserGeneratorService.generate(email, password, project_name) + end + end end