CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/msf/db_connector_spec.rb
Views: 11655
1
RSpec.describe Msf::DbConnector do
2
let(:file_fixtures_path) { File.join(Msf::Config.install_root, 'spec', 'file_fixtures') }
3
let(:empty_config_file) { File.join(file_fixtures_path, 'config_files', 'empty.ini') }
4
let(:default_remote_db_config_file) { File.join(file_fixtures_path, 'config_files', 'default_remote_db.ini') }
5
let(:config_file) { empty_config_file }
6
let(:db) do
7
instance_double(
8
Msf::DBManager,
9
connection_established?: false,
10
driver: 'driver',
11
active: true
12
)
13
end
14
let(:framework) do
15
instance_double(
16
::Msf::Framework,
17
version: 'VERSION',
18
db: db
19
)
20
end
21
before :each do
22
allow_any_instance_of(::Msf::Config).to receive(:config_file).and_return(config_file)
23
end
24
25
it { is_expected.to respond_to :db_connect_postgresql }
26
27
describe '#load_db_config' do
28
context 'when the config file does not exist' do
29
let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }
30
31
it 'returns nil' do
32
expect(subject.load_db_config('local-https-data-service')).to eql(nil)
33
end
34
end
35
36
context 'when there is no db config present' do
37
let(:config_file) { empty_config_file }
38
39
it 'returns nil' do
40
expect(subject.load_db_config('local-https-data-service')).to eql(nil)
41
end
42
end
43
44
context 'when there is a default database registered' do
45
let(:config_file) { default_remote_db_config_file }
46
47
it 'returns the cb config' do
48
expected_config = {
49
url: 'https://localhost:5443',
50
cert: '/Users/user/.msf4/msf-ws-cert.pem',
51
skip_verify: 'true',
52
api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'
53
}
54
expect(subject.load_db_config('local-https-data-service')).to eql(expected_config)
55
end
56
end
57
end
58
59
describe '#db_connect_from_config' do
60
let(:db_connect_response) { { result: 'mock result message', data_service_name: 'local-https-data-service' } }
61
before :each do
62
allow(subject).to receive(:db_connect).and_return(db_connect_response)
63
end
64
65
context 'when the config file does not exist' do
66
let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }
67
68
it 'returns an empty object' do
69
expect(subject.db_connect_from_config(framework)).to eql({})
70
end
71
end
72
73
context 'when there is no db config present' do
74
let(:config_file) { empty_config_file }
75
76
it 'returns an empty object' do
77
expect(subject.db_connect_from_config(framework)).to eql({})
78
end
79
end
80
81
context 'when there is a default database registered' do
82
let(:config_file) { default_remote_db_config_file }
83
84
it 'returns the db_connect_response' do
85
expected_config = {
86
url: 'https://localhost:5443',
87
cert: '/Users/user/.msf4/msf-ws-cert.pem',
88
skip_verify: 'true',
89
api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'
90
}
91
expect(subject.db_connect_from_config(framework)).to eql(db_connect_response)
92
expect(subject).to have_received(:db_connect).with(framework, expected_config)
93
end
94
end
95
end
96
97
describe '#data_service_search' do
98
context 'when the name is not present' do
99
let(:config_file) { empty_config_file }
100
101
it 'returns nil' do
102
expect(subject.data_service_search(name: 'local-https-data-service')).to eql nil
103
end
104
end
105
106
context 'when the name is present' do
107
let(:config_file) { default_remote_db_config_file }
108
109
it 'returns the name' do
110
expect(subject.data_service_search(name: 'local-https-data-service')).to eql 'local-https-data-service'
111
end
112
end
113
114
context 'when the url is not present' do
115
let(:config_file) { empty_config_file }
116
117
it 'returns nil' do
118
expect(subject.data_service_search(url: 'https://localhost:5443')).to eql nil
119
end
120
end
121
122
context 'when the url is present' do
123
let(:config_file) { default_remote_db_config_file }
124
125
it 'returns the name' do
126
expect(subject.data_service_search(url: 'https://localhost:5443')).to eql 'local-https-data-service'
127
end
128
end
129
end
130
end
131
132