diff --git a/lib/code0/license.rb b/lib/code0/license.rb index a0a78d9..8b0018d 100644 --- a/lib/code0/license.rb +++ b/lib/code0/license.rb @@ -47,6 +47,7 @@ def encryptor ATTRIBUTES = %i[ start_date end_date + grace_period_days licensee restrictions options @@ -61,16 +62,17 @@ def valid? return false if !licensee || !licensee.is_a?(Hash) || licensee.empty? return false if !start_date || !start_date.is_a?(Date) return false if (!end_date || !end_date.is_a?(Date)) && !options[:allow_missing_end_date] + return false if grace_period_days.nil? || !grace_period_days.is_a?(Integer) true end - def in_active_time? + def in_active_time?(allow_grace_period: true) return false if start_date > Date.today return true if !end_date && options[:allow_missing_end_date] return false if !end_date && !options[:allow_missing_end_date] - end_date >= Date.today + end_date + (allow_grace_period ? grace_period_days : 0) >= Date.today end def restricted?(attribute) @@ -92,6 +94,7 @@ def assign_attributes(data) send("#{property}=", value) end + send("grace_period_days=", data[:grace_period_days] || 0) send("licensee=", data[:licensee]) send("restrictions=", data[:restrictions] || {}) send("options=", data[:options] || {}) diff --git a/spec/code0/license_spec.rb b/spec/code0/license_spec.rb index 07fdde1..8c47d59 100644 --- a/spec/code0/license_spec.rb +++ b/spec/code0/license_spec.rb @@ -30,6 +30,7 @@ licensee: { company: "Code0" }, start_date: start_date, end_date: end_date, + grace_period_days: 0, restrictions: { users: 1 }, options: {} } @@ -123,6 +124,24 @@ it { is_expected.to be true } end + + context "when grace_period_days is not an integer" do + let(:license_data) { default_license_data.merge(grace_period_days: "5") } + + it { is_expected.to be false } + end + + context "when grace_period_days is an integer" do + let(:license_data) { default_license_data.merge(grace_period_days: 5) } + + it { is_expected.to be true } + end + + context "when grace_period_days is zero" do + let(:license_data) { default_license_data.merge(grace_period_days: 0) } + + it { is_expected.to be true } + end end describe "#in_active_time?" do @@ -164,6 +183,32 @@ it { is_expected.to be false } end + context "when end is before today but within grace period" do + let(:license_data) { default_license_data.merge(end_date: Date.today - 3, grace_period_days: 5) } + + it { is_expected.to be true } + end + + context "when end is before today and exactly at grace period boundary" do + let(:license_data) { default_license_data.merge(end_date: Date.today - 5, grace_period_days: 5) } + + it { is_expected.to be true } + end + + context "when end is before today and past grace period" do + let(:license_data) { default_license_data.merge(end_date: Date.today - 6, grace_period_days: 5) } + + it { is_expected.to be false } + end + + context "when end is before today within grace period but grace period disabled" do + subject { license.in_active_time?(allow_grace_period: false) } + + let(:license_data) { default_license_data.merge(end_date: Date.today - 3, grace_period_days: 5) } + + it { is_expected.to be false } + end + context "when end is not set" do let(:license_data) { default_license_data.merge(end_date: nil) }