Skip to content

Commit eb73da2

Browse files
committed
Clean specs
1 parent 179177d commit eb73da2

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

spec/lib/metasploit/framework/login_scanner/swg_spec.rb

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@
3939
described_class.new
4040
end
4141

42-
def mock_http_cli(res)
43-
cli = Rex::Proto::Http::Client
44-
allow(cli).to receive(:request_cgi).with(any_args)
45-
allow(cli).to receive(:send_recv).with(any_args).and_return(res)
46-
allow(cli).to receive(:set_config).with(any_args)
47-
allow(cli).to receive(:close)
48-
allow(cli).to receive(:connect)
49-
allow(Rex::Proto::Http::Client).to receive(:new).and_return(cli)
42+
let(:response) do
43+
Rex::Proto::Http::Response.new(200, 'OK')
44+
end
45+
46+
before(:each) do
47+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:request_cgi).with(any_args)
48+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:send_recv).with(any_args).and_return(response)
49+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:set_config).with(any_args)
50+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:close)
51+
allow_any_instance_of(Rex::Proto::Http::Client).to receive(:connect)
5052
end
5153

5254
describe '#check_setup' do
@@ -56,20 +58,15 @@ def mock_http_cli(res)
5658
res
5759
end
5860

59-
let(:empty_html_response) do
60-
Rex::Proto::Http::Response.new(200, 'OK')
61-
end
62-
6361
context 'when target is Symantec Web Gateway' do
62+
let(:response) { swg_html_response }
6463
it 'returns true' do
65-
mock_http_cli(swg_html_response)
6664
expect(subject.check_setup).to be_truthy
6765
end
6866
end
6967

7068
context 'when target is not Symantec Web Gateway' do
7169
it 'returns false' do
72-
mock_http_cli(empty_html_response)
7370
expect(subject.check_setup).to be_falsey
7471
end
7572
end
@@ -78,19 +75,21 @@ def mock_http_cli(res)
7875
describe '#send_request' do
7976
context 'when a valid request is sent' do
8077
it 'returns a response object' do
81-
expected_response = Rex::Proto::Http::Response.new(200, 'OK')
82-
mock_http_cli(expected_response)
8378
expect(subject.send_request({'uri'=>'/'})).to be_kind_of(Rex::Proto::Http::Response)
8479
end
8580
end
8681
end
8782

8883
describe '#get_last_sid' do
84+
let(:response) do
85+
res = Rex::Proto::Http::Response.new(200, 'OK')
86+
res.headers['Set-Cookie'] = session_id
87+
88+
res
89+
end
90+
8991
context 'when there is no session ID' do
9092
it 'returns a new session ID' do
91-
res = Rex::Proto::Http::Response.new(200, 'OK')
92-
res.headers['Set-Cookie'] = session_id
93-
mock_http_cli(res)
9493
expect(subject.get_last_sid).to include('PHPSESSID')
9594
end
9695
end
@@ -106,16 +105,16 @@ def mock_http_cli(res)
106105

107106
describe '#get_login_state' do
108107
context 'when the credential is valid' do
108+
let(:response) { successful_auth_response }
109109
it 'returns a hash indicating a successful login' do
110-
mock_http_cli(successful_auth_response)
111110
successful_status = Metasploit::Model::Login::Status::SUCCESSFUL
112111
expect(subject.get_login_state(username, good_password)[:status]).to eq(successful_status)
113112
end
114113
end
115114

116115
context 'when the creential is invalid' do
116+
let(:response) { fail_auth_response }
117117
it 'returns a hash indicating an incorrect cred' do
118-
mock_http_cli(fail_auth_response)
119118
incorrect_status = Metasploit::Model::Login::Status::INCORRECT
120119
expect(subject.get_login_state(username, good_password)[:status]).to eq(incorrect_status)
121120
end
@@ -124,19 +123,20 @@ def mock_http_cli(res)
124123

125124
describe '#attempt_login' do
126125
context 'when the credential is valid' do
126+
let(:response) { successful_auth_response }
127+
127128
it 'returns a Result object indicating a successful login' do
128129
cred_obj = Metasploit::Framework::Credential.new(public: username, private: good_password)
129-
mock_http_cli(successful_auth_response)
130130
result = subject.attempt_login(cred_obj)
131131
expect(result).to be_kind_of(::Metasploit::Framework::LoginScanner::Result)
132132
expect(result.status).to eq(Metasploit::Model::Login::Status::SUCCESSFUL)
133133
end
134134
end
135135

136136
context 'when the credential is invalid' do
137+
let(:response) { fail_auth_response }
137138
it 'returns a Result object indicating an incorrect cred' do
138139
cred_obj = Metasploit::Framework::Credential.new(public: username, private: bad_password)
139-
mock_http_cli(fail_auth_response)
140140
result = subject.attempt_login(cred_obj)
141141
expect(result).to be_kind_of(::Metasploit::Framework::LoginScanner::Result)
142142
expect(result.status).to eq(Metasploit::Model::Login::Status::INCORRECT)

0 commit comments

Comments
 (0)