From e39eabee7e728ce18359089efad9937625f3986e Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Thu, 19 May 2022 14:56:41 +0400 Subject: [PATCH 1/8] Updated tests for the user generator service --- .../services/user_generator_service_test.rb | 93 ++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/core/test/services/user_generator_service_test.rb b/core/test/services/user_generator_service_test.rb index 10cd99d4..1034a27a 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 StandardError 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 StandardError 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 StandardError 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 StandardError 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 From 3b79ff61840f040a02f2365d4642fa2bc0d65b4e Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Thu, 19 May 2022 15:19:23 +0400 Subject: [PATCH 2/8] Added test for the generate function about exception handle --- core/test/services/user_generator_service_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/test/services/user_generator_service_test.rb b/core/test/services/user_generator_service_test.rb index 1034a27a..8a211bb1 100644 --- a/core/test/services/user_generator_service_test.rb +++ b/core/test/services/user_generator_service_test.rb @@ -241,4 +241,14 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase UffizziCore::UserGeneratorService.generate(email, password, project_name) end end + + test '#safe_generate if exception' do + email = generate(:email) + password = generate(:password) + project_name = generate(:string) + UffizziCore::UserGeneratorService.stubs(:generate).raises(StandardError) + StandardError.any_instance.expects(:message).times(1) + + UffizziCore::UserGeneratorService.safe_generate(email, password, project_name) + end end From 9c77a87c5c6b379ffc304979bab55d1e3076bbde Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Thu, 19 May 2022 15:20:39 +0400 Subject: [PATCH 3/8] Moved the rescue to separate function --- core/app/services/uffizzi_core/user_generator_service.rb | 6 ++++++ core/lib/tasks/uffizzi_core_tasks.rake | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/app/services/uffizzi_core/user_generator_service.rb b/core/app/services/uffizzi_core/user_generator_service.rb index b91c521b..531cbfbf 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 StandardError => 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) 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 From 78c9ca0e2c0998cba9fc113593d60eb815a85d0d Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Tue, 17 May 2022 16:10:57 +0400 Subject: [PATCH 4/8] Added variables to helm template for the default user --- charts/uffizzi-app/templates/configmap-common.yaml | 3 +++ charts/uffizzi-app/values.yaml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/charts/uffizzi-app/templates/configmap-common.yaml b/charts/uffizzi-app/templates/configmap-common.yaml index ec6b7d5e..f37f7411 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.uffizzi_user_email }} + UFFIZZI_USER_PASSWORD: {{ .Values.uffizzi_user_password | quote }} + UFFIZZI_PROJECT_NAME: {{ .Values.uffizzi_project_name }} diff --git a/charts/uffizzi-app/values.yaml b/charts/uffizzi-app/values.yaml index 1a09508a..38acc71d 100644 --- a/charts/uffizzi-app/values.yaml +++ b/charts/uffizzi-app/values.yaml @@ -42,6 +42,9 @@ feature_email_delivery: false feature_google_maps: false feature_sentry: false feature_stripe: false +uffizzi_user_email: user@example.com +uffizzi_user_password: "" +uffizzi_project_name: default uffizzi-controller: #dependency clusterIssuer: "letsencrypt" From a2ca960bfb43eae3d4b8b34aa25d2f3c9304644b Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Tue, 17 May 2022 19:17:01 +0400 Subject: [PATCH 5/8] Updated create user function. Added task execution to the deployment args of the helm template --- charts/uffizzi-app/templates/web-deployment.yaml | 2 +- .../services/uffizzi_core/user_generator_service.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) 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/core/app/services/uffizzi_core/user_generator_service.rb b/core/app/services/uffizzi_core/user_generator_service.rb index 531cbfbf..72521188 100644 --- a/core/app/services/uffizzi_core/user_generator_service.rb +++ b/core/app/services/uffizzi_core/user_generator_service.rb @@ -37,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 @@ -59,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]) From 328477d6c3985b6aa32e81abae6ddede624fee33 Mon Sep 17 00:00:00 2001 From: Sergey Kirillov Date: Thu, 19 May 2022 12:27:13 +0400 Subject: [PATCH 6/8] Updated doc --- charts/uffizzi-app/README.md | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/charts/uffizzi-app/README.md b/charts/uffizzi-app/README.md index 9344d95f..9bd692e0 100644 --- a/charts/uffizzi-app/README.md +++ b/charts/uffizzi-app/README.md @@ -61,6 +61,8 @@ app_url: https://uffizzi.example.com webHostname: uffizzi.example.com allowed_hosts: uffizzi.example.com managed_dns_zone_dns_name: uffizzi.example.com +uffizzi_user_email: user@example.com +uffizzi_user_password: ChangeMeNow uffizzi-controller: ingress: hostname: controller.uffizzi.example.com @@ -68,7 +70,8 @@ uffizzi-controller: certEmail: admin@example.com ``` -Edit these values and save them in a file named `myvals.yaml` or similar. +Edit these values and save them in a file named `myvals.yaml` or similar. +The "uffizzi_user_email" and "uffizzi_user_password" are used for the default user creation, which is needed to access your Uffizzi installation. ## Installation @@ -103,17 +106,6 @@ 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 - -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: - -``` -kubectl exec -it deploy/my-uffizzi-app-web --namespace uffizzi -- rake uffizzi_core:create_user -Enter User Email (default: user@example.com): user@example.com -Enter Password: -Enter Project Name (default: default): -``` - ## Usage If everything went well, you can now connect to the Uffizzi API service and begin Continously Deploying Previews! Use [the Uffizzi CLI](https://github.com/UffizziCloud/uffizzi_cli) or [the Uffizzi GitHub Action](https://github.com/UffizziCloud/preview-action) or your own API client. From 1e606b64514e5ffcb2d0557ee5bf0d0cf4e04408 Mon Sep 17 00:00:00 2001 From: Adam Vollrath Date: Thu, 2 Jun 2022 11:32:14 -0500 Subject: [PATCH 7/8] Change value names to be more Helm idiomatic, and clarify README documentation, and bump Helm Chart version number, and This _partially_ reverts commit 48e8e902a3010a52b9b7e167474417f4ddefe13d. --- charts/uffizzi-app/Chart.yaml | 2 +- charts/uffizzi-app/README.md | 21 +++++++++++++++---- .../templates/configmap-common.yaml | 6 +++--- charts/uffizzi-app/values.yaml | 7 ++++--- 4 files changed, 25 insertions(+), 11 deletions(-) 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 9bd692e0..f3a77196 100644 --- a/charts/uffizzi-app/README.md +++ b/charts/uffizzi-app/README.md @@ -55,14 +55,15 @@ global: redis: password: ChangeMeNow uffizzi: + firstUser: + email: user@example.com + password: ChangeMeNow controller: password: ChangeMeNow app_url: https://uffizzi.example.com webHostname: uffizzi.example.com allowed_hosts: uffizzi.example.com managed_dns_zone_dns_name: uffizzi.example.com -uffizzi_user_email: user@example.com -uffizzi_user_password: ChangeMeNow uffizzi-controller: ingress: hostname: controller.uffizzi.example.com @@ -70,8 +71,7 @@ uffizzi-controller: certEmail: admin@example.com ``` -Edit these values and save them in a file named `myvals.yaml` or similar. -The "uffizzi_user_email" and "uffizzi_user_password" are used for the default user creation, which is needed to access your Uffizzi installation. +Edit these values and save them in a file named `myvals.yaml` or similar. ## Installation @@ -106,6 +106,19 @@ 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`. +### 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. + +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 +Enter User Email (default: user@example.com): user@example.com +Enter Password: +Enter Project Name (default: default): +``` + ## Usage If everything went well, you can now connect to the Uffizzi API service and begin Continously Deploying Previews! Use [the Uffizzi CLI](https://github.com/UffizziCloud/uffizzi_cli) or [the Uffizzi GitHub Action](https://github.com/UffizziCloud/preview-action) or your own API client. diff --git a/charts/uffizzi-app/templates/configmap-common.yaml b/charts/uffizzi-app/templates/configmap-common.yaml index f37f7411..7dc46ee2 100644 --- a/charts/uffizzi-app/templates/configmap-common.yaml +++ b/charts/uffizzi-app/templates/configmap-common.yaml @@ -11,6 +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.uffizzi_user_email }} - UFFIZZI_USER_PASSWORD: {{ .Values.uffizzi_user_password | quote }} - UFFIZZI_PROJECT_NAME: {{ .Values.uffizzi_project_name }} + 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/values.yaml b/charts/uffizzi-app/values.yaml index 38acc71d..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 @@ -42,9 +46,6 @@ feature_email_delivery: false feature_google_maps: false feature_sentry: false feature_stripe: false -uffizzi_user_email: user@example.com -uffizzi_user_password: "" -uffizzi_project_name: default uffizzi-controller: #dependency clusterIssuer: "letsencrypt" From 5e37f468a6ac292122ef2773c4f9152b4816d368 Mon Sep 17 00:00:00 2001 From: moklidia Date: Mon, 6 Jun 2022 12:10:35 +0200 Subject: [PATCH 8/8] [uffizzi_app_113] changed rescued exception type --- .../uffizzi_core/user_generator_service.rb | 2 +- .../services/user_generator_service_test.rb | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/core/app/services/uffizzi_core/user_generator_service.rb b/core/app/services/uffizzi_core/user_generator_service.rb index 72521188..157a028c 100644 --- a/core/app/services/uffizzi_core/user_generator_service.rb +++ b/core/app/services/uffizzi_core/user_generator_service.rb @@ -8,7 +8,7 @@ class UffizziCore::UserGeneratorService class << self def safe_generate(email, password, project_name) generate(email, password, project_name) - rescue StandardError => e + rescue ActiveRecord::RecordInvalid => e puts e.message end diff --git a/core/test/services/user_generator_service_test.rb b/core/test/services/user_generator_service_test.rb index 8a211bb1..aebd9987 100644 --- a/core/test/services/user_generator_service_test.rb +++ b/core/test/services/user_generator_service_test.rb @@ -158,7 +158,7 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase create(:user, :with_organizational_account, email: email) - assert_raises StandardError do + assert_raises ActiveRecord::RecordInvalid do UffizziCore::UserGeneratorService.generate(email, password, project_name) end end @@ -175,7 +175,7 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase console_mock.stubs(:gets).returns(email) IO.stubs(:console).returns(console_mock) - assert_raises StandardError do + assert_raises ActiveRecord::RecordInvalid do UffizziCore::UserGeneratorService.generate(email, password, project_name) end end @@ -206,7 +206,7 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase IO.stubs(:console).returns(nil) - assert_raises StandardError do + assert_raises ActiveRecord::RecordInvalid do UffizziCore::UserGeneratorService.generate(email, password, project_name) end end @@ -218,7 +218,7 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase IO.stubs(:console).returns(nil) - assert_raises StandardError do + assert_raises ActiveRecord::RecordInvalid do UffizziCore::UserGeneratorService.generate(email, password, project_name) end end @@ -241,14 +241,4 @@ class UffizziCore::UserGeneratorServiceTest < ActiveSupport::TestCase UffizziCore::UserGeneratorService.generate(email, password, project_name) end end - - test '#safe_generate if exception' do - email = generate(:email) - password = generate(:password) - project_name = generate(:string) - UffizziCore::UserGeneratorService.stubs(:generate).raises(StandardError) - StandardError.any_instance.expects(:message).times(1) - - UffizziCore::UserGeneratorService.safe_generate(email, password, project_name) - end end