Skip to content

Commit

Permalink
WIP: increasing test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Dec 31, 2018
1 parent cd4251f commit 4b623c4
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 2 deletions.
39 changes: 37 additions & 2 deletions test/lib/y2firewall/widgets/pages/zones_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,46 @@
describe Y2Firewall::Widgets::Pages::Zones::RemoveButton do
subject { described_class.new(double("pager"), double("table", value: "my_zone")) }

let(:known_zones) { { "dmz" => "DMZ" } }
let(:zone_to_remove) { double(name: "my_zone") }

before do
allow(Y2Firewall::Dialogs::Zone).to receive(:run)

allow(subject.firewall).to receive(:find_zone).and_return(double(name: "my_zone"))
allow(Y2Firewall::Firewalld::Zone).to receive(:known_zones).and_return(known_zones)
allow(subject.firewall).to receive(:find_zone).and_return(zone_to_remove)
end

include_examples "CWM::PushButton"

describe "#handle" do
context "when trying to remove a unkown zone" do
it "removes the zone" do
expect(subject.firewall).to receive(:remove_zone).with(zone_to_remove.name)

subject.handle
end

it "returns :redraw" do
expect(subject.handle).to eq(:redraw)
end
end

context "when trying to remove a known zone" do
let(:zone_to_remove) { double(name: "dmz") }

before do
allow(Yast2::Popup).to receive(:show)
end

it "shows an error popup" do
expect(Yast2::Popup).to receive(:show).with(anything, hash_including(headline: :error))

subject.handle
end

it "returns nil" do
expect(subject.handle).to be_nil
end
end
end
end
242 changes: 242 additions & 0 deletions test/lib/y2firewall/widgets/zone_widgets_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
#!/usr/bin/env rspec
# encoding: utf-8

# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../test_helper"

require "cwm/rspec"
require "y2firewall/firewalld/interface"
require "y2firewall/widgets/zone"

describe Y2Firewall::Dialogs::NameWidget do
subject(:widget) { described_class.new(eth0, disabled: disabled) }

let(:eth0) { Y2Firewall::Firewalld::Interface.new("eth0") }
let(:disabled) { false }
let(:value) { "zone_name" }

before do
allow(subject).to receive(:value).and_return(value)
end

include_examples "CWM::AbstractWidget"

describe "#init" do
it "sets the value with interface name" do
expect(subject).to receive(:value=).with("eth0")

subject.init
end

context "when widget was created enabled" do
xit "returns true" do
expect(subject.init).to be(true)
end
end

context "when widget was created disabled" do
let(:disabled) { true }

xit "returns false" do
expect(subject.init).to be(false)
end
end
end

describe "#validate" do
context "when value only contains alphanumeric chars" do
it "returns true" do
expect(subject.validate).to be(true)
end
end

context "when value has any not alphanumeric char" do
let(:value) { "Invalid zone name" }

before do
allow(Yast::Report).to receive(:Error)
allow(Yast::UI).to receive(:SetFocus)
end

it "reports an error" do
expect(Yast::Report).to receive(:Error).with(/alphanumeric/)

subject.validate
end

it "receives the focus" do
expect(Yast::UI).to receive(:SetFocus).with(Id(subject.widget_id))

subject.validate
end

it "returns false" do
expect(subject.validate).to be(false)
end
end
end

describe "#store" do
it "sets value as zone name" do
expect(eth0).to receive(:name=).with(value)

subject.store
end
end

describe("#focus") do
it "sets focus on itself" do
expect(Yast::UI).to receive(:SetFocus).with(Id(subject.widget_id))

subject.focus
end
end
end

describe Y2Firewall::Dialogs::DescriptionWidget do
subject(:widget) { described_class.new(eth0) }

let(:eth0) { Y2Firewall::Firewalld::Interface.new("eth0") }
let(:initial_value) { "The inicial zone desription" }
let(:new_value) { "A new zone description" }

before do
allow(subject).to receive(:value).and_return(new_value)
allow(eth0).to receive(:description).and_return(initial_value)
end

include_examples "CWM::AbstractWidget"

describe "#init" do
it "sets the value with the interface description" do
expect(subject).to receive(:value=).with(initial_value)

subject.init
end
end

describe "#validate" do
context "when value is not empty" do
it "returns true" do
expect(subject.validate).to be(true)
end
end

context "when value is empty" do
let(:new_value) { "" }

before do
allow(Yast::Report).to receive(:Error)
allow(Yast::UI).to receive(:SetFocus)
end

it "reports an error" do
expect(Yast::Report).to receive(:Error).with(/provide a description/)

subject.validate
end

it "receives the focus" do
expect(Yast::UI).to receive(:SetFocus).with(Id(subject.widget_id))

subject.validate
end

it "returns false" do
expect(subject.validate).to be(false)
end
end
end

describe "#store" do
it "sets value as zone description" do
expect(eth0).to receive(:description=).with(new_value)

subject.store
end
end

describe("#focus") do
it "sets focus on itself" do
expect(Yast::UI).to receive(:SetFocus).with(Id(subject.widget_id))

subject.focus
end
end
end

describe Y2Firewall::Dialogs::TargetWidget do
subject(:widget) { described_class.new(eth0) }

let(:eth0) { Y2Firewall::Firewalld::Interface.new("eth0") }

include_examples "CWM::ComboBox"

describe "#init" do
before do
allow(eth0).to receive(:target).and_return(target)
end

context "when zone has a target" do
let(:target) { :accept }

it "sets value to target" do
expect(subject).to receive(:value=).with(target)

subject.init
end
end

context "when zone does not have a target" do
let(:target) { nil }

it "sets value to 'default'" do
expect(subject).to receive(:value=).with("default")

subject.init
end
end
end

describe "#store" do
it "sets the zone target" do
expect(eth0).to receive(:target=).with(subject.value)

subject.store
end
end
end

describe Y2Firewall::Dialogs::MasqueradeWidget do
subject(:widget) { described_class.new(eth0) }

let(:eth0) { Y2Firewall::Firewalld::Interface.new("eth0") }

include_examples "CWM::CheckBox"

describe "#store" do
it "sets the zone masquerade" do
expect(eth0).to receive(:masquerade=).with(subject.value)

subject.store
end
end
end

0 comments on commit 4b623c4

Please sign in to comment.