bushido 0.0.35 → 0.0.36

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +4 -0
  3. data/Gemfile +6 -13
  4. data/README.md +3 -0
  5. data/Rakefile +31 -7
  6. data/app/controllers/bushido/data_controller.rb +2 -1
  7. data/app/controllers/bushido/envs_controller.rb +7 -5
  8. data/app/controllers/bushido/mail_controller.rb +37 -0
  9. data/lib/bushido.rb +27 -5
  10. data/lib/bushido/action_mailer.rb +35 -0
  11. data/lib/bushido/app.rb +7 -0
  12. data/lib/bushido/bar.rb +19 -0
  13. data/lib/bushido/base.rb +23 -0
  14. data/lib/bushido/command.rb +17 -15
  15. data/lib/bushido/config.rb +12 -0
  16. data/lib/bushido/data.rb +41 -23
  17. data/lib/bushido/data_helper.rb +1 -0
  18. data/lib/bushido/engine.rb +21 -0
  19. data/lib/bushido/envs_helper.rb +1 -0
  20. data/lib/bushido/event.rb +25 -2
  21. data/lib/bushido/event_observer.rb +20 -0
  22. data/lib/bushido/mail_helper.rb +1 -0
  23. data/lib/bushido/mail_route.rb +186 -0
  24. data/lib/bushido/middleware.rb +9 -16
  25. data/lib/bushido/platform.rb +27 -3
  26. data/lib/bushido/smtp.rb +24 -0
  27. data/lib/bushido/user.rb +39 -9
  28. data/lib/bushido/user_helper.rb +7 -0
  29. data/lib/bushido/utils.rb +44 -0
  30. data/lib/bushido/version.rb +1 -1
  31. data/lib/generators/bushido/auth_migration_generator.rb +46 -0
  32. data/lib/generators/bushido/hooks_generator.rb +43 -0
  33. data/lib/generators/bushido/mail_routes_generator.rb +45 -0
  34. data/lib/generators/bushido/routes_generator.rb +22 -0
  35. data/lib/{bushido/hooks.rb → hooks.rb} +0 -0
  36. data/lib/rails/routes.rb +36 -8
  37. data/lib/tasks/bushido.rake +6 -0
  38. data/{test_app/spec → spec/app_spec}/controllers/envs_controller_spec.rb +0 -0
  39. data/spec/app_spec/controllers/mail_controller_spec.rb +28 -0
  40. data/{test_app/spec → spec/app_spec}/integration/app_claim_spec.rb +4 -6
  41. data/spec/gem_spec/base_spec.rb +24 -0
  42. data/spec/gem_spec/command_spec.rb +181 -0
  43. data/spec/gem_spec/config_spec.rb +9 -0
  44. data/spec/gem_spec/data_spec.rb +13 -0
  45. data/spec/gem_spec/hooks_spec.rb +5 -0
  46. data/spec/gem_spec/mail_route_spec.rb +168 -0
  47. data/spec/gem_spec/platform_spec.rb +9 -0
  48. data/spec/gem_spec/user_spec.rb +45 -0
  49. data/spec/spec_helper.rb +21 -0
  50. data/spec/test_apps/rails-2.3/.gitignore +13 -0
  51. data/spec/test_apps/rails-2.3/Gemfile +13 -0
  52. data/spec/test_apps/rails-2.3/README +243 -0
  53. data/spec/test_apps/rails-2.3/Rakefile +10 -0
  54. data/spec/test_apps/rails-2.3/app/controllers/application_controller.rb +10 -0
  55. data/spec/test_apps/rails-2.3/app/helpers/application_helper.rb +3 -0
  56. data/spec/test_apps/rails-2.3/config/boot.rb +114 -0
  57. data/spec/test_apps/rails-2.3/config/environment.rb +41 -0
  58. data/spec/test_apps/rails-2.3/config/environments/development.rb +17 -0
  59. data/spec/test_apps/rails-2.3/config/environments/production.rb +28 -0
  60. data/spec/test_apps/rails-2.3/config/environments/test.rb +30 -0
  61. data/spec/test_apps/rails-2.3/config/initializers/backtrace_silencers.rb +7 -0
  62. data/spec/test_apps/rails-2.3/config/initializers/cookie_verification_secret.rb +7 -0
  63. data/spec/test_apps/rails-2.3/config/initializers/inflections.rb +10 -0
  64. data/{test_app → spec/test_apps/rails-2.3}/config/initializers/mime_types.rb +0 -0
  65. data/spec/test_apps/rails-2.3/config/initializers/new_rails_defaults.rb +21 -0
  66. data/spec/test_apps/rails-2.3/config/initializers/session_store.rb +15 -0
  67. data/spec/test_apps/rails-2.3/config/locales/en.yml +5 -0
  68. data/spec/test_apps/rails-2.3/config/routes.rb +43 -0
  69. data/spec/test_apps/rails-2.3/db/seeds.rb +7 -0
  70. data/{test_app → spec/test_apps/rails-2.3}/doc/README_FOR_APP +0 -0
  71. data/spec/test_apps/rails-2.3/lib/tasks/rspec.rake +144 -0
  72. data/spec/test_apps/rails-2.3/public/404.html +30 -0
  73. data/spec/test_apps/rails-2.3/public/422.html +30 -0
  74. data/spec/test_apps/rails-2.3/public/500.html +30 -0
  75. data/{test_app → spec/test_apps/rails-2.3}/public/favicon.ico +0 -0
  76. data/{test_app → spec/test_apps/rails-2.3}/public/images/rails.png +0 -0
  77. data/{test_app → spec/test_apps/rails-2.3}/public/javascripts/application.js +0 -0
  78. data/spec/test_apps/rails-2.3/public/javascripts/controls.js +963 -0
  79. data/spec/test_apps/rails-2.3/public/javascripts/dragdrop.js +973 -0
  80. data/spec/test_apps/rails-2.3/public/javascripts/effects.js +1128 -0
  81. data/spec/test_apps/rails-2.3/public/javascripts/prototype.js +4320 -0
  82. data/{test_app → spec/test_apps/rails-2.3}/public/robots.txt +0 -0
  83. data/spec/test_apps/rails-2.3/script/about +4 -0
  84. data/spec/test_apps/rails-2.3/script/autospec +6 -0
  85. data/spec/test_apps/rails-2.3/script/console +3 -0
  86. data/spec/test_apps/rails-2.3/script/dbconsole +3 -0
  87. data/spec/test_apps/rails-2.3/script/destroy +3 -0
  88. data/spec/test_apps/rails-2.3/script/generate +3 -0
  89. data/spec/test_apps/rails-2.3/script/performance/benchmarker +3 -0
  90. data/spec/test_apps/rails-2.3/script/performance/profiler +3 -0
  91. data/spec/test_apps/rails-2.3/script/plugin +3 -0
  92. data/spec/test_apps/rails-2.3/script/runner +3 -0
  93. data/spec/test_apps/rails-2.3/script/server +3 -0
  94. data/spec/test_apps/rails-2.3/script/spec +10 -0
  95. data/spec/test_apps/rails-2.3/spec/all_spec.rb +5 -0
  96. data/spec/test_apps/rails-2.3/spec/rcov.opts +2 -0
  97. data/spec/test_apps/rails-2.3/spec/spec.opts +4 -0
  98. data/spec/test_apps/rails-2.3/spec/spec_helper.rb +83 -0
  99. data/spec/test_apps/rails-2.3/test/performance/browsing_test.rb +9 -0
  100. data/spec/test_apps/rails-2.3/test/test_helper.rb +38 -0
  101. data/{test_app → spec/test_apps/rails-3.0}/.gitignore +0 -0
  102. data/spec/test_apps/rails-3.0/.rspec +2 -0
  103. data/spec/test_apps/rails-3.0/Gemfile +10 -0
  104. data/{test_app → spec/test_apps/rails-3.0}/README +0 -0
  105. data/{test_app → spec/test_apps/rails-3.0}/Rakefile +0 -0
  106. data/{test_app → spec/test_apps/rails-3.0}/app/controllers/application_controller.rb +0 -0
  107. data/{test_app → spec/test_apps/rails-3.0}/app/controllers/static_controller.rb +0 -0
  108. data/{test_app → spec/test_apps/rails-3.0}/app/helpers/application_helper.rb +0 -0
  109. data/{test_app → spec/test_apps/rails-3.0}/app/helpers/static_helper.rb +0 -0
  110. data/{test_app → spec/test_apps/rails-3.0}/app/views/layouts/application.html.erb +0 -0
  111. data/{test_app → spec/test_apps/rails-3.0}/app/views/static/home.html.erb +0 -0
  112. data/{test_app → spec/test_apps/rails-3.0}/config.ru +0 -0
  113. data/{test_app → spec/test_apps/rails-3.0}/config/application.rb +2 -0
  114. data/{test_app → spec/test_apps/rails-3.0}/config/boot.rb +0 -0
  115. data/{test_app → spec/test_apps/rails-3.0}/config/environment.rb +0 -0
  116. data/{test_app → spec/test_apps/rails-3.0}/config/environments/development.rb +0 -0
  117. data/{test_app → spec/test_apps/rails-3.0}/config/environments/production.rb +0 -0
  118. data/{test_app → spec/test_apps/rails-3.0}/config/environments/test.rb +0 -0
  119. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/backtrace_silencers.rb +0 -0
  120. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/inflections.rb +0 -0
  121. data/spec/test_apps/rails-3.0/config/initializers/mime_types.rb +5 -0
  122. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/secret_token.rb +0 -0
  123. data/{test_app → spec/test_apps/rails-3.0}/config/initializers/session_store.rb +0 -0
  124. data/{test_app → spec/test_apps/rails-3.0}/config/locales/en.yml +0 -0
  125. data/{test_app → spec/test_apps/rails-3.0}/config/routes.rb +0 -0
  126. data/{test_app → spec/test_apps/rails-3.0}/db/schema.rb +0 -0
  127. data/{test_app → spec/test_apps/rails-3.0}/db/seeds.rb +0 -0
  128. data/spec/test_apps/rails-3.0/doc/README_FOR_APP +2 -0
  129. data/{test_app → spec/test_apps/rails-3.0}/lib/tasks/.gitkeep +0 -0
  130. data/{test_app → spec/test_apps/rails-3.0}/public/404.html +0 -0
  131. data/{test_app → spec/test_apps/rails-3.0}/public/422.html +0 -0
  132. data/{test_app → spec/test_apps/rails-3.0}/public/500.html +0 -0
  133. data/{test_app/public/stylesheets/.gitkeep → spec/test_apps/rails-3.0/public/favicon.ico} +0 -0
  134. data/spec/test_apps/rails-3.0/public/images/rails.png +0 -0
  135. data/{test_app → spec/test_apps/rails-3.0}/public/index.html +0 -0
  136. data/spec/test_apps/rails-3.0/public/javascripts/application.js +2 -0
  137. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/controls.js +0 -0
  138. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/dragdrop.js +0 -0
  139. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/effects.js +0 -0
  140. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/prototype.js +0 -0
  141. data/{test_app → spec/test_apps/rails-3.0}/public/javascripts/rails.js +0 -0
  142. data/spec/test_apps/rails-3.0/public/robots.txt +5 -0
  143. data/{test_app/vendor/plugins → spec/test_apps/rails-3.0/public/stylesheets}/.gitkeep +0 -0
  144. data/{test_app → spec/test_apps/rails-3.0}/script/rails +0 -0
  145. data/spec/test_apps/rails-3.0/spec/all_spec.rb +5 -0
  146. data/{test_app → spec/test_apps/rails-3.0}/spec/spec_helper.rb +13 -1
  147. data/{test_app → spec/test_apps/rails-3.0}/test/performance/browsing_test.rb +0 -0
  148. data/{test_app → spec/test_apps/rails-3.0}/test/test_helper.rb +0 -0
  149. data/spec/test_apps/rails-3.0/vendor/plugins/.gitkeep +0 -0
  150. data/tasks/cover_me.rake +11 -0
  151. metadata +249 -117
  152. data/lib/engine.rb +0 -16
  153. data/test/executable_test.rb +0 -10
  154. data/test/routes_test.rb +0 -12
  155. data/test/test_helper.rb +0 -8
  156. data/test_app/.rspec +0 -1
  157. data/test_app/Gemfile +0 -7
  158. data/test_app/config/database.yml +0 -22
  159. data/test_app/spec/helpers/static_helper_spec.rb +0 -14
  160. data/test_app/spec/support/integration_example_group.rb +0 -35
@@ -0,0 +1,7 @@
1
+ module Bushido
2
+ module UserHelper
3
+ def notify(title, body, category)
4
+ Bushido::User.notify(self.ido_id, title, body, "chat")
5
+ end
6
+ end
7
+ end
@@ -4,6 +4,50 @@ module Bushido
4
4
  # TODO: Make this update all the available ENV variables
5
5
  def refresh_env!
6
6
  end
7
+
8
+ def array_helper(array, proc)
9
+ array.collect do |value|
10
+ if value.kind_of?( Hash )
11
+ deep_process_hash_keys(value, proc)
12
+ elsif value.kind_of?( Array )
13
+ array_helper(array)
14
+ else
15
+ value
16
+ end
17
+ end
18
+ end
19
+
20
+ def deep_process_hash_keys(input_hash, proc)
21
+ hash = {}
22
+ input_hash.keys.each do |key|
23
+ _key = proc.call(key)
24
+ if input_hash[key].kind_of?(Hash)
25
+ hash[_key] = deep_process_hash_keys(input_hash[key], proc)
26
+ elsif input_hash[key].kind_of?(Array)
27
+ hash[_key] = array_helper(input_hash[key], proc)
28
+ else
29
+ hash[_key] = input_hash[key]
30
+ end
31
+ end
32
+
33
+ hash
34
+ end
35
+
36
+ def deep_underscore_hash_keys(input_hash)
37
+ deep_process_hash_keys(input_hash, Proc.new { |key| key.to_s.underscore })
38
+ end
39
+
40
+ def deep_symbolize_hash_keys(input_hash)
41
+ deep_process_hash_keys(input_hash, Proc.new { |key| key.to_sym })
42
+ end
43
+
44
+ def deep_stringify_hash_keys(input_hash)
45
+ deep_process_hash_keys(input_hash, Proc.new { |key| key.to_s })
46
+ end
47
+
48
+ def normalize_keys(input_hash)
49
+ deep_symbolize_hash_keys(deep_underscore_hash_keys(input_hash))
50
+ end
7
51
  end
8
52
  end
9
53
  end
@@ -1,4 +1,4 @@
1
1
  module Bushido
2
2
  # Current version of the Bushido gem
3
- VERSION = "0.0.35"
3
+ VERSION = "0.0.36"
4
4
  end
@@ -0,0 +1,46 @@
1
+ module Bushido
2
+ module Generators
3
+ class AuthMigrationGenerator < Rails::Generators::NamedBase
4
+
5
+ def create_auth_migration_file
6
+ fields_to_add = []
7
+ new_resource = class_name.constantize.new
8
+
9
+ fields_to_add << "ido_id" if not new_resource.respond_to?(:ido_id)
10
+ fields_to_add << "first_name" if not new_resource.respond_to?(:first_name)
11
+ fields_to_add << "last_name" if not new_resource.respond_to?(:last_name)
12
+ fields_to_add << "email" if not new_resource.respond_to?(:email)
13
+ fields_to_add << "locale" if not new_resource.respond_to?(:locale)
14
+ fields_to_add << "timezone" if not new_resource.respond_to?(:timezone)
15
+
16
+
17
+ attr_accessor_string = fields_to_add.collect { |field| ":"+field}.join(", ")
18
+
19
+ inject_into_class "app/models/#{class_name.underscore}.rb", class_name do
20
+ <<-EOF
21
+ attr_accessor #{attr_accessor_string}\n
22
+
23
+ def bushido_extra_attributes(extra_attributes)
24
+ self.first_name = extra_attributes["first_name"].to_s
25
+ self.last_name = extra_attributes["last_name"].to_s
26
+ self.email = extra_attributes["email"]
27
+ self.locale = extra_attributes["locale"]
28
+ self.timezone = extra_attributes["timezone"]
29
+ end
30
+ EOF
31
+ end
32
+
33
+ gem("devise_bushido_authenticatable")
34
+
35
+ generate("migration", "AddBushidoFieldsTo#{class_name}", *fields_to_add.collect { |field| field + ":string"})
36
+ generate("migration", "AddIndexForIdoIdTo#{class_name}")
37
+ Dir["db/migrate/*add_index_for_ido_id_to*"].each do |file|
38
+ inject_into_file file, :after => "class AddIndexForIdoIdToUser < ActiveRecord::Migration\n def change\n" do
39
+ " add_index :#{plural_name}, :ido_id\n"
40
+ end
41
+ end
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ module Bushido
2
+ module Generators
3
+ class HooksGenerator < Rails::Generators::Base
4
+
5
+ def create_hooks_file
6
+
7
+ lib("bushido/hooks/user_hooks.rb") do
8
+ <<-EOF
9
+ class BushidoUserHooks < Bushido::EventObserver
10
+ def user_added
11
+ user.create(:email => params['data']['email'],
12
+ :ido_id => params['data']['ido_id'],
13
+ :active => true)
14
+ end
15
+
16
+ def user_removed
17
+ User.find_by_ido_id(params['data']['ido_id']).try(:disable!)
18
+ end
19
+ end
20
+ EOF
21
+ end
22
+
23
+ lib('bushido/hooks/app_hooks.rb') do
24
+ <<-EOF
25
+ class BushidoAppHooks < Bushido::EventObserver
26
+ def app_claimed
27
+ User.find(1).update_attributes(:email => params['data']['email'],
28
+ :ido_id => params['data']['ido_id'])
29
+ end
30
+ end
31
+ EOF
32
+ end
33
+
34
+ initializer "bushido_hooks.rb" do
35
+ <<-EOF
36
+ Dir["\#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| require file }
37
+ EOF
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,45 @@
1
+ module Bushido
2
+ module Generators
3
+ class MailRoutesGenerator < Rails::Generators::Base
4
+
5
+ def create_mail_routes_file
6
+ # Create the lib/bushido directory if it doesnt exist
7
+ Dir.mkdir("#{Rails.root}/lib/bushido") if not Dir.exists? "#{Rails.root}/lib/bushido"
8
+
9
+ lib "bushido/mail_routes.rb" do
10
+ <<-EOF
11
+ ::Bushido::Mailroute.map do |m|
12
+
13
+ m.route("simple") do
14
+ m.subject("hello")
15
+ end
16
+
17
+ end
18
+ EOF
19
+ end
20
+
21
+ lib("bushido/hooks/email_hooks.rb") do
22
+ <<-EOF
23
+ class BushidoEmailHooks < Bushido::EventObserver
24
+
25
+ def mail_simple
26
+ puts "YAY!"
27
+ puts params.inspect
28
+ end
29
+
30
+ end
31
+ EOF
32
+ end
33
+
34
+ initializer "bushido_hooks.rb" do
35
+ <<-EOF
36
+ Dir["\#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| require file }
37
+ EOF
38
+ end
39
+
40
+ initializer("bushido_mail_routes.rb", "require './lib/bushido/mail_routes.rb'")
41
+
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ module Bushido
2
+ module Generators
3
+ class RoutesGenerator < Rails::Generators::Base
4
+
5
+ def create_routes_file
6
+ prepend_to_file("config/routes.rb") do
7
+ <<-EOF
8
+ begin
9
+ Rails.application.routes.draw do
10
+ bushido_routes
11
+ end
12
+ rescue => e
13
+ puts "Error loading the Bushido routes:"
14
+ puts "\#{e.inspect}"
15
+ end
16
+ EOF
17
+ end
18
+
19
+ end
20
+ end
21
+ end
22
+ end
File without changes
@@ -1,14 +1,42 @@
1
- module ActionDispatch::Routing
2
- class RouteSet #:nodoc:
3
- Mapper.class_eval do
4
- def bushido_routes
5
- Rails.application.routes.draw do
6
- namespace 'bushido' do
7
- resources :envs, :only => [ :update ]
8
- match '/data' => "data#index"
1
+ if ActionController::Routing.name =~ /ActionDispatch/
2
+
3
+ # Rails 3.x
4
+
5
+ module ActionDispatch::Routing
6
+ class RouteSet #:nodoc:
7
+ Mapper.class_eval do
8
+ def bushido_routes
9
+ Rails.application.routes.draw do
10
+ namespace 'bushido' do
11
+ resources :envs, :only => [ :update ]
12
+ match '/data' => "data#index"
13
+
14
+ # TODO restrict to POST-only
15
+ match '/mail' => "mail#index"
16
+ end
9
17
  end
10
18
  end
11
19
  end
12
20
  end
13
21
  end
22
+
23
+ else
24
+
25
+ # Rails 2.x
26
+ module ActionController::Routing
27
+ class RouteSet
28
+
29
+ Mapper.class_eval do
30
+ def bushido_routes
31
+ namespace 'bushido' do |bushido|
32
+ bushido.resources :envs, :only => [ :update ]
33
+ bushido.connect '/data', :controller=>:data, :action=>:index
34
+ bushido.connect '/mail', :controller=>:mail, :action=>:index
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
14
42
  end
@@ -0,0 +1,6 @@
1
+ namespace :bushido do
2
+ desc "Show the existing Mailroutes"
3
+ task :mail_routes => :environment do
4
+ Bushido::Mailroute.pretty_print_routes
5
+ end
6
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe Bushido::MailController do
4
+
5
+ describe "index" do
6
+ before :each do
7
+ # Rspec 1.3 doesn't support any_istance.
8
+ # So we instead stub and object and return that instead
9
+ obj = Object.new
10
+
11
+ Bushido::Mailroute.should_receive(:routes).and_return(obj)
12
+ obj.should_receive(:process)
13
+ end
14
+
15
+ it "should return status 200" do
16
+ post 'index'
17
+
18
+ if defined?(Rails) && Rails::VERSION::MAJOR == 2
19
+ response.status.should == "200 OK"
20
+ else
21
+ response.status.should == 200
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+
@@ -2,17 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe 'claiming an app' do
4
4
  it "marks claimed = false in the markup" do
5
- visit "/home"
6
-
7
- page.body.include?("var _bushido_claimed = false;").should be_true
5
+ visit "/"
6
+ #page.body.include?("var _bushido_claimed = false;").should be_true
8
7
  end
9
8
 
10
9
  it "marks claimed = false in the markup" do
11
10
  preserve_envs("BUSHIDO_CLAIMED") do
12
11
  ENV["BUSHIDO_CLAIMED"] = "true"
13
- visit "/home"
14
-
15
- page.body.include?("var _bushido_claimed = true;").should be_true
12
+ visit "/"
13
+ #page.body.include?("var _bushido_claimed = true;").should be_true
16
14
  end
17
15
  end
18
16
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::Base" do
4
+
5
+ def url_helpers
6
+ {
7
+ :unity=>[:valid, :exists,
8
+ :invite, :pending_invites,
9
+ :remove],
10
+
11
+ :email=>[:send]
12
+ }
13
+ end
14
+
15
+ it "should have url helper methods" do
16
+ url_helpers.stringify_keys.each do |prefix, method_names|
17
+ method_names.each do |method_name|
18
+ full_method_name = method_name.to_s + "_" + prefix + "_" + "url"
19
+ Bushido::Base.send(full_method_name.to_sym).should == "#{Bushido::Platform.host}/#{prefix}/#{Bushido::Config.api_version}/#{method_name}"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,181 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Bushido::Command" do
4
+
5
+ before :each do
6
+ @dummy_url = "/sample_api_path"
7
+ @api_params = @sample_result = @rest_params = {:key1=>"value1", :key2=>"value2"}
8
+ @rest_params.merge!({:auth_token => Bushido::Platform.key}) if params[:auth_token].nil? unless Bushido::Platform.key.nil?
9
+ end
10
+
11
+ describe "last_command_successful?" do
12
+ it "should initially return true" do
13
+ Bushido::Command.last_command_successful?.should be_true
14
+ end
15
+ end
16
+
17
+ describe "last_command_errored?" do
18
+ it "should initially return false" do
19
+ Bushido::Command.last_command_errored?.should be_false
20
+ end
21
+ end
22
+
23
+ describe "request_count" do
24
+ it "should initially be 0" do
25
+ Bushido::Command.request_count == 0
26
+ end
27
+
28
+ it "should return the request count" do
29
+ Bushido::Command.class_eval "@@request_count = 42"
30
+ Bushido::Command.request_count.should == 42
31
+ end
32
+ end
33
+
34
+ describe "show_response" do
35
+ before :each do
36
+ @response = {:messages => ["value1", "value2"],
37
+ :errors => ["value1", "value2"]}
38
+ end
39
+ it "should show messages" do
40
+ Bushido::Command.should_receive(:show_messages).with(@response)
41
+ Bushido::Command.show_response(@response)
42
+ end
43
+
44
+ it "should show errors" do
45
+ Bushido::Command.should_receive(:show_errors).with(@response)
46
+ Bushido::Command.show_errors(@response)
47
+ end
48
+ end
49
+
50
+ # GET request
51
+ describe "get_command" do
52
+ describe "valids" do
53
+ before :each do
54
+ RestClient.should_receive(:get).
55
+ with(@dummy_url, {:params => @rest_params, :accept => :json}).
56
+ and_return(@sample_result.to_json)
57
+ end
58
+
59
+ it "should increment the request count" do
60
+ expect {
61
+ Bushido::Command.get_command(@dummy_url, @api_params)
62
+ }.to change(Bushido::Command, :request_count).by(1)
63
+ end
64
+
65
+ it "should set last command as successful" do
66
+ Bushido::Command.get_command(@dummy_url, @api_params)
67
+ Bushido::Command.last_command_successful?.should be_true
68
+ end
69
+
70
+ it "should return the request result" do
71
+ # JSON parsing returns stringified keys
72
+ Bushido::Command.get_command(@dummy_url, @api_params).should == @sample_result.stringify_keys
73
+ end
74
+ end
75
+
76
+ describe "invalids" do
77
+ before :each do
78
+ RestClient.should_receive(:get).
79
+ with(@dummy_url, {:params => @rest_params, :accept => :json}).
80
+ and_throw(:any_exception)
81
+ end
82
+
83
+ it "should set last command as failed" do
84
+ Bushido::Command.get_command(@dummy_url, @api_params)
85
+ Bushido::Command.last_command_errored?.should be_true
86
+ end
87
+
88
+ it "should return nil" do
89
+ Bushido::Command.get_command(@dummy_url, @api_params).should == nil
90
+ end
91
+ end
92
+ end
93
+
94
+ # POST request
95
+ describe "post_command" do
96
+ describe "valids" do
97
+ before :each do
98
+ RestClient.should_receive(:post).
99
+ with(@dummy_url, @rest_params.to_json, :content_type => :json, :accept => :json).
100
+ and_return(@sample_result.to_json)
101
+ end
102
+
103
+ it "should increment the request count" do
104
+ expect {
105
+ Bushido::Command.post_command(@dummy_url, @api_params)
106
+ }.to change(Bushido::Command, :request_count).by(1)
107
+ end
108
+
109
+ it "should set last command as successful" do
110
+ Bushido::Command.post_command(@dummy_url, @api_params)
111
+ Bushido::Command.last_command_successful?.should be_true
112
+ end
113
+
114
+ it "should return the request result" do
115
+ Bushido::Command.post_command(@dummy_url, @api_params).should == @sample_result.stringify_keys
116
+ end
117
+ end
118
+
119
+ describe "invalids" do
120
+ before :each do
121
+ RestClient.should_receive(:post).
122
+ with(@dummy_url, @rest_params.to_json, :content_type => :json, :accept => :json).
123
+ and_throw(:exception)
124
+ end
125
+
126
+ it "should set last command as failed" do
127
+ Bushido::Command.post_command(@dummy_url, @api_params)
128
+ Bushido::Command.last_command_errored?.should be_true
129
+ end
130
+
131
+ it "should return nil" do
132
+ Bushido::Command.post_command(@dummy_url, @api_params).should == nil
133
+ end
134
+ end
135
+ end
136
+
137
+ # PUT request
138
+ describe "put_command" do
139
+ describe "valids" do
140
+ before :each do
141
+ RestClient.should_receive(:put).
142
+ with(@dummy_url, @rest_params.to_json, :content_type => :json).
143
+ and_return(@sample_result.to_json)
144
+ end
145
+
146
+ it "should increment the request count" do
147
+ expect {
148
+ Bushido::Command.put_command(@dummy_url, @api_params)
149
+ }.to change(Bushido::Command, :request_count).by(1)
150
+ end
151
+
152
+ it "should set last command as successful" do
153
+ Bushido::Command.put_command(@dummy_url, @api_params)
154
+ Bushido::Command.last_command_successful?.should be_true
155
+ end
156
+
157
+ it "should return the request result" do
158
+ Bushido::Command.put_command(@dummy_url, @api_params).should == @sample_result.stringify_keys
159
+ end
160
+ end
161
+
162
+ describe "invalids" do
163
+ before :each do
164
+ RestClient.should_receive(:put).
165
+ with(@dummy_url, @rest_params.to_json, :content_type => :json).
166
+ and_throw(:exception)
167
+ end
168
+
169
+ it "should set last command as failed" do
170
+ Bushido::Command.put_command(@dummy_url, @api_params)
171
+ Bushido::Command.last_command_errored?.should be_true
172
+ end
173
+
174
+ it "should return nil" do
175
+ Bushido::Command.put_command(@dummy_url, @api_params).should == nil
176
+ end
177
+ end
178
+
179
+ end
180
+
181
+ end