From 6e97a80044f7b69e4039134264a69561eeb44de9 Mon Sep 17 00:00:00 2001 From: jseraf Date: Thu, 8 Oct 2020 12:46:19 -0500 Subject: [PATCH] PR feedback: use shared examples in specs #627 --- spec/models/panel_spec.rb | 47 ++++++++++++++++------- spec/system/panels/panels_spec.rb | 63 ++++++++++++++----------------- 2 files changed, 62 insertions(+), 48 deletions(-) diff --git a/spec/models/panel_spec.rb b/spec/models/panel_spec.rb index 08fd7cda..b57005ee 100644 --- a/spec/models/panel_spec.rb +++ b/spec/models/panel_spec.rb @@ -61,20 +61,33 @@ end context 'meeting_link' do - it 'requires a valid secure url' do - panel.meeting_link = 'invalid' - expect(panel).not_to be_valid - expect(panel.errors.full_messages).to include 'Meeting Link is not a valid secure URL.' - panel.meeting_link = 'ftp://abc.com/' - expect(panel).not_to be_valid - expect(panel.errors.full_messages).to include 'Meeting Link is not a valid secure URL.' - panel.meeting_link = 'http://abc.com/' - expect(panel).not_to be_valid - expect(panel.errors.full_messages).to include 'Meeting Link is not a valid secure URL.' - panel.meeting_link = 'https://abc.com/' - expect(panel).to be_valid - panel.meeting_link = 'https://college.zoom.us/z/123456789' - expect(panel).to be_valid + context 'requires a valid secure url' do + it 'may not be a string' do + panel.meeting_link = 'invalid' + is_invalid_meeting_link(panel: panel) + end + + it 'may not be ftp' do + panel.meeting_link = 'ftp://abc.com/' + is_invalid_meeting_link(panel: panel) + end + + it 'may not be http' do + panel.meeting_link = 'http://abc.com/' + is_invalid_meeting_link(panel: panel) + end + end + + context 'valid secure url' do + it 'validates url' do + panel.meeting_link = 'https://abc.com/' + expect(panel).to be_valid + end + + it 'validates full path url' do + panel.meeting_link = 'https://college.zoom.us/z/123456789' + expect(panel).to be_valid + end end end end @@ -106,4 +119,10 @@ end end end + + def is_invalid_meeting_link(panel:) + expect(panel).not_to be_valid + expect(panel.errors.full_messages).to include 'Meeting Link is not a valid secure URL.' + end + end diff --git a/spec/system/panels/panels_spec.rb b/spec/system/panels/panels_spec.rb index da634c29..bfdf2223 100644 --- a/spec/system/panels/panels_spec.rb +++ b/spec/system/panels/panels_spec.rb @@ -1,45 +1,33 @@ require 'rails_helper' RSpec.describe 'Panels', type: :system, js: true do - button_text = 'Update Panel Information' - - let(:grant) { create(:open_grant_with_users_and_form_and_submission_and_reviewer) } - let(:admin) { grant.admins.first } - let(:editor) { grant.editors.first } - let(:viewer) { grant.viewers.first } + let(:grant) { create(:open_grant_with_users_and_form_and_submission_and_reviewer) } + let(:admin) { grant.admins.first } + let(:editor) { grant.editors.first } + let(:viewer) { grant.viewers.first } + let(:button_text) { 'Update Panel Information' } describe 'Edit' do context 'user' do context 'with grant_permission' do context 'grant_admin' do - before(:each) do - login_as admin, scope: admin.type.underscore.to_sym - visit edit_grant_panel_path(grant) - end - scenario 'can visit the edit page' do - expect(page).not_to have_content 'You are not authorized to perform this action.' + login_as admin, scope: admin.type.underscore.to_sym + can_vist_edit_page(user: admin) end end context 'grant_editor' do - before(:each) do - login_as editor, scope: editor.type.underscore.to_sym - visit edit_grant_panel_path(grant) - end - scenario 'can visit the edit page' do - expect(page).not_to have_content 'You are not authorized to perform this action.' + login_as admin, scope: admin.type.underscore.to_sym + can_vist_edit_page(user: editor) end end context 'grant_viewer' do - before(:each) do + scenario 'cannot visit the edit page' do login_as viewer, scope: viewer.type.underscore.to_sym visit edit_grant_panel_path(grant) - end - - scenario 'cannot visit the edit page' do expect(page).to have_content 'You are not authorized to perform this action.' end end @@ -54,30 +42,21 @@ context 'grant_admin' do before(:each) do login_as admin, scope: admin.type.underscore.to_sym - visit edit_grant_panel_path(grant) + # visit edit_grant_panel_path(grant) end scenario 'may update' do - new_address = Faker::Address.full_address - page.fill_in 'Meeting Location', with: new_address, fill_options: { clear: :backspace } - click_button button_text - expect(page).to have_content 'Panel information successfully updated.' - expect(grant.panel.meeting_location).to eql new_address + can_update_panel(user: admin) end end context 'grant_editor' do before(:each) do login_as editor, scope: editor.type.underscore.to_sym - visit edit_grant_panel_path(grant) end scenario 'may update' do - new_address = Faker::Address.full_address - page.fill_in 'Meeting Location', with: new_address, fill_options: { clear: :backspace } - click_button button_text - expect(page).to have_content 'Panel information successfully updated.' - expect(grant.panel.meeting_location).to eql new_address + can_update_panel(user: editor) end end end @@ -150,4 +129,20 @@ end end end + + def can_vist_edit_page(user:) + visit edit_grant_panel_path(grant) + + expect(page).not_to have_content 'You are not authorized to perform this action.' + end + + def can_update_panel(user:) + visit edit_grant_panel_path(grant) + new_address = Faker::Address.full_address + page.fill_in 'Meeting Location', with: new_address, fill_options: { clear: :backspace } + click_button button_text + expect(page).to have_content 'Panel information successfully updated.' + expect(grant.panel.meeting_location).to eql new_address + end + end