-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
version_spec.rb
241 lines (196 loc) · 8.49 KB
/
version_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# 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: versions
#
# id :integer not null, primary key
# user_id :integer
# item_id :integer
# item_type :string(255)
# event :string(32) default("create")
# info :string(255) default("")
# private :boolean default(FALSE)
# created_at :datetime
# updated_at :datetime
#
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
describe Version, versioning: true do
before do
login
PaperTrail.whodunnit = current_user.id.to_s
end
it "should create a new instance given valid attributes" do
FactoryGirl.create(:version, whodunnit: PaperTrail.whodunnit, item: FactoryGirl.create(:lead))
end
describe "with multiple version records" do
before do
@lead = FactoryGirl.create(:lead)
%w(create destroy update view).each do |event|
FactoryGirl.create(:version, event: event, item: @lead, whodunnit: PaperTrail.whodunnit)
FactoryGirl.create(:version, event: event, item: @lead, whodunnit: "1")
end
end
it "should not include view events" do
@versions = Version.for(current_user).exclude_events(:view)
expect(@versions.map(&:event).sort).not_to include('view')
end
it "should exclude create, update and destroy events" do
@versions = Version.for(current_user).exclude_events(:create, :update, :destroy)
expect(@versions.map(&:event)).not_to include('create')
expect(@versions.map(&:event)).not_to include('update')
expect(@versions.map(&:event)).not_to include('destroy')
end
it "should include only destroy events" do
@versions = Version.for(current_user).include_events(:destroy)
expect(@versions.map(&:event).uniq).to eq(%w(destroy))
end
it "should include create and update events" do
@versions = Version.for(current_user).include_events(:create, :update)
expect(@versions.map(&:event).uniq.sort).to eq(%w(create update))
end
it "should select all versions for a given user" do
@versions = Version.for(current_user)
expect(@versions.map(&:whodunnit).uniq).to eq([current_user.id.to_s])
end
end
%w(account campaign contact lead opportunity task).each do |item|
describe "Create, update, and delete (#{item})" do
before :each do
@item = FactoryGirl.create(item.to_sym, user: current_user)
@conditions = {item_id: @item.id, item_type: @item.class.name, whodunnit: PaperTrail.whodunnit}
end
it "should add a version when creating new #{item}" do
@version = Version.where(@conditions.merge(event: 'create')).first
expect(@version).not_to eq(nil)
end
it "should add a version when updating existing #{item}" do
if @item.respond_to?(:full_name)
@item.update_attributes(first_name: "Billy", last_name: "Bones")
else
@item.update_attributes(name: "Billy Bones")
end
@version = Version.where(@conditions.merge(event: 'update')).first
expect(@version).not_to eq(nil)
end
it "should add a version when deleting #{item}" do
@item.destroy
@version = Version.where(@conditions.merge(event: 'destroy')).first
expect(@version).not_to eq(nil)
end
it "should add a version when commenting on a #{item}" do
@comment = FactoryGirl.create(:comment, commentable: @item, user: current_user)
@version = Version.where({related_id: @item.id, related_type: @item.class.name, whodunnit: PaperTrail.whodunnit, event: 'create'}).first
expect(@version).not_to eq(nil)
end
end
end
describe "Recently viewed items (task)" do
before do
@task = FactoryGirl.create(:task)
@conditions = {item_id: @task.id, item_type: @task.class.name}
end
it "creating a new task should not add it to recently viewed items list" do
@versions = Version.where(@conditions)
expect(@versions.map(&:event)).to include('create') # but not view
end
it "updating a new task should not add it to recently viewed items list" do
@task.update(name: 'New Name')
@versions = Version.where(@conditions)
expect(@versions.map(&:event).sort).to eq(%w(create update)) # but not view
end
end
describe "Action refinements for task updates" do
before do
@task = FactoryGirl.create(:task, user: current_user)
@conditions = {item_id: @task.id, item_type: @task.class.name, whodunnit: PaperTrail.whodunnit}
end
it "should create 'completed' task event" do
@task.update_attribute(:completed_at, 1.second.ago)
@versions = Version.where(@conditions)
expect(@versions.map(&:event)).to include('complete')
end
it "should create 'reassigned' task event" do
@task.update_attribute(:assigned_to, current_user.id + 1)
@versions = Version.where(@conditions)
expect(@versions.map(&:event)).to include('reassign')
end
it "should create 'rescheduled' task event" do
@task.update_attribute(:bucket, "due_tomorrow") # FactoryGirl creates :due_asap task
@versions = Version.where(@conditions)
expect(@versions.map(&:event)).to include('reschedule')
end
end
describe "Rejecting a lead" do
before do
@lead = FactoryGirl.create(:lead, user: current_user, status: "new")
@conditions = {item_id: @lead.id, item_type: @lead.class.name, whodunnit: PaperTrail.whodunnit}
end
it "should create 'rejected' lead event" do
@lead.update_attribute(:status, "rejected")
@versions = Version.where(@conditions)
expect(@versions.map(&:event)).to include('reject')
end
end
describe "Permissions" do
before do
@user = FactoryGirl.create(:user)
Version.delete_all
end
it "should not show the create/update versions if the item is private" do
@item = FactoryGirl.create(:account, user: current_user, access: "Private")
@item.update(name: 'New Name')
@versions = Version.where({item_id: @item.id, item_type: @item.class.name})
expect(@versions.map(&:event).sort).to eq(%w(create update))
@versions = Version.latest.visible_to(@user)
expect(@versions).to eq([])
end
it "should not show the destroy version if the item is private" do
@item = FactoryGirl.create(:account, user: current_user, access: "Private")
@item.destroy
@versions = Version.where({item_id: @item.id, item_type: @item.class.name})
expect(@versions.map(&:event).sort).to eq(%w(create destroy))
@versions = Version.latest.visible_to(@user)
expect(@versions).to eq([])
end
it "should not show create/update versions if the item was not shared with the user" do
@item = FactoryGirl.create(:account,
user: current_user,
access: "Shared",
permissions: [ FactoryGirl.build(:permission, user: current_user, asset: @item) ]
)
@item.update(name: 'New Name')
@versions = Version.where({item_id: @item.id, item_type: @item.class.name})
expect(@versions.map(&:event).sort).to eq(%w(create update))
@versions = Version.latest.visible_to(@user)
expect(@versions).to eq([])
end
it "should not show the destroy version if the item was not shared with the user" do
@item = FactoryGirl.create(:account,
user: current_user,
access: "Shared",
permissions: [ FactoryGirl.build(:permission, user: current_user, asset: @item) ]
)
@item.destroy
@versions = Version.where({item_id: @item.id, item_type: @item.class.name})
expect(@versions.map(&:event).sort).to eq(%w(create destroy))
@versions = Version.latest.visible_to(@user)
expect(@versions).to eq([])
end
it "should show create/update versions if the item was shared with the user" do
@item = FactoryGirl.create(:account,
user: current_user,
access: "Shared",
permissions: [ FactoryGirl.build(:permission, user: @user, asset: @item) ]
)
@item.update(name: 'New Name')
@versions = Version.where({item_id: @item.id, item_type: @item.class.name})
expect(@versions.map(&:event).sort).to eq(%w(create update))
@versions = Version.latest.visible_to(@user)
expect(@versions.map(&:event).sort).to eq(%w(create update))
end
end
end