Skip to content

Commit

Permalink
adding support for basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed May 23, 2013
1 parent 0ddfc91 commit 7154708
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 207 deletions.
7 changes: 1 addition & 6 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
source 'http://rubygems.org'

gem 'rails', '>=3.1.0'
gem 'jquery-rails'

group :development do
gem 'jeweler'
end
gemspec
33 changes: 6 additions & 27 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
require 'rubygems'
require 'rake/testtask'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = 'api_docs'
gem.homepage = 'http://github.com/twg/api_docs'
gem.license = 'MIT'
gem.summary = 'Generate API documentation using integration tests in Ruby on Rails 3'
gem.description = 'Generate API documentation using integration tests in Ruby on Rails 3'
gem.email = '[email protected]'
gem.authors = ['Oleg Khabarov', 'Jack Neto', 'The Working Group Inc.']
end
Jeweler::RubygemsDotOrgTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
Rake::TestTask.new(:test) do |test|
test.libs << 'lib' << 'test'
test.pattern = 'test/**/*_test.rb'
test.verbose = true
end


require 'rake/testtask'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end
task :default => :test
120 changes: 18 additions & 102 deletions api_docs.gemspec
Original file line number Diff line number Diff line change
@@ -1,104 +1,20 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# encoding: utf-8

Gem::Specification.new do |s|
s.name = "api_docs"
s.version = "1.0.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Oleg Khabarov", "Jack Neto", "The Working Group Inc."]
s.date = "2013-02-28"
s.description = "Generate API documentation using integration tests in Ruby on Rails 3"
s.email = "[email protected]"
s.extra_rdoc_files = [
"LICENSE",
"README.md"
]
s.files = [
"Gemfile",
"LICENSE",
"README.md",
"Rakefile",
"VERSION",
"api_docs.gemspec",
"app/assets/javascripts/api_docs.js",
"app/assets/stylesheets/api_docs.css",
"app/controllers/api_docs/docs_controller.rb",
"app/views/api_docs/docs/_action.html.erb",
"app/views/api_docs/docs/_request_params.html.erb",
"app/views/api_docs/docs/index.html.erb",
"config/routes.rb",
"doc/screenshot.png",
"lib/api_docs.rb",
"lib/api_docs/configuration.rb",
"lib/api_docs/engine.rb",
"lib/api_docs/test_helper.rb",
"lib/tasks/api_docs.rake",
"script/rails",
"test/api_docs_test.rb",
"test/docs_controller_test.rb",
"test/dummy/README.rdoc",
"test/dummy/Rakefile",
"test/dummy/app/assets/images/glyphicons-halflings-white.png",
"test/dummy/app/assets/images/glyphicons-halflings.png",
"test/dummy/app/assets/javascripts/application.js",
"test/dummy/app/assets/javascripts/bootstrap.min.js",
"test/dummy/app/assets/stylesheets/application.css",
"test/dummy/app/assets/stylesheets/bootstrap.min.css",
"test/dummy/app/controllers/application_controller.rb",
"test/dummy/app/helpers/application_helper.rb",
"test/dummy/app/mailers/.gitkeep",
"test/dummy/app/models/.gitkeep",
"test/dummy/app/views/layouts/application.html.erb",
"test/dummy/config.ru",
"test/dummy/config/application.rb",
"test/dummy/config/boot.rb",
"test/dummy/config/environment.rb",
"test/dummy/config/environments/development.rb",
"test/dummy/config/environments/production.rb",
"test/dummy/config/environments/test.rb",
"test/dummy/config/initializers/backtrace_silencers.rb",
"test/dummy/config/initializers/inflections.rb",
"test/dummy/config/initializers/mime_types.rb",
"test/dummy/config/initializers/secret_token.rb",
"test/dummy/config/initializers/session_store.rb",
"test/dummy/config/initializers/wrap_parameters.rb",
"test/dummy/config/locales/en.yml",
"test/dummy/config/routes.rb",
"test/dummy/lib/assets/.gitkeep",
"test/dummy/log/.gitkeep",
"test/dummy/public/404.html",
"test/dummy/public/422.html",
"test/dummy/public/500.html",
"test/dummy/public/favicon.ico",
"test/dummy/script/rails",
"test/test_helper.rb",
"test/test_helper_test.rb"
]
s.homepage = "http://github.com/twg/api_docs"
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = "1.8.23"
s.summary = "Generate API documentation using integration tests in Ruby on Rails 3"

if s.respond_to? :specification_version then
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<rails>, [">= 3.1.0"])
s.add_runtime_dependency(%q<jquery-rails>, [">= 0"])
s.add_development_dependency(%q<jeweler>, [">= 0"])
else
s.add_dependency(%q<rails>, [">= 3.1.0"])
s.add_dependency(%q<jquery-rails>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
end
else
s.add_dependency(%q<rails>, [">= 3.1.0"])
s.add_dependency(%q<jquery-rails>, [">= 0"])
s.add_dependency(%q<jeweler>, [">= 0"])
end
end
$:.unshift File.expand_path('../lib', __FILE__)
require 'api_docs/version'

Gem::Specification.new do |s|
s.name = "api_docs"
s.version = ApiDocs::VERSION
s.authors = ["Oleg Khabarov"]
s.email = ["[email protected]"]
s.homepage = "http://github.com/twg/api_docs"
s.summary = "Generate API documentation using integration tests in Ruby on Rails"
s.description = "Generate API documentation using integration tests in Ruby on Rails"

s.files = `git ls-files`.split("\n")
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']

s.add_dependency 'rails', '>=3.1.0'
end
1 change: 1 addition & 0 deletions lib/api_docs.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'api_docs/version'
require 'api_docs/engine'
require 'api_docs/configuration'
require 'api_docs/test_helper'
Expand Down
100 changes: 53 additions & 47 deletions lib/api_docs/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,74 @@
module ApiDocs::TestHelper

# Method that allows test creation and will document results in a YAML file
# Example usage:
# api_call(:get, '/users/:id', :id => 12345) do |doc|
# doc.description = 'Something for the docs'
# ... regular test code
# end
def api_call(method, path, params = { })
parsed_params = params.dup
parsed_path = path.dup
module InstanceMethods
# Method that allows test creation and will document results in a YAML file
# Example usage:
# api_call(:get, '/users/:id', :id => 12345) do |doc|
# doc.description = 'Something for the docs'
# ... regular test code
# end
def api_call(method, path, params = { })
parsed_params = params.dup
parsed_path = path.dup

parsed_params.each do |k, v|
parsed_params.delete(k) if parsed_path.gsub!(":#{k}", v.to_s)
end
parsed_params.each do |k, v|
parsed_params.delete(k) if parsed_path.gsub!(":#{k}", v.to_s)
end

if credentials = parsed_params.delete('HTTP_AUTHORIZATION')
auth = {'HTTP_AUTHORIZATION' => credentials}
end

# Making actual test request. Based on the example above:
# get '/users/12345'
doc = OpenStruct.new
send(method, parsed_path, parsed_params)
# Making actual test request. Based on the example above:
# get '/users/12345'
doc = OpenStruct.new
send(method, parsed_path, parsed_params, auth)

yield doc if block_given?
yield doc if block_given?

# Assertions inside test block didn't fail. Preparing file
# content to be written
c = request.filtered_parameters['controller']
a = request.filtered_parameters['action']
# Assertions inside test block didn't fail. Preparing file
# content to be written
c = request.filtered_parameters['controller']
a = request.filtered_parameters['action']

file_path = File.expand_path("#{c.gsub('/', ':')}.yml", ApiDocs.config.docs_path)
params = api_deep_clean_params(params)
file_path = File.expand_path("#{c.gsub('/', ':')}.yml", ApiDocs.config.docs_path)
params = ApiDocs::TestHelper.api_deep_clean_params(params)

# Marking response as an unique
key = 'ID-' + Digest::MD5.hexdigest("
#{method}#{path}#{doc.description}#{params}#{response.status}}
")
# Marking response as an unique
key = 'ID-' + Digest::MD5.hexdigest("
#{method}#{path}#{doc.description}#{params}#{response.status}}
")

data = if File.exists?(file_path)
YAML.load_file(file_path) rescue Hash.new
else
Hash.new
end
data = if File.exists?(file_path)
YAML.load_file(file_path) rescue Hash.new
else
Hash.new
end

data[a] ||= { }
data[a][key] = {
'description' => doc.description,
'method' => request.method,
'path' => path,
'params' => api_deep_clean_params(params),
'status' => response.status,
'body' => response.body
}
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w'){|f| f.write(data.to_yaml)}
data[a] ||= { }
data[a][key] = {
'description' => doc.description,
'method' => request.method,
'path' => path,
'params' => ApiDocs::TestHelper.api_deep_clean_params(params),
'status' => response.status,
'body' => response.body
}
FileUtils.mkdir_p(File.dirname(file_path))
File.open(file_path, 'w'){|f| f.write(data.to_yaml)}
end
end

# Cleans up params. Removes things like File object handlers
# Sets up ignored values so we don't generate new keys for same data
def api_deep_clean_params(params)
def self.api_deep_clean_params(params)
case params
when Hash
params.each_with_object({}) do |(key, value), res|
res[key.to_s] = api_deep_clean_params(value)
res[key.to_s] = ApiDocs::TestHelper.api_deep_clean_params(value)
end
when Array
params.collect{|value| api_deep_clean_params(value)}
params.collect{|value| ApiDocs::TestHelper.api_deep_clean_params(value)}
else
case params
when Rack::Test::UploadedFile
Expand All @@ -74,4 +80,4 @@ def api_deep_clean_params(params)
end
end

ActionDispatch::IntegrationTest.send :include, ApiDocs::TestHelper
ActionDispatch::IntegrationTest.send :include, ApiDocs::TestHelper::InstanceMethods
3 changes: 3 additions & 0 deletions lib/api_docs/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ApiDocs
VERSION = "1.0.5"
end
24 changes: 5 additions & 19 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery

def index
respond_to do |format|
format.json {
render :text => [{
:id => 1,
:name => 'Test User',
:created_at => 1.day.ago
}].to_json
}
format.xml {
render :text => [{
:id => 1,
:name => 'Test User',
:created_at => 1.day.ago
}].to_xml(:root => 'users')
}
end
end
http_basic_authenticate_with :name => 'user', :password => 'secret', :only => :authenticate

def show
status = :ok
Expand All @@ -41,4 +23,8 @@ def show
end
end
end

def authenticate
render :text => {:message => 'Authenticated'}.to_json
end
end
3 changes: 2 additions & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
mount ApiDocs::Engine => '/'
match '/users/:id' => 'application#show'
match '/users/:id' => 'application#show'
match '/authenticate' => 'application#authenticate'
end
Loading

0 comments on commit 7154708

Please sign in to comment.