Skip to content

Commit

Permalink
updated user deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
timalces committed Feb 15, 2024
1 parent 3815107 commit f46447b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
10 changes: 3 additions & 7 deletions app/jobs/user_deletion_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ class UserDeletionJob < ApplicationJob
)

def perform(user, cloud_service_config, **options)
# If the user doesn't have any cloud or billing IDs we can just delete it
# If the user doesn't have a cloud ID we can just delete it
# without involving the middleware.
if user.cloud_user_id.nil? && user.project_id.nil? && user.billing_acct_id.nil?
user.destroy!
return
end
return user.destroy! if user.cloud_user_id.nil?

runner = Runner.new(
user: user,
cloud_service_config: cloud_service_config,
Expand Down Expand Up @@ -70,9 +68,7 @@ def body
project_id: @cloud_service_config.admin_project_id,
},
user_info: {
billing_acct_id: @user.billing_acct_id,
cloud_user_id: @user.cloud_user_id,
project_id: @user.project_id,
}
}
end
Expand Down
3 changes: 1 addition & 2 deletions app/presenters/user_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def status
end

def delete_confirmation_message
"Are you sure you want to delete user #{o.name} (#{o.login})?" \
" This will delete all of their racks and devices."
"Are you sure you want to delete user #{o.name} (#{o.login})?"
end

def team_role_list
Expand Down
4 changes: 1 addition & 3 deletions spec/jobs/user_deletion_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@
})
end

it "contains the user's cloud env and billing ids" do
it "contains the user's cloud env id" do
expect(subject[:user_info]).to be_a Hash
expect(subject[:user_info][:cloud_user_id]).to eq user.cloud_user_id
expect(subject[:user_info][:project_id]).to eq user.project_id
expect(subject[:user_info][:billing_acct_id]).to eq user.billing_acct_id
end
end

Expand Down
49 changes: 13 additions & 36 deletions spec/system/users/edit_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

describe "form elements" do
context "when user does not have cloud and biling IDs" do
context "when user does not have a cloud ID" do
let(:user) { create(:user) }

it "contains expected fields" do
Expand All @@ -22,51 +22,33 @@
expect(form).to have_field("Name", with: user.name)
expect(form).to have_field("Cloud User ID")
expect(form.find_field("Cloud User ID").text).to be_blank
expect(form.find_field("Project ID").text).to be_blank
expect(form.find_field("Billing Account ID").text).to be_blank
end

it "contains the expected hints" do
it "contains the expected hint" do
visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
hints = [
{ label: "Cloud User ID", hint_text: /cloud user ID will be updated automatically/ },
{ label: "Project ID", hint_text: /project ID will be updated automatically/ },
{ label: "Billing Account ID", hint_text: /billing account ID will be updated automatically/ },
]
hints.each do |hint|
field = form.find_field(hint[:label])
expect(field).to have_sibling(".hint")
expect(field.sibling(".hint")).to have_text(hint[:hint_text])
end
field = form.find_field("Cloud User ID")
expect(field).to have_sibling(".hint")
expect(field.sibling(".hint")).to have_text(/cloud user ID will be updated automatically/)
end
end

context "when user has cloud and biling IDs" do
context "when user has a cloud ID" do
let(:user) { create(:user, :with_openstack_account) }

it "contains expected fields" do
visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
expect(form).to have_field("Name", with: user.name)
expect(form).to have_field("Cloud User ID", with: user.cloud_user_id)
expect(form).to have_field("Project ID", with: user.project_id)
expect(form).to have_field("Billing Account ID", with: user.billing_acct_id)
end

it "contains the expected hints" do
it "contains the expected hint" do
visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
hints = [
{ label: "Cloud User ID", hint_text: /Changing the user's cloud user ID/ },
{ label: "Project ID", hint_text: /Changing the user's project ID/ },
{ label: "Billing Account ID", hint_text: /Changing the user's billing account ID/ },
]
hints.each do |hint|
field = form.find_field(hint[:label])
expect(field).to have_sibling(".hint")
expect(field.sibling(".hint")).to have_text(hint[:hint_text])
end
field = form.find_field("Cloud User ID")
expect(field).to have_sibling(".hint")
expect(field.sibling(".hint")).to have_text(/Changing the user's cloud user ID/)
end
end
end
Expand All @@ -77,39 +59,34 @@
context "with valid values" do
it "updates the expected fields" do
expect(user.cloud_user_id).to be_nil
expect(user.project_id).to be_nil
expect(user.billing_acct_id).to be_nil

visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
form.fill_in "Cloud User ID", with: "my new cloud user id"
form.fill_in "Project ID", with: "my new project id"
form.click_on "Update User"

expect(page).to have_text "Successfully updated user"
visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
expect(form).to have_field("Cloud User ID", with: "my new cloud user id")
expect(form).to have_field("Project ID", with: "my new project id")
expect(form).to have_field("Billing Account ID", with: "")
end
end

context "with invalid values" do
it "displays the errors and keeps the user's input" do
expect(user.project_id).to be_nil
expect(user.cloud_user_id).to be_nil

visit edit_user_path(user)
form = find("form[id='edit_user_#{user.id}']")
form.fill_in "Name", with: ""
form.fill_in "Project ID", with: "my new project id"
form.fill_in "Cloud User ID", with: "my new project id"
form.click_on "Update User"

expect(page).to have_text "Unable to update user"
form = find("form[id='edit_user_#{user.id}']")
expect(form).to have_field("Name", with: "")
expect(form.find_field("Name")).to have_sibling(".error", text: "can't be blank")
expect(form).to have_field("Project ID", with: "my new project id")
expect(form).to have_field("Cloud User ID", with: "my new project id")
end
end
end
Expand Down

0 comments on commit f46447b

Please sign in to comment.