Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/msf/db_connector_spec.rb
Views: 11655
RSpec.describe Msf::DbConnector do1let(:file_fixtures_path) { File.join(Msf::Config.install_root, 'spec', 'file_fixtures') }2let(:empty_config_file) { File.join(file_fixtures_path, 'config_files', 'empty.ini') }3let(:default_remote_db_config_file) { File.join(file_fixtures_path, 'config_files', 'default_remote_db.ini') }4let(:config_file) { empty_config_file }5let(:db) do6instance_double(7Msf::DBManager,8connection_established?: false,9driver: 'driver',10active: true11)12end13let(:framework) do14instance_double(15::Msf::Framework,16version: 'VERSION',17db: db18)19end20before :each do21allow_any_instance_of(::Msf::Config).to receive(:config_file).and_return(config_file)22end2324it { is_expected.to respond_to :db_connect_postgresql }2526describe '#load_db_config' do27context 'when the config file does not exist' do28let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }2930it 'returns nil' do31expect(subject.load_db_config('local-https-data-service')).to eql(nil)32end33end3435context 'when there is no db config present' do36let(:config_file) { empty_config_file }3738it 'returns nil' do39expect(subject.load_db_config('local-https-data-service')).to eql(nil)40end41end4243context 'when there is a default database registered' do44let(:config_file) { default_remote_db_config_file }4546it 'returns the cb config' do47expected_config = {48url: 'https://localhost:5443',49cert: '/Users/user/.msf4/msf-ws-cert.pem',50skip_verify: 'true',51api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'52}53expect(subject.load_db_config('local-https-data-service')).to eql(expected_config)54end55end56end5758describe '#db_connect_from_config' do59let(:db_connect_response) { { result: 'mock result message', data_service_name: 'local-https-data-service' } }60before :each do61allow(subject).to receive(:db_connect).and_return(db_connect_response)62end6364context 'when the config file does not exist' do65let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }6667it 'returns an empty object' do68expect(subject.db_connect_from_config(framework)).to eql({})69end70end7172context 'when there is no db config present' do73let(:config_file) { empty_config_file }7475it 'returns an empty object' do76expect(subject.db_connect_from_config(framework)).to eql({})77end78end7980context 'when there is a default database registered' do81let(:config_file) { default_remote_db_config_file }8283it 'returns the db_connect_response' do84expected_config = {85url: 'https://localhost:5443',86cert: '/Users/user/.msf4/msf-ws-cert.pem',87skip_verify: 'true',88api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'89}90expect(subject.db_connect_from_config(framework)).to eql(db_connect_response)91expect(subject).to have_received(:db_connect).with(framework, expected_config)92end93end94end9596describe '#data_service_search' do97context 'when the name is not present' do98let(:config_file) { empty_config_file }99100it 'returns nil' do101expect(subject.data_service_search(name: 'local-https-data-service')).to eql nil102end103end104105context 'when the name is present' do106let(:config_file) { default_remote_db_config_file }107108it 'returns the name' do109expect(subject.data_service_search(name: 'local-https-data-service')).to eql 'local-https-data-service'110end111end112113context 'when the url is not present' do114let(:config_file) { empty_config_file }115116it 'returns nil' do117expect(subject.data_service_search(url: 'https://localhost:5443')).to eql nil118end119end120121context 'when the url is present' do122let(:config_file) { default_remote_db_config_file }123124it 'returns the name' do125expect(subject.data_service_search(url: 'https://localhost:5443')).to eql 'local-https-data-service'126end127end128end129end130131132