-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtext_spec.rb
More file actions
600 lines (515 loc) · 21.2 KB
/
text_spec.rb
File metadata and controls
600 lines (515 loc) · 21.2 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# frozen_string_literal: true
require_relative "../../../../spec_helper"
describe Entitlements::Data::Groups::Calculated::Text do
before(:each) do
Entitlements::Extras.load_extra("ldap_group")
Entitlements::Extras.load_extra("orgchart")
setup_default_filters
end
let(:people_obj) { Entitlements::Data::People::YAML.new(filename: fixture("people.yaml")) }
let(:cache) { { people_obj: people_obj } }
let(:filename) { fixture("ldap-config/text/example.txt") }
let(:subject) { described_class.new(filename: filename) }
describe "#members" do
it "returns the expected member list" do
result = subject.members
result_set = Set.new(result.map { |i| i.uid })
answer_array = %w[blackmanx russianblue RAGAMUFFIn mainecoon]
answer_set = Set.new(answer_array)
expect(result_set).to eq(answer_set)
end
it "handles case-insensitivity properly" do
filename = fixture("ldap-config/text/example2.txt")
subject = described_class.new(filename: filename)
result = subject.members
result_set = Set.new(result.map { |i| i.uid })
answer_array = %w[oJosazuLEs NEBELUNg khaomanee cyprus cheetoh chausie]
answer_set = Set.new(answer_array)
expect(result_set).to eq(answer_set)
end
end
describe "#description" do
it "returns the string when one is set" do
filename = fixture("ldap-config/text/example.txt")
subject = described_class.new(filename: filename)
expect(subject.description).to eq("Example")
end
it "returns an empty-string when description is undefined" do
filename = fixture("ldap-config/text/no-description.txt")
subject = described_class.new(filename: filename)
expect(subject.description).to eq("")
end
it "raises an error when there is a duplicate description" do
filename = fixture("ldap-config/text/duplicate-description.txt")
subject = described_class.new(filename: filename)
expect { subject.description }.to raise_error(RuntimeError, /description key is duplicated in .+duplicate-description.txt!/)
end
it "raises an error when the description is specified with !=" do
filename = fixture("ldap-config/text/not-equals-description.txt")
subject = described_class.new(filename: filename)
expect do
subject.description
end.to raise_error(RuntimeError, /description cannot use '!=' operator in .+not-equals-description.txt!/)
end
end
describe "#initialize_filters" do
it "returns defaults when no filters are specified" do
filename = fixture("ldap-config/filters/no-filters.txt")
subject = described_class.new(filename: filename)
answer = default_filters
expect(subject.filters).to eq(answer)
end
it "sets contractors filter to none when specified in file" do
filename = fixture("ldap-config/filters/one-filter-true.txt")
subject = described_class.new(filename: filename)
answer = default_filters
expect(subject.filters).to eq(answer.merge("contractors" => :none))
end
it "sets contractors filter to all when specified in file" do
filename = fixture("ldap-config/filters/one-filter-false.txt")
subject = described_class.new(filename: filename)
answer = default_filters
expect(subject.filters).to eq(answer.merge("contractors" => :all))
end
it "sets contractors filter to an array when specified in file" do
filename = fixture("ldap-config/filters/multiple-contractors-1.txt")
subject = described_class.new(filename: filename)
answer = default_filters
# case insensitivity comes later
expect(subject.filters).to eq(answer.merge("contractors" => %w[pixiEBOB SErengeti]))
end
it "raises an error when key == 'filter_'" do
filename = fixture("ldap-config/filters/bad-data-structure.txt")
expect do
described_class.new(filename: filename)
end.to raise_error(/In .+\/bad-data-structure.txt, cannot have a key named "filter_"!/)
end
it "returns an array with a single entry for a non-keyword" do
filename = fixture("ldap-config/filters/one-filter-value.txt")
subject = described_class.new(filename: filename)
answer = default_filters
expect(subject.filters).to eq(answer.merge("contractors" => %w[kittens]))
end
it "raises an error when the key of a filter is not expected" do
filename = fixture("ldap-config/filters/one-filter-invalid-key.txt")
expect do
described_class.new(filename: filename)
end.to raise_error(/In .+\/one-filter-invalid-key.txt, the key filter_fluffy_kittens is invalid!/)
end
it "raises an error when the key of a filter is repeated" do
filename = fixture("ldap-config/filters/one-filter-repeated.txt")
expect do
described_class.new(filename: filename)
end.to raise_error(/In .+\/one-filter-repeated.txt, filter_contractors cannot contain multiple entries when 'all' or 'none' is used!/)
end
it "raises an error when != is used in a filter" do
filename = fixture("ldap-config/filters/filter-not-equal.txt")
expect do
described_class.new(filename: filename)
end.to raise_error(/The filter contractors cannot use '!=' operator in .+filter-not-equal.txt!/)
end
it "treats an expired contractor filter as not even being present" do
filename = fixture("ldap-config/filters/expiration-contractor-expired.txt")
subject = described_class.new(filename: filename)
expect(subject.filters).to eq(default_filters)
end
it "treats a non-expired contractor filter normally" do
filename = fixture("ldap-config/filters/expiration-contractor-nonexpired.txt")
subject = described_class.new(filename: filename)
expect(subject.filters).to eq(default_filters.merge("contractors" => %w[pixiebob]))
end
it "removes expired contractor filters and keeps non-expired ones" do
filename = fixture("ldap-config/filters/expiration-contractor-mixedexpired.txt")
subject = described_class.new(filename: filename)
expect(subject.filters).to eq(default_filters.merge("contractors" => %w[pixiebob]))
end
end
describe "#initialize_metadata" do
it "returns an empty hash if there is no metadata method" do
filename = fixture("ldap-config/metadata/undefined.txt")
subject = described_class.new(filename: filename)
expect(subject.metadata).to eq({})
end
it "raises an error if metadata contains a nil key" do
filename = fixture("ldap-config/metadata/bad-data-key.txt")
message = "In #{filename}, cannot have a key named \"metadata_\"!"
expect do
described_class.new(filename: filename)
end.to raise_error(message)
end
it "raises an error if metadata contains a repeated key" do
filename = fixture("ldap-config/metadata/repeated-data-key.txt")
message = "In #{filename}, the key metadata_kittens is repeated!"
expect do
described_class.new(filename: filename)
end.to raise_error(message)
end
it "raises an error if != operator is used" do
filename = fixture("ldap-config/metadata/not-equal-metadata.txt")
message = "The key metadata_kittens cannot use '!=' operator in #{filename}!"
expect do
described_class.new(filename: filename)
end.to raise_error(message)
end
it "returns the hash of metadata" do
filename = fixture("ldap-config/metadata/good.txt")
subject = described_class.new(filename: filename)
expect(subject.metadata).to eq("kittens" => "awesome", "puppies" => "young dogs")
end
it "does not raise an error when metadata values contain semicolons" do
filename = fixture("ldap-config/metadata/semicolon.txt")
expect { described_class.new(filename: filename) }.not_to raise_error
subject = described_class.new(filename: filename)
expect(subject.metadata).to eq("kittens" => "awesome", "justification" => "Need access; for project work")
end
end
describe "#modifiers" do
it "returns an empty hash if there is no modifiers method" do
filename = fixture("ldap-config/metadata/undefined.txt")
subject = described_class.new(filename: filename)
expect(subject.modifiers).to eq({})
end
it "returns a hash of the modifier methods" do
filename = fixture("ldap-config/expiration/valid-text.txt")
subject = described_class.new(filename: filename)
expect(subject.modifiers).to eq("expiration"=>"2043-01-01")
end
it "raises an error if a modifier has additional settings" do
filename = fixture("ldap-config/expiration/additional-settings.txt")
message = "In #{filename}, the key modifier_expiration cannot have additional setting(s) \"expiration\"!"
expect { described_class.new(filename: filename).modifiers }.to raise_error(message)
end
end
describe "#rules" do
let(:filename) { fixture("ldap-config/text/multiple.txt") }
it "returns OR of the conditions without reserved keys" do
answer = {
"or"=>[
{"management"=>"MAINECOON"},
{"group"=>"cn=chickens,ou=Poultry,dc=kittens,dc=net"},
{"username"=>"BlackManx"}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
context "with no conditions" do
let(:filename) { fixture("ldap-config/text/empty.txt") }
it "raises an error" do
expect { subject.send(:rules) }.to raise_error(RuntimeError, /No conditions were found in .+empty\.txt!/)
end
end
context "with no conditions, but metadata_no_conditions_ok set" do
let(:filename) { fixture("ldap-config/text/empty-but-ok.txt") }
it "returns an empty hash" do
expect(subject.send(:rules)).to eq({"always" => false})
end
end
context "including an unknown method" do
let(:filename) { fixture("ldap-config/text/unknown.txt") }
it "raises an error" do
expect { subject.send(:rules) }.to raise_error(RuntimeError, /The method "foobar" is not allowed in .+unknown\.txt!/)
end
end
context "including a non-whitelisted method" do
let(:filename) { fixture("ldap-config/text/disallowed.txt") }
it "raises an error" do
expect { subject.send(:rules) }.to raise_error(RuntimeError, /The method "fizzbuzz" is not allowed in .+disallowed\.txt!/)
end
end
context "including an alias for a whitelisted method" do
let(:filename) { fixture("ldap-config/text/alias_method.txt") }
it "returns the expected rule set" do
answer = {"or"=>[{"group"=>"pizza_teams/from_username"}]}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "including an alias for a non-whitelisted method" do
let(:filename) { fixture("ldap-config/text/disallowed_alias.txt") }
it "raises an error referencing the alias" do
expect(subject).to receive(:function_for).with("fizzbuzz_alias").and_return("fizzbuzz")
expect(subject).to receive(:function_for).with("management").and_return("management")
expect do
subject.send(:rules)
end.to raise_error(RuntimeError, /The method "fizzbuzz_alias" is not allowed in .+disallowed_alias\.txt!/)
end
end
context "when a key is duplicated" do
let(:filename) { fixture("ldap-config/text/duplicated.txt") }
it "puts all of the values into the OR array" do
answer = {
"or"=>[
{"management"=>"MAINECOON"},
{"management"=>"balinese"},
{"username"=>"nebelung"},
{"username"=>"cheetoh"},
{"username"=>"cyprus"}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with a single != operator" do
let(:filename) { fixture("ldap-config/text/one-not-equal.txt") }
it "constructs and/not tree" do
answer = {
"and" => [
{"or"=>[
{"management"=>"MAINECOON"},
{"group"=>"cn=chickens,ou=Poultry,dc=kittens,dc=net"},
{"username"=>"BlackManx"}
]},
{"and"=>[
{"not"=>{"username"=>"russianblue"}}
]}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with multiple != operators" do
let(:filename) { fixture("ldap-config/text/multiple-not-equal.txt") }
it "constructs and/not tree" do
answer = {
"and" => [
{"or"=>[
{"management"=>"MAINECOON"},
{"group"=>"cn=chickens,ou=Poultry,dc=kittens,dc=net"}
]},
{"and"=>[
{"not"=>{"username"=>"BlackManx"}},
{"not"=>{"username"=>"russianblue"}}
]}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with only a != operator" do
let(:filename) { fixture("ldap-config/text/only-not-equal.txt") }
it "raises an error" do
expect do
subject.send(:rules)
end.to raise_error(RuntimeError, /No conditions were found in .+only-not-equal.txt!/)
end
end
context "with negative group" do
let(:filename) { fixture("ldap-config/text/positive-negative-ldap-group.txt") }
it "does not exclude contractors" do
answer = {
"and" => [
{"or"=>[
{"management"=>"MAINECOON"}
]},
{"and"=>[
{"not"=>{"group"=>"no_contractors"}}
]}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with a &= custom filter specified" do
let(:filename) { fixture("ldap-config/text/custom-filter.txt") }
it "applies 'and' logic with the stated condition" do
answer = {
"and" => [
{ "group" => "pizza_teams/grumpy-cat-eng" },
{ "or" => [
{ "username" => "BlackManx" },
{ "username" => "ragamuffin" },
{ "username" => "peterbald" },
{ "username" => "mainecoon" }
]}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with multiple &= custom filters specified" do
let(:filename) { fixture("ldap-config/text/custom-filter-multiple.txt") }
it "applies 'and' logic with the stated conditions" do
answer = {
"and" => [
{ "or" => [
{ "group" => "pizza_teams/grumpy-cat-eng" },
{ "direct_report" => "ojosazules" },
]},
{ "or" => [
{ "username" => "BlackManx" },
{ "username" => "ragamuffin" },
{ "username" => "peterbald" },
{ "username" => "mainecoon" },
{ "username" => "nebelung" },
{ "username" => "ojosazules" }
]}
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "with expiration" do
context "not expired" do
let(:filename) { fixture("ldap-config/text/expiration-not-expired.txt") }
it "constructs the correct rule set" do
answer = {
"or" => [
{ "username" => "blackmanx" },
{ "username" => "russianblue" },
{ "username" => "mainecoon" }
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "already expired" do
let(:filename) { fixture("ldap-config/text/expiration-already-expired.txt") }
it "constructs the correct rule set" do
answer = {
"or" => [
{ "username" => "mainecoon" }
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "already expired but expirations are disabled" do
let(:filename) { fixture("ldap-config/text/expiration-already-expired.txt") }
it "constructs the correct rule set" do
Entitlements.config["ignore_expirations"] = true
answer = {
"or" => [
{ "username" => "blackmanx" },
{ "username" => "russianblue" },
{ "username" => "mainecoon" }
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "mix of not expired and already expired" do
let(:filename) { fixture("ldap-config/text/expiration-mixed-expired.txt") }
it "constructs the correct rule set" do
answer = {
"or" => [
{ "username" => "blackmanx" },
{ "username" => "mainecoon" }
]
}
result = subject.send(:rules)
expect(result).to eq(answer)
end
end
context "expiration date unparseable" do
let(:filename) { fixture("ldap-config/text/expiration-date-unparseable.txt") }
it "raises an error" do
expect do
subject.send(:rules)
end.to raise_error(ArgumentError, /Invalid expiration date "FluffyKittens12345" in .+expiration-date-unparseable.txt/)
end
end
context "expiration predicate unparseable" do
let(:filename) { fixture("ldap-config/text/expiration-predicate-unparseable.txt") }
it "raises an error" do
expect do
subject.send(:rules)
end.to raise_error(ArgumentError, /Unparseable semicolon predicate "expiration = Fluffy Kittens 12345" in .+expiration-predicate-unparseable.txt!/)
end
end
end
end
describe "#parsed_data" do
it "returns a hash of parsed key-value pairs" do
result = subject.send(:parsed_data)
expect(result).to eq(
"description" =>{"=" => [{ key: "Example" }], "!=" => [], "&=" => []},
"management" => {"=" => [{ key: "MAINECOON" }], "!=" => [], "&=" => []}
)
end
context "with shorthand filter lines" do
let(:filename) { fixture("ldap-config/text/shorthand.txt") }
it "adjusts keys to support filter data structure" do
result = subject.send(:parsed_data)
answer = {
"description" => {"=" => [{ key: "A place for contractors and employees to hang out" }], "!=" => [], "&=" => []},
"filter_contractors" => {"=" => [{ key: "none" }], "!=" => [], "&=" => []},
"filter_pre-hires" => {"=" => [{ key: "all" }], "!=" => [], "&=" => []},
"username" => {"=" => [{ key: "pixiebob" }, { key: "russianblue" }], "!=" => [], "&=" => []}
}
expect(result).to eq(answer)
end
end
context "with shorthand filter lines causing duplication" do
let(:filename) { fixture("ldap-config/text/shorthand-duplicated.txt") }
it "raises due to the duplication" do
expect do
described_class.new(filename: filename)
end.to raise_error(%r{In .+/text/shorthand-duplicated.txt, filter_contractors cannot contain multiple entries when 'all' or 'none' is used!})
end
end
context "when a line is unparseable" do
let(:filename) { fixture("ldap-config/text/invalid.txt") }
it "raises an error" do
expect do
subject.send(:parsed_data)
end.to raise_error(RuntimeError, /Unparseable line "description: Example" in .+invalid.txt!/)
end
end
context "with the 'contractor' method used" do
let(:filename) { fixture("ldap-config/text/contractor.txt") }
it "raises an error" do
expect do
subject.send(:parsed_data)
end.to raise_error(RuntimeError, /Rule Error: contractor is not a valid function .+contractor.txt!/)
end
end
context "with the predicate containing expiration semicolons" do
let(:filename) { fixture("ldap-config/text/expiration-not-expired.txt") }
it "parses correctly" do
result = subject.send(:parsed_data)
answer = {
"description" => {"=" => [{ key: "Expiration rule testing" }], "!=" => [], "&=" => []},
"username" => {
"=" => [
{ key: "blackmanx", expiration: "2018-05-01" },
{ key: "russianblue", expiration: "2018-05-01" },
{ key: "mainecoon" }
],
"!=" => [],
"&=" => []
}
}
expect(result).to eq(answer)
end
end
context "with the predicate containing invalid semicolons" do
let(:filename) { fixture("ldap-config/text/invalid-semicolons.txt") }
it "raises an error" do
expect do
subject.send(:parsed_data)
end.to raise_error(ArgumentError, /Rule Error: Invalid semicolon predicate "expires" in .+invalid-semicolons.txt!/)
end
end
context "with the description containing semicolons" do
let(:filename) { fixture("ldap-config/text/semicolons-in-description.txt") }
it "parses correctly" do
result = subject.send(:parsed_data)
answer = {
"description" => {"=" => [{ key: "the; description; can; have; semicolons" }], "!=" => [], "&=" => []},
"username" => {"=" => [{ key: "mainecoon" }], "!=" => [], "&=" => []}
}
expect(result).to eq(answer)
end
end
end
end