Skip to content

Commit

Permalink
Merge pull request #2 from code0-tech/use-symbolized-keys
Browse files Browse the repository at this point in the history
Switch to symbolized keys in license data
  • Loading branch information
Taucher2003 authored May 23, 2024
2 parents eb0e7bc + 0391c1e commit 99d3122
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
20 changes: 12 additions & 8 deletions lib/code0/license.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def load(data)

decrypted_license = encryptor.decrypt(data)

new(JSON.parse(decrypted_license))
new(JSON.parse(decrypted_license, symbolize_names: true))
rescue JSON::ParserError
raise ValidationError, "License data is invalid JSON"
end
Expand Down Expand Up @@ -60,19 +60,23 @@ def initialize(data)
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 (!end_date || !end_date.is_a?(Date)) && !options[:allow_missing_end_date]

true
end

def in_active_time?
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"]
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

def restricted?(attribute)
restrictions.key?(attribute)
end

def data
ATTRIBUTES.to_h { |attr| [attr, send(attr)] }
end
Expand All @@ -82,15 +86,15 @@ def data
attr_writer(*ATTRIBUTES)

def assign_attributes(data)
%w[start_date end_date].each do |property|
%i[start_date end_date].each do |property|
value = data[property]
value = parse_date(data[property]) unless data[property].is_a?(Date)
send("#{property}=", value)
end

send("licensee=", data["licensee"])
send("restrictions=", data["restrictions"] || {})
send("options=", data["options"] || {})
send("licensee=", data[:licensee])
send("restrictions=", data[:restrictions] || {})
send("options=", data[:options] || {})
end

def parse_date(str)
Expand Down
4 changes: 3 additions & 1 deletion sig/code0/license.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ module Code0

def in_active_time?: -> bool

def restricted?: (Symbol) -> bool

def valid?: -> bool

private

def assign_attributes: (Hash[String, any]) -> void
def assign_attributes: (Hash[Symbol, any]) -> void

def parse_date: (String) -> Date?
end
Expand Down
54 changes: 32 additions & 22 deletions spec/code0/license_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
let(:license) { described_class.new(license_data) }
let(:default_license_data) do
{
"licensee" => { "company" => "Code0" },
"start_date" => "2024-05-01",
"end_date" => "2025-05-01",
"restrictions" => { "users" => 1 },
"options" => {}
licensee: { company: "Code0" },
start_date: "2024-05-01",
end_date: "2025-05-01",
restrictions: { users: 1 },
options: {}
}
end

Expand All @@ -24,10 +24,10 @@
it "data returns hash of attributes" do
expect(license.data).to match(
{
licensee: { "company" => "Code0" },
licensee: { company: "Code0" },
start_date: Date.new(2024, 5, 1),
end_date: Date.new(2025, 5, 1),
restrictions: { "users" => 1 },
restrictions: { users: 1 },
options: {}
}
)
Expand Down Expand Up @@ -82,39 +82,39 @@
it { is_expected.to be true }

context "when missing licensee" do
let(:license_data) { default_license_data.except("licensee") }
let(:license_data) { default_license_data.except(:licensee) }

it { is_expected.to be false }
end

context "when missing licensee is invalid" do
let(:license_data) { default_license_data.merge("licensee" => "") }
let(:license_data) { default_license_data.merge(licensee: "") }

it { is_expected.to be false }
end

context "when missing licensee is empty" do
let(:license_data) { default_license_data.merge("licensee" => {}) }
let(:license_data) { default_license_data.merge(licensee: {}) }

it { is_expected.to be false }
end

context "when missing start date" do
let(:license_data) { default_license_data.except("start_date") }
let(:license_data) { default_license_data.except(:start_date) }

it { is_expected.to be false }
end

context "when missing end date" do
let(:license_data) { default_license_data.except("end_date") }
let(:license_data) { default_license_data.except(:end_date) }

it { is_expected.to be false }
end

context "when missing end date with missing end date allowed" do
let(:license_data) do
data = default_license_data.except("end_date")
data["options"].merge!("allow_missing_end_date" => true)
data = default_license_data.except(:end_date)
data[:options].merge!(allow_missing_end_date: true)
data
end

Expand All @@ -126,53 +126,63 @@
subject { license.in_active_time? }

context "when start is after today" do
let(:license_data) { default_license_data.merge("start_date" => Date.today + 1) }
let(:license_data) { default_license_data.merge(start_date: Date.today + 1) }

it { is_expected.to be false }
end

context "when start is today" do
let(:license_data) { default_license_data.merge("start_date" => Date.today) }
let(:license_data) { default_license_data.merge(start_date: Date.today) }

it { is_expected.to be true }
end

context "when start is before today" do
let(:license_data) { default_license_data.merge("start_date" => Date.today - 1) }
let(:license_data) { default_license_data.merge(start_date: Date.today - 1) }

it { is_expected.to be true }
end

context "when end is after today" do
let(:license_data) { default_license_data.merge("end_date" => Date.today + 1) }
let(:license_data) { default_license_data.merge(end_date: Date.today + 1) }

it { is_expected.to be true }
end

context "when end is today" do
let(:license_data) { default_license_data.merge("end_date" => Date.today) }
let(:license_data) { default_license_data.merge(end_date: Date.today) }

it { is_expected.to be true }
end

context "when end is before today" do
let(:license_data) { default_license_data.merge("end_date" => Date.today - 1) }
let(:license_data) { default_license_data.merge(end_date: Date.today - 1) }

it { is_expected.to be false }
end

context "when end is not set" do
let(:license_data) { default_license_data.merge("end_date" => nil) }
let(:license_data) { default_license_data.merge(end_date: nil) }

it { is_expected.to be false }
end

context "when end is not set and missing end allowed" do
let(:license_data) do
default_license_data.merge("end_date" => nil, "options" => { "allow_missing_end_date" => true })
default_license_data.merge(end_date: nil, options: { allow_missing_end_date: true })
end

it { is_expected.to be true }
end
end

describe "#restricted?" do
it "returns true if restriction is set" do
expect(license.restricted?(:users)).to be true
end

it "returns fals if restriction is not set" do
expect(license.restricted?(:user_count)).to be false
end
end
end

0 comments on commit 99d3122

Please sign in to comment.