Adds Abilities for Debates

This commit is contained in:
kikito
2015-08-10 12:38:22 +02:00
parent f8becd1b23
commit 8b53ae6f08
2 changed files with 77 additions and 0 deletions

22
app/models/ability.rb Normal file
View File

@@ -0,0 +1,22 @@
class Ability
include CanCan::Ability
def initialize(user)
# Not logged in users
can :read, Debate
if user # logged-in users
can [:read, :create, :vote], Debate
can :edit, Debate do |debate|
debate.editable_by?(user)
end
if user.moderator? or user.administrator?
elsif user.administrator?
end
end
end
end

View File

@@ -0,0 +1,55 @@
require 'rails_helper'
require 'cancan/matchers'
describe Ability do
subject(:ability) { Ability.new(user) }
let(:debate) { Debate.new }
describe "Non-logged in users" 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) }
end
describe "Citizens" 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) }
describe "editing debates" do
let(:own_debate) { create(:debate, author: user) }
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
end
describe "Moderators" do
let(:user) { create(:user) }
before { create(:moderator, user: user) }
it { should be_able_to(:index, Debate) }
it { should be_able_to(:show, debate) }
it { should be_able_to(:vote, debate) }
end
describe "Administrators" do
let(:user) { create(:user) }
before { create(:administrator, user: user) }
it { should be_able_to(:index, Debate) }
it { should be_able_to(:show, debate) }
it { should be_able_to(:vote, debate) }
end
end