Guest User

Untitled

a guest
Mar 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. # RSpec
  2. require 'spec/expectations'
  3.  
  4. # Webrat
  5. require 'webrat'
  6.  
  7. require 'test/unit/assertions'
  8. World(Test::Unit::Assertions)
  9.  
  10. Webrat.configure do |config|
  11. config.mode = :mechanize
  12. end
  13.  
  14. World do
  15. session = Webrat::Session.new
  16. session.extend(Webrat::Methods)
  17. session.extend(Webrat::Matchers)
  18. session
  19. end
  20.  
  21. # Helper method for running shell commands
  22. def run(command, verbose = false, message = nil)
  23. if verbose then
  24. puts "#{message}"
  25. puts command
  26. result = `#{command}`
  27. puts result
  28. return result
  29. else
  30. `#{command}`
  31. end
  32. end
  33.  
  34.  
  35.  
  36. # Switch to the test database
  37. @database_config_path = File.dirname(__FILE__)+"/../../modules/_dbselect.php"
  38. @original_database_config = File.read(@database_config_path)
  39. @@test_database_location = "localhost"
  40. @@test_database_name = "chits_testing"
  41. @@test_database_username = "chits_tester"
  42. @@test_database_password = "useless_password"
  43. @path_to_core_data = File.dirname(__FILE__)+"/../../db/core_data.sql"
  44.  
  45. File.open(@database_config_path, "w") do |file|
  46. file.puts "
  47. <?php
  48. session_start();
  49. $database_location = '#{@@test_database_location}';
  50. $database_name = '#{@@test_database_name}';
  51. $database_username = '#{@@test_database_username}';
  52. $database_password = '#{@@test_database_password}';
  53. $_SESSION['dbname'] = $database_name;
  54. $_SESSION['dbuser'] = $database_username;
  55. $_SESSION['dbpass'] = $database_password;
  56. $conn = mysql_connect($database_location, $database_username, $database_password);
  57. $db->connectdb($database_name);
  58. ?>
  59. "
  60. end
  61.  
  62. # Check to see if test database exists, if not create the user p
  63. puts "Checking that test database exists, then switching to it..."
  64. unless(run("echo \"SHOW DATABASES;\" | mysql -u #{@@test_database_username} --password=#{@@test_database_password} 2>&1").match @@test_database_name ) then
  65. puts "\nOops! Looks like you don't have a test database yet, so you need to create one. Luckily this is easy!\nRun the following commands and enter your password when necessary (your root mysql password may be blank).\n\n"
  66. puts "echo \"CREATE DATABASE #{@@test_database_name};\" | mysql -u root -p;"
  67. # puts "echo \"INSERT INTO user SET user='#{@@test_database_username}',password=password('#{@@test_database_password}'),host='#{@@test_database_location}';\" | mysql -u root -p mysql;"
  68.  
  69. puts "echo \"GRANT ALL PRIVILEGES ON #{@@test_database_name}.* TO #{@@test_database_username}@#{@@test_database_location} IDENTIFIED BY '#{@@test_database_password}'\" | mysql -u root -p"
  70. puts "mysql -u #{@@test_database_username} --password=#{@@test_database_password} #{@@test_database_name} < #{@path_to_core_data};"
  71. exit
  72. end
  73.  
  74. # Switch back to whatever was loaded originally
  75. def rollback_to_core_data
  76. File.open(@database_config_path, "w") do |file|
  77. file.puts @original_database_config
  78. end
  79. # puts "Cucumber finished, Resetting test database"
  80. # run "mysql -u #{@@test_database_username} --password=#{@@test_database_password} #{@@test_database_name} < #{@path_to_core_data}"
  81. end
  82.  
  83. at_exit do
  84. rollback_to_core_data
  85. end
  86.  
  87. module Webrat
  88. # For some reason we are getting duplicated GET params on the current_url, so monkeypatch to fix it
  89. class Link
  90. def absolute_href
  91. if href =~ /^\?/
  92. "#{@session.current_url.gsub(/\?.*/,"")}#{href}"
  93. elsif href !~ %r{^https?://} && (href !~ /^\//)
  94. "#{@session.current_url}/#{href}"
  95. else
  96. href
  97. end
  98. end
  99. end
  100.  
  101. # For extra debug info we monkeypatch this in
  102. module Locators
  103. class Locator # :nodoc:
  104. def locate!
  105. locate || raise(NotFoundError.new(error_message))
  106. rescue Webrat::NotFoundError => e
  107. filepath = '/tmp/webrat_debug.html'
  108. File.open(filepath, "w") do |file|
  109. file.puts @session.send(:response_body)
  110. end
  111. raise "#{e.message}\n#{@session.send(:response_body).gsub(/\n/, "\n ")}\nURL: #{@session.current_url}\nResponse saved: #{filepath}"
  112. end
  113. end
  114. end
  115. end
Add Comment
Please, Sign In to add comment