Given /^Chubbies is running$/ do Chubbies.run unless Chubbies.running? end Given /^Chubbies has been killed$/ do Chubbies.ensure_killed end Given /^Chubbies is registered on my pod$/ do packaged_manifest = JSON.parse(RestClient.get("localhost:#{Chubbies::PORT}/manifest.json").body) public_key = OpenSSL::PKey::RSA.new(packaged_manifest['public_key']) manifest = JWT.decode(packaged_manifest['jwt'], public_key) client = OAuth2::Provider.client_class.create_or_reset_from_manifest!(manifest, public_key) params = {:client_id => client.oauth_identifier, :client_secret => client.oauth_secret, :host => "localhost:9887"} RestClient.post("localhost:#{Chubbies::PORT}/register", params) end And /^I should see my "([^"]+)"/ do |code| page.should have_content(@me.person.instance_eval(code).to_s) end And /^there is only one Chubbies$/ do OAuth2::Provider.client_class.where(:name => "Chubbies").count.should == 1 end And /^I remove all traces of Chubbies on the pod$/ do OAuth2::Provider.client_class.destroy_all end When /^I try to authorize Chubbies$/ do # We need to reset the tokens saved in Chubbies, # as we are clearing the Diaspora DB every scenario Then 'I visit "/new" on Chubbies' ### And "I fill in \"Diaspora Handle\" with \"#{@me.diaspora_handle}\"" And 'I press "Connect to Diaspora"' Then 'I should be on the new user session page' And "I fill in \"Username\" with \"#{@me.username}\"" And "I fill in \"Password\" with \"#{@me.password}\"" And 'I press "Sign in"' Then 'I should be on the oauth authorize page' Then 'I should see "Chubbies"' And 'I should see "The best way to chub."' end When /^I visit "([^"]+)" on Chubbies$/ do |path| former_host = Capybara.app_host Capybara.app_host = "localhost:#{Chubbies::PORT}" visit(path) Capybara.app_host = former_host end class Chubbies PORT = 9292 def self.run @pid = fork do Process.exec "cd #{Rails.root}/spec/chubbies/ && bundle exec rackup -p #{PORT} #{nullify}" end at_exit do Chubbies.kill end while(!running?) do sleep(1) end end def self.kill `kill -9 #{get_pid}` end def self.nullify ENV["CI"] ? '' : "2> /dev/null 1> /dev/null" end def self.ensure_killed if !(@killed) && self.running? self.kill @killed = true end end def self.running? begin begin RestClient.get("localhost:#{PORT}/running") rescue RestClient::ResourceNotFound end true rescue Errno::ECONNREFUSED false end end def self.get_pid @pid ||= lambda { processes = `ps -ax -o pid,command | grep "rackup -p #{PORT}"`.split("\n") processes = processes.select{|p| !p.include?("grep") } processes.first.split(" ").first }.call end end