From b9269c8d3800bab8f1d37bfec755eb7b0e860553 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 29 Aug 2024 04:38:17 -0400 Subject: [PATCH] Convert `admin/reset` controller spec to system spec (#31643) --- .../admin/accounts/_local_account.html.haml | 2 +- .../admin/resets_controller_spec.rb | 35 --------------- spec/system/admin/reset_spec.rb | 45 +++++++++++++++++++ 3 files changed, 46 insertions(+), 36 deletions(-) delete mode 100644 spec/controllers/admin/resets_controller_spec.rb create mode 100644 spec/system/admin/reset_spec.rb diff --git a/app/views/admin/accounts/_local_account.html.haml b/app/views/admin/accounts/_local_account.html.haml index 3ad47d8df..5357ebcce 100644 --- a/app/views/admin/accounts/_local_account.html.haml +++ b/app/views/admin/accounts/_local_account.html.haml @@ -52,7 +52,7 @@ - if can?(:reset_password, account.user) %tr %td - = table_link_to 'key', t('admin.accounts.reset_password'), admin_account_reset_path(account.id), method: :create, data: { confirm: t('admin.accounts.are_you_sure') } + = table_link_to 'key', t('admin.accounts.reset_password'), admin_account_reset_path(account.id), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } %tr %th= t('simple_form.labels.defaults.locale') %td= standard_locale_name(account.user_locale) diff --git a/spec/controllers/admin/resets_controller_spec.rb b/spec/controllers/admin/resets_controller_spec.rb deleted file mode 100644 index 0cbc3b60a..000000000 --- a/spec/controllers/admin/resets_controller_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe Admin::ResetsController do - render_views - - subject { post :create, params: { account_id: account.id } } - - let(:account) { Fabricate(:account) } - - before do - sign_in Fabricate(:user, role: UserRole.find_by(name: 'Admin')), scope: :user - end - - describe 'POST #create', :inline_jobs do - it 'redirects to admin accounts page' do - emails = capture_emails { subject } - - expect(emails.size) - .to eq(2) - expect(emails).to have_attributes( - first: have_attributes( - to: include(account.user.email), - subject: I18n.t('devise.mailer.password_change.subject') - ), - last: have_attributes( - to: include(account.user.email), - subject: I18n.t('devise.mailer.reset_password_instructions.subject') - ) - ) - expect(response).to redirect_to(admin_account_path(account.id)) - end - end -end diff --git a/spec/system/admin/reset_spec.rb b/spec/system/admin/reset_spec.rb new file mode 100644 index 000000000..50fb4b46d --- /dev/null +++ b/spec/system/admin/reset_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Admin::Reset' do + it 'Resets password for account user' do + account = Fabricate :account + sign_in admin_user + visit admin_account_path(account.id) + + emails = capture_emails do + expect { submit_reset } + .to change(Admin::ActionLog.where(target: account.user), :count).by(1) + end + + expect(emails.first) + .to be_present + .and(deliver_to(account.user.email)) + .and(have_subject(password_change_subject)) + + expect(emails.last) + .to be_present + .and(deliver_to(account.user.email)) + .and(have_subject(reset_instructions_subject)) + + expect(page) + .to have_content(account.username) + end + + def admin_user + Fabricate(:user, role: UserRole.find_by(name: 'Admin')) + end + + def submit_reset + click_on I18n.t('admin.accounts.reset_password') + end + + def password_change_subject + I18n.t('devise.mailer.password_change.subject') + end + + def reset_instructions_subject + I18n.t('devise.mailer.reset_password_instructions.subject') + end +end