/
security_proposal_test.rb
executable file
·188 lines (149 loc) · 6.11 KB
/
security_proposal_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env rspec
# Copyright (c) [2017] 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.rb"
require "installation/clients/security_proposal"
require "bootloader/config_dialog"
describe Installation::Clients::SecurityProposal do
subject(:client) { described_class.new }
let(:proposal_settings) { Installation::SecuritySettings.create_instance }
describe "#initialize" do
it "instantiates a new proposal settings" do
expect(Installation::SecuritySettings).to receive(:instance)
described_class.new
end
end
describe "#description" do
it "returns a hash with 'id', 'rich_text_title' and 'menu_title'" do
description = client.description
expect(description).to be_a Hash
expect(description).to include("id", "menu_title", "rich_text_title")
end
end
describe "#ask_user" do
let(:param) { { "chosen_id" => action } }
let(:dialog) { instance_double(Installation::Dialogs::Security) }
before do
allow(Installation::Dialogs::Security).to receive(:new).and_return(dialog)
end
context "when 'chosen_id' is equal to the description id" do
let(:action) { client.description["id"] }
it "runs the proposal dialog" do
expect(dialog).to receive(:run)
client.ask_user(param)
end
it "returns a hash with the proposal dialog result for 'workflow_sequence' key" do
allow(dialog).to receive(:run).and_return(:result)
result = client.ask_user(param)
expect(result).to be_a(Hash)
expect(result["workflow_sequence"]).to eq :result
end
end
context "when 'chosen_id' corresponds to some of the services links" do
let(:action) { described_class::SERVICES_LINKS.first }
it "calls the corresponding action for the chosen link" do
expect(client).to receive("call_proposal_action_for").with(action)
client.ask_user(param)
end
it "returns a hash with :next for 'workflow_sequence' key" do
result = client.ask_user(param)
expect(result).to be_a(Hash)
expect(result["workflow_sequence"]).to eq :next
end
end
context "when 'chosen_id' corresponds to cpu_mitigations" do
let(:action) { "security--cpu_mitigations" }
it "opens bootloader dialog" do
allow(::Bootloader::ConfigDialog).to receive(:new).with(initial_tab: :kernel)
.and_return(double(run: :next))
allow(Yast::Wizard).to receive(:CreateDialog)
allow(Yast::Wizard).to receive(:CloseDialog)
expect(client).to receive("call_proposal_action_for").with(action)
client.ask_user(param)
end
end
end
describe "#make_proposal" do
let(:firewall_enabled) { false }
before do
allow(proposal_settings).to receive("enable_firewall").and_return(firewall_enabled)
allow(proposal_settings.selinux_config).to receive(:configurable?)
.and_return(false)
end
it "returns a hash with 'preformatted_proposal', 'links', 'warning_level' and 'warning'" do
allow(Yast::HTML).to receive("List").and_return("<ul><li>Proposal link</li></ul>")
proposal = client.make_proposal({})
expect(proposal).to be_a Hash
expect(proposal).to include("preformatted_proposal", "links", "warning_level")
end
context "when firewalld is disabled" do
it "returns the 'preformatted_proposal' without links to open/close ports" do
proposal = client.make_proposal({})
expect(proposal["preformatted_proposal"]).to_not include("port")
end
end
context "when firewalld is enabled" do
let(:firewall_enabled) { true }
it "returns the 'preformatted_proposal' with links to open/close ports" do
proposal = client.make_proposal({})
expect(proposal["preformatted_proposal"]).to include("port")
end
end
context "when selinux is configurable" do
it "contains in proposal selinux configuration" do
allow(proposal_settings.selinux_config).to receive(:configurable?)
.and_return(true)
allow(Yast::Bootloader).to receive(:kernel_param).and_return(:missing)
proposal = client.make_proposal({})
expect(proposal["preformatted_proposal"]).to include("SELinux Default Mode")
end
end
context "when the user uses only SSH key based authentication" do
let(:ssh_enabled) { true }
let(:ssh_open) { true }
before do
allow(proposal_settings).to receive(:only_public_key_auth).and_return(true)
proposal_settings.enable_sshd = ssh_enabled
proposal_settings.open_ssh = ssh_open
end
context "and the SSH service is enabled" do
context "and the SSH port is open" do
it "the 'proposal' does not contain a warning message" do
proposal = client.make_proposal({})
expect(proposal["warning"]).to be_nil
end
end
context "and the SSH port is close" do
let(:ssh_open) { false }
it "returns the proposal warning about the situation" do
proposal = client.make_proposal({})
expect(proposal["warning"]).to include("might not be allowed")
end
end
end
context "and the SSH is disabled" do
let(:ssh_enabled) { false }
it "returns the proposal warning about the situation" do
proposal = client.make_proposal({})
expect(proposal["warning"]).to include("might not be allowed")
end
end
end
end
end