diff --git a/app/controllers/dashboard/poster_controller.rb b/app/controllers/dashboard/poster_controller.rb index 5fbd85adf..8bb155c10 100644 --- a/app/controllers/dashboard/poster_controller.rb +++ b/app/controllers/dashboard/poster_controller.rb @@ -5,7 +5,7 @@ class Dashboard::PosterController < Dashboard::BaseController respond_to do |format| format.html format.pdf do - render pdf: 'poster', page_size: 'A3' + render pdf: 'poster', page_size: 'A3', show_as_html: Rails.env.test? end end end diff --git a/app/models/dashboard/action.rb b/app/models/dashboard/action.rb index 45818c23b..3bff7fd9f 100644 --- a/app/models/dashboard/action.rb +++ b/app/models/dashboard/action.rb @@ -77,8 +77,4 @@ class Dashboard::Action < ActiveRecord::Base def self.next_goal_for(proposal) course_for(proposal).first end - - def request_to_administrators? - request_to_administrators || false - end end diff --git a/app/models/poll.rb b/app/models/poll.rb index be313d75b..8bf4b485b 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -104,9 +104,10 @@ class Poll < ActiveRecord::Base end def only_one_active - if Poll.overlaping_with(self).any? - errors.add(:starts_at, I18n.t('activerecord.errors.messages.another_poll_active')) - end + return unless starts_at.present? + return unless ends_at.present? + return unless Poll.overlaping_with(self).any? + errors.add(:starts_at, I18n.t('activerecord.errors.messages.another_poll_active')) end def public? diff --git a/app/models/proposal_dashboard_action.rb b/app/models/proposal_dashboard_action.rb deleted file mode 100644 index 221034ce5..000000000 --- a/app/models/proposal_dashboard_action.rb +++ /dev/null @@ -1,88 +0,0 @@ -class ProposalDashboardAction < ActiveRecord::Base - include Documentable - documentable max_documents_allowed: 3, - max_file_size: 3.megabytes, - accepted_content_types: [ 'application/pdf' ] - - include Linkable - - acts_as_paranoid column: :hidden_at - include ActsAsParanoidAliases - - has_many :proposal_executed_dashboard_actions, dependent: :restrict_with_error - has_many :proposals, through: :proposal_executed_dashboard_actions - - enum action_type: [:proposed_action, :resource] - - validates :title, - presence: true, - allow_blank: false, - length: { in: 4..80 } - - validates :action_type, presence: true - - validates :day_offset, - presence: true, - numericality: { - only_integer: true, - greater_than_or_equal_to: 0 - } - - validates :required_supports, - presence: true, - numericality: { - only_integer: true, - greater_than_or_equal_to: 0 - } - - scope :active, -> { where(active: true) } - scope :inactive, -> { where(active: false) } - scope :resources, -> { where(action_type: 1) } - scope :proposed_actions, -> { where(action_type: 0) } - scope :active_for, ->(proposal) do - published_at = proposal.published_at&.to_date || Date.today - - active - .where('required_supports <= ?', proposal.votes_for.size) - .where('day_offset <= ?', (Date.today - published_at).to_i) - end - - scope :course_for, lambda { |proposal| - active - .resources - .where('required_supports > ?', proposal.votes_for.size) - .order(required_supports: :asc) - } - - def active_for?(proposal) - published_at = proposal.published_at&.to_date || Date.today - - required_supports <= proposal.votes_for.size && day_offset <= (Date.today - published_at).to_i - end - - def requested_for?(proposal) - executed_action = proposal_executed_dashboard_actions.find_by(proposal: proposal) - return false if executed_action.nil? - - executed_action.administrator_tasks.any? - end - - def executed_for?(proposal) - executed_action = proposal_executed_dashboard_actions.find_by(proposal: proposal) - return false if executed_action.nil? - - executed_action.administrator_tasks.where.not(executed_at: nil).any? - end - - def self.next_goal_for(proposal) - course_for(proposal).first - end - - def request_to_administrators? - request_to_administrators || false - end - - def request_to_administrators? - request_to_administrators - end -end diff --git a/app/models/proposal_executed_dashboard_action.rb b/app/models/proposal_executed_dashboard_action.rb deleted file mode 100644 index 8b7b2f918..000000000 --- a/app/models/proposal_executed_dashboard_action.rb +++ /dev/null @@ -1,10 +0,0 @@ -class ProposalExecutedDashboardAction < ActiveRecord::Base - belongs_to :proposal - belongs_to :proposal_dashboard_action - - has_many :administrator_tasks, as: :source, dependent: :destroy - - validates :proposal, presence: true, uniqueness: { scope: :proposal_dashboard_action } - validates :proposal_dashboard_action, presence: true - validates :executed_at, presence: true -end diff --git a/spec/factories.rb b/spec/factories.rb index 2e875c90f..3baecfb1d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1038,7 +1038,7 @@ LOREM_IPSUM end factory :dashboard_action, class: 'Dashboard::Action' do - title { Faker::Lorem.sentence[0..80] } + title { Faker::Lorem.sentence[0..79] } description { Faker::Lorem.sentence } link nil request_to_administrators true diff --git a/spec/features/admin/dashboard/actions_spec.rb b/spec/features/admin/dashboard/actions_spec.rb index 55a00d19a..3d790eac4 100644 --- a/spec/features/admin/dashboard/actions_spec.rb +++ b/spec/features/admin/dashboard/actions_spec.rb @@ -47,6 +47,11 @@ feature 'Admin dashboard actions' do expect(page).to have_content(action.title) end + + scenario 'Renders create form in case data is invalid' do + click_button 'Save' + expect(page).to have_content('errors prevented this Dashboard/Action from being saved.') + end end context 'when editing an action' do @@ -64,6 +69,12 @@ feature 'Admin dashboard actions' do expect(page).to have_content(title) end + + scenario 'Renders edit form in case data is invalid' do + fill_in 'dashboard_action_title', with: 'x' + click_button 'Save' + expect(page).to have_content('error prevented this Dashboard/Action from being saved.') + end end context 'when destroying an action' do @@ -80,5 +91,15 @@ feature 'Admin dashboard actions' do expect(page).not_to have_content(action.title) end + + scenario 'can not delete actions that have been executed', js: true do + _executed_action = create(:dashboard_executed_action, action: action) + + page.accept_confirm do + click_link 'Delete' + end + + expect(page).to have_content('Cannot delete record because dependent executed actions exist') + end end end diff --git a/spec/features/dashboard/dashboard_spec.rb b/spec/features/dashboard/dashboard_spec.rb index 269eb567a..4d03fb353 100644 --- a/spec/features/dashboard/dashboard_spec.rb +++ b/spec/features/dashboard/dashboard_spec.rb @@ -126,6 +126,18 @@ feature "Proposal's dashboard" do expect(page).to have_content('The request for the administrator has been successfully sent.') end + scenario 'Request already requested resource with admin request', js: true do + feature = create(:dashboard_action, :resource, :active, :admin_request) + + visit proposal_dashboard_index_path(proposal) + click_link(feature.title) + + create(:dashboard_executed_action, action: feature, proposal: proposal) + + click_button 'Request' + expect(page).to have_content('Proposal has already been taken') + end + scenario 'Resource without admin request do not have a request link', js: true do feature = create(:dashboard_action, :resource, :active) diff --git a/spec/features/dashboard/polls_spec.rb b/spec/features/dashboard/polls_spec.rb index 9e95b7b9f..b0a2dc09f 100644 --- a/spec/features/dashboard/polls_spec.rb +++ b/spec/features/dashboard/polls_spec.rb @@ -19,13 +19,13 @@ feature 'Polls' do start_date = 1.week.from_now end_date = 2.weeks.from_now - fill_in "poll_name", with: "Upcoming poll" - fill_in 'poll_starts_at', with: start_date.strftime("%d/%m/%Y") - fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y") + fill_in "poll_name", with: 'Upcoming poll' + fill_in 'poll_starts_at', with: start_date.strftime('%d/%m/%Y') + fill_in 'poll_ends_at', with: end_date.strftime('%d/%m/%Y') fill_in 'poll_description', with: "Upcomming poll's description. This poll..." - expect(page).not_to have_css("#poll_results_enabled") - expect(page).not_to have_css("#poll_stats_enabled") + expect(page).not_to have_css('#poll_results_enabled') + expect(page).not_to have_css('#poll_stats_enabled') click_link 'Add question' @@ -34,13 +34,22 @@ feature 'Polls' do click_link 'Add answer' fill_in 'Title', with: 'First answer' - click_button "Create poll" + click_button 'Create poll' - expect(page).to have_content "Poll created successfully" - expect(page).to have_content "Upcoming poll" + expect(page).to have_content 'Poll created successfully' + expect(page).to have_content 'Upcoming poll' expect(page).to have_content I18n.l(start_date.to_date) end + scenario 'Create a poll redirects back to form when invalid data', js: true do + click_link 'Polls' + click_link 'Create poll' + + click_button 'Create poll' + + expect(page).to have_content('New poll') + end + scenario 'Edit poll is allowed for upcoming polls' do poll = create(:poll, :incoming, related: proposal) @@ -57,6 +66,24 @@ feature 'Polls' do expect(page).to have_content 'Poll updated successfully' end + scenario 'Edit poll redirects back when invalid data', js: true do + poll = create(:poll, :incoming, related: proposal) + + visit proposal_dashboard_polls_path(proposal) + + within "div#poll_#{poll.id}" do + expect(page).to have_content('Edit survey') + + click_link 'Edit survey' + end + + fill_in "poll_name", with: '' + + click_button 'Update poll' + + expect(page).to have_content('Edit poll') + end + scenario 'Edit poll is not allowed for current polls' do poll = create(:poll, :current, related: proposal) diff --git a/spec/features/dashboard/poster_spec.rb b/spec/features/dashboard/poster_spec.rb index b245dead3..73d9dccb9 100644 --- a/spec/features/dashboard/poster_spec.rb +++ b/spec/features/dashboard/poster_spec.rb @@ -29,5 +29,14 @@ feature 'Poster' do expect(page).not_to have_link('Preview') expect(page).to have_link('Download') end + + scenario 'PDF contains the proposal details', js: true do + click_link 'Download' + + page.driver.browser.switch_to.window page.driver.browser.window_handles.last do + expect(page).to have_content(proposal.title) + expect(page).to have_content(proposal.code) + end + end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d353073df..379703518 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,3 +1,8 @@ +unless ENV['TRAVIS'] + require 'simplecov' + SimpleCov.start 'rails' +end + ENV['RAILS_ENV'] ||= 'test' if ENV['TRAVIS'] require 'coveralls'