diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb new file mode 100644 index 000000000..ab3db9c2b --- /dev/null +++ b/app/models/abilities/administrator.rb @@ -0,0 +1,37 @@ +module Abilities + class Administrator + include CanCan::Ability + + def initialize(user) + self.merge Abilities::Moderation.new(user) + + can :restore, Comment + cannot :restore, Comment, hidden_at: nil + + can :restore, Debate + cannot :restore, Debate, hidden_at: nil + + can :restore, Proposal + cannot :restore, Proposal, hidden_at: nil + + can :restore, User + cannot :restore, User, hidden_at: nil + + can :confirm_hide, Comment + cannot :confirm_hide, Comment, hidden_at: nil + + can :confirm_hide, Debate + cannot :confirm_hide, Debate, hidden_at: nil + + can :confirm_hide, Proposal + cannot :confirm_hide, Proposal, hidden_at: nil + + can :confirm_hide, User + cannot :confirm_hide, User, hidden_at: nil + + can :comment_as_administrator, [Debate, Comment, Proposal] + + can [:search, :create, :index, :destroy], ::Moderator + end + end +end diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb new file mode 100644 index 000000000..b8b951896 --- /dev/null +++ b/app/models/abilities/common.rb @@ -0,0 +1,44 @@ +module Abilities + class Common + include CanCan::Ability + + def initialize(user) + self.merge Abilities::Everyone.new(user) + + can [:read, :update], User, id: user.id + + can :read, Debate + can :update, Debate do |debate| + debate.editable_by?(user) + end + + can :read, Proposal + can :update, Proposal do |proposal| + proposal.editable_by?(user) + end + + can :create, Comment + can :create, Debate + can :create, Proposal + + can [:flag, :unflag], Comment + cannot [:flag, :unflag], Comment, user_id: user.id + + can [:flag, :unflag], Debate + cannot [:flag, :unflag], Debate, author_id: user.id + + can [:flag, :unflag], Proposal + cannot [:flag, :unflag], Proposal, author_id: user.id + + unless user.organization? + can :vote, Debate + can :vote, Comment + end + + if user.level_two_or_three_verified? + can :vote, Proposal + end + + end + end +end diff --git a/app/models/abilities/everyone.rb b/app/models/abilities/everyone.rb new file mode 100644 index 000000000..5f5de51b7 --- /dev/null +++ b/app/models/abilities/everyone.rb @@ -0,0 +1,10 @@ +module Abilities + class Everyone + include CanCan::Ability + + def initialize(user) + can :read, Debate + can :read, Proposal + end + end +end diff --git a/app/models/abilities/moderation.rb b/app/models/abilities/moderation.rb new file mode 100644 index 000000000..2fa26a607 --- /dev/null +++ b/app/models/abilities/moderation.rb @@ -0,0 +1,48 @@ +module Abilities + class Moderation + include CanCan::Ability + + def initialize(user) + self.merge Abilities::Common.new(user) + + can :read, Organization + can(:verify, Organization){ |o| !o.verified? } + can(:reject, Organization){ |o| !o.rejected? } + + can :read, Comment + + can :hide, Comment, hidden_at: nil + cannot :hide, Comment, user_id: user.id + + can :ignore_flag, Comment, ignored_flag_at: nil, hidden_at: nil + cannot :ignore_flag, Comment, user_id: user.id + + can :moderate, Comment + cannot :moderate, Comment, user_id: user.id + + can :hide, Debate, hidden_at: nil + cannot :hide, Debate, author_id: user.id + + can :ignore_flag, Debate, ignored_flag_at: nil, hidden_at: nil + cannot :ignore_flag, Debate, author_id: user.id + + can :moderate, Debate + cannot :moderate, Debate, author_id: user.id + + can :hide, Proposal, hidden_at: nil + cannot :hide, Proposal, author_id: user.id + + can :ignore_flag, Proposal, ignored_flag_at: nil, hidden_at: nil + cannot :ignore_flag, Proposal, author_id: user.id + + can :moderate, Proposal + cannot :moderate, Proposal, author_id: user.id + + can :hide, User + cannot :hide, User, id: user.id + + can :block, User + cannot :block, User, id: user.id + end + end +end diff --git a/app/models/abilities/moderator.rb b/app/models/abilities/moderator.rb new file mode 100644 index 000000000..f6c5c5004 --- /dev/null +++ b/app/models/abilities/moderator.rb @@ -0,0 +1,11 @@ +module Abilities + class Moderator + include CanCan::Ability + + def initialize(user) + self.merge Abilities::Moderation.new(user) + + can :comment_as_moderator, [Debate, Comment, Proposal] + end + end +end diff --git a/app/models/ability.rb b/app/models/ability.rb index 2d4c98536..3d5c15d91 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -2,124 +2,20 @@ class Ability include CanCan::Ability def initialize(user) - # If someone can hide something, he can also hide it # from the moderation screen alias_action :hide_in_moderation_screen, to: :hide - # Not logged in users - can :read, Debate - can :read, Proposal - if user # logged-in users - can [:read, :update], User, id: user.id - - can :read, Debate - can :update, Debate do |debate| - debate.editable_by?(user) - end - - can :read, Proposal - can :update, Proposal do |proposal| - proposal.editable_by?(user) - end - - can :create, Comment - can :create, Debate - can :create, Proposal - - can [:flag, :unflag], Comment - cannot [:flag, :unflag], Comment, user_id: user.id - - can [:flag, :unflag], Debate - cannot [:flag, :unflag], Debate, author_id: user.id - - can [:flag, :unflag], Proposal - cannot [:flag, :unflag], Proposal, author_id: user.id - - unless user.organization? - can :vote, Debate - can :vote, Comment - end - - if user.level_two_or_three_verified? - can :vote, Proposal - end - - if user.moderator? || user.administrator? - can :read, Organization - can(:verify, Organization){ |o| !o.verified? } - can(:reject, Organization){ |o| !o.rejected? } - - can :read, Comment - - can :hide, Comment, hidden_at: nil - cannot :hide, Comment, user_id: user.id - - can :ignore_flag, Comment, ignored_flag_at: nil, hidden_at: nil - cannot :ignore_flag, Comment, user_id: user.id - - can :moderate, Comment - cannot :moderate, Comment, user_id: user.id - - can :hide, Debate, hidden_at: nil - cannot :hide, Debate, author_id: user.id - - can :ignore_flag, Debate, ignored_flag_at: nil, hidden_at: nil - cannot :ignore_flag, Debate, author_id: user.id - - can :moderate, Debate - cannot :moderate, Debate, author_id: user.id - - can :hide, Proposal, hidden_at: nil - cannot :hide, Proposal, author_id: user.id - - can :ignore_flag, Proposal, ignored_flag_at: nil, hidden_at: nil - cannot :ignore_flag, Proposal, author_id: user.id - - can :moderate, Proposal - cannot :moderate, Proposal, author_id: user.id - - can :hide, User - cannot :hide, User, id: user.id - - can :block, User - cannot :block, User, id: user.id - end - - if user.moderator? - can :comment_as_moderator, [Debate, Comment, Proposal] - end - if user.administrator? - can :restore, Comment - cannot :restore, Comment, hidden_at: nil - - can :restore, Debate - cannot :restore, Debate, hidden_at: nil - - can :restore, Proposal - cannot :restore, Proposal, hidden_at: nil - - can :restore, User - cannot :restore, User, hidden_at: nil - - can :confirm_hide, Comment - cannot :confirm_hide, Comment, hidden_at: nil - - can :confirm_hide, Debate - cannot :confirm_hide, Debate, hidden_at: nil - - can :confirm_hide, Proposal - cannot :confirm_hide, Proposal, hidden_at: nil - - can :confirm_hide, User - cannot :confirm_hide, User, hidden_at: nil - - can :comment_as_administrator, [Debate, Comment, Proposal] - - can :manage, Moderator + self.merge Abilities::Administrator.new(user) + elsif user.moderator? + self.merge Abilities::Moderator.new(user) + else + self.merge Abilities::Common.new(user) end + else + self.merge Abilities::Everyone.new(user) end end diff --git a/spec/features/admin/moderators_spec.rb b/spec/features/admin/moderators_spec.rb index d190e75a0..e8e29b182 100644 --- a/spec/features/admin/moderators_spec.rb +++ b/spec/features/admin/moderators_spec.rb @@ -2,21 +2,20 @@ require 'rails_helper' feature 'Admin moderators' do background do - @user = create(:user, username: 'Jose Luis Balbin') - @moderator = create(:moderator) @admin = create(:administrator) + @user = create(:user, username: 'Jose Luis Balbin') + @moderator = create(:moderator) login_as(@admin.user) + visit admin_moderators_path end scenario 'Index' do - visit admin_moderators_path expect(page).to have_content @moderator.name expect(page).to have_content @moderator.email expect(page).to_not have_content @user.name end scenario 'Create Moderator', :js do - visit admin_moderators_path fill_in 'email', with: @user.email click_button 'Search' @@ -28,7 +27,6 @@ feature 'Admin moderators' do end scenario 'Delete Moderator' do - visit admin_moderators_path click_link 'Delete' within("#moderators") do diff --git a/spec/models/abilities/administrator_spec.rb b/spec/models/abilities/administrator_spec.rb new file mode 100644 index 000000000..16b568652 --- /dev/null +++ b/spec/models/abilities/administrator_spec.rb @@ -0,0 +1,52 @@ +require 'rails_helper' +require 'cancan/matchers' + +describe "Abilities::Administrator" do + subject(:ability) { Ability.new(user) } + let(:user) { administrator.user } + let(:administrator) { create(:administrator) } + + let(:other_user) { create(:user) } + let(:hidden_user) { create(:user, :hidden) } + + let(:debate) { create(:debate) } + let(:comment) { create(:comment) } + let(:proposal) { create(:proposal) } + + let(:hidden_debate) { create(:debate, :hidden) } + let(:hidden_comment) { create(:comment, :hidden) } + let(:hidden_proposal) { create(:proposal, :hidden) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + it { should be_able_to(:vote, debate) } + + it { should be_able_to(:index, Proposal) } + it { should be_able_to(:show, proposal) } + + it { should_not be_able_to(:restore, comment) } + it { should_not be_able_to(:restore, debate) } + it { should_not be_able_to(:restore, proposal) } + it { should_not be_able_to(:restore, other_user) } + + it { should be_able_to(:restore, hidden_comment) } + it { should be_able_to(:restore, hidden_debate) } + it { should be_able_to(:restore, hidden_proposal) } + it { should be_able_to(:restore, hidden_user) } + + it { should_not be_able_to(:confirm_hide, comment) } + it { should_not be_able_to(:confirm_hide, debate) } + it { should_not be_able_to(:confirm_hide, proposal) } + it { should_not be_able_to(:confirm_hide, other_user) } + + it { should be_able_to(:confirm_hide, hidden_comment) } + it { should be_able_to(:confirm_hide, hidden_debate) } + it { should be_able_to(:confirm_hide, hidden_proposal) } + it { should be_able_to(:confirm_hide, hidden_user) } + + it { should be_able_to(:comment_as_administrator, debate) } + it { should_not be_able_to(:comment_as_moderator, debate) } + + it { should be_able_to(:comment_as_administrator, proposal) } + it { should_not be_able_to(:comment_as_moderator, proposal) } +end diff --git a/spec/models/abilities/common_spec.rb b/spec/models/abilities/common_spec.rb new file mode 100644 index 000000000..f15fb13c1 --- /dev/null +++ b/spec/models/abilities/common_spec.rb @@ -0,0 +1,92 @@ +require 'rails_helper' +require 'cancan/matchers' + +describe "Abilities::Common" do + subject(:ability) { Ability.new(user) } + + let(:user) { create(:user) } + + let(:debate) { create(:debate) } + let(:comment) { create(:comment) } + let(:proposal) { create(:proposal) } + let(:own_debate) { create(:debate, author: user) } + let(:own_comment) { create(:comment, author: user) } + let(:own_proposal) { create(:proposal, author: user) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + it { should be_able_to(:vote, debate) } + + it { should be_able_to(:show, user) } + it { should be_able_to(:edit, user) } + + it { should be_able_to(:create, Comment) } + it { should be_able_to(:vote, Comment) } + + it { should be_able_to(:index, Proposal) } + it { should be_able_to(:show, proposal) } + it { should_not be_able_to(:vote, Proposal) } + + it { should_not be_able_to(:comment_as_administrator, debate) } + it { should_not be_able_to(:comment_as_moderator, debate) } + it { should_not be_able_to(:comment_as_administrator, proposal) } + it { should_not be_able_to(:comment_as_moderator, proposal) } + + describe 'flagging content' do + it { should be_able_to(:flag, debate) } + it { should be_able_to(:unflag, debate) } + + it { should be_able_to(:flag, comment) } + it { should be_able_to(:unflag, comment) } + + it { should be_able_to(:flag, proposal) } + it { should be_able_to(:unflag, proposal) } + + describe "own content" do + it { should_not be_able_to(:flag, own_comment) } + it { should_not be_able_to(:unflag, own_comment) } + + it { should_not be_able_to(:flag, own_debate) } + it { should_not be_able_to(:unflag, own_debate) } + + it { should_not be_able_to(:flag, own_proposal) } + it { should_not be_able_to(:unflag, own_proposal) } + end + end + + describe "other users" do + let(:other_user) { create(:user) } + it { should_not be_able_to(:show, other_user) } + it { should_not be_able_to(:edit, other_user) } + end + + describe "editing debates" do + let(:own_debate_non_editable) { create(:debate, author: user) } + before { allow(own_debate_non_editable).to receive(:editable?).and_return(false) } + + it { should be_able_to(:edit, own_debate) } + it { should_not be_able_to(:edit, debate) } # Not his + it { should_not be_able_to(:edit, own_debate_non_editable) } + end + + describe "editing proposals" do + let(:own_proposal_non_editable) { create(:proposal, author: user) } + before { allow(own_proposal_non_editable).to receive(:editable?).and_return(false) } + + it { should be_able_to(:edit, own_proposal) } + it { should_not be_able_to(:edit, proposal) } # Not his + it { should_not be_able_to(:edit, own_proposal_non_editable) } + end + + describe "when level 2 verified" do + before{ user.update(residence_verified_at: Time.now, confirmed_phone: "1") } + + it { should be_able_to(:vote, Proposal) } + end + + describe "when level 3 verified" do + before{ user.update(verified_at: Time.now) } + + it { should be_able_to(:vote, Proposal) } + end +end diff --git a/spec/models/abilities/everyone_spec.rb b/spec/models/abilities/everyone_spec.rb new file mode 100644 index 000000000..4c532b7a4 --- /dev/null +++ b/spec/models/abilities/everyone_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' +require 'cancan/matchers' + +describe "Abilities::Everyone" do + subject(:ability) { Ability.new(user) } + + let(:user) { nil } + let(:debate) { create(:debate) } + let(:proposal) { create(:proposal) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + it { should_not be_able_to(:edit, Debate) } + it { should_not be_able_to(:vote, Debate) } + it { should_not be_able_to(:flag, Debate) } + it { should_not be_able_to(:unflag, Debate) } + + it { should be_able_to(:index, Proposal) } + it { should be_able_to(:show, proposal) } + it { should_not be_able_to(:edit, Proposal) } + it { should_not be_able_to(:vote, Proposal) } + it { should_not be_able_to(:flag, Proposal) } + it { should_not be_able_to(:unflag, Proposal) } +end diff --git a/spec/models/abilities/moderator_spec.rb b/spec/models/abilities/moderator_spec.rb new file mode 100644 index 000000000..c49fe572a --- /dev/null +++ b/spec/models/abilities/moderator_spec.rb @@ -0,0 +1,107 @@ +require 'rails_helper' +require 'cancan/matchers' + +describe "Abilities::Moderator" do + subject(:ability) { Ability.new(user) } + let(:user) { moderator.user } + let(:moderator) { create(:moderator) } + + let(:other_user) { create(:user) } + + let(:debate) { create(:debate) } + let(:comment) { create(:comment) } + let(:proposal) { create(:proposal) } + + let(:own_debate) { create(:debate, author: user) } + let(:own_comment) { create(:comment, author: user) } + let(:own_proposal) { create(:proposal, author: user) } + + let(:hidden_debate) { create(:debate, :hidden) } + let(:hidden_comment) { create(:comment, :hidden) } + let(:hidden_proposal) { create(:proposal, :hidden) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + it { should be_able_to(:vote, debate) } + + it { should be_able_to(:index, Proposal) } + it { should be_able_to(:show, proposal) } + + it { should be_able_to(:read, Organization) } + + describe "organizations" do + let(:pending_organization) { create(:organization) } + let(:rejected_organization) { create(:organization, :rejected) } + let(:verified_organization) { create(:organization, :verified) } + + it { should be_able_to( :verify, pending_organization) } + it { should be_able_to( :reject, pending_organization) } + + it { should_not be_able_to(:verify, verified_organization) } + it { should be_able_to( :reject, verified_organization) } + + it { should_not be_able_to(:reject, rejected_organization) } + it { should be_able_to( :verify, rejected_organization) } + end + + describe "hiding, reviewing and restoring" do + let(:ignored_comment) { create(:comment, :with_ignored_flag) } + let(:ignored_debate) { create(:debate, :with_ignored_flag) } + let(:ignored_proposal) { create(:proposal,:with_ignored_flag) } + + it { should be_able_to(:hide, comment) } + it { should be_able_to(:hide_in_moderation_screen, comment) } + it { should_not be_able_to(:hide, hidden_comment) } + it { should_not be_able_to(:hide, own_comment) } + + it { should be_able_to(:moderate, comment) } + it { should_not be_able_to(:moderate, own_comment) } + + it { should be_able_to(:hide, debate) } + it { should be_able_to(:hide_in_moderation_screen, debate) } + it { should_not be_able_to(:hide, hidden_debate) } + it { should_not be_able_to(:hide, own_debate) } + + it { should be_able_to(:hide, proposal) } + it { should be_able_to(:hide_in_moderation_screen, proposal) } + it { should_not be_able_to(:hide, hidden_proposal) } + it { should_not be_able_to(:hide, own_proposal) } + + it { should be_able_to(:ignore_flag, comment) } + it { should_not be_able_to(:ignore_flag, hidden_comment) } + it { should_not be_able_to(:ignore_flag, ignored_comment) } + it { should_not be_able_to(:ignore_flag, own_comment) } + + it { should be_able_to(:ignore_flag, debate) } + it { should_not be_able_to(:ignore_flag, hidden_debate) } + it { should_not be_able_to(:ignore_flag, ignored_debate) } + it { should_not be_able_to(:ignore_flag, own_debate) } + + it { should be_able_to(:moderate, debate) } + it { should_not be_able_to(:moderate, own_debate) } + + it { should be_able_to(:ignore_flag, proposal) } + it { should_not be_able_to(:ignore_flag, hidden_proposal) } + it { should_not be_able_to(:ignore_flag, ignored_proposal) } + it { should_not be_able_to(:ignore_flag, own_proposal) } + + it { should be_able_to(:moderate, proposal) } + it { should_not be_able_to(:moderate, own_proposal) } + + it { should_not be_able_to(:hide, user) } + it { should be_able_to(:hide, other_user) } + + it { should_not be_able_to(:block, user) } + it { should be_able_to(:block, other_user) } + + it { should_not be_able_to(:restore, comment) } + it { should_not be_able_to(:restore, debate) } + it { should_not be_able_to(:restore, proposal) } + it { should_not be_able_to(:restore, other_user) } + + it { should be_able_to(:comment_as_moderator, debate) } + it { should be_able_to(:comment_as_moderator, proposal) } + it { should_not be_able_to(:comment_as_administrator, debate) } + it { should_not be_able_to(:comment_as_administrator, proposal) } + end +end diff --git a/spec/models/abilities/organization_spec.rb b/spec/models/abilities/organization_spec.rb new file mode 100644 index 000000000..1d7687b64 --- /dev/null +++ b/spec/models/abilities/organization_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' +require 'cancan/matchers' + +describe "Abilities::Organization" do + subject(:ability) { Ability.new(user) } + let(:user) { organization.user } + let(:organization) { create(:organization) } + let(:debate) { create(:debate) } + let(:proposal) { create(:proposal) } + + it { should be_able_to(:show, user) } + it { should be_able_to(:edit, user) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + it { should_not be_able_to(:vote, debate) } + + it { should be_able_to(:index, Proposal) } + it { should be_able_to(:show, proposal) } + it { should_not be_able_to(:vote, Proposal) } + + it { should be_able_to(:create, Comment) } + it { should_not be_able_to(:vote, Comment) } +end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb deleted file mode 100644 index badb9fda6..000000000 --- a/spec/models/ability_spec.rb +++ /dev/null @@ -1,268 +0,0 @@ -require 'rails_helper' -require 'cancan/matchers' - -describe Ability do - subject(:ability) { Ability.new(user) } - let(:debate) { create(:debate) } - let(:comment) { create(:comment) } - let(:proposal) { create(:proposal) } - - let(:own_debate) { create(:debate, author: user) } - let(:own_comment) { create(:comment, author: user) } - let(:own_proposal) { create(:proposal, author: user) } - - let(:hidden_debate) { create(:debate, :hidden) } - let(:hidden_comment) { create(:comment, :hidden) } - let(:hidden_proposal) { create(:proposal, :hidden) } - - describe "Non-logged in user" do - let(:user) { nil } - - it { should be_able_to(:index, Debate) } - it { should be_able_to(:show, debate) } - it { should_not be_able_to(:edit, Debate) } - it { should_not be_able_to(:vote, Debate) } - it { should_not be_able_to(:flag, Debate) } - it { should_not be_able_to(:unflag, Debate) } - - it { should be_able_to(:index, Proposal) } - it { should be_able_to(:show, proposal) } - it { should_not be_able_to(:edit, Proposal) } - it { should_not be_able_to(:vote, Proposal) } - it { should_not be_able_to(:flag, Proposal) } - it { should_not be_able_to(:unflag, Proposal) } - end - - describe "Citizen" do - let(:user) { create(:user) } - - it { should be_able_to(:index, Debate) } - it { should be_able_to(:show, debate) } - it { should be_able_to(:vote, debate) } - - it { should be_able_to(:show, user) } - it { should be_able_to(:edit, user) } - - it { should be_able_to(:create, Comment) } - it { should be_able_to(:vote, Comment) } - - it { should be_able_to(:index, Proposal) } - it { should be_able_to(:show, proposal) } - it { should_not be_able_to(:vote, Proposal) } - - it { should_not be_able_to(:comment_as_administrator, debate) } - it { should_not be_able_to(:comment_as_moderator, debate) } - it { should_not be_able_to(:comment_as_administrator, proposal) } - it { should_not be_able_to(:comment_as_moderator, proposal) } - - describe 'flagging content' do - it { should be_able_to(:flag, debate) } - it { should be_able_to(:unflag, debate) } - - it { should be_able_to(:flag, comment) } - it { should be_able_to(:unflag, comment) } - - it { should be_able_to(:flag, proposal) } - it { should be_able_to(:unflag, proposal) } - - describe "own content" do - it { should_not be_able_to(:flag, own_comment) } - it { should_not be_able_to(:unflag, own_comment) } - - it { should_not be_able_to(:flag, own_debate) } - it { should_not be_able_to(:unflag, own_debate) } - - it { should_not be_able_to(:flag, own_proposal) } - it { should_not be_able_to(:unflag, own_proposal) } - end - end - - describe "other users" do - let(:other_user) { create(:user) } - it { should_not be_able_to(:show, other_user) } - it { should_not be_able_to(:edit, other_user) } - end - - describe "editing debates" do - let(:own_debate_non_editable) { create(:debate, author: user) } - before { allow(own_debate_non_editable).to receive(:editable?).and_return(false) } - - it { should be_able_to(:edit, own_debate) } - it { should_not be_able_to(:edit, debate) } # Not his - it { should_not be_able_to(:edit, own_debate_non_editable) } - end - - describe "editing proposals" do - let(:own_proposal_non_editable) { create(:proposal, author: user) } - before { allow(own_proposal_non_editable).to receive(:editable?).and_return(false) } - - it { should be_able_to(:edit, own_proposal) } - it { should_not be_able_to(:edit, proposal) } # Not his - it { should_not be_able_to(:edit, own_proposal_non_editable) } - end - - describe "when level 2 verified" do - before{ user.update(residence_verified_at: Time.now, confirmed_phone: "1") } - - it { should be_able_to(:vote, Proposal) } - end - - describe "when level 3 verified" do - before{ user.update(verified_at: Time.now) } - - it { should be_able_to(:vote, Proposal) } - end - end - - describe "Organization" do - let(:user) { create(:user) } - before(:each) { create(:organization, user: user) } - - it { should be_able_to(:show, user) } - it { should be_able_to(:edit, user) } - - it { should be_able_to(:index, Debate) } - it { should be_able_to(:show, debate) } - it { should_not be_able_to(:vote, debate) } - - it { should be_able_to(:index, Proposal) } - it { should be_able_to(:show, proposal) } - it { should_not be_able_to(:vote, Proposal) } - - it { should be_able_to(:create, Comment) } - it { should_not be_able_to(:vote, Comment) } - end - - describe "Moderator" do - let(:user) { create(:user) } - before { create(:moderator, user: user) } - let(:other_user) { create(:user) } - - - it { should be_able_to(:index, Debate) } - it { should be_able_to(:show, debate) } - it { should be_able_to(:vote, debate) } - - it { should be_able_to(:index, Proposal) } - it { should be_able_to(:show, proposal) } - - it { should be_able_to(:read, Organization) } - - describe "organizations" do - let(:pending_organization) { create(:organization) } - let(:rejected_organization) { create(:organization, :rejected) } - let(:verified_organization) { create(:organization, :verified) } - - it { should be_able_to( :verify, pending_organization) } - it { should be_able_to( :reject, pending_organization) } - - it { should_not be_able_to(:verify, verified_organization) } - it { should be_able_to( :reject, verified_organization) } - - it { should_not be_able_to(:reject, rejected_organization) } - it { should be_able_to( :verify, rejected_organization) } - end - - describe "hiding, reviewing and restoring" do - let(:ignored_comment) { create(:comment, :with_ignored_flag) } - let(:ignored_debate) { create(:debate, :with_ignored_flag) } - let(:ignored_proposal) { create(:proposal,:with_ignored_flag) } - - it { should be_able_to(:hide, comment) } - it { should be_able_to(:hide_in_moderation_screen, comment) } - it { should_not be_able_to(:hide, hidden_comment) } - it { should_not be_able_to(:hide, own_comment) } - - it { should be_able_to(:moderate, comment) } - it { should_not be_able_to(:moderate, own_comment) } - - it { should be_able_to(:hide, debate) } - it { should be_able_to(:hide_in_moderation_screen, debate) } - it { should_not be_able_to(:hide, hidden_debate) } - it { should_not be_able_to(:hide, own_debate) } - - it { should be_able_to(:hide, proposal) } - it { should be_able_to(:hide_in_moderation_screen, proposal) } - it { should_not be_able_to(:hide, hidden_proposal) } - it { should_not be_able_to(:hide, own_proposal) } - - it { should be_able_to(:ignore_flag, comment) } - it { should_not be_able_to(:ignore_flag, hidden_comment) } - it { should_not be_able_to(:ignore_flag, ignored_comment) } - it { should_not be_able_to(:ignore_flag, own_comment) } - - it { should be_able_to(:ignore_flag, debate) } - it { should_not be_able_to(:ignore_flag, hidden_debate) } - it { should_not be_able_to(:ignore_flag, ignored_debate) } - it { should_not be_able_to(:ignore_flag, own_debate) } - - it { should be_able_to(:moderate, debate) } - it { should_not be_able_to(:moderate, own_debate) } - - it { should be_able_to(:ignore_flag, proposal) } - it { should_not be_able_to(:ignore_flag, hidden_proposal) } - it { should_not be_able_to(:ignore_flag, ignored_proposal) } - it { should_not be_able_to(:ignore_flag, own_proposal) } - - it { should be_able_to(:moderate, proposal) } - it { should_not be_able_to(:moderate, own_proposal) } - - it { should_not be_able_to(:hide, user) } - it { should be_able_to(:hide, other_user) } - - it { should_not be_able_to(:block, user) } - it { should be_able_to(:block, other_user) } - - it { should_not be_able_to(:restore, comment) } - it { should_not be_able_to(:restore, debate) } - it { should_not be_able_to(:restore, proposal) } - it { should_not be_able_to(:restore, other_user) } - - it { should be_able_to(:comment_as_moderator, debate) } - it { should be_able_to(:comment_as_moderator, proposal) } - it { should_not be_able_to(:comment_as_administrator, debate) } - it { should_not be_able_to(:comment_as_administrator, proposal) } - end - end - - describe "Administrator" do - let(:user) { create(:user) } - before { create(:administrator, user: user) } - - let(:other_user) { create(:user) } - let(:hidden_user) { create(:user, :hidden) } - - it { should be_able_to(:index, Debate) } - it { should be_able_to(:show, debate) } - it { should be_able_to(:vote, debate) } - - it { should be_able_to(:index, Proposal) } - it { should be_able_to(:show, proposal) } - - it { should_not be_able_to(:restore, comment) } - it { should_not be_able_to(:restore, debate) } - it { should_not be_able_to(:restore, proposal) } - it { should_not be_able_to(:restore, other_user) } - - it { should be_able_to(:restore, hidden_comment) } - it { should be_able_to(:restore, hidden_debate) } - it { should be_able_to(:restore, hidden_proposal) } - it { should be_able_to(:restore, hidden_user) } - - it { should_not be_able_to(:confirm_hide, comment) } - it { should_not be_able_to(:confirm_hide, debate) } - it { should_not be_able_to(:confirm_hide, proposal) } - it { should_not be_able_to(:confirm_hide, other_user) } - - it { should be_able_to(:confirm_hide, hidden_comment) } - it { should be_able_to(:confirm_hide, hidden_debate) } - it { should be_able_to(:confirm_hide, hidden_proposal) } - it { should be_able_to(:confirm_hide, hidden_user) } - - it { should be_able_to(:comment_as_administrator, debate) } - it { should_not be_able_to(:comment_as_moderator, debate) } - - it { should be_able_to(:comment_as_administrator, proposal) } - it { should_not be_able_to(:comment_as_moderator, proposal) } - end -end