Skip to content

Commit

Permalink
Add a Y2Packager::ProductLicense class
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Apr 4, 2018
1 parent 1f38011 commit 827bc2d
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 5 deletions.
9 changes: 6 additions & 3 deletions library/packages/src/lib/y2packager/license.rb
Expand Up @@ -43,7 +43,7 @@ def find(product_name, source)
return cache[product_name] if cache[product_name]

# This could be done in the constructor.
fetcher = LicensesFetchers.for(source)
fetcher = LicensesFetchers.for(source, product_name)
new_license = License.new(fetcher)
return unless new_license.id

Expand Down Expand Up @@ -74,9 +74,12 @@ def initialize(fetcher)
#
# This identifier is based on the given default language translation.
#
# @return [String] Unique identifier
# @return [String,nil] Unique identifier; nil if the license was not found.
def id
@id ||= Digest::MD5.hexdigest(content_for(DEFAULT_LANG))
return @id if @id
content = content_for(DEFAULT_LANG)
return unless content
@id = Digest::MD5.hexdigest(content_for(DEFAULT_LANG))
end

# Return the license translated content for the given language
Expand Down
5 changes: 3 additions & 2 deletions library/packages/src/lib/y2packager/licenses_fetchers.rb
Expand Up @@ -19,8 +19,9 @@ module LicensesFetchers
# Return the licenses proper fetcher for a given source
#
# @param source [Symbol] :rpm or :url
def self.for(source)
const_get(source.to_s.capitalize)
def self.for(source, product_name)
klass = const_get(source.to_s.capitalize)
klass.new(product_name)
end
end
end
77 changes: 77 additions & 0 deletions library/packages/src/lib/y2packager/product_license.rb
@@ -0,0 +1,77 @@
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require "yast"
require "forwardable"
require "y2packager/license"

module Y2Packager
class ProductLicense
# This class holds the license stuff for a given product
#
# Why a separate ProductLicense class? First of all, we wanted to extract
# the license handling from Y2Packager::Product and moving this logic to
# Y2Packager::License was not a good idea because different products could
# share the same license. Additionally, this class offers an API to work
# with licenses with a proper Product or Addon object is not available
# (backward compatibility reasons).
#
# @see Y2Packager::Product
# @see Y2Packager::License

extend Forwardable
def_delegators :@license, :content_for, :locales, :accept!, :reject!

attr_reader :license

class << self
# Find license for a given product
#
# @param product_name [String] Product's name
# @param source [:rpm,:url] Source to get the license from. For the time being,
# only :rpm is really supported.
# @return [ProductLicense]

def find(product_name, source = :rpm)
return cache[product_name] if cache[product_name]
license = License.find(product_name, source)
return nil unless license
cache[product_name] = ProductLicense.new(product_name, license)
end

# Clear product licenses cache
def clear_cache
@cache = nil
end

private def cache
@cache ||= {}
end
end

# Constructor
#
# @param product_name [String] Product name to get licenses for
# @param source [Symbol] Source to use for fetching the license from
def initialize(product_name, license)
@product_name = product_name
@license = license
end

# Determines whether the license have been accepted or not
#
# @return [Boolean] true if the license has been accepted; false otherwise.
def accepted?
license.accepted?
end
end
end
10 changes: 10 additions & 0 deletions library/packages/test/y2packager/license_test.rb
Expand Up @@ -63,6 +63,16 @@
it "returns the license unique identifier" do
expect(license.id).to eq("9a0364b9e99bb480dd25e1f0284c8555")
end

context "when the license is not found" do
before do
allow(fetcher).to receive(:license_content).and_return(nil)
end

it "returns nil" do
expect(license.id).to be_nil
end
end
end

describe "#content_for" do
Expand Down
92 changes: 92 additions & 0 deletions library/packages/test/y2packager/product_license_test.rb
@@ -0,0 +1,92 @@
#!/usr/bin/env rspec
# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC, All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# ------------------------------------------------------------------------------

require_relative "../test_helper"

require "y2packager/product_license"

describe Y2Packager::ProductLicense do
subject(:product_license) { Y2Packager::ProductLicense.new(product_name, license) }

let(:license) { instance_double(Y2Packager::License) }
let(:product_name) { "SLES" }

before do
described_class.clear_cache
end

describe ".find" do
before do
allow(Y2Packager::License).to receive(:find).and_return(license)
end

it "returns a product license for the given product" do
expect(Y2Packager::License).to receive(:find).with("SLES", :rpm).and_return(license)
product_license = described_class.find("SLES", :rpm)
expect(product_license).to be_a(Y2Packager::ProductLicense)
expect(product_license.license).to eq(license)
end

context "when the product license was already built" do
it "returns the already built instance" do
cached_product_license = described_class.find("SLES", :rpm)
product_license = described_class.find("SLES", :rpm)
expect(product_license).to be(cached_product_license)
end
end

context "when license does not have an id" do
it "returns nil"
end
end

describe "#content_for" do
it "delegates to License#content_for" do
expect(license).to receive(:content_for).with("es_ES").and_return("contenido")
expect(product_license.content_for("es_ES")).to eq("contenido")
end
end

describe "#locales" do
it "delegates to License#locales" do
expect(license).to receive(:locales).and_return(["en_US", "de_DE"])
expect(product_license.locales).to eq(["en_US", "de_DE"])
end
end

describe "#accept!" do
it "delegates to License#accept!" do
expect(license).to receive(:accept!)
product_license.accept!
end
end

describe "#reject!" do
it "delegates to License#reject!" do
expect(license).to receive(:reject!)
product_license.reject!
end
end

describe "#accepted?" do
context "if the product license has been accepted" do
it "returns true"
end

context "if the product license has been accepted" do
it "returns false"
end

context "synchronizes the license status"
end
end

0 comments on commit 827bc2d

Please sign in to comment.