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
|
|
|
|
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)
|
|
|
|
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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
|