Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/code0/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def encryptor
ATTRIBUTES = %i[
start_date
end_date
grace_period_days
licensee
restrictions
options
Expand All @@ -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)
Expand All @@ -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] || {})
Expand Down
45 changes: 45 additions & 0 deletions spec/code0/license_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
licensee: { company: "Code0" },
start_date: start_date,
end_date: end_date,
grace_period_days: 0,
restrictions: { users: 1 },
options: {}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) }

Expand Down
Loading