Skip to content

Commit

Permalink
Program CRUD tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpugach committed Mar 2, 2023
1 parent 37e4d6e commit 7c74d52
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 18 deletions.
20 changes: 11 additions & 9 deletions Gemfile.lock
Expand Up @@ -48,7 +48,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.4.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
afm (0.2.2)
airbrussh (1.4.0)
sshkit (>= 1.6.1, != 1.7.0)
Expand Down Expand Up @@ -79,13 +80,13 @@ GEM
i18n
rake (>= 10.0.0)
sshkit (>= 1.9.0)
capybara (2.9.2)
capybara (2.18.0)
addressable
mime-types (>= 1.16)
mini_mime (>= 0.1.3)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
xpath (>= 2.0, < 4.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
Expand Down Expand Up @@ -193,7 +194,7 @@ GEM
method_source (0.8.2)
mime-types (3.4.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2022.0105)
mime-types-data (3.2023.0218.1)
mini_magick (4.5.1)
mini_mime (1.1.2)
mini_portile2 (2.1.0)
Expand Down Expand Up @@ -225,8 +226,8 @@ GEM
ruby-rc4
ttfunk
pg (0.20.0)
poltergeist (1.10.0)
capybara (~> 2.1)
poltergeist (1.18.1)
capybara (>= 2.1, < 4)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
prawn (2.2.2)
Expand All @@ -249,11 +250,12 @@ GEM
pry (>= 0.9.10, < 0.11.0)
pry-rails (0.3.4)
pry (>= 0.9.10)
public_suffix (4.0.7)
puma (3.12.2)
pundit (1.1.0)
activesupport (>= 3.0.0)
raabro (1.1.6)
rack (2.2.3.1)
rack (2.2.6.2)
rack-contrib (1.2.0)
rack (>= 0.9.1)
rack-protection (1.5.3)
Expand Down Expand Up @@ -430,7 +432,7 @@ GEM
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
xpath (2.0.0)
xpath (2.1.0)
nokogiri (~> 1.3)

PLATFORMS
Expand Down
9 changes: 8 additions & 1 deletion app/policies/person_policy.rb
Expand Up @@ -12,7 +12,14 @@ def update_image?
end

def group_admins_index?
academic_group_writable
user.can_act?(
%w[
academic_group:edit
academic_group:new
program:edit
program:new
]
)
end

def group_curators_index?
Expand Down
5 changes: 4 additions & 1 deletion app/policies/questionnaire_policy.rb
Expand Up @@ -18,7 +18,10 @@ def save_answers?
end

def ui_index?
(user_activities & ['program:update', 'program:create']).any?
user.can_act?(%w[
program:edit
program:new
])
end

private
Expand Down
4 changes: 3 additions & 1 deletion app/views/programs/_form.html.haml
Expand Up @@ -20,7 +20,9 @@
.col-xs-6
= f.input :position

= f.association :manager, label_method: :complex_name, collection: [f.object.manager].compact,
= f.association :manager,
label_method: :complex_name,
collection: [f.object.manager].compact,
input_html: { data: { placeholder: t('.select_manager_placeholder'), 'ajax--url': ui_group_admins_path } }

= f.association :questionnaires,
Expand Down
73 changes: 73 additions & 0 deletions spec/features/programs/add_program_spec.rb
@@ -0,0 +1,73 @@
require 'rails_helper'

describe 'Add program:', :js do
Given(:user) { create(:person)}
Given(:activities) { %w[program:index] }
Given(:new_program_link_selector) { "a.btn-success[href=\"#{new_program_path}\"]" }

Given { user.roles << create(:role, activities: activities) }
Given { login_as(user) }

When { visit programs_path }

describe 'without rights' do
describe 'link navigation' do
Then { expect(page).not_to have_selector(new_program_link_selector) }
end

describe 'direct navigation' do
When { visit new_program_path }

Then { expect(find('.alert-dismissible')).to have_content(I18n.t('not_authorized')) }
And { expect(current_path).not_to eq(new_program_path) }
end
end

describe 'with rights to view form' do
Given(:activities) { %w[program:index program:new] }

describe 'navigates to the page directly' do
When { visit new_program_path }

Then { expect(current_path).to eq(new_program_path) }
end

describe 'does not allow to create' do
When { find(new_program_link_selector).click }
When { click_button I18n.t('programs.new.submit') }

Then { expect(find('.alert-dismissible')).to have_content(I18n.t('not_authorized')) }
And { expect(current_path).to eq(new_program_path) }
end
end

describe 'with rights to create' do
Given(:activities) { %w[program:index program:new program:create] }

describe 'creates the program' do
When { find(new_program_link_selector).click }
When { fill_program_data manager: user }
When { click_button I18n.t('programs.new.submit') }

Then { expect(current_path).to eq(programs_path) }
And { expect(page).to have_selector('.alert-notice') }
end
end

def fill_program_data(params = {})
program = build_stubbed(:program, params)

fill_in 'program_title_uk', with: program.title_uk
fill_in 'program_title_ru', with: program.title_ru
fill_in 'program_description_uk', with: program.description_uk
fill_in 'program_description_ru', with: program.description_ru

visibility_label = I18n.t("simple_form.options.program.visible.#{program.visible ? 'visible' : 'invisible' }")

select visibility_label, from: 'program_visible'

select2_single('program_manager', program.manager.complex_name)

program
end
end
45 changes: 45 additions & 0 deletions spec/features/programs/delete_program_spec.rb
@@ -0,0 +1,45 @@
require 'rails_helper'

describe 'Delete program:', :js do
Given!(:program) { create(:program) }
Given(:user) { create(:person)}
Given(:activities) { %w[program:index] }
Given(:delete_program_link_selector) { "a.btn-danger" }

Given { user.roles << create(:role, activities: activities) }
Given { login_as(user) }

When { visit programs_path }

describe 'without rights' do
Then { expect(page).not_to have_selector(delete_program_link_selector) }
end

describe 'with rights but with study application' do
Given(:activities) { %w[program:index program:destroy] }
Given(:student) { create(:person, :student) }

Given { StudyApplication.create(person_id: student.id, program_id: program.id) }

Then { expect(find('tbody tr:nth-child(1) td:nth-child(3)')).to have_content('1') }
And { expect(find('tbody tr:nth-child(1) td:nth-child(7)')).to have_selector('a.btn-danger.disabled') }
end

describe 'with rights to destroy' do
Given(:activities) { %w[program:index program:destroy] }
Given(:program_title) { program["title_#{user.locale}"] }

describe 'dismiss_confirm' do
When { dismiss_confirm { find(delete_program_link_selector).click } }

Then { expect(find('tbody tr:nth-child(1) td:nth-child(1)')).to have_content(program_title) }
end

describe 'accept_confirm' do
When { accept_confirm { find(delete_program_link_selector).click } }

Then { expect(page).not_to have_selector('tbody tr') }
And { expect(current_path).to eq(programs_path) }
end
end
end
74 changes: 74 additions & 0 deletions spec/features/programs/update_program_spec.rb
@@ -0,0 +1,74 @@
require 'rails_helper'

describe 'Update program:', :js do
Given!(:program) { create(:program) }
Given(:user) { create(:person)}
Given(:activities) { %w[program:index] }
Given(:edit_path) { edit_program_path(program.id) }
Given(:edit_program_link_selector) { "a.btn-warning[href=\"#{edit_path}\"]" }

Given { user.roles << create(:role, activities: activities) }
Given { login_as(user) }

When { visit programs_path }

describe 'without rights' do
describe 'link navigation' do
Then { expect(page).not_to have_selector(edit_program_link_selector) }
end

describe 'direct navigation' do
When { visit edit_path }

Then { expect(find('.alert-dismissible')).to have_content(I18n.t('not_authorized')) }
And { expect(current_path).not_to eq(edit_path) }
end
end

describe 'with rights to view form' do
Given(:activities) { %w[program:index program:edit] }

describe 'navigates to the page directly' do
When { visit edit_path }

Then { expect(current_path).to eq(edit_path) }
end

describe 'does not allow to save' do
When { find(edit_program_link_selector).click }
When { click_button I18n.t('programs.edit.submit') }

Then { expect(find('.alert-dismissible')).to have_content(I18n.t('not_authorized')) }
And { expect(current_path).to eq(edit_path) }
end
end

describe 'with rights to create' do
Given(:activities) { %w[program:index program:edit program:update] }
Given(:title_id) { "program_title_#{user.locale}"}
Given(:new_title_value) { 'Some title here' }

describe 'check the state of unmodified fields' do
Then { expect(find('tbody tr:nth-child(1) td:nth-child(1)')).not_to have_content(new_title_value) }
And { expect(find('tbody tr:nth-child(1) td:nth-child(6)')).to have_content('0') }
And { expect(current_path).to eq(programs_path) }
end

describe 'updates the program' do
Given(:questionnaire_title_field) { "title_#{user.locale}" }
Given!(:questionnaire_1) { create(:questionnaire) }
Given!(:questionnaire_2) { create(:questionnaire) }

When { find(edit_program_link_selector).click }
When { fill_in title_id, with: new_title_value }
When { select2_multi('program_questionnaires', questionnaire_1[questionnaire_title_field]) }
When { select2_multi('program_questionnaires', questionnaire_2[questionnaire_title_field]) }
When { click_button I18n.t('programs.edit.submit') }

Then { expect(current_path).to eq(programs_path) }
And { expect(page).to have_selector('.alert-notice') }
Then { expect(find('tbody tr:nth-child(1) td:nth-child(1)')).to have_content(new_title_value) }
And { expect(find('tbody tr:nth-child(1) td:nth-child(6)')).to have_content('2') }
end
end
end
5 changes: 4 additions & 1 deletion spec/policies/person_policy_spec.rb
Expand Up @@ -61,7 +61,10 @@

context 'given user\'s role activities' do
permissions :group_admins_index? do
it_behaves_like :allow_with_activities, %w(academic_group:edit academic_group:new)
it_behaves_like(
:allow_with_activities,
%w[academic_group:edit academic_group:new program:edit program:new]
)
end

permissions :group_curators_index? do
Expand Down
9 changes: 7 additions & 2 deletions spec/support/capybara.rb
Expand Up @@ -2,10 +2,15 @@
require 'capybara/rspec'
require 'capybara/rails'

Capybara.default_max_wait_time = 5
Capybara.default_max_wait_time = 15

Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, debug: false, window_size: [1300, 1000])
Capybara::Poltergeist::Driver.new(
app,
debug: false,
js_errors: false,
window_size: [1300, 1000]
)
end

Capybara.javascript_driver = :poltergeist
Expand Down
7 changes: 4 additions & 3 deletions spec/support/helper_methods.rb
Expand Up @@ -20,9 +20,10 @@ def all_activities
def screenshot
sleep 10

# rubocop:disable Lint/Debugger
save_screenshot(Rails.root.join('tmp/capybara', 'Screenshot' << Time.zone.now.strftime('%Y%m%d%H%M%S%L') << '.png'))
# rubocop:enable Lint/Debugger
file_path = Rails.root.join('tmp/capybara', 'Screenshot' << Time.zone.now.strftime('%Y%m%d%H%M%S%L')).to_s

save_screenshot(file_path + '.png')
save_page(file_path + '.html')
end

def select2_single(klass, option)
Expand Down

0 comments on commit 7c74d52

Please sign in to comment.