Merge pull request #116 from AyuntamientoMadrid/admin-77
Administrator and Moderator basic interface
This commit is contained in:
@@ -31,4 +31,12 @@ FactoryGirl.define do
|
||||
debate
|
||||
end
|
||||
|
||||
end
|
||||
factory :administrator do
|
||||
user
|
||||
end
|
||||
|
||||
factory :moderator do
|
||||
user
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -10,6 +10,7 @@ feature 'Account' do
|
||||
login_as(@user)
|
||||
visit root_path
|
||||
click_link "My account"
|
||||
expect(current_path).to eq(account_path)
|
||||
|
||||
expect(page).to have_selector("input[value='Manuela']")
|
||||
expect(page).to have_selector("input[value='Colau']")
|
||||
@@ -48,4 +49,4 @@ feature 'Account' do
|
||||
expect(page).to have_selector("input[id='account_email_on_debate_comment'][value='1']")
|
||||
expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
34
spec/features/admin_spec.rb
Normal file
34
spec/features/admin_spec.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'Admin' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
scenario 'Access as regular user is not authorized' do
|
||||
login_as(user)
|
||||
visit admin_root_path
|
||||
|
||||
expect(current_path).to eq(root_path)
|
||||
expect(page).to have_content "not authorized"
|
||||
end
|
||||
|
||||
scenario 'Access as a moderator is not authorized' do
|
||||
create(:moderator, user: user)
|
||||
|
||||
login_as(user)
|
||||
visit admin_root_path
|
||||
|
||||
expect(current_path).to eq(root_path)
|
||||
expect(page).to have_content "not authorized"
|
||||
end
|
||||
|
||||
scenario 'Access as an administrator is authorized' do
|
||||
create(:administrator, user: user)
|
||||
|
||||
login_as(user)
|
||||
visit admin_root_path
|
||||
|
||||
expect(current_path).to eq(admin_root_path)
|
||||
expect(page).to_not have_content "not authorized"
|
||||
end
|
||||
|
||||
end
|
||||
@@ -98,9 +98,9 @@ feature 'Debates' do
|
||||
expect(debate).to be_editable
|
||||
login_as(create(:user))
|
||||
|
||||
expect {
|
||||
visit edit_debate_path(debate)
|
||||
}.to raise_error ActiveRecord::RecordNotFound
|
||||
visit edit_debate_path(debate)
|
||||
expect(current_path).to eq(root_path)
|
||||
expect(page).to have_content 'not authorized'
|
||||
end
|
||||
|
||||
scenario 'Update should not be posible if debate is not editable' do
|
||||
@@ -109,17 +109,19 @@ feature 'Debates' do
|
||||
expect(debate).to_not be_editable
|
||||
login_as(debate.author)
|
||||
|
||||
expect {
|
||||
visit edit_debate_path(debate)
|
||||
}.to raise_error ActiveRecord::RecordNotFound
|
||||
visit edit_debate_path(debate)
|
||||
edit_debate_path(debate)
|
||||
expect(current_path).to eq(root_path)
|
||||
expect(page).to have_content 'not authorized'
|
||||
end
|
||||
|
||||
scenario 'Update should be posible for the author of an editable debate' do
|
||||
debate = create(:debate)
|
||||
login_as(debate.author)
|
||||
|
||||
visit debate_path(debate)
|
||||
click_link 'Edit'
|
||||
visit edit_debate_path(debate)
|
||||
expect(current_path).to eq(edit_debate_path(debate))
|
||||
|
||||
fill_in 'debate_title', with: "End child poverty"
|
||||
fill_in 'debate_description', with: "Let's..."
|
||||
|
||||
|
||||
34
spec/features/moderation_spec.rb
Normal file
34
spec/features/moderation_spec.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
require 'rails_helper'
|
||||
|
||||
feature 'Admin' do
|
||||
let(:user) { create(:user) }
|
||||
|
||||
scenario 'Access as regular user is not authorized' do
|
||||
login_as(user)
|
||||
visit moderation_root_path
|
||||
|
||||
expect(current_path).to eq(root_path)
|
||||
expect(page).to have_content "not authorized"
|
||||
end
|
||||
|
||||
scenario 'Access as a moderator is authorized' do
|
||||
create(:moderator, user: user)
|
||||
|
||||
login_as(user)
|
||||
visit moderation_root_path
|
||||
|
||||
expect(current_path).to eq(moderation_root_path)
|
||||
expect(page).to_not have_content "not authorized"
|
||||
end
|
||||
|
||||
scenario 'Access as an administrator is authorized' do
|
||||
create(:administrator, user: user)
|
||||
|
||||
login_as(user)
|
||||
visit moderation_root_path
|
||||
|
||||
expect(current_path).to eq(moderation_root_path)
|
||||
expect(page).to_not have_content "not authorized"
|
||||
end
|
||||
|
||||
end
|
||||
67
spec/models/ability_spec.rb
Normal file
67
spec/models/ability_spec.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
require 'rails_helper'
|
||||
require 'cancan/matchers'
|
||||
|
||||
describe Ability do
|
||||
subject(:ability) { Ability.new(user) }
|
||||
let(:debate) { Debate.new }
|
||||
|
||||
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) }
|
||||
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) }
|
||||
|
||||
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) { 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 "Moderator" 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 "Administrator" 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
|
||||
@@ -7,13 +7,13 @@ describe User do
|
||||
@user = create(:user)
|
||||
end
|
||||
|
||||
it "should return {} if no debate" do
|
||||
it "returns {} if no debate" do
|
||||
expect(@user.votes_on_debates()).to eq({})
|
||||
expect(@user.votes_on_debates([])).to eq({})
|
||||
expect(@user.votes_on_debates([nil, nil])).to eq({})
|
||||
end
|
||||
|
||||
it "should return a hash of debates ids and votes" do
|
||||
it "returns a hash of debates ids and votes" do
|
||||
debate1 = create(:debate)
|
||||
debate2 = create(:debate)
|
||||
debate3 = create(:debate)
|
||||
@@ -87,4 +87,28 @@ describe User do
|
||||
end
|
||||
end
|
||||
|
||||
describe "administrator?" do
|
||||
it "is false when the user is not an admin" do
|
||||
expect(subject.administrator?).to be false
|
||||
end
|
||||
|
||||
it "is true when the user is an admin" do
|
||||
subject.save
|
||||
create(:administrator, user: subject)
|
||||
expect(subject.administrator?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
describe "moderator?" do
|
||||
it "is false when the user is not a moderator" do
|
||||
expect(subject.moderator?).to be false
|
||||
end
|
||||
|
||||
it "is true when the user is a moderator" do
|
||||
subject.save
|
||||
create(:moderator, user: subject)
|
||||
expect(subject.moderator?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user