Merge pull request #963 from sonalkr132/user-tests

Remove unused methods of user model and tests
This commit is contained in:
Henne Vogelsang 2016-04-20 15:10:21 +02:00
commit cd1dc09573
5 changed files with 322 additions and 89 deletions

View file

@ -274,7 +274,7 @@ module ApplicationHelper
# Outputs the roles of a user, including the conferences for which the user has the roles # Outputs the roles of a user, including the conferences for which the user has the roles
# Eg. organizer(oSC13, oSC14), cfp(oSC12, oSC13) # Eg. organizer(oSC13, oSC14), cfp(oSC12, oSC13)
def show_roles(roles) def show_roles(roles)
roles.map { |x| x[0].titleize + ' ' + x[1] }.join ', ' roles.map{ |x| x[0].titleize + ' (' + x[1].join(', ') + ')' }.join ', '
end end
def can_manage_volunteers(conference) def can_manage_volunteers(conference)

View file

@ -61,13 +61,6 @@ class User < ActiveRecord::Base
self.subscriptions.find_by(conference_id: conference.id).present? self.subscriptions.find_by(conference_id: conference.id).present?
end end
# Returns the purchased ticket
# ====Returns
# * +TicketUser::ActiveRecord_Relation+ -> user
def ticket(id)
ticket_purchases.where(ticket_id: id).first
end
def supports? conference def supports? conference
ticket_purchases.find_by(conference_id: conference.id).present? ticket_purchases.find_by(conference_id: conference.id).present?
end end
@ -122,38 +115,22 @@ class User < ActiveRecord::Base
user user
end end
def setup_role
if User.count == 1 && User.first.email == 'deleted@localhost.osem'
self.is_admin = true
end
end
# Gets the roles of the user, groups them by role.name and returns the resource(s) of each role # Gets the roles of the user, groups them by role.name and returns the resource(s) of each role
# ====Returns # ====Returns
# * +Hash+ * -> e.g. 'organizer' => "(conf1, conf2)" # * +Hash+ * -> e.g. 'organizer' => [conf1, conf2]
def get_roles def get_roles
result = {} result = {}
Role.all.find_each do |role| roles.each do |role|
resources = self.roles.map{ |myrole| Conference.find(myrole.resource_id).short_title }.join ', ' resource = Conference.find(role.resource_id).short_title
result[role.name] = "(#{ resources })" unless resources.blank? if result[role.name].nil?
result[role.name] = [resource]
else
result[role.name] << resource
end
end end
result result
end end
def self.prepare(params)
email = params['email']
user = User.where(email: email).first_or_initialize
# If there is a new user, add the necessary attributes
if user.new_record?
user.password = Devise.friendly_token[0, 20]
user.skip_confirmation!
user.attributes = params
end
user
end
def registered def registered
registrations = self.registrations registrations = self.registrations
if registrations.count == 0 if registrations.count == 0
@ -176,11 +153,6 @@ class User < ActiveRecord::Base
!confirmed_at.nil? !confirmed_at.nil?
end end
def attending_conference?(conference)
Registration.where(conference_id: conference.id,
user_id: id).count
end
def proposals(conference) def proposals(conference)
events.where('program_id = ? AND event_users.event_role=?', conference.program.id, 'submitter') events.where('program_id = ? AND event_users.event_role=?', conference.program.id, 'submitter')
end end
@ -189,18 +161,11 @@ class User < ActiveRecord::Base
proposals(conference).count proposals(conference).count
end end
def biography_word_count
if biography.nil?
0
else
biography.split.size
end
end
private private
def biography_limit def setup_role
errors.add(:abstract, 'cannot have more than 150 words') if biography && if User.count == 1 && User.first.email == 'deleted@localhost.osem'
biography.split.size > 150 self.is_admin = true
end
end end
end end

View file

@ -1 +0,0 @@
Lorem ipsum dolorem...

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe ApplicationHelper, type: :helper do
describe 'show_roles' do
it 'formats the hash passed' do
roles = { 'organizer' => ['oSC16', 'oSC15'], 'cfp' => ['oSC16'] }
expect(show_roles(roles)).to eq 'Organizer (oSC16, oSC15), Cfp (oSC16)'
end
end
end

View file

@ -2,21 +2,286 @@ require 'spec_helper'
describe User do describe User do
# It is necessary to use bang version of let to build roles before user let(:user_admin) { create(:admin) }
let!(:user_admin) { create(:admin) } let(:conference) { create(:conference, short_title: 'oSC16', title: 'openSUSE Conference 2016') }
let!(:conference) { create(:conference) } let(:conference2) { create(:conference, short_title: 'oSC15', title: 'openSUSE Conference 2015') }
let!(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) } let(:organizer_role) { Role.find_by(name: 'organizer', resource: conference) }
let!(:cfp_role) { Role.find_by(name: 'cfp', resource: conference) } let(:cfp_role) { Role.find_by(name: 'cfp', resource: conference) }
let!(:volunteers_coordinator_role) { Role.find_by(name: 'volunteers_coordinator', resource: conference) } let(:volunteers_coordinator_role) { Role.find_by(name: 'volunteers_coordinator', resource: conference) }
let!(:organizer) { create(:user, role_ids: [organizer_role.id]) } let(:organizer) { create(:user, role_ids: [organizer_role.id]) }
let!(:user) { create(:user) } let(:user) { create(:user) }
it 'User.for_ichain_username raises exception if user is disabled' do describe 'validation' do
user.is_disabled = true it 'has a valid factory' do
user.save expect(build(:user)).to be_valid
expect{User.for_ichain_username(user.username, email: user.email)}.to raise_error(UserDisabled)
end end
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:username) }
it { is_expected.to validate_uniqueness_of(:username) }
end
describe 'association' do
it { is_expected.to have_many(:openids) }
it { is_expected.to have_many(:event_users).dependent(:destroy) }
it { is_expected.to have_many(:events).through(:event_users) }
it { is_expected.to have_many(:registrations).dependent(:destroy) }
it { is_expected.to have_many(:ticket_purchases).dependent(:destroy) }
it { is_expected.to have_many(:tickets).through(:ticket_purchases) }
it { is_expected.to have_many(:votes).dependent(:destroy) }
it { is_expected.to have_many(:subscriptions).dependent(:destroy) }
end
describe 'scope and nested attribute' do
it { should accept_nested_attributes_for :roles }
describe '.admin' do
it 'includes users with admin flag' do
expect(User.admin).to include(user_admin)
end
it 'excludes users without admin flag' do
expect(User.admin).not_to include(user)
end
end
describe '.comment_notifiable' do
let(:cfp_user) { create(:user, role_ids: [cfp_role.id]) }
it 'includes organizer and cfp user' do
expect(User.comment_notifiable(conference)).to include(organizer, cfp_user)
end
it 'excludes ordinary user' do
expect(User.comment_notifiable(conference)).not_to include(user)
end
end
end
describe 'methods' do
describe '#name' do
it 'returns the username as name if there is not name' do
user = create(:user, name: nil)
expect(user.name).to eq(user.username)
end
end
describe '#subscribed?' do
context 'user has subscribed to conference' do
before { create(:subscription, user: user, conference: conference) }
it 'returns true' do
expect(user.subscribed?(conference)).to be true
end
end
context 'user has not subscribed to conference' do
it 'return false' do
expect(user.subscribed?(conference)).to be false
end
end
end
describe '.supports?' do
context 'user has bought tickets' do
before { create(:ticket_purchase, user: user, conference: conference) }
it 'returns true' do
expect(user.supports?(conference)).to be true
end
end
context 'user has not bought any ticket' do
it 'return false' do
expect(user.supports?(conference)).to be false
end
end
end
describe '.for_ichain_username' do
before { user.update_attributes(current_sign_in_at: Date.new(2014, 12, 12)) }
context 'user exists' do
it 'updates last_sign_in_at of user' do
expect do
User.for_ichain_username(user.username, email: user.email)
user.reload
end.to change { user.last_sign_in_at }
end
it 'updates current_sign_in_at of user' do
expect do
User.for_ichain_username(user.username, email: user.email)
user.reload
end.to change { user.current_sign_in_at }
end
end
context 'user is disabled' do
before { user.update_attributes(is_disabled: true) }
it 'User.for_ichain_username raises exception if user is disabled' do
expect{ User.for_ichain_username(user.username, email: user.email) }
.to raise_error(UserDisabled)
end
end
end
describe '.find_for_database_authentication' do
context 'login with username' do
it 'can find user by jumbled username' do
scrambled_username = user.username.chars.map{|c| rand > 0.5 ? c.capitalize : c}.join
expect(User.find_for_database_authentication(login: scrambled_username)).to eq(user)
end
end
context 'login with email' do
it 'can find user by jumbled email' do
scrambled_email = user.email.chars.map{|c| rand > 0.5 ? c.capitalize : c}.join
expect(User.find_for_database_authentication(login: scrambled_email)).to eq(user)
end
end
end
describe '.find_for_auth' do
let(:auth) do
OmniAuth::AuthHash.new(provider: 'google',
uid: 'google-test-uid-1',
info: {
name: 'new user name',
email: 'test-1@gmail.com',
username: 'newuser'
},
credentials: {
token: 'mock_token',
secret: 'mock_secret'
}
)
end
context 'user is not signed in' do
context 'first visit to website' do
before { @auth_user = User.find_for_auth(auth, nil) }
it 'initializes new user' do
expect(@auth_user.new_record?).to be true
end
it 'sets name, email, username and password' do
regex_base64 = %r{^(?:[A-Za-z_\-0-9+\/]{4}\n?)*(?:[A-Za-z_\-0-9+\/]{2}|[A-Za-z_\-0-9+\/]{3}=)?$}
expect(@auth_user.name).to eq 'new user name'
expect(@auth_user.email).to eq 'test-1@gmail.com'
expect(@auth_user.username).to eq 'newuser'
expect(@auth_user.password).to match regex_base64
end
end
context 'user returns to website' do
let!(:auth_user) { create(:user, email: 'test-1@gmail.com') }
it 'finds corresponding user' do
expect(User.find_for_auth(auth, nil)).to eq auth_user
end
end
end
end
describe '#get_roles' do
let(:conf2_organizer_role) { Role.find_by(name: 'organizer', resource: conference2) }
before do
user.update_attributes(role_ids: [organizer_role.id, cfp_role.id, conf2_organizer_role.id])
end
it 'returns hash of role and conference' do
expected_hash = {
'organizer' => ['oSC16', 'oSC15'],
'cfp' => ['oSC16']
}
expect(user.get_roles).to eq expected_hash
end
end
describe '#registered' do
context 'user has not registered to any conference' do
it 'returns None' do
expect(user.registered).to eq 'None'
end
end
context 'user has registered to conferences' do
before do
create(:registration, user: user, conference: conference)
create(:registration, user: user, conference: conference2)
end
it 'returns registered conferences title' do
expect(user.registered).to eq('openSUSE Conference 2016, openSUSE Conference 2015')
end
end
end
describe '#attended' do
context 'user has not attended any conference' do
it 'returns None' do
expect(user.attended).to eq 'None'
end
end
context 'user has attended conferences' do
before do
create(:registration, user: user, conference: conference, attended: true)
create(:registration, user: user, conference: conference2, attended: true)
end
it 'returns attended conferences title' do
expect(user.attended).to eq('openSUSE Conference 2016, openSUSE Conference 2015')
end
end
end
describe '#confirmed?' do
context 'confirmed user' do
it 'returns true' do
expect(user.confirmed?).to eq true
end
end
context 'unconfirmed user' do
before { user.update_attributes(confirmed_at: nil) }
it 'returns false' do
expect(user.confirmed?).to eq false
end
end
end
describe 'proposals methods' do
let(:submitter) { create(:submitter, user: user) }
let(:event1) { create(:event, program: conference.program) }
let(:event2) { create(:event, program: conference.program) }
before do
event1.event_users << create(:event_user, user: user, event_role: 'submitter')
event2.event_users << create(:event_user, user: user, event_role: 'submitter')
end
describe '#proposals' do
it 'returns events submitted by user' do
expect(user.proposals(conference)).to match [event1, event2]
end
end
describe '#proposal_count' do
it 'returns number of events submitted by user' do
expect(user.proposal_count(conference)).to eq 2
end
end
end
end
describe 'rolify' do
it 'returns the correct role' do it 'returns the correct role' do
expect(user_admin.is_admin).to eq(true) expect(user_admin.is_admin).to eq(true)
expect(organizer.roles.first).to eq(organizer_role) expect(organizer.roles.first).to eq(organizer_role)
@ -33,13 +298,6 @@ describe User do
expect(another_user.roles[1]).to eq(cfp_role) expect(another_user.roles[1]).to eq(cfp_role)
end end
describe '#name' do
it 'returns the username as name if there is not name' do
user = create(:user, name: nil)
expect(user.name).to eq(user.username)
end
end
describe '#has_role?' do describe '#has_role?' do
describe 'when user has a role' do describe 'when user has a role' do
it 'returns true when the user has the role' do it 'returns true when the user has the role' do
@ -58,6 +316,7 @@ describe User do
expect(user.has_role?('organizer', conference)).to be false expect(user.has_role?('organizer', conference)).to be false
end end
end end
end
describe 'assigns admin attribute' do describe 'assigns admin attribute' do
it 'to second user when first user is deleted_user' do it 'to second user when first user is deleted_user' do