|
1 | 1 | # frozen_string_literal: true |
2 | 2 | require_relative "../../../../spec_helper" |
3 | 3 |
|
| 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 | + |
4 | 6 | describe Entitlements::Data::Groups::Calculated::YAML do |
5 | 7 | let(:people_obj) { Entitlements::Data::People::YAML.new(filename: fixture("people.yaml")) } |
6 | 8 | let(:cache) { { people_obj: people_obj } } |
|
35 | 37 | end |
36 | 38 | end |
37 | 39 |
|
| 40 | + describe "#schema_version" do |
| 41 | + it "returns the version string when one is set" do |
| 42 | + filename = fixture("ldap-config/filters/no-filters-with-schema-version.yaml") |
| 43 | + subject = described_class.new(filename: filename) |
| 44 | + expect(subject.schema_version).to eq("entitlements/1.2.3") |
| 45 | + end |
| 46 | + |
| 47 | + it "returns the version string when one is set without the patch" do |
| 48 | + filename = fixture("ldap-config/filters/no-filters-with-schema-version-no-patch.yaml") |
| 49 | + subject = described_class.new(filename: filename) |
| 50 | + expect(subject.schema_version).to eq("entitlements/1.2") |
| 51 | + end |
| 52 | + |
| 53 | + it "returns the version string when one is set with just the major version" do |
| 54 | + filename = fixture("ldap-config/filters/no-filters-with-schema-version-major.yaml") |
| 55 | + subject = described_class.new(filename: filename) |
| 56 | + expect(subject.schema_version).to eq("entitlements/1") |
| 57 | + end |
| 58 | + |
| 59 | + it "returns the version string when one is set with just the major version (with v prefix)" do |
| 60 | + filename = fixture("ldap-config/filters/no-filters-with-schema-version-major-with-v.yaml") |
| 61 | + subject = described_class.new(filename: filename) |
| 62 | + expect(subject.schema_version).to eq("entitlements/v1") |
| 63 | + end |
| 64 | + |
| 65 | + |
| 66 | + it "returns the version string when one is set (with v prefix)" do |
| 67 | + filename = fixture("ldap-config/filters/no-filters-with-schema-version-with-v.yaml") |
| 68 | + subject = described_class.new(filename: filename) |
| 69 | + expect(subject.schema_version).to eq("entitlements/v1.2.3") |
| 70 | + end |
| 71 | + |
| 72 | + it "returns the default version when schema_version is undefined" do |
| 73 | + filename = fixture("ldap-config/filters/no-filters-description.yaml") |
| 74 | + subject = described_class.new(filename: filename) |
| 75 | + expect(subject.schema_version).to eq("entitlements/v1") |
| 76 | + end |
| 77 | + |
| 78 | + it "throws an error when an invalid schema_version string is provided" do |
| 79 | + filename = fixture("ldap-config/filters/no-filters-with-bad-schema-version.yaml") |
| 80 | + subject = described_class.new(filename: filename) |
| 81 | + expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/) |
| 82 | + end |
| 83 | + |
| 84 | + it "throws an error when the version string is missing the namespace" do |
| 85 | + filename = fixture("ldap-config/filters/no-filters-with-missing-version-namespace.yaml") |
| 86 | + subject = described_class.new(filename: filename) |
| 87 | + expect { subject.schema_version }.to raise_error(RuntimeError, /Invalid schema version format/) |
| 88 | + end |
| 89 | + end |
| 90 | + |
38 | 91 | describe "#initialize_filters" do |
39 | 92 | it "returns the default filter hash when no filters are defined" do |
40 | 93 | filename = fixture("ldap-config/filters/no-filters.yaml") |
|
232 | 285 | context "complex structure" do |
233 | 286 | let(:filename) { fixture("ldap-config/yaml/expiration-complex.yaml") } |
234 | 287 |
|
235 | | - 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 |
236 | 294 | answer = { |
237 | 295 | "or"=>[ |
238 | 296 | {"username"=>"peterbald"}, |
|
245 | 303 | expect(result).to eq(answer) |
246 | 304 | end |
247 | 305 | 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 |
248 | 404 | end |
249 | 405 | end |
250 | 406 | end |
0 commit comments