Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from yast/caasp_merge
Caasp merge
- Loading branch information
Showing
3 changed files
with
356 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
# encoding: utf-8 | ||
|
||
# ------------------------------------------------------------------------------ | ||
# Copyright (c) 2017 SUSE LLC | ||
# | ||
# | ||
# 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. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program; if not, contact SUSE. | ||
# | ||
# To contact SUSE about this file by physical or electronic mail, you may find | ||
# current contact information at www.suse.com. | ||
# ------------------------------------------------------------------------------ | ||
|
||
require "yast" | ||
require "cwm/widget" | ||
require "uri" | ||
require "registration/registration_ui" | ||
require "registration/sw_mgmt" | ||
|
||
module Registration | ||
module Widgets | ||
class RegistrationCode < CWM::InputField | ||
VALID_URL_SCHEMES = ["http", "https"].freeze | ||
|
||
def initialize | ||
textdomain "registration" | ||
end | ||
|
||
def label | ||
_("Registration Code or SMT Server URL") | ||
end | ||
|
||
# Initialize the widget with stored values | ||
def init | ||
reg_code = options.reg_code.to_s | ||
|
||
self.value = reg_code.empty? ? init_url : options.reg_code | ||
end | ||
|
||
# Set registration options according to the value and try to register the | ||
# system. | ||
def store | ||
if valid_url? | ||
options.reg_code = "" | ||
options.custom_url = value | ||
else | ||
options.reg_code = value | ||
options.custom_url = default_url | ||
end | ||
|
||
register | ||
end | ||
|
||
# Try to register the system against SCC or a custom SMT depending on if | ||
# the value is an URL or not. | ||
# | ||
# @return [Boolean] false if not attempted or failed and true if success | ||
def register | ||
if skip? | ||
log.info("Empty value, skipping registration") | ||
return false | ||
end | ||
|
||
if Registration.is_registered? | ||
log.info("The system is already registered so skipped registration.") | ||
return false | ||
end | ||
|
||
if !SwMgmt.find_base_product | ||
Helpers.report_no_base_product | ||
return false | ||
end | ||
|
||
log.info("Registering the system and the base product.") | ||
# Error reports and logs about the registration are mostly handled | ||
# by ConnectHelpers.catch_registration_errors and used by instances | ||
# of RegistrationUI. | ||
return false if !register_system_and_base_product | ||
|
||
Storage::InstallationOptions.instance.base_registered = true | ||
end | ||
|
||
def validate | ||
(url? & !valid_url?) ? error(_("Not valid url.")) : true | ||
end | ||
|
||
def help | ||
_( | ||
"<p>\n" \ | ||
"The SMT Server URL must use http or https protocol, " \ | ||
"other schemes are not supported." \ | ||
"</p>\n" | ||
) | ||
end | ||
|
||
private | ||
|
||
def error(message) | ||
Yast::Popup.Error(message) | ||
|
||
false | ||
end | ||
|
||
def url? | ||
return false if value.to_s.empty? | ||
|
||
uri = URI(value) | ||
uri.scheme ? true : false | ||
rescue URI::InvalidURIError | ||
false | ||
end | ||
|
||
def valid_url? | ||
return false if value.to_s.empty? | ||
|
||
uri = URI(value) | ||
VALID_URL_SCHEMES.include?(uri.scheme) | ||
rescue URI::InvalidURIError | ||
false | ||
end | ||
|
||
# run the system and the base product registration | ||
# @return [Boolean] true on success | ||
def register_system_and_base_product | ||
url = UrlHelpers.registration_url | ||
return false if UrlHelpers.registration_url == :cancel | ||
|
||
registration = Registration.new(url) | ||
registration_ui = RegistrationUI.new(registration) | ||
|
||
success, product_service = registration_ui.register_system_and_base_product | ||
|
||
if product_service && !registration_ui.install_updates? | ||
registration_ui.disable_update_repos(product_service) | ||
end | ||
|
||
success | ||
end | ||
|
||
# Default registration server | ||
# | ||
# The boot_url takes precedence over the SUSE::Connect default | ||
# one. | ||
# | ||
# @return [String] URL for the registration server | ||
def default_url | ||
boot_url || SUSE::Connect::Config.new.url | ||
end | ||
|
||
def init_url | ||
case options.custom_url | ||
when nil, "", SUSE::Connect::Config.new.url | ||
boot_url | ||
else | ||
options.custom_url | ||
end | ||
end | ||
|
||
# Registration server URL given through Linuxrc | ||
# | ||
# @return [String,nil] URL for the registration server; nil if not given. | ||
def boot_url | ||
UrlHelpers.boot_reg_url | ||
end | ||
|
||
# Skip registration if the value is empty or nil | ||
def skip? | ||
value.to_s.empty? | ||
end | ||
|
||
def options | ||
Storage::InstallationOptions.instance | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#!/usr/bin/env rspec | ||
|
||
require_relative "spec_helper" | ||
require "registration/widgets/registration_code" | ||
|
||
describe Registration::Widgets::RegistrationCode do | ||
let(:options) { Registration::Storage::InstallationOptions.instance } | ||
let(:base_product) { Registration::SwMgmt::FAKE_BASE_PRODUCT } | ||
|
||
before do | ||
allow(options).to receive(:reg_code).and_return("") | ||
allow(options).to receive(:custom_url).and_return(nil) | ||
allow(subject).to receive(:boot_url).and_return(nil) | ||
end | ||
|
||
it "has help text" do | ||
expect(subject.help).to_not be_empty | ||
end | ||
|
||
context "initialization" do | ||
context "when a previous registration code exists" do | ||
it "initializes the widget with it" do | ||
allow(options).to receive(:reg_code).and_return("previous_code") | ||
expect(subject).to receive(:value=).with("previous_code") | ||
|
||
subject.init | ||
end | ||
end | ||
|
||
context "when no previous registration code exists" do | ||
it "initializes the widget with custom url if exists" do | ||
allow(options).to receive(:reg_code).and_return("") | ||
allow(options).to receive(:custom_url).and_return("http://smt.example.com") | ||
|
||
expect(subject).to receive(:value=).with("http://smt.example.com") | ||
|
||
subject.init | ||
end | ||
|
||
it "initializes the widget with the cmdline boot url if no custom url" do | ||
allow(options).to receive(:reg_code).and_return("") | ||
allow(options).to receive(:custom_url).and_return(nil) | ||
allow(subject).to receive(:boot_url).and_return("http://boot.example.de") | ||
expect(subject).to receive(:value=).with("http://boot.example.de") | ||
|
||
subject.init | ||
end | ||
|
||
end | ||
end | ||
|
||
context "validation" do | ||
it "reports an error in case of a url but not valid one" do | ||
allow(subject).to receive(:value).and_return("ftp://smt.example.com") | ||
expect(subject).to receive(:error).and_return(false) | ||
|
||
expect(subject.validate).to eq false | ||
end | ||
end | ||
|
||
context "store" do | ||
context "when the value is empty or not an URL" do | ||
before do | ||
allow(subject).to receive(:valid_url?).and_return(false) | ||
allow(options).to receive(:custom_url=) | ||
allow(options).to receive(:reg_code=) | ||
allow(subject).to receive(:register) | ||
allow(subject).to receive(:default_url).and_return("default_url") | ||
end | ||
|
||
it "stores the current value" do | ||
allow(subject).to receive(:value).and_return(nil) | ||
expect(options).to receive(:reg_code=).with(nil) | ||
|
||
subject.store | ||
end | ||
|
||
it "stores as the custom url the default one" do | ||
allow(subject).to receive(:value).and_return("871263") | ||
expect(options).to receive(:reg_code=).with("871263") | ||
expect(options).to receive(:custom_url=).with("default_url") | ||
|
||
subject.store | ||
end | ||
|
||
it "tries to register to the default url" do | ||
expect(subject).to receive(:register) | ||
|
||
subject.store | ||
end | ||
end | ||
|
||
context "when the value is a valid URL" do | ||
it "stores the custom url and an empty registration code" do | ||
valid_url = "http://smt.example.com" | ||
allow(subject).to receive(:register) | ||
allow(subject).to receive(:value).and_return(valid_url) | ||
expect(options).to receive(:reg_code=).with("") | ||
expect(options).to receive(:custom_url=).with(valid_url) | ||
|
||
subject.store | ||
end | ||
|
||
it "tries to register to the given URL" do | ||
allow(options).to receive(:reg_code=) | ||
allow(options).to receive(:custom_url=) | ||
expect(subject).to receive(:register) | ||
|
||
subject.store | ||
end | ||
end | ||
end | ||
|
||
describe "#register" do | ||
before do | ||
allow(subject).to receive(:skip?).and_return(false) | ||
allow(Registration::Registration).to receive(:is_registered?).and_return(false) | ||
allow(Registration::SwMgmt).to receive(:find_base_product).and_return(base_product) | ||
end | ||
|
||
it "skips registration if empty and returns false" do | ||
allow(subject).to receive(:skip?).and_call_original | ||
expect(subject).to receive(:value).and_return("") | ||
expect(Registration::Registration).not_to receive(:is_registered?) | ||
|
||
expect(subject.register).to eq false | ||
end | ||
|
||
it "skips registration if already registered and returns false" do | ||
expect(Registration::Registration).to receive(:is_registered?).and_return(true) | ||
expect(subject).not_to receive(:register_system_and_base_product) | ||
|
||
expect(subject.register).to eq false | ||
end | ||
|
||
it "skips registration if not base product, reports and error and returns false" do | ||
expect(Registration::Registration).to receive(:is_registered?).and_return(false) | ||
expect(Registration::SwMgmt).to receive(:find_base_product).and_return(nil) | ||
expect(Registration::Helpers).to receive(:report_no_base_product) | ||
expect(subject).not_to receive(:register_system_and_base_product) | ||
|
||
expect(subject.register).to eq false | ||
end | ||
|
||
it "returns false if registration fails" do | ||
expect(subject).to receive(:register_system_and_base_product).and_return(false) | ||
|
||
expect(subject.register).to eq false | ||
end | ||
|
||
it "saves the state of the system registration if success and returns true" do | ||
expect(subject).to receive(:register_system_and_base_product).and_return(true) | ||
expect(options).to receive(:base_registered=).with(true) | ||
|
||
expect(subject.register).to eq true | ||
end | ||
end | ||
end |