osem-fcy/spec/models/user_spec.rb

58 lines
2 KiB
Ruby
Raw Normal View History

2014-05-10 15:57:38 +02:00
require 'spec_helper'
describe User do
2014-05-12 20:02:38 +02:00
# It is necessary to use bang version of let to build roles before user
2014-08-12 11:51:59 +03:00
let!(:admin) { create(:admin) }
let!(:participant) { create(:participant) }
let!(:organizer_conference_1) { create(:organizer_conference_1 ) }
let!(:organizer_conference_1_role) { Role.where(resource_type: 'Conference', resource_id: 1).first }
2014-05-10 15:57:38 +02:00
it 'returns the correct role' do
2014-08-12 11:51:59 +03:00
expect(organizer_conference_1.roles.first).to eq(organizer_conference_1_role)
2014-05-10 15:57:38 +02:00
end
it 'returns the correct roles' do
2014-08-12 11:51:59 +03:00
participant_role = create(:participant_role)
roles = [participant_role.id, organizer_conference_1_role.id]
2014-05-10 15:57:38 +02:00
user_with_all_roles = create(:user, email: 'participant@example.de')
user_with_all_roles.role_ids = roles
user_with_all_roles.save
2014-08-12 11:51:59 +03:00
expect(user_with_all_roles.roles.length).to eq(2)
2014-05-10 15:57:38 +02:00
expect(user_with_all_roles.roles[0]).to eq(participant_role)
2014-08-12 11:51:59 +03:00
expect(user_with_all_roles.roles[1]).to eq(organizer_conference_1_role )
2014-05-10 15:57:38 +02:00
end
2014-08-12 11:51:59 +03:00
describe '#has_role?' do
2014-05-10 15:57:38 +02:00
shared_examples '#role?' do |user, role, expected|
it "returns #{expected} for #{role}" do
user_obj = create(user, email: 'e@example.com')
2014-08-12 11:51:59 +03:00
expect(user_obj.has_role?(role.downcase, :any)).to be expected
2014-05-10 15:57:38 +02:00
end
end
2014-08-12 11:51:59 +03:00
context 'organizer' do
it_behaves_like '#role?', :organizer_conference_1, 'organizer', true
it_behaves_like '#role?', :organizer_conference_1, 'participant', false
end
2014-05-10 15:57:38 +02:00
2014-08-12 11:51:59 +03:00
context 'admin' do
2014-05-10 15:57:38 +02:00
it 'assigns first user admin role' do
2014-08-12 11:51:59 +03:00
expect(User.first.is_admin).to be true
expect(admin.is_admin).to eq(true)
2014-05-10 15:57:38 +02:00
end
end
context 'participant' do
it_behaves_like '#role?', :participant, 'adMin', false
2014-08-12 11:51:59 +03:00
it_behaves_like '#role?', :participant, 'participant', true
2014-05-10 15:57:38 +02:00
2014-08-12 11:51:59 +03:00
# it 'assigns second user participant role' do
# participant = create(:user, email: 'participant@example.de')
# expect(participant.role_ids).to match_array([participant_role.id])
# end
2014-05-10 15:57:38 +02:00
end
end
end