fat_free_crm 0.14.2 → 0.15.0.beta

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fat_free_crm might be problematic. Click here for more details.

Files changed (95) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +15 -5
  3. data/{CHANGELOG → CHANGELOG.md} +9 -8
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +78 -70
  6. data/README.md +8 -10
  7. data/app/controllers/admin/users_controller.rb +6 -1
  8. data/app/controllers/application_controller.rb +2 -0
  9. data/app/controllers/users_controller.rb +1 -0
  10. data/app/helpers/tags_helper.rb +1 -1
  11. data/app/models/users/user.rb +10 -9
  12. data/config/initializers/paper_trail.rb +1 -0
  13. data/fat_free_crm.gemspec +4 -4
  14. data/lib/development_tasks/rspec.rake +2 -6
  15. data/lib/fat_free_crm/engine.rb +5 -0
  16. data/lib/fat_free_crm/version.rb +3 -3
  17. data/spec/factories/field_factories.rb +1 -1
  18. data/spec/factories/opportunity_factories.rb +1 -1
  19. data/spec/mailers/subscription_mailer_spec.rb +5 -3
  20. data/spec/mailers/user_mailer_spec.rb +6 -6
  21. data/spec/models/users/abilities/user_ability_spec.rb +3 -3
  22. data/spec/models/users/user_spec.rb +73 -62
  23. data/spec/support/auth_macros.rb +8 -1
  24. data/spec/views/accounts/_edit.haml_spec.rb +1 -1
  25. data/spec/views/accounts/create.js.haml_spec.rb +1 -1
  26. data/spec/views/accounts/destroy.js.haml_spec.rb +1 -1
  27. data/spec/views/accounts/edit.js.haml_spec.rb +2 -2
  28. data/spec/views/accounts/index.haml_spec.rb +2 -2
  29. data/spec/views/accounts/index.js.haml_spec.rb +1 -1
  30. data/spec/views/accounts/update.js.haml_spec.rb +1 -1
  31. data/spec/views/admin/field_groups/create.js.haml_spec.rb +1 -1
  32. data/spec/views/admin/field_groups/edit.js.haml_spec.rb +1 -1
  33. data/spec/views/admin/field_groups/new.js.haml_spec.rb +1 -1
  34. data/spec/views/admin/field_groups/update.js.haml_spec.rb +1 -1
  35. data/spec/views/admin/users/create.js.haml_spec.rb +1 -1
  36. data/spec/views/admin/users/destroy.js.haml_spec.rb +1 -1
  37. data/spec/views/admin/users/edit.js.haml_spec.rb +2 -2
  38. data/spec/views/admin/users/index.haml_spec.rb +1 -1
  39. data/spec/views/admin/users/index.js.haml_spec.rb +2 -2
  40. data/spec/views/admin/users/reactivate.js.haml_spec.rb +1 -1
  41. data/spec/views/admin/users/suspend.js.haml_spec.rb +1 -1
  42. data/spec/views/admin/users/update.js.haml_spec.rb +1 -1
  43. data/spec/views/application/auto_complete.haml_spec.rb +3 -3
  44. data/spec/views/campaigns/_edit.haml_spec.rb +1 -1
  45. data/spec/views/campaigns/create.js.haml_spec.rb +2 -2
  46. data/spec/views/campaigns/destroy.js.haml_spec.rb +1 -1
  47. data/spec/views/campaigns/edit.js.haml_spec.rb +2 -2
  48. data/spec/views/campaigns/index.haml_spec.rb +1 -1
  49. data/spec/views/campaigns/index.js.haml_spec.rb +1 -1
  50. data/spec/views/campaigns/show.haml_spec.rb +4 -4
  51. data/spec/views/campaigns/update.js.haml_spec.rb +1 -1
  52. data/spec/views/contacts/_new.haml_spec.rb +1 -1
  53. data/spec/views/contacts/create.js.haml_spec.rb +3 -3
  54. data/spec/views/contacts/destroy.js.haml_spec.rb +1 -1
  55. data/spec/views/contacts/edit.js.haml_spec.rb +3 -3
  56. data/spec/views/contacts/index.haml_spec.rb +1 -1
  57. data/spec/views/contacts/index.js.html_spec.rb +1 -1
  58. data/spec/views/contacts/new.js.haml_spec.rb +1 -1
  59. data/spec/views/contacts/update.js.haml_spec.rb +2 -2
  60. data/spec/views/home/index.haml_spec.rb +1 -1
  61. data/spec/views/home/index.js.haml_spec.rb +2 -2
  62. data/spec/views/home/options.js.haml_spec.rb +2 -2
  63. data/spec/views/leads/_convert.haml_spec.rb +3 -3
  64. data/spec/views/leads/_edit.haml_spec.rb +2 -2
  65. data/spec/views/leads/_new.haml_spec.rb +1 -1
  66. data/spec/views/leads/_sidebar_show.haml_spec.rb +1 -1
  67. data/spec/views/leads/convert.js.haml_spec.rb +4 -4
  68. data/spec/views/leads/create.js.haml_spec.rb +3 -3
  69. data/spec/views/leads/destroy.js.haml_spec.rb +2 -2
  70. data/spec/views/leads/edit.js.haml_spec.rb +4 -4
  71. data/spec/views/leads/index.haml_spec.rb +1 -1
  72. data/spec/views/leads/index.js.haml_spec.rb +1 -1
  73. data/spec/views/leads/new.js.haml_spec.rb +1 -1
  74. data/spec/views/leads/promote.js.haml_spec.rb +7 -7
  75. data/spec/views/leads/reject.js.haml_spec.rb +2 -2
  76. data/spec/views/leads/show.haml_spec.rb +2 -2
  77. data/spec/views/leads/update.js.haml_spec.rb +4 -4
  78. data/spec/views/opportunities/_edit.haml_spec.rb +5 -5
  79. data/spec/views/opportunities/_new.haml_spec.rb +1 -1
  80. data/spec/views/opportunities/create.js.haml_spec.rb +3 -3
  81. data/spec/views/opportunities/destroy.js.haml_spec.rb +3 -3
  82. data/spec/views/opportunities/edit.js.haml_spec.rb +3 -3
  83. data/spec/views/opportunities/index.haml_spec.rb +1 -1
  84. data/spec/views/opportunities/index.js.haml_spec.rb +1 -1
  85. data/spec/views/opportunities/new.js.haml_spec.rb +1 -1
  86. data/spec/views/opportunities/update.js.haml_spec.rb +4 -4
  87. data/spec/views/tasks/_edit.haml_spec.rb +1 -1
  88. data/spec/views/tasks/complete.js.haml_spec.rb +4 -4
  89. data/spec/views/tasks/create.js.haml_spec.rb +5 -5
  90. data/spec/views/tasks/destroy.js.haml_spec.rb +2 -2
  91. data/spec/views/tasks/index.haml_spec.rb +4 -4
  92. data/spec/views/tasks/uncomplete.js.haml_spec.rb +2 -2
  93. data/spec/views/tasks/update.js.haml_spec.rb +18 -18
  94. data/spec/views/users/upload_avatar.js.haml_spec.rb +2 -2
  95. metadata +17 -14
@@ -44,8 +44,10 @@ class Admin::UsersController < Admin::ApplicationController
44
44
  # POST /admin/users.xml AJAX
45
45
  #----------------------------------------------------------------------------
46
46
  def create
47
+ params[:user][:email].try(:strip!)
47
48
  params[:user][:password_confirmation] = nil if params[:user][:password_confirmation].blank?
48
49
  @user = User.new(user_params)
50
+ @user.check_if_needs_approval
49
51
  @user.save_without_session_maintenance
50
52
 
51
53
  respond_with(@user)
@@ -55,6 +57,7 @@ class Admin::UsersController < Admin::ApplicationController
55
57
  # PUT /admin/users/1.xml AJAX
56
58
  #----------------------------------------------------------------------------
57
59
  def update
60
+ params[:user][:email].try(:strip!)
58
61
  params[:user][:password_confirmation] = nil if params[:user][:password_confirmation].blank?
59
62
  @user = User.find(params[:id])
60
63
  @user.attributes = user_params
@@ -73,7 +76,9 @@ class Admin::UsersController < Admin::ApplicationController
73
76
  # DELETE /admin/users/1.xml AJAX
74
77
  #----------------------------------------------------------------------------
75
78
  def destroy
76
- flash[:warning] = t(:msg_cant_delete_user, @user.full_name) unless @user.destroy
79
+ unless @user.destroyable? && @user.destroy
80
+ flash[:warning] = t(:msg_cant_delete_user, @user.full_name)
81
+ end
77
82
 
78
83
  respond_with(@user)
79
84
  end
@@ -6,6 +6,8 @@
6
6
  class ApplicationController < ActionController::Base
7
7
  protect_from_forgery
8
8
 
9
+ before_filter :set_paper_trail_whodunnit
10
+
9
11
  before_action :set_context
10
12
  before_action :clear_setting_cache
11
13
  before_action :cors_preflight_check
@@ -134,6 +134,7 @@ class UsersController < ApplicationController
134
134
  protected
135
135
 
136
136
  def user_params
137
+ params[:user][:email].try(:strip!)
137
138
  params[:user].permit(
138
139
  :username,
139
140
  :email,
@@ -15,7 +15,7 @@ module TagsHelper
15
15
  elsif !query.include?(hashtag)
16
16
  query += " #{hashtag}"
17
17
  end
18
- out << link_to_function(tag, "crm.search_tagged('#{escape_javascript(query)}', '#{model.class.to_s.tableize}')", title: tag)
18
+ out << link_to_function(tag, "crm.search_tagged('#{query}', '#{model.class.to_s.tableize}')", title: tag)
19
19
  end
20
20
  end
21
21
 
@@ -39,9 +39,6 @@
39
39
  #
40
40
 
41
41
  class User < ActiveRecord::Base
42
- before_create :check_if_needs_approval
43
- before_destroy :check_if_current_user, :check_if_has_related_assets
44
-
45
42
  has_one :avatar, as: :entity, dependent: :destroy # Personal avatar.
46
43
  has_many :avatars # As owner who uploaded it, ex. Contact avatar.
47
44
  has_many :comments, as: :commentable # As owner who created a comment.
@@ -143,7 +140,9 @@ class User < ActiveRecord::Base
143
140
  [name].to_xml
144
141
  end
145
142
 
146
- private
143
+ def destroyable?
144
+ check_if_current_user && !has_related_assets?
145
+ end
147
146
 
148
147
  # Suspend newly created user if signup requires an approval.
149
148
  #----------------------------------------------------------------------------
@@ -159,15 +158,17 @@ class User < ActiveRecord::Base
159
158
 
160
159
  # Prevent deleting a user unless she has no artifacts left.
161
160
  #----------------------------------------------------------------------------
162
- def check_if_has_related_assets
163
- artifacts = %w(Account Campaign Lead Contact Opportunity Comment Task).inject(0) do |sum, asset|
161
+ def has_related_assets?
162
+ sum = %w(Account Campaign Lead Contact Opportunity Comment Task).detect do |asset|
164
163
  klass = asset.constantize
165
- sum += klass.assigned_to(self).count if asset != "Comment"
166
- sum += klass.created_by(self).count
164
+
165
+ asset != "Comment" && klass.assigned_to(self).exists? || klass.created_by(self).exists?
167
166
  end
168
- artifacts == 0
167
+ !sum.nil?
169
168
  end
170
169
 
170
+ private
171
+
171
172
  # Define class methods
172
173
  #----------------------------------------------------------------------------
173
174
  class << self
@@ -0,0 +1 @@
1
+ PaperTrail.config.track_associations = false
@@ -10,11 +10,11 @@ require 'fat_free_crm/version'
10
10
 
11
11
  Gem::Specification.new do |gem|
12
12
  gem.name = 'fat_free_crm'
13
- gem.authors = ['Michael Dvorkin', 'Ben Tillman', 'Nathan Broadbent', 'Stephen Kenworthy']
13
+ gem.authors = ['Michael Dvorkin', 'Ben Tillman', 'Nathan Broadbent', 'Stephen Kenworthy', "Daniel O'Connor"]
14
14
  gem.summary = 'Fat Free CRM'
15
15
  gem.description = 'An open source, Ruby on Rails customer relationship management platform'
16
16
  gem.homepage = 'http://fatfreecrm.com'
17
- gem.email = ['mike@fatfreecrm.com', 'nathan@fatfreecrm.com', 'warp@fatfreecrm.com', 'steveyken@gmail.com']
17
+ gem.email = ['mike@fatfreecrm.com', 'nathan@fatfreecrm.com', 'warp@fatfreecrm.com', 'steveyken@gmail.com', 'daniel.oconnor@gmail.com']
18
18
  gem.files = files
19
19
  gem.version = FatFreeCRM::VERSION::STRING
20
20
  gem.required_ruby_version = '>= 2.0.0'
@@ -28,13 +28,13 @@ Gem::Specification.new do |gem|
28
28
  gem.add_dependency 'jquery-rails'
29
29
  gem.add_dependency 'jquery-migrate-rails'
30
30
  gem.add_dependency 'jquery-ui-rails'
31
- gem.add_dependency 'select2-rails', '~> 3.5.9.3'
31
+ gem.add_dependency 'select2-rails'
32
32
  gem.add_dependency 'simple_form'
33
33
  gem.add_dependency 'will_paginate'
34
34
  gem.add_dependency 'paperclip'
35
35
  # Manually added paperclip gem dependency "cocaine" in order to fix load error: "no such file to load -- cocaine"
36
36
  gem.add_dependency 'cocaine'
37
- gem.add_dependency 'paper_trail', '>= 4.0.0.beta2'
37
+ gem.add_dependency 'paper_trail', '~> 6.0.0'
38
38
  gem.add_dependency 'authlogic', '>= 3.4.4'
39
39
  gem.add_dependency 'acts_as_commentable'
40
40
  gem.add_dependency 'acts-as-taggable-on', '>= 3.4.3'
@@ -7,8 +7,8 @@ if defined?(RSpec)
7
7
  require 'rspec/core/rake_task'
8
8
 
9
9
  namespace :spec do
10
- desc "Preparing test env"
11
- task :prepare do
10
+ desc "Preparing test env db"
11
+ task :preparedb do
12
12
  tmp_env = Rails.env
13
13
  Rails.env = "test"
14
14
  Rake::Task["ffcrm:config:copy_database_yml"].invoke
@@ -17,8 +17,4 @@ if defined?(RSpec)
17
17
  Rails.env = tmp_env
18
18
  end
19
19
  end
20
-
21
- Rake::Task["spec"].prerequisites.clear
22
- Rake::Task["spec"].prerequisites.push("spec:prepare")
23
-
24
20
  end
@@ -23,5 +23,10 @@ module FatFreeCRM
23
23
  end
24
24
  end
25
25
 
26
+ config.to_prepare do
27
+ Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
28
+ require_dependency(c)
29
+ end
30
+ end
26
31
  end
27
32
  end
@@ -6,9 +6,9 @@
6
6
  module FatFreeCRM
7
7
  module VERSION #:nodoc:
8
8
  MAJOR = 0
9
- MINOR = 14
10
- TINY = 2
11
- PRE = nil
9
+ MINOR = 15
10
+ TINY = 0
11
+ PRE = 'beta'
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
14
14
  end
@@ -22,7 +22,7 @@ FactoryGirl.define do
22
22
 
23
23
  factory :field do
24
24
  type "Field"
25
- field_group { FactoryGirl.create(:field_group) }
25
+ field_group
26
26
  position { FactoryGirl.generate(:field_position) }
27
27
  label { FactoryGirl.generate(:field_label) }
28
28
  name { |f| f.label.downcase.gsub(/[^a-z0-9]+/, '_') }
@@ -15,7 +15,7 @@ FactoryGirl.define do
15
15
  factory :opportunity do
16
16
  user
17
17
  campaign
18
- account { FactoryGirl.create(:account) }
18
+ account
19
19
  assigned_to nil
20
20
  name { FFaker::Lorem.sentence[0, 64] }
21
21
  access "Public"
@@ -7,9 +7,11 @@ require 'spec_helper'
7
7
 
8
8
  describe SubscriptionMailer do
9
9
  describe "comment notification" do
10
- let(:user) { FactoryGirl.create(:user, email: 'notify_me@example.com') }
11
- let(:commentable) { FactoryGirl.create(:opportunity, id: 47, name: 'Opportunity name') }
12
- let(:comment) { FactoryGirl.create(:comment, commentable: commentable) }
10
+ let(:user) { build(:user, email: 'notify_me@example.com') }
11
+ let(:campaign) { build(:campaign, user: user) }
12
+ let(:account) { build(:account, user: user) }
13
+ let(:commentable) { build_stubbed(:opportunity, id: 47, name: 'Opportunity name', account: account, campaign: campaign, user: user) }
14
+ let(:comment) { build(:comment, commentable: commentable, user: user) }
13
15
  let(:mail) { SubscriptionMailer.comment_notification(user, comment) }
14
16
 
15
17
  before :each do
@@ -7,7 +7,7 @@ require 'spec_helper'
7
7
 
8
8
  describe UserMailer do
9
9
  describe "password_reset_instructions" do
10
- let(:user) { FactoryGirl.create(:user, email: "forgot_my_password@example.com") }
10
+ let(:user) { FactoryGirl.build(:user, email: "forgot_my_password@example.com") }
11
11
  let(:mail) { UserMailer.password_reset_instructions(user) }
12
12
 
13
13
  before(:each) do
@@ -33,11 +33,11 @@ describe UserMailer do
33
33
  end
34
34
 
35
35
  describe "assigned_entity_notification" do
36
- let(:assigner) { FactoryGirl.create(:user, first_name: "Bob", last_name: "Hope") }
37
- let(:assignee) { FactoryGirl.create(:user, email: "assignee@example.com") }
36
+ let(:assigner) { FactoryGirl.build(:user, first_name: "Bob", last_name: "Hope") }
37
+ let(:assignee) { FactoryGirl.build(:user, email: "assignee@example.com") }
38
38
 
39
39
  context "for an account" do
40
- let(:account) { FactoryGirl.create(:account, id: 16, name: 'Ghostbusters', user: assigner, assignee: assignee) }
40
+ let(:account) { FactoryGirl.build_stubbed(:account, id: 16, name: 'Ghostbusters', user: assigner, assignee: assignee) }
41
41
  let(:mail) { UserMailer.assigned_entity_notification(account, assigner) }
42
42
 
43
43
  it "sets fatfree as the sender" do
@@ -62,7 +62,7 @@ describe UserMailer do
62
62
  end
63
63
 
64
64
  context "for a contact" do
65
- let(:contact) { FactoryGirl.create(:contact, id: 56, first_name: 'Harold', last_name: 'Ramis', user: assigner, assignee: assignee) }
65
+ let(:contact) { FactoryGirl.build_stubbed(:contact, id: 56, first_name: 'Harold', last_name: 'Ramis', user: assigner, assignee: assignee) }
66
66
  let(:mail) { UserMailer.assigned_entity_notification(contact, assigner) }
67
67
 
68
68
  it "sets fatfree as the sender" do
@@ -87,7 +87,7 @@ describe UserMailer do
87
87
  end
88
88
 
89
89
  context "for a lead" do
90
- let(:lead) { FactoryGirl.create(:lead, id: 42, first_name: 'Bill', last_name: 'Murray', user: assigner, assignee: assignee) }
90
+ let(:lead) { FactoryGirl.build_stubbed(:lead, id: 42, first_name: 'Bill', last_name: 'Murray', user: assigner, assignee: assignee) }
91
91
  let(:mail) { UserMailer.assigned_entity_notification(lead, assigner) }
92
92
 
93
93
  it "sets fatfree as the sender" do
@@ -7,17 +7,17 @@ end
7
7
 
8
8
  describe "User abilities" do
9
9
  subject(:ability) { Ability.new(user) }
10
- let(:subject_user) { create :user }
10
+ let(:subject_user) { build :user }
11
11
 
12
12
  context "when site manager, I" do
13
- let(:user) { create :user, admin: true }
13
+ let(:user) { build :user, admin: true }
14
14
  all_actions.each do |do_action|
15
15
  it { is_expected.to be_able_to(do_action, subject_user) }
16
16
  end
17
17
  end
18
18
 
19
19
  context "when myself, I" do
20
- let(:user) { create :user }
20
+ let(:user) { build :user }
21
21
  let(:subject_user) { user }
22
22
  all_actions.each do |do_action|
23
23
  it { is_expected.to be_able_to(do_action, subject_user) }
@@ -42,61 +42,59 @@ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
42
42
 
43
43
  describe User do
44
44
  it "should create a new instance given valid attributes" do
45
- User.create!(
45
+ expect(User.new(
46
46
  username: "username",
47
47
  email: "user@example.com",
48
48
  password: "password",
49
49
  password_confirmation: "password"
50
- )
50
+ ).valid?).to eq true
51
51
  end
52
52
 
53
53
  it "should have a valid factory" do
54
54
  expect(FactoryGirl.build(:user)).to be_valid
55
55
  end
56
56
 
57
- describe "Destroying users with and without related assets" do
58
- before do
59
- @user = FactoryGirl.create(:user)
60
- end
57
+ describe '#destroyable?' do
58
+ describe "Destroying users with and without related assets" do
59
+ before do
60
+ @user = FactoryGirl.build(:user)
61
+ end
61
62
 
62
- %w(account campaign lead contact opportunity).each do |asset|
63
- it "should not destroy the user if she owns #{asset}" do
64
- FactoryGirl.create(asset, user: @user)
65
- @user.destroy
66
- expect { User.find(@user.id) }.to_not raise_error
67
- expect(@user.destroyed?).to eq(false)
63
+ %w(account campaign lead contact opportunity).each do |asset|
64
+ it "should not destroy the user if she owns #{asset}" do
65
+ FactoryGirl.create(asset, user: @user)
66
+
67
+ expect(@user.destroyable?).to eq(false)
68
+ end
69
+
70
+ it "should not destroy the user if she has #{asset} assigned" do
71
+ FactoryGirl.create(asset, assignee: @user)
72
+ expect(@user.destroyable?).to eq(false)
73
+ end
68
74
  end
69
75
 
70
- it "should not destroy the user if she has #{asset} assigned" do
71
- FactoryGirl.create(asset, assignee: @user)
72
- @user.destroy
73
- expect { User.find(@user.id) }.to_not raise_error
74
- expect(@user.destroyed?).to eq(false)
76
+ it "should not destroy the user if she owns a comment" do
77
+ login
78
+ account = build(:account, user: current_user)
79
+ FactoryGirl.create(:comment, user: @user, commentable: account)
80
+ expect(@user.destroyable?).to eq(false)
75
81
  end
76
- end
77
82
 
78
- it "should not destroy the user if she owns a comment" do
79
- login
80
- account = FactoryGirl.create(:account, user: current_user)
81
- FactoryGirl.create(:comment, user: @user, commentable: account)
82
- @user.destroy
83
- expect { User.find(@user.id) }.to_not raise_error
84
- expect(@user.destroyed?).to eq(false)
85
- end
83
+ it "should not destroy the current user" do
84
+ login
86
85
 
87
- it "should not destroy the current user" do
88
- login
89
- current_user.destroy
90
- expect { current_user.reload }.to_not raise_error
91
- expect(current_user).not_to be_destroyed
92
- end
86
+ expect(current_user.destroyable?).to eq(false)
87
+ end
93
88
 
94
- it "should destroy the user" do
95
- @user.destroy
96
- expect { User.find(@user.id) }.to raise_error(ActiveRecord::RecordNotFound)
97
- expect(@user).to be_destroyed
89
+ it "should destroy the user" do
90
+ expect(@user.destroyable?).to eq(true)
91
+ end
92
+ end
93
+ end
94
+ describe '#destroy' do
95
+ before do
96
+ @user = FactoryGirl.create(:user)
98
97
  end
99
-
100
98
  it "once the user gets deleted all her permissions must be deleted too" do
101
99
  FactoryGirl.create(:permission, user: @user, asset: FactoryGirl.create(:account))
102
100
  FactoryGirl.create(:permission, user: @user, asset: FactoryGirl.create(:contact))
@@ -114,69 +112,82 @@ describe User do
114
112
  end
115
113
  end
116
114
 
117
- it "should set suspended timestamp upon creation if signups need approval and the user is not an admin" do
118
- allow(Setting).to receive(:user_signup).and_return(:needs_approval)
119
- @user = FactoryGirl.create(:user, suspended_at: nil)
120
- expect(@user).to be_suspended
121
- end
115
+ describe '#check_if_needs_approval' do
116
+ it "should set suspended timestamp upon creation if signups need approval and the user is not an admin" do
117
+ allow(Setting).to receive(:user_signup).and_return(:needs_approval)
118
+ @user = FactoryGirl.build(:user, suspended_at: nil)
119
+
120
+ @user.check_if_needs_approval
121
+
122
+ expect(@user).to be_suspended
123
+ end
122
124
 
123
- it "should not set suspended timestamp upon creation if signups need approval and the user is an admin" do
124
- allow(Setting).to receive(:user_signup).and_return(:needs_approval)
125
- @user = FactoryGirl.create(:user, admin: true, suspended_at: nil)
126
- expect(@user).not_to be_suspended
125
+ it "should not set suspended timestamp upon creation if signups need approval and the user is an admin" do
126
+ allow(Setting).to receive(:user_signup).and_return(:needs_approval)
127
+ @user = FactoryGirl.build(:user, admin: true, suspended_at: nil)
128
+
129
+ @user.check_if_needs_approval
130
+
131
+ expect(@user).not_to be_suspended
132
+ end
127
133
  end
128
134
 
129
135
  context "scopes" do
130
136
  describe "have_assigned_opportunities" do
131
- before :each do
137
+ before do
132
138
  @user1 = FactoryGirl.create(:user)
133
- FactoryGirl.create(:opportunity, assignee: @user1, stage: 'analysis')
139
+ FactoryGirl.create(:opportunity, assignee: @user1, stage: 'analysis', account: nil, campaign: nil, user: nil)
134
140
 
135
141
  @user2 = FactoryGirl.create(:user)
136
142
 
137
143
  @user3 = FactoryGirl.create(:user)
138
- FactoryGirl.create(:opportunity, assignee: @user3, stage: 'won')
144
+ FactoryGirl.create(:opportunity, assignee: @user3, stage: 'won', account: nil, campaign: nil, user: nil)
139
145
 
140
146
  @user4 = FactoryGirl.create(:user)
141
- FactoryGirl.create(:opportunity, assignee: @user4, stage: 'lost')
147
+ FactoryGirl.create(:opportunity, assignee: @user4, stage: 'lost', account: nil, campaign: nil, user: nil)
148
+
149
+ @result = User.have_assigned_opportunities
142
150
  end
143
151
 
144
152
  it "includes users with assigned opportunities" do
145
- expect(User.have_assigned_opportunities).to include(@user1)
153
+ expect(@result).to include(@user1)
146
154
  end
147
155
 
148
156
  it "excludes users without any assigned opportunities" do
149
- expect(User.have_assigned_opportunities).not_to include(@user2)
157
+ expect(@result).not_to include(@user2)
150
158
  end
151
159
 
152
160
  it "excludes users with opportunities that have been won or lost" do
153
- expect(User.have_assigned_opportunities).not_to include(@user3)
154
- expect(User.have_assigned_opportunities).not_to include(@user4)
161
+ expect(@result).not_to include(@user3)
162
+ expect(@result).not_to include(@user4)
155
163
  end
156
164
  end
157
165
  end
158
166
 
159
167
  context "instance methods" do
160
168
  describe "assigned_opportunities" do
161
- before :each do
169
+ before do
162
170
  @user = FactoryGirl.create(:user)
163
- @opportunity1 = FactoryGirl.create(:opportunity, assignee: @user)
164
- @opportunity2 = FactoryGirl.create(:opportunity, assignee: FactoryGirl.create(:user))
171
+
172
+ @opportunity1 = FactoryGirl.create(:opportunity, assignee: @user, account: nil, campaign: nil, user: nil)
173
+ @opportunity2 = FactoryGirl.create(:opportunity, assignee: FactoryGirl.create(:user), account: nil, campaign: nil, user: nil)
174
+
175
+ @result = @user.assigned_opportunities
165
176
  end
166
177
 
167
178
  it "includes opportunities assigned to user" do
168
- expect(@user.assigned_opportunities).to include(@opportunity1)
179
+ expect(@result).to include(@opportunity1)
169
180
  end
170
181
 
171
182
  it "does not include opportunities assigned to another user" do
172
- expect(@user.assigned_opportunities).not_to include(@opportunity2)
183
+ expect(@result).not_to include(@opportunity2)
173
184
  end
174
185
  end
175
186
  end
176
187
 
177
188
  describe "Setting I18n.locale" do
178
189
  before do
179
- @user = FactoryGirl.create(:user)
190
+ @user = FactoryGirl.build(:user)
180
191
  @locale = I18n.locale
181
192
  end
182
193
 
@@ -199,14 +210,14 @@ describe User do
199
210
 
200
211
  describe "Setting single access token" do
201
212
  it "should update single_access_token attribute if it is not set already" do
202
- @user = FactoryGirl.create(:user, single_access_token: nil)
213
+ @user = FactoryGirl.build(:user, single_access_token: nil)
203
214
 
204
215
  @user.set_single_access_token
205
216
  expect(@user.single_access_token).not_to eq(nil)
206
217
  end
207
218
 
208
219
  it "should not update single_access_token attribute if it is set already" do
209
- @user = FactoryGirl.create(:user, single_access_token: "token")
220
+ @user = FactoryGirl.build(:user, single_access_token: "token")
210
221
 
211
222
  @user.set_single_access_token
212
223
  expect(@user.single_access_token).to eq("token")