Skip to content

Commit

Permalink
testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
schubi2 committed Apr 17, 2024
1 parent 2e4fe5d commit 76ab89b
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 119 deletions.
7 changes: 3 additions & 4 deletions src/lib/y2storage/dialogs/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ def encryption_error
!@proposal.settings.encryption_method.nil? &&
@proposal.settings.encryption_password.nil?
ret = Yast::HTML.Para(
_("Missing encryption password - Proposal has been done without encryption.")
) +
Yast::HTML.Newline +
_("Please use \"Guided Setup\" in order to set the password or to disable encryption.")
_("Missing encryption password - Proposal has been done without encryption.") +
Yast::HTML.Newline +
_("Please use \"Guided Setup\" in order to set the password or to disable encryption."))
ret = Yast::HTML.Colorize(ret, "red")
end
ret
Expand Down
4 changes: 2 additions & 2 deletions test/support/widgets_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ def expect_not_select(id, value: true)
end

def expect_enable(id)
expect(Yast::UI).to receive(:ChangeWidget).once.with(Id(id), :Enabled, true)
expect(Yast::UI).to receive(:ChangeWidget).with(Id(id), :Enabled, true)
end

def expect_not_enable(id)
expect(Yast::UI).not_to receive(:ChangeWidget).with(Id(id), :Enabled, true)
end

def expect_disable(id)
expect(Yast::UI).to receive(:ChangeWidget).once.with(Id(id), :Enabled, false)
expect(Yast::UI).to receive(:ChangeWidget).with(Id(id), :Enabled, false)
end

def select_widget(id, value: true)
Expand Down
267 changes: 154 additions & 113 deletions test/y2storage/dialogs/guided_setup/select_scheme_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env rspec
# coding: utf-8
# Copyright (c) [2017] SUSE LLC
#
# All Rights Reserved.
Expand Down Expand Up @@ -47,7 +48,7 @@
describe "#run" do
let(:password) { "" }
let(:repeat_password) { password }
let(:encryption_method) { :luks1 }
let(:encryption_method) { :luks2 }

context "when settings has not LVM" do
before do
Expand Down Expand Up @@ -135,162 +136,202 @@
end
end

context "when settings has not encryption password" do
before do
settings.encryption_password = nil
end
describe "checkbox for #encryption" do
context "when settings has not encryption password" do
before do
settings.encryption_password = nil
end

it "does not select encryption by default" do
expect_not_select(:encryption)
subject.run
end
end
context "and encryption method is not set" do
before do
settings.encryption_method = nil
end

context "when settings has encryption password" do
before do
settings.encryption_password = "12345678"
end
it "does not select encryption by default" do
expect_not_select(:encryption)
subject.run
end
end

it "selects encryption by default" do
expect_select(:encryption)
subject.run
end
end
context "and encryption method is set" do
before do
settings.encryption_method = Y2Storage::EncryptionMethod::LUKS2
end

context "when encryption is not selected" do
before do
settings.encryption_password = "12345678"
not_select_widget(:encryption)
it "selects encryption by default" do
expect_select(:encryption)
subject.run
end
end
end

it "disables password fields" do
expect_disable(:password)
expect_disable(:repeat_password)
subject.run
end
context "when settings has encryption password" do
before do
settings.encryption_password = "12345678"
end

it "sets password to nil" do
subject.run
expect(settings.encryption_password).to be_nil
end
end
context "and encryption method is not set" do
before do
settings.encryption_method = nil
end

context "when encryption is selected" do
before do
select_widget(:encryption)
select_widget(:password, value: password)
select_widget(:repeat_password, value: repeat_password)
select_widget(:encryption_method, value: encryption_method)
settings.encryption_password = nil
end
it "selects encryption by default" do
expect_select(:encryption)
subject.run
end
end

it "enables password fields" do
expect_enable(:password)
expect_enable(:repeat_password)
subject.run
context "and encryption method is set" do
before do
settings.encryption_method = Y2Storage::EncryptionMethod::LUKS2
end

it "selects encryption by default" do
expect_select(:encryption)
subject.run
end
end
end

context "and password is valid" do
let(:password) { "Val1d_pass" }
context "when encryption is not selected" do
before do
settings.encryption_password = "12345678"
not_select_widget(:encryption)
end

it "does not show an error message" do
expect(Yast::Report).not_to receive(:Warning)
it "disables password, encryption fields and " do
expect_disable(:password)
expect_disable(:repeat_password)
expect_disable(:encryption_method)
expect_disable(:encryption_pbkdf)
subject.run
end

it "saves password in settings" do
it "sets password to nil" do
subject.run
expect(subject.settings.encryption_password).to eq(password)
expect(settings.encryption_password).to be_nil
end
end

context "but password is missing" do
let(:password) { "" }
include_examples("wrong password")
end

context "but passwords do not match" do
let(:password) { "pass1" }
let(:repeat_password) { "pass2" }
include_examples("wrong password")
end

context "but password is short" do
let(:password) { "pass" }
include_examples("wrong password")
end

context "but password contains forbidden chars" do
let(:password) { "pássw0rd1" }
include_examples("wrong password")
end

context "and password is weak" do
context "when encryption is selected" do
before do
allow(Yast::InstExtensionImage).to receive(:LoadExtension)
.with(/cracklib/, anything).and_return(true)
allow(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".crack"), password)
.and_return("an error message")
allow(Yast::Popup).to receive(:AnyQuestion).and_return(password_accepted)
select_widget(:encryption)
select_widget(:password, value: password)
select_widget(:repeat_password, value: repeat_password)
select_widget(:encryption_method, value: encryption_method)
settings.encryption_password = nil
end

let(:password) { "12345678" }
let(:password_accepted) { false }

it "shows an error message" do
expect(Yast::Popup).to receive(:AnyQuestion)
it "enables password, encryption method fields" do
expect_enable(:password)
expect_enable(:repeat_password)
expect_enable(:encryption_method)
subject.run
end

context "and password is accepted" do
let(:password_accepted) { true }
context "and password is valid" do
let(:password) { "Val1d_pass" }

it "does not show an error message" do
expect(Yast::Report).not_to receive(:Warning)
subject.run
end

it "saves password in settings" do
subject.run
expect(subject.settings.encryption_password).to eq(password)
end
end

context "and password is not accepted" do
context "but password is missing" do
let(:password) { "" }
include_examples("wrong password")
end

context "but passwords do not match" do
let(:password) { "pass1" }
let(:repeat_password) { "pass2" }
include_examples("wrong password")
end

context "but password is short" do
let(:password) { "pass" }
include_examples("wrong password")
end

context "but password contains forbidden chars" do
let(:password) { "pássw0rd1" }
include_examples("wrong password")
end

context "and password is weak" do
before do
allow(Yast::InstExtensionImage).to receive(:LoadExtension)
.with(/cracklib/, anything).and_return(true)
allow(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".crack"), password)
.and_return("an error message")
allow(Yast::Popup).to receive(:AnyQuestion).and_return(password_accepted)
end

let(:password) { "12345678" }
let(:password_accepted) { false }

it "does not save password in settings" do
it "shows an error message" do
expect(Yast::Popup).to receive(:AnyQuestion)
subject.run
expect(subject.settings).not_to receive(:encryption_password=)
expect(subject.settings.encryption_password).to eq(nil)
end

context "and password is accepted" do
let(:password_accepted) { true }

it "saves password in settings" do
subject.run
expect(subject.settings.encryption_password).to eq(password)
end
end

context "and password is not accepted" do
let(:password_accepted) { false }

it "does not save password in settings" do
subject.run
expect(subject.settings).not_to receive(:encryption_password=)
expect(subject.settings.encryption_password).to eq(nil)
end
end
end
end
end

context "when encryption is clicked" do
before do
select_widget(:encryption)
select_widget(:encryption_method, value: encryption_method)
allow(Yast::UI).to receive(:UserInput).and_return(:encryption, :abort)
end
context "when encryption is clicked" do
before do
select_widget(:encryption)
select_widget(:encryption_method, value: encryption_method)
allow(Yast::UI).to receive(:UserInput).and_return(:encryption, :abort)
end

it "focuses password field" do
expect(Yast::UI).to receive(:SetFocus)
subject.run
it "focuses password field" do
expect(Yast::UI).to receive(:SetFocus).at_least(1).times
subject.run
end
end
end

context "when settings are valid" do
before do
select_widget(:lvm)
select_widget(:encryption)
select_widget(:password, value: password)
select_widget(:repeat_password, value: password)
select_widget(:encryption_method, value: encryption_method)
end
context "when settings are valid" do
before do
select_widget(:lvm)
select_widget(:encryption)
select_widget(:password, value: password)
select_widget(:repeat_password, value: password)
select_widget(:encryption_method, value: encryption_method)
end

let(:password) { "Val1d_pass" }
let(:password) { "Val1d_pass" }

it "saves settings correctly" do
subject.run
expect(subject.settings.use_lvm).to eq(true)
expect(subject.settings.encryption_password).to eq(password)
it "saves settings correctly" do
subject.run
expect(subject.settings.use_lvm).to eq(true)
expect(subject.settings.encryption_password).to eq(password)
expect(subject.settings.encryption_method.id).to eq(encryption_method)
end
end
end
end
Expand Down

0 comments on commit 76ab89b

Please sign in to comment.