From c40cd79c4d90ad0613616c59cc029ab83d55ee48 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 15 Oct 2024 10:27:46 -0400 Subject: [PATCH] Convert `admin/invites` controller specs to system specs (#32450) --- app/views/admin/invites/_invite.html.haml | 2 +- .../admin/invites_controller_spec.rb | 59 ----------------- spec/system/admin/invites_spec.rb | 63 +++++++++++++++++++ 3 files changed, 64 insertions(+), 60 deletions(-) delete mode 100644 spec/controllers/admin/invites_controller_spec.rb create mode 100644 spec/system/admin/invites_spec.rb diff --git a/app/views/admin/invites/_invite.html.haml b/app/views/admin/invites/_invite.html.haml index 53eac1d0cd..e3e5d32542 100644 --- a/app/views/admin/invites/_invite.html.haml +++ b/app/views/admin/invites/_invite.html.haml @@ -1,4 +1,4 @@ -%tr +%tr{ id: dom_id(invite) } %td .input-copy .input-copy__wrapper diff --git a/spec/controllers/admin/invites_controller_spec.rb b/spec/controllers/admin/invites_controller_spec.rb deleted file mode 100644 index b6471e80b2..0000000000 --- a/spec/controllers/admin/invites_controller_spec.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -RSpec.describe Admin::InvitesController do - render_views - - let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) } - - before do - sign_in user, scope: :user - end - - describe 'GET #index' do - subject { get :index, params: { available: true } } - - let!(:invite) { Fabricate(:invite) } - - it 'renders index page' do - expect(subject).to render_template :index - expect(response.body) - .to include(invite.code) - end - end - - describe 'POST #create' do - subject { post :create, params: { invite: { max_uses: '10', expires_in: 1800 } } } - - it 'succeeds to create a invite' do - expect { subject }.to change(Invite, :count).by(1) - expect(subject).to redirect_to admin_invites_path - expect(Invite.last).to have_attributes(user_id: user.id, max_uses: 10) - end - end - - describe 'DELETE #destroy' do - subject { delete :destroy, params: { id: invite.id } } - - let!(:invite) { Fabricate(:invite, expires_at: nil) } - - it 'expires invite' do - expect(subject).to redirect_to admin_invites_path - expect(invite.reload).to be_expired - end - end - - describe 'POST #deactivate_all' do - before { Fabricate(:invite, expires_at: nil) } - - it 'expires all invites, then redirects to admin_invites_path' do - expect { post :deactivate_all } - .to change { Invite.exists?(expires_at: nil) } - .from(true) - .to(false) - - expect(response).to redirect_to admin_invites_path - end - end -end diff --git a/spec/system/admin/invites_spec.rb b/spec/system/admin/invites_spec.rb new file mode 100644 index 0000000000..f2cee626c6 --- /dev/null +++ b/spec/system/admin/invites_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Admin Invites' do + describe 'Invite interaction' do + let!(:invite) { Fabricate(:invite, expires_at: nil) } + + let(:user) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')) } + + before { sign_in user } + + it 'allows invite listing and creation' do + visit admin_invites_path + + expect(page) + .to have_title(I18n.t('admin.invites.title')) + for_invite(invite) do + expect(find('input').value) + .to include(invite.code) + end + + select I18n.t('invites.max_uses', count: 10), from: max_use_field + + expect { generate_invite } + .to change(Invite, :count).by(1) + expect(user.invites.last) + .to have_attributes(max_uses: 10) + end + + it 'allows invite expiration' do + visit admin_invites_path + + for_invite(invite) do + expect { expire_invite } + .to change { invite.reload.expired? }.from(false).to(true) + end + end + + it 'allows invite deactivation' do + visit admin_invites_path + + expect { click_on I18n.t('admin.invites.deactivate_all') } + .to change { Invite.exists?(expires_at: nil) }.from(true).to(false) + end + + def for_invite(invite, &block) + within("#invite_#{invite.id}", &block) + end + + def expire_invite + click_on I18n.t('invites.delete') + end + + def generate_invite + click_on I18n.t('invites.generate') + end + + def max_use_field + I18n.t('simple_form.labels.defaults.max_uses') + end + end +end