/
contact_spec.rb
219 lines (188 loc) · 7.14 KB
/
contact_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# frozen_string_literal: true
# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
# == Schema Information
#
# Table name: contacts
#
# id :integer not null, primary key
# user_id :integer
# lead_id :integer
# assigned_to :integer
# reports_to :integer
# first_name :string(64) default(""), not null
# last_name :string(64) default(""), not null
# access :string(8) default("Public")
# title :string(64)
# department :string(64)
# source :string(32)
# email :string(64)
# alt_email :string(64)
# phone :string(32)
# mobile :string(32)
# fax :string(32)
# blog :string(128)
# linkedin :string(128)
# facebook :string(128)
# twitter :string(128)
# born_on :date
# do_not_call :boolean default(FALSE), not null
# deleted_at :datetime
# created_at :datetime
# updated_at :datetime
# background_info :string(255)
# skype :string(128)
#
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Contact do
it "should create a new instance given valid attributes" do
Contact.create!(first_name: "Billy", last_name: "Bones")
end
describe "Update existing contact" do
before(:each) do
@contact = create(:contact, account: create(:account))
end
it "should create new account if requested so" do
expect do
@contact.update_with_account_and_permissions(
account: { name: "New account" },
contact: { first_name: "Billy" }
)
end.to change(Account, :count).by(1)
expect(Account.last.name).to eq("New account")
expect(@contact.first_name).to eq("Billy")
end
it "should change account if another account was selected" do
@another_account = FactoryGirl.create(:account)
expect do
@contact.update_with_account_and_permissions(
account: { id: @another_account.id },
contact: { first_name: "Billy" }
)
end.not_to change(Account, :count)
expect(@contact.account).to eq(@another_account)
expect(@contact.first_name).to eq("Billy")
end
it "should drop existing Account if [create new account] is blank" do
expect do
@contact.update_with_account_and_permissions(
account: { name: "" },
contact: { first_name: "Billy" }
)
end.not_to change(Account, :count)
expect(@contact.account).to eq(nil)
expect(@contact.first_name).to eq("Billy")
end
it "should drop existing Account if [-- None --] is selected from list of accounts" do
expect do
@contact.update_with_account_and_permissions(
account: { id: "" },
contact: { first_name: "Billy" }
)
end.not_to change(Account, :count)
expect(@contact.account).to eq(nil)
expect(@contact.first_name).to eq("Billy")
end
it "should change account if entered name of another account was found" do
@another_account = FactoryGirl.create(:account, name: "Another name")
expect do
@contact.update_with_account_and_permissions(
account: { name: "Another name" },
contact: { first_name: "Billy" }
)
end.not_to change(Account, :count)
expect(@contact.account).to eq(@another_account)
expect(@contact.first_name).to eq("Billy")
end
end
describe "Attach" do
before do
@contact = FactoryGirl.create(:contact)
end
it "should return nil when attaching existing asset" do
@task = FactoryGirl.create(:task, asset: @contact)
@opportunity = FactoryGirl.create(:opportunity)
@contact.opportunities << @opportunity
expect(@contact.attach!(@task)).to eq(nil)
expect(@contact.attach!(@opportunity)).to eq(nil)
end
it "should return non-empty list of attachments when attaching new asset" do
@task = FactoryGirl.create(:task)
@opportunity = FactoryGirl.create(:opportunity)
expect(@contact.attach!(@task)).to eq([@task])
expect(@contact.attach!(@opportunity)).to eq([@opportunity])
end
end
describe "Discard" do
before do
@contact = FactoryGirl.create(:contact)
end
it "should discard a task" do
@task = FactoryGirl.create(:task, asset: @contact)
expect(@contact.tasks.count).to eq(1)
@contact.discard!(@task)
expect(@contact.reload.tasks).to eq([])
expect(@contact.tasks.count).to eq(0)
end
it "should discard an opportunity" do
@opportunity = FactoryGirl.create(:opportunity)
@contact.opportunities << @opportunity
expect(@contact.opportunities.count).to eq(1)
@contact.discard!(@opportunity)
expect(@contact.opportunities).to eq([])
expect(@contact.opportunities.count).to eq(0)
end
end
describe "Exportable" do
describe "assigned contact" do
let(:contact1) { FactoryGirl.build(:contact, assignee: FactoryGirl.create(:user)) }
let(:contact2) { FactoryGirl.build(:contact, user: FactoryGirl.create(:user, first_name: nil, last_name: nil), assignee: FactoryGirl.create(:user, first_name: nil, last_name: nil)) }
it_should_behave_like("exportable") do
let(:exported) { [contact1, contact2] }
end
end
describe "unassigned contact" do
let(:contact1) { FactoryGirl.build(:contact, assignee: nil) }
let(:contact2) { FactoryGirl.build(:contact, user: FactoryGirl.create(:user, first_name: nil, last_name: nil), assignee: nil) }
it_should_behave_like("exportable") do
let(:exported) { [contact1, contact2] }
end
end
end
describe "permissions" do
it_should_behave_like Ability, Contact
end
describe "text_search" do
before do
@contact = FactoryGirl.create(:contact, first_name: "Bob", last_name: "Dillion", email: 'bob_dillion@example.com', phone: '+1 123 456 789')
end
it "should search first_name" do
expect(Contact.text_search('Bob')).to eq([@contact])
end
it "should search last_name" do
expect(Contact.text_search('Dillion')).to eq([@contact])
end
it "should search whole name" do
expect(Contact.text_search('Bob Dillion')).to eq([@contact])
end
it "should search whole name reversed" do
expect(Contact.text_search('Dillion Bob')).to eq([@contact])
end
it "should search email" do
expect(Contact.text_search('example')).to eq([@contact])
end
it "should search phone" do
expect(Contact.text_search('123')).to eq([@contact])
end
it "should not break with a single quote" do
contact2 = FactoryGirl.create(:contact, first_name: "Shamus", last_name: "O'Connell", email: 'bob_dillion@example.com', phone: '+1 123 456 789')
expect(Contact.text_search("O'Connell")).to eq([contact2])
end
it "should not break on special characters" do
expect(Contact.text_search('@$%#^@!')).to eq([])
end
end
end