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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# frozen_string_literal: true
RSpec.shared_examples 'a timebox' do |timebox_type|
let(:project) { create(:project, :public) }
let(:group) { create(:group) }
let(:timebox_args) { [] }
let(:timebox) { create(timebox_type, *timebox_args, project: project) }
let(:issue) { create(:issue, project: project) }
let(:user) { create(:user) }
let(:timebox_table_name) { timebox_type.to_s.pluralize.to_sym }
# Values implementions can override
let(:mid_point) { Time.now.utc.to_date }
let(:open_on_left) { nil }
let(:open_on_right) { nil }
describe 'modules' do
context 'with a project' do
it_behaves_like 'AtomicInternalId' do
let(:internal_id_attribute) { :iid }
let(:instance) { build(timebox_type, *timebox_args, project: create(:project), group: nil) }
let(:scope) { :project }
let(:scope_attrs) { { project: instance.project } }
let(:usage) { timebox_table_name }
end
end
context 'with a group' do
it_behaves_like 'AtomicInternalId' do
let(:internal_id_attribute) { :iid }
let(:instance) { build(timebox_type, *timebox_args, project: nil, group: create(:group)) }
let(:scope) { :group }
let(:scope_attrs) { { namespace: instance.group } }
let(:usage) { timebox_table_name }
end
end
end
describe "Validation" do
before do
allow(subject).to receive(:set_iid).and_return(false)
end
describe 'start_date' do
it 'adds an error when start_date is greater then due_date' do
timebox = build(timebox_type, *timebox_args, start_date: Date.tomorrow, due_date: Date.yesterday)
expect(timebox).not_to be_valid
expect(timebox.errors[:due_date]).to include("must be greater than start date")
end
it 'adds an error when start_date is greater than 9999-12-31' do
timebox = build(timebox_type, *timebox_args, start_date: Date.new(10000, 1, 1))
expect(timebox).not_to be_valid
expect(timebox.errors[:start_date]).to include("date must not be after 9999-12-31")
end
end
describe 'due_date' do
it 'adds an error when due_date is greater than 9999-12-31' do
timebox = build(timebox_type, *timebox_args, due_date: Date.new(10000, 1, 1))
expect(timebox).not_to be_valid
expect(timebox.errors[:due_date]).to include("date must not be after 9999-12-31")
end
end
describe 'title' do
it { is_expected.to validate_presence_of(:title) }
it 'is invalid if title would be empty after sanitation' do
timebox = build(timebox_type, *timebox_args, project: project, title: '<img src=x onerror=prompt(1)>')
expect(timebox).not_to be_valid
expect(timebox.errors[:title]).to include("can't be blank")
end
end
describe '#timebox_type_check' do
it 'is invalid if it has both project_id and group_id' do
timebox = build(timebox_type, *timebox_args, group: group)
timebox.project = project
expect(timebox).not_to be_valid
expect(timebox.errors[:project_id]).to include("#{timebox_type} should belong either to a project or a group.")
end
end
end
describe "Associations" do
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:group) }
it { is_expected.to have_many(:issues) }
it { is_expected.to have_many(:merge_requests) }
it { is_expected.to have_many(:labels) }
end
describe '#timebox_name' do
it 'returns the name of the model' do
expect(timebox.timebox_name).to eq(timebox_type.to_s)
end
end
describe '#project_timebox?' do
context 'when project_id is present' do
it 'returns true' do
expect(timebox.project_timebox?).to be_truthy
end
end
context 'when project_id is not present' do
let(:timebox) { build(timebox_type, *timebox_args, group: group) }
it 'returns false' do
expect(timebox.project_timebox?).to be_falsey
end
end
end
describe '#group_timebox?' do
context 'when group_id is present' do
let(:timebox) { build(timebox_type, *timebox_args, group: group) }
it 'returns true' do
expect(timebox.group_timebox?).to be_truthy
end
end
context 'when group_id is not present' do
it 'returns false' do
expect(timebox.group_timebox?).to be_falsey
end
end
end
describe '#safe_title' do
let(:timebox) { create(timebox_type, *timebox_args, title: "<b>foo & bar -> 2.2</b>") }
it 'normalizes the title for use as a slug' do
expect(timebox.safe_title).to eq('foo-bar-22')
end
end
describe '#resource_parent' do
context 'when group is present' do
let(:timebox) { build(timebox_type, *timebox_args, group: group) }
it 'returns the group' do
expect(timebox.resource_parent).to eq(group)
end
end
context 'when project is present' do
it 'returns the project' do
expect(timebox.resource_parent).to eq(project)
end
end
end
describe "#title" do
let(:timebox) { create(timebox_type, *timebox_args, title: "<b>foo & bar -> 2.2</b>") }
it "sanitizes title" do
expect(timebox.title).to eq("foo & bar -> 2.2")
end
end
describe '#merge_requests_enabled?' do
context "per project" do
it "is true for projects with MRs enabled" do
project = create(:project, :merge_requests_enabled)
timebox = create(timebox_type, *timebox_args, project: project)
expect(timebox.merge_requests_enabled?).to be_truthy
end
it "is false for projects with MRs disabled" do
project = create(:project, :repository_enabled, :merge_requests_disabled)
timebox = create(timebox_type, *timebox_args, project: project)
expect(timebox.merge_requests_enabled?).to be_falsey
end
it "is false for projects with repository disabled" do
project = create(:project, :repository_disabled)
timebox = create(timebox_type, *timebox_args, project: project)
expect(timebox.merge_requests_enabled?).to be_falsey
end
end
context "per group" do
let(:timebox) { create(timebox_type, *timebox_args, group: group) }
it "is always true for groups, for performance reasons" do
expect(timebox.merge_requests_enabled?).to be_truthy
end
end
end
describe '#to_ability_name' do
it 'returns timebox' do
timebox = build(timebox_type, *timebox_args)
expect(timebox.to_ability_name).to eq(timebox_type.to_s)
end
end
describe '.within_timeframe' do
let(:factory) { timebox_type }
let(:min_date) { mid_point - 10.days }
let(:max_date) { mid_point + 10.days }
def box(from, to)
create(factory, *timebox_args,
start_date: from || open_on_left,
due_date: to || open_on_right)
end
it 'can find overlapping timeboxes' do
fully_open = box(nil, nil)
# ----| ................ # Not overlapping
non_overlapping_open_on_left = box(nil, min_date - 1.day)
# |--| ................ # Not overlapping
non_overlapping_closed_on_left = box(min_date - 2.days, min_date - 1.day)
# ------|............... # Overlapping
overlapping_open_on_left_just = box(nil, min_date)
# -----------------------| # Overlapping
overlapping_open_on_left_fully = box(nil, max_date + 1.day)
# ---------|............ # Overlapping
overlapping_open_on_left_partial = box(nil, min_date + 1.day)
# |-----|............ # Overlapping
overlapping_closed_partial = box(min_date - 1.day, min_date + 1.day)
# |--------------| # Overlapping
exact_match = box(min_date, max_date)
# |--------------------| # Overlapping
larger = box(min_date - 1.day, max_date + 1.day)
# ...|-----|...... # Overlapping
smaller = box(min_date + 1.day, max_date - 1.day)
# .........|-----| # Overlapping
at_end = box(max_date - 1.day, max_date)
# .........|--------- # Overlapping
at_end_open = box(max_date - 1.day, nil)
# |-------------------- # Overlapping
cover_from_left = box(min_date - 1.day, nil)
# .........|--------| # Overlapping
cover_from_middle_closed = box(max_date - 1.day, max_date + 1.day)
# ...............|--| # Overlapping
overlapping_at_end_just = box(max_date, max_date + 1.day)
# ............... |-| # Not Overlapping
not_overlapping_at_right_closed = box(max_date + 1.day, max_date + 2.days)
# ............... |-- # Not Overlapping
not_overlapping_at_right_open = box(max_date + 1.day, nil)
matches = described_class.within_timeframe(min_date, max_date)
expect(matches).to include(
overlapping_open_on_left_just,
overlapping_open_on_left_fully,
overlapping_open_on_left_partial,
overlapping_closed_partial,
exact_match,
larger,
smaller,
at_end,
at_end_open,
cover_from_left,
cover_from_middle_closed,
overlapping_at_end_just
)
expect(matches).not_to include(
non_overlapping_open_on_left,
non_overlapping_closed_on_left,
not_overlapping_at_right_closed,
not_overlapping_at_right_open
)
# Whether we match the 'fully-open' range depends on whether
# it is in fact open (i.e. whether the class allows infinite
# ranges)
if open_on_left.nil? && open_on_right.nil?
expect(matches).not_to include(fully_open)
else
expect(matches).to include(fully_open)
end
end
end
end
|