Merge pull request #1 from github/named-routes

Named routes
This commit is contained in:
Ben Lavender 2016-05-19 15:25:12 -05:00
Родитель 87496c8fb0 effd781871
Коммит 2173af1091
14 изменённых файлов: 94 добавлений и 451 удалений

1
.gitignore поставляемый
Просмотреть файл

@ -9,3 +9,4 @@
/tmp/
/bin/
/vendor/gems
/spec/dummy/log/

Просмотреть файл

@ -22,7 +22,7 @@ Some routing boilerplate is required in `config/routes.rb`:
```ruby
Rails.application.routes.draw do
post "/_chatops" => "chatops#execute"
post "/_chatops/:action", controller: "anonymous"
get "/_chatops" => "chatops#list"
end
```

Просмотреть файл

@ -8,13 +8,17 @@ Gem::Specification.new do |s|
s.name = "chatops_controller"
s.version = ChatopsController::VERSION
s.authors = ["Ben Lavender"]
s.homepage = "https://github.com/github/chatops_controller"
s.email = ["bhuga@github.com"]
s.summary = %q{Rails helpers to create JSON-RPC chatops}
s.license = "unknown - maybe we'll open source this?"
s.summary = %q{Rails helpers to create JSON-RPC chatops}
s.description = %q{See the README for documentation"}
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
s.files = Dir["{app,config,db,lib}/**/*", "README.md"]
s.test_files = Dir["spec/**/*"]
s.add_dependency "rails", "~> 4.2.6"
s.add_dependency "rails", "~> 4.0"
s.add_development_dependency "rspec-rails"
s.add_development_dependency "rspec-rails", "~> 0"
s.add_development_dependency "pry", "~> 0"
end

Просмотреть файл

@ -3,24 +3,31 @@ module ChatOps
extend ActiveSupport::Concern
included do
before_filter :ensure_chatops_authenticated, :only => [:execute, :list]
before_filter :ensure_user_given, :only => [:execute, :list]
end
def execute
method = params[:method].to_sym
return jsonrpc_method_not_found unless self.class.chatops[method].present?
send method
before_filter :ensure_chatops_authenticated
before_filter :ensure_user_given
before_filter :ensure_method_exists
end
def list
chatops = self.class.chatops
chatops.each { |name, hash| hash[:path] = name }
render :json => {
namespace: self.class.chatops_namespace,
help: self.class.chatops_help,
methods: self.class.chatops }
methods: chatops }
end
def process(*args)
if params[:method].present?
params[:action] = params[:method]
end
super
rescue AbstractController::ActionNotFound
return jsonrpc_method_not_found
end
protected
def jsonrpc_params
params["params"] || {}
end
@ -58,13 +65,13 @@ module ChatOps
end
def ensure_user_given
return true unless params[:action] == "execute"
return true unless chatop_names.include?(params[:action].to_sym)
return true if params[:user].present?
jsonrpc_invalid_params("A username must be supplied as 'user'")
end
def ensure_chatops_authenticated
return true unless %w{execute list}.include?(params[:action])
return true unless (chatop_names + [:list]).include?(params[:action].to_sym)
authenticated = authenticate_with_http_basic do |u, p|
if ENV["CHATOPS_AUTH_TOKEN"].nil?
raise StandardError, "Attempting to authenticate chatops with nil token"
@ -81,6 +88,14 @@ module ChatOps
end
end
def ensure_method_exists
return jsonrpc_method_not_found unless (chatop_names + [:list]).include?(params[:action].to_sym)
end
def chatop_names
self.class.chatops.keys
end
module ClassMethods
def chatop(method_name, regex, help, &block)
chatops[method_name] = { help: help,

Просмотреть файл

@ -7,7 +7,7 @@ module ChatOpsSpecHelpers
args = params.dup.symbolize_keys
user = args.delete :user
post(:execute, {:method => method, :user => user, :params => args})
post method, :method => method, :user => user, :params => args
end
def chatop_response

Просмотреть файл

@ -14,13 +14,13 @@ Rails.application.configure do
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
#config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
#config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large

Просмотреть файл

@ -1,4 +0,0 @@
 (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
 (0.4ms) select sqlite_version(*)
 (1.3ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"

Просмотреть файл

@ -1,416 +0,0 @@
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 7ms (Views: 6.5ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 4ms (Views: 3.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 6ms (Views: 5.6ms)
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 4ms (Views: 3.5ms)
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 2ms (Views: 1.8ms)
Processing by AnonymousController#list as HTML
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 500 Internal Server Error in 0ms
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 5ms (Views: 5.1ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 1.0ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.4ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.3ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 2ms (Views: 2.0ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 1ms (Views: 0.5ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 1ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 1ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 5ms (Views: 5.1ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 1ms (Views: 0.4ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 1ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 2ms (Views: 2.1ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.3ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 5ms (Views: 4.6ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.4ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.5ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 1ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 2ms (Views: 1.9ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 1ms (Views: 0.3ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 5ms (Views: 4.4ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 2ms (Views: 1.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 1ms (Views: 0.4ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 1ms (Views: 0.4ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 1ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 5ms (Views: 4.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 2ms (Views: 1.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#list as HTML
Rendered text template (0.0ms)
Filter chain halted as :ensure_chatops_authenticated rendered or redirected
Completed 403 Forbidden in 0ms (Views: 0.2ms)
Processing by AnonymousController#non_chatop_method as HTML
Rendered text template (0.0ms)
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#list as HTML
Completed 200 OK in 0ms (Views: 0.3ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar"}
Filter chain halted as :ensure_user_given rendered or redirected
Completed 400 Bad Request in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"barfoo", "user"=>"foo"}
Completed 404 Not Found in 1ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"foobar", "user"=>"foo"}
Completed 200 OK in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.1ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"foo"}}
Completed 200 OK in 0ms (Views: 0.2ms)
Processing by AnonymousController#execute as HTML
Parameters: {"method"=>"wcid", "user"=>"foo", "params"=>{"app"=>"nope"}}
Completed 400 Bad Request in 0ms (Views: 0.2ms)

Просмотреть файл

@ -6,6 +6,8 @@ describe ActionController::Base, type: :controller do
chatops_namespace :test
chatops_help "ChatOps of and relating to testing"
before_filter :ensure_app_given, :only => [:wcid]
chatop :wcid,
/(?:where can i deploy|wcid)(?: (?<app>\S+))?/,
"where can i deploy?" do
@ -20,16 +22,26 @@ describe ActionController::Base, type: :controller do
jsonrpc_success "You just foo and bar like it just don't matter"
end
skip_before_filter :ensure_method_exists, only: :non_chatop_method
def non_chatop_method
render :text => "Why would you have something thats not a chatop?"
end
def unexcluded_chatop_method
render :text => "Sadly, I'll never be reached"
end
def ensure_app_given
return jsonrpc_invalid_params("I need an app, every time") unless jsonrpc_params[:app].present?
end
end
before :each do
routes.draw do
post "/_chatops" => "anonymous#execute"
get "/_chatops" => "anonymous#list"
post "/_chatops/:action", controller: "anonymous"
get "/other" => "anonymous#non_chatop_method"
get "/other_will_fail" => "anonymous#unexcluded_chatop_method"
end
ENV["CHATOPS_AUTH_TOKEN"] = "foo"
@ -84,20 +96,21 @@ describe ActionController::Base, type: :controller do
"wcid" => {
"help" => "where can i deploy?",
"regex" => /(?:where can i deploy|wcid)(?: (?<app>\S+))?/.source,
"params" => ["app"]
"params" => ["app"],
"path" => "wcid"
},
"foobar" => {
"help" => "how to foo and bar",
"regex" => /(?:how can i foo and bar all at once)?/.source,
"params" => []
"params" => [],
"path" => "foobar"
}
}
})
end
it "requires a user be sent to chatops" do
post :execute, :method => "foobar"
post :foobar
expect(response.status).to eq 400
expect(json_response).to eq({
"jsonrpc" => "2.0",
@ -110,7 +123,20 @@ describe ActionController::Base, type: :controller do
end
it "returns method not found for a not found method" do
post :execute, :method => "barfoo", :user => "foo"
post :barfoo, :user => "foo"
expect(json_response).to eq({
"jsonrpc" => "2.0",
"id" => nil,
"error" => {
"code" => -32601,
"message" => "Method not found"
}
})
expect(response.status).to eq 404
end
it "requires skipping a before_filter to find non-chatop methods, sorry about that" do
get :unexcluded_chatop_method
expect(json_response).to eq({
"jsonrpc" => "2.0",
"id" => nil,
@ -123,7 +149,7 @@ describe ActionController::Base, type: :controller do
end
it "runs a known method" do
post :execute, :method => "foobar", :user => "foo"
post :foobar, :user => "foo"
expect(json_response).to eq({
"jsonrpc" => "2.0",
"id" => nil,
@ -133,7 +159,7 @@ describe ActionController::Base, type: :controller do
end
it "passes parameters to methods" do
post :execute, :method => "wcid", :user => "foo", :params => { "app" => "foo" }
post :wcid, :user => "foo", :params => { "app" => "foo" }
expect(json_response).to eq({
"jsonrpc" => "2.0",
"id" => nil,
@ -142,8 +168,21 @@ describe ActionController::Base, type: :controller do
expect(response.status).to eq 200
end
it "uses typical controller fun like before_filter" do
post :wcid, :user => "foo", :params => {}
expect(json_response).to eq({
"jsonrpc" => "2.0",
"id" => nil,
"error" => {
"code" => -32602,
"message" => "I need an app, every time"
}
})
expect(response.status).to eq 400
end
it "allows methods to return invalid params with a message" do
post :execute, :method => "wcid", :user => "foo", :params => { "app" => "nope" }
post :wcid, :user => "foo", :params => { "app" => "nope" }
expect(response.status).to eq 400
expect(json_response).to eq({
"jsonrpc" => "2.0",

Просмотреть файл

@ -5,6 +5,7 @@ require File.expand_path("../dummy/config/environment", __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'pry'
require 'rspec/rails'
require 'chatops/controller/rspec'
# Add additional requires below this line. Rails is not loaded until this point!
@ -32,7 +33,10 @@ Dir[Rails.root.join "spec/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.alias_example_to :fit, :focus => true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.

Двоичные данные
vendor/cache/coderay-1.1.1.gem поставляемый Normal file

Двоичный файл не отображается.

Двоичные данные
vendor/cache/method_source-0.8.2.gem поставляемый Normal file

Двоичный файл не отображается.

Двоичные данные
vendor/cache/pry-0.10.3.gem поставляемый Normal file

Двоичный файл не отображается.

Двоичные данные
vendor/cache/slop-3.6.0.gem поставляемый Normal file

Двоичный файл не отображается.