db_connector_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. it { is_expected.to respond_to :db_connect_postgresql }
  25. describe '#load_db_config' do
  26. context 'when the config file does not exist' do
  27. let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }
  28. it 'returns nil' do
  29. expect(subject.load_db_config('local-https-data-service')).to eql(nil)
  30. end
  31. end
  32. context 'when there is no db config present' do
  33. let(:config_file) { empty_config_file }
  34. it 'returns nil' do
  35. expect(subject.load_db_config('local-https-data-service')).to eql(nil)
  36. end
  37. end
  38. context 'when there is a default database registered' do
  39. let(:config_file) { default_remote_db_config_file }
  40. it 'returns the cb config' do
  41. expected_config = {
  42. url: 'https://localhost:5443',
  43. cert: '/Users/user/.msf4/msf-ws-cert.pem',
  44. skip_verify: 'true',
  45. api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'
  46. }
  47. expect(subject.load_db_config('local-https-data-service')).to eql(expected_config)
  48. end
  49. end
  50. end
  51. describe '#db_connect_from_config' do
  52. let(:db_connect_response) { { result: 'mock result message', data_service_name: 'local-https-data-service' } }
  53. before :each do
  54. allow(subject).to receive(:db_connect).and_return(db_connect_response)
  55. end
  56. context 'when the config file does not exist' do
  57. let(:config_file) { File.join(file_fixtures_path, 'config_files', 'non_existent_file.ini') }
  58. it 'returns an empty object' do
  59. expect(subject.db_connect_from_config(framework)).to eql({})
  60. end
  61. end
  62. context 'when there is no db config present' do
  63. let(:config_file) { empty_config_file }
  64. it 'returns an empty object' do
  65. expect(subject.db_connect_from_config(framework)).to eql({})
  66. end
  67. end
  68. context 'when there is a default database registered' do
  69. let(:config_file) { default_remote_db_config_file }
  70. it 'returns the db_connect_response' do
  71. expected_config = {
  72. url: 'https://localhost:5443',
  73. cert: '/Users/user/.msf4/msf-ws-cert.pem',
  74. skip_verify: 'true',
  75. api_token: 'b1cd123e2f160a8a1fbf79baed180b8dc480de5b994f53eee42e57771e3f65e13bec737e4a4acbb2'
  76. }
  77. expect(subject.db_connect_from_config(framework)).to eql(db_connect_response)
  78. expect(subject).to have_received(:db_connect).with(framework, expected_config)
  79. end
  80. end
  81. end
  82. describe '#data_service_search' do
  83. context 'when the name is not present' do
  84. let(:config_file) { empty_config_file }
  85. it 'returns nil' do
  86. expect(subject.data_service_search(name: 'local-https-data-service')).to eql nil
  87. end
  88. end
  89. context 'when the name is present' do
  90. let(:config_file) { default_remote_db_config_file }
  91. it 'returns the name' do
  92. expect(subject.data_service_search(name: 'local-https-data-service')).to eql 'local-https-data-service'
  93. end
  94. end
  95. context 'when the url is not present' do
  96. let(:config_file) { empty_config_file }
  97. it 'returns nil' do
  98. expect(subject.data_service_search(url: 'https://localhost:5443')).to eql nil
  99. end
  100. end
  101. context 'when the url is present' do
  102. let(:config_file) { default_remote_db_config_file }
  103. it 'returns the name' do
  104. expect(subject.data_service_search(url: 'https://localhost:5443')).to eql 'local-https-data-service'
  105. end
  106. end
  107. end
  108. end