Skip to content

Instantly share code, notes, and snippets.

@pavel-sazonov
Created August 5, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavel-sazonov/15df040857ff4b6315e8573000397573 to your computer and use it in GitHub Desktop.
Save pavel-sazonov/15df040857ff4b6315e8573000397573 to your computer and use it in GitHub Desktop.
require 'rails_helper'
RSpec.describe Ability do
subject(:ability) { Ability.new(user) }
describe "for guest" do
let(:user) { nil }
it { should be_able_to :read, :all }
it { should_not be_able_to :manage, :all }
end
describe "for user" do
let(:user) { create :user }
let(:other) { create :user }
it { should be_able_to :read, :all }
it { should_not be_able_to :manage, :all }
context "user" do
let(:question) { create :question, user: other }
let(:answer) { create :answer, question: question, user: other }
let(:comment) { create :comment, user: other, commentable: question }
let(:attachment) { create :attachment, attachable: question }
it { should be_able_to :create, Question }
it { should be_able_to :create, Answer }
it { should be_able_to :create, Attachment }
it { should be_able_to :create, Comment }
it { should_not be_able_to %i[update destroy], question, user_id: user.id }
it { should_not be_able_to %i[update destroy], answer, user_id: user.id }
it { should_not be_able_to :destroy, comment, user_id: user.id }
it { should_not be_able_to :set_best, answer, user_id: user.id }
it { should_not be_able_to :destroy, attachment, user_id: user.id }
end
context "author" do
let(:question) { create :question, user: user }
let(:answer) { create :answer, question: question, user: user }
let(:other_answer) { create :answer, question: question, user: other }
let(:comment) { create :comment, user: user, commentable: question }
let(:attachment) { create :attachment, attachable: question }
it { should be_able_to %i[update destroy], question, user_id: user.id }
it { should be_able_to %i[update destroy], answer, user_id: user.id }
it { should be_able_to :destroy, comment, user_id: user.id }
it { should be_able_to :set_best, other_answer, user_id: user.id }
it { should be_able_to :destroy, attachment, user_id: user.id }
end
context "vote" do
let(:question) { create :question, user: user }
let(:other_question) { create :question, user: other }
let(:vote) { create :vote, user: user, votable: question }
let(:other_vote) { create :vote, user: other, votable: other_question }
it {
should be_able_to(
%i[vote_up vote_down],
# можно ли здесь использовать new, а не create, чтобы не все атрибуты вводить?
other_question.votes.new(votable: other_question)
)
}
it {
should_not be_able_to(
%i[vote_up vote_down],
question.votes.new(votable: question)
)
}
it { should be_able_to :cancel_vote, vote, user_id: user.id }
it { should_not be_able_to :cancel_vote, other_vote, user_id: user.id }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment