2013-04-17 19:26:16 +00:00
|
|
|
# -*- coding:binary -*-
|
2013-04-15 18:08:12 +00:00
|
|
|
require 'metasploit/framework/database'
|
|
|
|
|
2013-04-01 21:06:40 +00:00
|
|
|
shared_context 'DatabaseCleaner' do
|
2013-09-30 18:47:53 +00:00
|
|
|
def with_established_connection
|
|
|
|
begin
|
|
|
|
ActiveRecord::Base.connection_pool.with_connection do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::ConnectionNotEstablished
|
|
|
|
# if there isn't a connection established, then established one and try
|
|
|
|
# again
|
|
|
|
ActiveRecord::Base.configurations = Metasploit::Framework::Database.configurations
|
|
|
|
spec = ActiveRecord::Base.configurations[Metasploit::Framework.env]
|
|
|
|
ActiveRecord::Base.establish_connection(spec)
|
2013-04-01 21:06:40 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
2013-04-01 21:06:40 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
# clean before all in case last test run was interrupted before
|
|
|
|
# after(:each) could clean up
|
|
|
|
before(:all) do
|
|
|
|
with_established_connection do
|
|
|
|
DatabaseCleaner.clean_with(:truncation)
|
|
|
|
end
|
|
|
|
end
|
2013-04-01 21:06:40 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
# Clean up after each test
|
|
|
|
after(:each) do
|
|
|
|
with_established_connection do
|
|
|
|
# Testing using both :truncation and :deletion; :truncation took long
|
|
|
|
# for testing.
|
|
|
|
DatabaseCleaner.clean_with(:deletion)
|
|
|
|
end
|
|
|
|
end
|
2013-04-17 19:26:16 +00:00
|
|
|
end
|