Skip to content

Commit 0e09396

Browse files
authored
Merge pull request #61 from github/yaml-exp-tests
Improved testing around yaml expirations 🧪
2 parents ff3219d + 639b395 commit 0e09396

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

spec/unit/entitlements/data/groups/calculated/yaml_spec.rb

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22
require_relative "../../../../spec_helper"
33

4+
# NOTE: The test suite mocks all dates with allow(Time).to receive(:now).and_return(Time.utc(2018, 4, 1, 12, 0, 0))
5+
46
describe Entitlements::Data::Groups::Calculated::YAML do
57
let(:people_obj) { Entitlements::Data::People::YAML.new(filename: fixture("people.yaml")) }
68
let(:cache) { { people_obj: people_obj } }
@@ -283,7 +285,12 @@
283285
context "complex structure" do
284286
let(:filename) { fixture("ldap-config/yaml/expiration-complex.yaml") }
285287

286-
it "constructs the correct rule set" do
288+
it "constructs the correct rule set with complex nested expiration" do
289+
# Expected results based on expiration-complex.yaml:
290+
# - username: peterbald (no expiration) -> kept
291+
# - and: group foo/bar (Sept 2018, not expired) and foo/baz (March 2018, expired) -> only foo/bar kept
292+
# - or: all usernames expired (March 2018) -> empty array
293+
# - or: cheetoh (March 2018, expired) and nebelung (Sept 2018, not expired) -> only nebelung kept
287294
answer = {
288295
"or"=>[
289296
{"username"=>"peterbald"},
@@ -296,6 +303,104 @@
296303
expect(result).to eq(answer)
297304
end
298305
end
306+
307+
context "individual username expiration" do
308+
let(:filename) { fixture("ldap-config/yaml/expiration-individual-usernames.yaml") }
309+
310+
it "filters out expired usernames while keeping non-expired ones" do
311+
answer = {
312+
"or" => [
313+
{ "username" => "alice" },
314+
{ "username" => "charlie" },
315+
{ "username" => "diana" }
316+
]
317+
}
318+
result = subject.send(:rules)
319+
expect(result).to eq(answer)
320+
end
321+
end
322+
323+
context "group expiration" do
324+
let(:filename) { fixture("ldap-config/yaml/expiration-groups.yaml") }
325+
326+
it "filters out expired groups while keeping non-expired ones" do
327+
answer = {
328+
"or" => [
329+
{ "group" => "team/active" },
330+
{ "group" => "team/future" },
331+
{ "username" => "standalone" }
332+
]
333+
}
334+
result = subject.send(:rules)
335+
expect(result).to eq(answer)
336+
end
337+
end
338+
339+
context "mixed expiration with nested structures" do
340+
let(:filename) { fixture("ldap-config/yaml/expiration-mixed-nested.yaml") }
341+
342+
it "correctly handles expiration in nested and/or structures" do
343+
answer = {
344+
"or" => [
345+
{ "username" => "always-active" },
346+
{ "and" => [
347+
{ "group" => "team/core" }
348+
]
349+
},
350+
{ "or" => [
351+
{ "username" => "still-active" }
352+
]
353+
}
354+
]
355+
}
356+
result = subject.send(:rules)
357+
expect(result).to eq(answer)
358+
end
359+
end
360+
361+
context "all individual entries expired" do
362+
let(:filename) { fixture("ldap-config/yaml/expiration-all-individual-expired.yaml") }
363+
364+
it "returns empty arrays for containers with all expired entries" do
365+
answer = {
366+
"or" => []
367+
}
368+
result = subject.send(:rules)
369+
expect(result).to eq(answer)
370+
end
371+
end
372+
373+
context "expired entries but expirations are disabled" do
374+
let(:filename) { fixture("ldap-config/yaml/expiration-ignore-test.yaml") }
375+
376+
it "ignores all expiration dates when ignore_expirations is true" do
377+
begin
378+
Entitlements.config["ignore_expirations"] = true
379+
380+
answer = {
381+
"or" => [
382+
{ "username" => "active-user" },
383+
{ "username" => "expired-user" },
384+
{ "group" => "expired-group" }
385+
]
386+
}
387+
result = subject.send(:rules)
388+
expect(result).to eq(answer)
389+
ensure
390+
Entitlements.config.delete("ignore_expirations")
391+
end
392+
end
393+
end
394+
395+
context "invalid expiration date" do
396+
let(:filename) { fixture("ldap-config/yaml/expiration-invalid-date.yaml") }
397+
398+
it "raises an error for invalid expiration date format" do
399+
expect do
400+
subject.send(:rules)
401+
end.to raise_error(ArgumentError, /Invalid expiration date "not-a-date"/)
402+
end
403+
end
299404
end
300405
end
301406
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
description: All individual entries expired
3+
rules:
4+
or:
5+
- username: expired1
6+
expiration: "2018-02-01"
7+
- username: expired2
8+
expiration: "2018-02-01"
9+
- group: team/expired
10+
expiration: "2018-02-01"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
description: Group expiration test
3+
rules:
4+
or:
5+
- group: team/active
6+
- group: team/expired
7+
expiration: "2018-02-01"
8+
- group: team/future
9+
expiration: "2018-06-01"
10+
- username: standalone
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
description: Test with expired entries but ignoring expiration
3+
rules:
4+
or:
5+
- username: active-user
6+
- username: expired-user
7+
expiration: "2018-02-01"
8+
- group: expired-group
9+
expiration: "2018-02-01"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
description: Individual username expiration test
3+
rules:
4+
or:
5+
- username: alice
6+
- username: bob
7+
expiration: "2018-02-01"
8+
- username: charlie
9+
expiration: "2018-06-01"
10+
- username: diana
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
description: Test with invalid expiration date
3+
rules:
4+
or:
5+
- username: valid-user
6+
- username: invalid-expiry
7+
expiration: "not-a-date"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: Mixed expiration test with nested structures
3+
rules:
4+
or:
5+
- username: always-active
6+
- and:
7+
- group: team/core
8+
- username: temp-user
9+
expiration: "2018-02-01"
10+
- or:
11+
- username: expired1
12+
expiration: "2018-02-01"
13+
- username: expired2
14+
expiration: "2018-02-01"
15+
- username: still-active
16+
expiration: "2018-06-01"

0 commit comments

Comments
 (0)