rspec-swagger 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 69120da3aba2240072d19dbf8ba8236e0bf79892
4
- data.tar.gz: 253427f8a67826e42ae7aa67283cd1ecf5ce9a0c
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NGNkZjA5NThmNjU0Yzg1ZDhjNWM4ODcyZjUyMTYwZmQyZDg3MDI4Zg==
5
+ data.tar.gz: !binary |-
6
+ ODI1NDBiNGY4M2Q1ZjlhZTdmYjAzMTZlMDljZGE1MjI0MTA5MWVkMQ==
5
7
  SHA512:
6
- metadata.gz: 42d3e7de7b294c4578fccf786bd7da647da9998ad744784392755010c458b73547efee9a110d00158b0eaf60e7d43972e86fc0ec65b063dcf9dd19474b262d15
7
- data.tar.gz: 8ce96e472366e11b7e8044b983754c28df39fee5a0efbdd01e21982c8c048b1b0f6b74e534e069dc27bdd98ea5585d2ccade6918594e7ca961cdd99750447f06
8
+ metadata.gz: !binary |-
9
+ Njg5NDE5YjI0NmQ2N2MzNjUwMDc4MjVlYjFkNDJlYjMzOTNhZTgyNTg5NWQy
10
+ YjdhNjA5ZDZlYmQ3MTc0OTI0OGNjMjk2Y2Y5YTMwNWRkZjQ2N2Q0NmI3ZjIz
11
+ YWQ1NTE1YzQ1ODU2YzRlOWUwN2Y1NDVlNDI1NGVkOWJjYmFkYWM=
12
+ data.tar.gz: !binary |-
13
+ OTUyNmUzNGQ1MzgzOTlmMzQxOWJjN2I2MzkwM2U1YTEwMTRlZWFmMmY3MzFk
14
+ MWE3OWJjNGU2ZDk2MDkxMzBlZDQ2MzEzNmIyYzQ3ZTNjMGUxNTVkMmQ1YjQx
15
+ MjFiODRmZjUzNGIzMjllNGMwMWQzOGNiNGYyNmQ2MWEzZWE3MDE=
data/Rakefile CHANGED
@@ -1,2 +1,4 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/swagger/tasks'
2
3
 
4
+ task default: ['rspec:swagger:install']
@@ -1,7 +1,13 @@
1
+ require 'colorize'
2
+
3
+ require "rspec/swagger/formatter"
4
+ require "rspec/swagger/routes_with_no_required_params"
5
+ require "rspec/swagger/routes_with_sample_values"
6
+ require "rspec/swagger/swagger_loader"
7
+ require "rspec/swagger/test_helpers"
1
8
  require "rspec/swagger/version"
2
9
 
3
10
  module Rspec
4
11
  module Swagger
5
- # Your code goes here...
6
12
  end
7
13
  end
@@ -0,0 +1,35 @@
1
+ module Rspec
2
+ module Swagger
3
+ class Formatter
4
+ def initialize(table_widths: [25, 40])
5
+ @table_widths = table_widths.freeze
6
+ end
7
+
8
+ # format()
9
+ #
10
+ # Set passed to true for green output, false for red. You can color
11
+ # any part of the left or right text by using <...> (which are invalid
12
+ # URL characters anyway, so shouldn't ever appear in the output
13
+ def format(passed: true, left_text: "", right_text: "")
14
+ if passed
15
+ str = "<.> "
16
+ else
17
+ str = "<F> "
18
+ end
19
+
20
+ str += sprintf("%#{@table_widths[0]}.#{@table_widths[0]}s | ", left_text)
21
+ str += sprintf("%-#{@table_widths[1]}.#{@table_widths[1]}s", right_text)
22
+
23
+ if passed
24
+ str = str.gsub(/\<(.*?)\>/) { |match| $1.green }
25
+ str = str.gsub(/\<(.*?)$/) { |match| $1.green }
26
+ else
27
+ str = str.gsub(/\<(.*?)\>/) { |match| $1.red }
28
+ str = str.gsub(/\<(.*?)$/) { |match| $1.red }
29
+ end
30
+
31
+ puts str
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ module Rspec
2
+ module Swagger
3
+ module RoutesWithNoRequiredParams
4
+ def test_200_for_routes_with_no_required_parameters
5
+ @loader.documented_paths.each do |api|
6
+ unless api =~ /\{.*\}/
7
+ # see if the API returns a 200 code
8
+ begin
9
+ get api
10
+
11
+ @formatter.format passed: last_response.status == 200,
12
+ left_text: api,
13
+ right_text: "was <#{last_response.status}>"
14
+ rescue
15
+ @formatter.format passed: false,
16
+ left_text: api,
17
+ right_text: "<#{$!.message}>"
18
+ end
19
+
20
+ # see if the 200 response is documented
21
+ if operation = @loader.operation(:GET, api)
22
+ passed = false
23
+
24
+ operation["responseMessages"].each do |msg|
25
+ passed = true if msg["code"] == 200
26
+ end
27
+
28
+ @formatter.format passed: passed,
29
+ left_text: api,
30
+ right_text: "200 #{"<not> " unless passed}<documented>"
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,64 @@
1
+ module Rspec
2
+ module Swagger
3
+ module RoutesWithSampleValues
4
+ def test_sample_values_for_path_params_on_get_routes
5
+ @loader.documented_paths.each do |api|
6
+ if api =~ /\{.*\}/
7
+ if operation = @loader.operation(:GET, api)
8
+ ["200", "403", "404"].each do |code|
9
+ api_with_values = api.clone
10
+
11
+ operation["parameters"].each do |param|
12
+ if sample_value = sample_value_from_param(param, code)
13
+ api_with_values = api_with_values.gsub("{#{param['name']}}", sample_value)
14
+ end
15
+ end
16
+
17
+ unless api_with_values.include?("{")
18
+ begin
19
+ get api_with_values
20
+
21
+ @formatter.format passed: last_response.status.to_i == code.to_i,
22
+ left_text: api_with_values,
23
+ right_text: "was <#{last_response.status}>"
24
+
25
+ @formatter.format passed: last_response.status.to_i == code.to_i,
26
+ left_text: api_with_values,
27
+ right_text: "#{code} <documented>"
28
+ rescue
29
+ @formatter.format passed: false,
30
+ left_text: api_with_values,
31
+ right_text: "<#{$!.message}>"
32
+
33
+ @formatter.format passed: false,
34
+ left_text: api_with_values,
35
+ right_text: "#{code} <documented>"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def sample_value_from_param(param, code)
47
+ has_sample_values_hash = param["sampleValues"].respond_to?(:has_key?) &&
48
+ param["sampleValues"].has_key?(code.to_s)
49
+
50
+ if has_sample_values_hash
51
+ if param["sampleValues"][code.to_s].respond_to?(:has_key?)
52
+ param["sampleValues"][code.to_s][environment]
53
+ else
54
+ param["sampleValues"][code.to_s]
55
+ end
56
+ end
57
+ end
58
+
59
+ def environment
60
+ ENV["ENVIRONMENT"] || "development"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,48 @@
1
+ module Rspec
2
+ module Swagger
3
+ class SwaggerLoader
4
+ def initialize
5
+ @root = JSON.parse(File.read("docs/swagger.json"))
6
+
7
+ @resources = @root["apis"].map do |api|
8
+ path = "#{api['path']}.json"
9
+
10
+ if File.exists?("docs" + path)
11
+ JSON.parse(File.read("docs" + path))
12
+ else
13
+ raise "file not found: docs#{path}"
14
+ end
15
+ end
16
+
17
+ @root.freeze
18
+ @resources.freeze
19
+ end
20
+
21
+ attr_reader :root, :resources
22
+
23
+ # documented_paths()
24
+ #
25
+ # Get a list of routes that are documented. Returns an array of Strings.
26
+ def documented_paths
27
+ @documented_paths ||= begin
28
+ @resources.inject([]) { |memo, r| memo += r["apis"].map { |api| api["path"] } }
29
+ end
30
+ end
31
+
32
+ # operation()
33
+ #
34
+ # Get an operation by HTTP verb and path.
35
+ #
36
+ # Example: @loader.operation(:GET, "/foo")
37
+ def operation(verb, path)
38
+ @resources.each do |resource|
39
+ resource["apis"].each do |api|
40
+ api["operations"].each do |op|
41
+ return op if op["httpMethod"] == verb.to_s && path == api["path"]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ namespace :rspec do
2
+ namespace :swagger do
3
+ task default: [:install]
4
+
5
+ templates_dir = Rake.application.original_dir + "/templates"
6
+
7
+ desc "Install rspec-swagger."
8
+ task install: ["docs", "docs/swagger.json", "docs/example_resource.json", "spec", "spec/swagger_spec.rb"]
9
+
10
+ directory "docs"
11
+
12
+ file "docs/swagger.json" do
13
+ cp File.expand_path("#{templates_dir}/swagger.json", __FILE__), "docs/swagger.json"
14
+ end
15
+
16
+ file "docs/example_resource.json" do
17
+ cp File.expand_path("#{templates_dir}/example_resource.json", __FILE__), "docs/example_resource.json"
18
+ end
19
+
20
+ directory "spec"
21
+
22
+ file "spec/swagger_spec.rb" do
23
+ cp File.expand_path("#{templates_dir}/swagger_spec.rb", __FILE__), "spec/swagger_spec.rb"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ module Rspec
2
+ module Swagger
3
+ module TestHelpers
4
+ include RoutesWithNoRequiredParams
5
+ include RoutesWithSampleValues
6
+
7
+ # test_api_correctness()
8
+ #
9
+ # Tests the correctness of the Swagger-formatted docs stored
10
+ # in the docs/swagger.json and docs/*.json files. You can either
11
+ # run this method for all of the tests, or pick and choose from
12
+ # the tests referenced in this method.
13
+ def test_api_correctness
14
+ @loader = SwaggerLoader.new
15
+ @formatter = Formatter.new
16
+
17
+ test_200_for_routes_with_no_required_parameters
18
+ test_sample_values_for_path_params_on_get_routes
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Swagger
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -3,13 +3,16 @@ lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'rspec/swagger/version'
5
5
 
6
+ REQUIREMENT_LINE = "require 'rspec/swagger/tasks'\n"
7
+ RAKEFILE = 'Rakefile'
8
+
6
9
  Gem::Specification.new do |spec|
7
10
  spec.name = "rspec-swagger"
8
11
  spec.version = Rspec::Swagger::VERSION
9
12
  spec.authors = ["Colin Rymer"]
10
13
  spec.email = ["colin.rymer@gmail.com"]
11
- spec.summary = %q{Generate Swagger docs from your specs.}
12
- spec.description = %q{Generate Swagger docs from your specs.}
14
+ spec.summary = %q{Test Swagger-documented APIs automatically.}
15
+ spec.description = %q{Test Swagger-documented APIs automatically.}
13
16
  spec.homepage = ""
14
17
  spec.license = "MIT"
15
18
 
@@ -18,6 +21,18 @@ Gem::Specification.new do |spec|
18
21
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
22
  spec.require_paths = ["lib"]
20
23
 
24
+ spec.add_development_dependency "rspec"
21
25
  spec.add_development_dependency "bundler", "~> 1.7"
22
26
  spec.add_development_dependency "rake", "~> 10.0"
27
+
28
+ spec.add_dependency "colorize"
29
+
30
+ Gem.post_install do
31
+ if File.exists? RAKEFILE
32
+ original_file = File.read RAKEFILE
33
+ unless original_file.include?( REQUIREMENT_LINE )
34
+ File.write RAKEFILE, "#{REQUIREMENT_LINE}#{original_file}"
35
+ end
36
+ end
37
+ end
23
38
  end
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require "rspec/swagger"
7
+ require "rspec"
8
+ require 'rspec/autorun'
9
+
10
+ # Requires supporting ruby files with custom matchers and macros, etc,
11
+ # in spec/support/ and its subdirectories.
12
+ # Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
13
+
14
+ RSpec.configure do |config|
15
+ # == Mock Framework
16
+ #
17
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
18
+ #
19
+ # config.mock_with :mocha
20
+ # config.mock_with :flexmock
21
+ # config.mock_with :rr
22
+ config.mock_with :rspec
23
+ end
@@ -0,0 +1,26 @@
1
+ {
2
+ "apiVersion": "1",
3
+ "swaggerVersion": "1.2",
4
+ "basePath": "http://local.api.apartmentguide.com",
5
+ "resourcePath": "/example_resource",
6
+ "apis": [
7
+ {
8
+ "path": "/example_resource",
9
+ "description": "Example Resource",
10
+ "operations": [
11
+ {
12
+ "parameters": [],
13
+ "httpMethod": "GET",
14
+ "notes": null,
15
+ "embeds": [],
16
+ "scopes": [],
17
+ "errorResponses": [],
18
+ "summary": "Find example_resource",
19
+ "nickname": "index",
20
+ "responseClass": "string",
21
+ "responseTypeInternal": "string"
22
+ }
23
+ ]
24
+ }
25
+ ]
26
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ "apiVersion": "1",
3
+ "swaggerVersion": "1.2",
4
+ "apis": [
5
+ {
6
+ "path": "/example_resource",
7
+ "description": "Operations about example_resource"
8
+ }
9
+ ],
10
+ "info": {
11
+ "title": "Swagger Sample App",
12
+ "description": "This is a sample server. You can find out more about Swagger \n at <a href=\"http://swagger.wordnik.com\">http://swagger.wordnik.com</a> or on irc.freenode.net, #swagger. For this sample,\n you can use the api key \"special-key\" to test the authorization filters",
13
+ "termsOfServiceUrl": "http://helloreverb.com/terms/",
14
+ "contact": "apiteam@wordnik.com",
15
+ "license": "Apache 2.0",
16
+ "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html"
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rspec::Swagger do
4
+ include Rspec::Swagger::TestHelpers
5
+
6
+ it "should describe the api" do
7
+ test_api_correctness
8
+ end
9
+ end
metadata CHANGED
@@ -1,58 +1,96 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-swagger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Rymer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-08 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
- - - "~>"
31
+ - - ~>
18
32
  - !ruby/object:Gem::Version
19
33
  version: '1.7'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
- - - "~>"
38
+ - - ~>
25
39
  - !ruby/object:Gem::Version
26
40
  version: '1.7'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ~>
32
46
  - !ruby/object:Gem::Version
33
47
  version: '10.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ~>
39
53
  - !ruby/object:Gem::Version
40
54
  version: '10.0'
41
- description: Generate Swagger docs from your specs.
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Test Swagger-documented APIs automatically.
42
70
  email:
43
71
  - colin.rymer@gmail.com
44
72
  executables: []
45
73
  extensions: []
46
74
  extra_rdoc_files: []
47
75
  files:
48
- - ".gitignore"
76
+ - .gitignore
49
77
  - Gemfile
50
78
  - LICENSE.txt
51
79
  - README.md
52
80
  - Rakefile
53
81
  - lib/rspec/swagger.rb
82
+ - lib/rspec/swagger/formatter.rb
83
+ - lib/rspec/swagger/routes_with_no_required_params.rb
84
+ - lib/rspec/swagger/routes_with_sample_values.rb
85
+ - lib/rspec/swagger/swagger_loader.rb
86
+ - lib/rspec/swagger/tasks.rb
87
+ - lib/rspec/swagger/test_helpers.rb
54
88
  - lib/rspec/swagger/version.rb
55
89
  - rspec-swagger.gemspec
90
+ - spec/spec_helper.rb
91
+ - templates/example_resource.json
92
+ - templates/swagger.json
93
+ - templates/swagger_spec.rb
56
94
  homepage: ''
57
95
  licenses:
58
96
  - MIT
@@ -63,12 +101,12 @@ require_paths:
63
101
  - lib
64
102
  required_ruby_version: !ruby/object:Gem::Requirement
65
103
  requirements:
66
- - - ">="
104
+ - - ! '>='
67
105
  - !ruby/object:Gem::Version
68
106
  version: '0'
69
107
  required_rubygems_version: !ruby/object:Gem::Requirement
70
108
  requirements:
71
- - - ">="
109
+ - - ! '>='
72
110
  - !ruby/object:Gem::Version
73
111
  version: '0'
74
112
  requirements: []
@@ -76,5 +114,6 @@ rubyforge_project:
76
114
  rubygems_version: 2.2.2
77
115
  signing_key:
78
116
  specification_version: 4
79
- summary: Generate Swagger docs from your specs.
80
- test_files: []
117
+ summary: Test Swagger-documented APIs automatically.
118
+ test_files:
119
+ - spec/spec_helper.rb