Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added rake task. Testing inside a real app

  • Loading branch information...
commit e4dbd3dfa5ac913c49ba0ab779c159e21abc07e5 1 parent da5bc5a
Tammer Saleh authored
View
1  lib/hoptoad_notifier.rb
@@ -361,3 +361,4 @@ def rescue_action_in_public(exception)
include HoptoadNotifier::Catcher
end
end
+
View
23 lib/hoptoad_tasks.rb
@@ -0,0 +1,23 @@
+require 'net/http'
+require 'uri'
+require 'active_support'
+
+module HoptoadTasks
+ def self.deploy_to(rails_env)
+ if HoptoadNotifier.api_key.blank?
+ puts "I don't seem to be configured with an API key. Please check your configuration."
+ return false
+ end
+
+ if rails_env.blank?
+ puts "I don't know to which Rails environment you are deploying (use the TO='production' option)."
+ return false
+ end
+
+ url = URI.parse("http://hoptoadapp.com/deploys")
+ response = Net::HTTP.post_form(url, :api_key => HoptoadNotifier.api_key, :deploy => {:rails_env => rails_env})
+ puts response.body
+ return Net::HTTPSuccess === response
+ end
+end
+
View
6 tasks/hoptoad_notifier_tasks.rake
@@ -1,4 +1,10 @@
namespace :hoptoad do
+ desc "Notify Hoptoad of a new deploy."
+ task :deploy => :environment do
+ require 'hoptoad_tasks'
+ HoptoadTasks.create_deploy(ENV['TO'])
+ end
+
desc "Verify your plugin installation by sending a test exception to the hoptoad service"
task :test => :environment do
require 'action_controller/test_process'
View
101 test/hoptoad_tasks_test.rb
@@ -0,0 +1,101 @@
+require File.dirname(__FILE__) + '/helper'
+require 'rubygems'
+
+require File.dirname(__FILE__) + '/../lib/hoptoad_tasks'
+require 'fakeweb'
+
+FakeWeb.allow_net_connect = false
+
+class HoptoadTasksTest < ActiveSupport::TestCase
+ def successful_response(body = "")
+ response = Net::HTTPSuccess.new('1.2', '200', 'OK')
+ response.stubs(:body).returns(body)
+ return response
+ end
+
+ def unsuccessful_response(body = "")
+ response = Net::HTTPClientError.new('1.2', '200', 'OK')
+ response.stubs(:body).returns(body)
+ return response
+ end
+
+ context "being quiet" do
+ setup { HoptoadTasks.stubs(:puts) }
+
+ context "in a configured project" do
+ setup { HoptoadNotifier.configure { |config| config.api_key = "1234123412341234" } }
+
+ context "on deploy_to(nil)" do
+ setup { @output = HoptoadTasks.deploy_to(nil) }
+
+ before_should "complain about missing rails env" do
+ HoptoadTasks.expects(:puts).with(regexp_matches(/rails environment/i))
+ end
+
+ should "return false" do
+ assert !@output
+ end
+ end
+
+ context "on deploy_to('staging')" do
+ setup { @output = HoptoadTasks.deploy_to("staging") }
+
+ before_should "post to http://hoptoadapp.com/deploys" do
+ URI.stubs(:parse).with('http://hoptoadapp.com/deploys').returns(:uri)
+ Net::HTTP.expects(:post_form).with(:uri, kind_of(Hash)).returns(successful_response)
+ end
+
+ before_should "use the project api key" do
+ Net::HTTP.expects(:post_form).
+ with(kind_of(URI), has_entries(:api_key => "1234123412341234")).
+ returns(successful_response)
+ end
+
+ before_should "use valid deploy params" do
+ deploy_params = {:rails_env => "staging"}
+ Net::HTTP.expects(:post_form).
+ with(kind_of(URI), has_entries(:deploy => deploy_params)).
+ returns(successful_response)
+ end
+
+ before_should "puts the response body on success" do
+ HoptoadTasks.expects(:puts).with("body")
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(successful_response('body'))
+ end
+
+ before_should "puts the response body on failure" do
+ HoptoadTasks.expects(:puts).with("body")
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
+ end
+
+ should "return false on failure", :before => lambda {
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(unsuccessful_response('body'))
+ } do
+ assert !@output
+ end
+
+ should "return true on success", :before => lambda {
+ Net::HTTP.expects(:post_form).with(any_parameters).returns(successful_response('body'))
+ } do
+ assert @output
+ end
+ end
+ end
+
+ context "when not configured" do
+ setup { HoptoadNotifier.configure { |config| config.api_key = "" } }
+
+ context "on deploy_to('staging')" do
+ setup { @output = HoptoadTasks.deploy_to("staging") }
+
+ before_should "complain about missing api key" do
+ HoptoadTasks.expects(:puts).with(regexp_matches(/api key/i))
+ end
+
+ should "return false" do
+ assert !@output
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.