webspicy 0.16.3 → 0.20.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -3
  3. data/bin/webspicy +6 -1
  4. data/doc/1-black-box-scene.md +109 -0
  5. data/doc/2-black-box-testing.md +27 -0
  6. data/doc/3-specification-importance.md +41 -0
  7. data/doc/4-sequence-diagram.md +82 -0
  8. data/lib/webspicy.rb +18 -9
  9. data/lib/webspicy/configuration.rb +57 -8
  10. data/lib/webspicy/configuration/scope.rb +22 -16
  11. data/lib/webspicy/formaldoc.fio +7 -5
  12. data/lib/webspicy/specification.rb +10 -9
  13. data/lib/webspicy/specification/condition.rb +48 -0
  14. data/lib/webspicy/specification/err.rb +18 -0
  15. data/lib/webspicy/specification/oldies.rb +4 -0
  16. data/lib/webspicy/specification/oldies/bridge.rb +29 -0
  17. data/lib/webspicy/specification/oldies/errcondition.rb +22 -0
  18. data/lib/webspicy/specification/{postcondition.rb → oldies/postcondition.rb} +6 -0
  19. data/lib/webspicy/specification/{precondition.rb → oldies/precondition.rb} +6 -2
  20. data/lib/webspicy/specification/post.rb +20 -0
  21. data/lib/webspicy/specification/post/missing_condition_impl.rb +15 -0
  22. data/lib/webspicy/specification/post/unexpected_condition_impl.rb +15 -0
  23. data/lib/webspicy/specification/pre.rb +19 -0
  24. data/lib/webspicy/specification/{precondition → pre}/global_request_headers.rb +4 -4
  25. data/lib/webspicy/specification/{precondition → pre}/robust_to_invalid_input.rb +4 -4
  26. data/lib/webspicy/specification/service.rb +58 -24
  27. data/lib/webspicy/specification/test_case.rb +10 -17
  28. data/lib/webspicy/support.rb +32 -0
  29. data/lib/webspicy/support/data_object.rb +25 -0
  30. data/lib/webspicy/support/hooks.rb +65 -0
  31. data/lib/webspicy/support/world.rb +47 -0
  32. data/lib/webspicy/tester.rb +180 -60
  33. data/lib/webspicy/tester/asserter.rb +9 -4
  34. data/lib/webspicy/tester/assertions.rb +8 -9
  35. data/lib/webspicy/tester/client.rb +4 -50
  36. data/lib/webspicy/tester/failure.rb +6 -0
  37. data/lib/webspicy/tester/fakeses.rb +41 -0
  38. data/lib/webspicy/tester/fakeses/email.rb +38 -0
  39. data/lib/webspicy/tester/fakesmtp.rb +39 -0
  40. data/lib/webspicy/tester/fakesmtp/email.rb +27 -0
  41. data/lib/webspicy/tester/file_checker.rb +22 -0
  42. data/lib/webspicy/tester/invocation.rb +15 -196
  43. data/lib/webspicy/tester/reporter.rb +85 -0
  44. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  45. data/lib/webspicy/tester/reporter/documentation.rb +74 -0
  46. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  47. data/lib/webspicy/tester/reporter/exceptions.rb +62 -0
  48. data/lib/webspicy/tester/reporter/file_progress.rb +25 -0
  49. data/lib/webspicy/tester/reporter/file_summary.rb +43 -0
  50. data/lib/webspicy/tester/reporter/progress.rb +30 -0
  51. data/lib/webspicy/tester/reporter/summary.rb +63 -0
  52. data/lib/webspicy/tester/result.rb +142 -0
  53. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  54. data/lib/webspicy/tester/result/check.rb +33 -0
  55. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  56. data/lib/webspicy/tester/result/error_schema_met.rb +25 -0
  57. data/lib/webspicy/tester/result/invocation_succeeded.rb +13 -0
  58. data/lib/webspicy/tester/result/output_schema_met.rb +25 -0
  59. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  60. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  61. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  62. data/lib/webspicy/version.rb +2 -2
  63. data/lib/webspicy/web.rb +4 -0
  64. data/lib/webspicy/web/client.rb +15 -0
  65. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  66. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  67. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  68. data/lib/webspicy/web/invocation.rb +68 -0
  69. data/lib/webspicy/web/mocker.rb +90 -0
  70. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  71. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  72. data/lib/webspicy/web/openapi/generator.rb +129 -0
  73. data/spec/unit/configuration/scope/test_each_service.rb +2 -2
  74. data/spec/unit/configuration/scope/test_each_specification.rb +7 -7
  75. data/spec/unit/specification/{precondition → pre}/test_global_request_headers.rb +9 -4
  76. data/spec/unit/specification/test_condition.rb +44 -0
  77. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  78. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  79. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  80. data/spec/unit/support/world/fixtures/array.json +8 -0
  81. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  82. data/spec/unit/support/world/fixtures/single.json +11 -0
  83. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  84. data/spec/unit/support/world/test_world.rb +56 -0
  85. data/spec/unit/test_configuration.rb +50 -1
  86. data/spec/unit/tester/fakeses/test_email.rb +40 -0
  87. data/spec/unit/tester/test_asserter.rb +198 -3
  88. data/spec/unit/tester/test_assertions.rb +8 -6
  89. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  90. data/spec/unit/web/openapi/test_generator.rb +31 -0
  91. metadata +90 -67
  92. data/examples/restful/Gemfile +0 -5
  93. data/examples/restful/Gemfile.lock +0 -105
  94. data/examples/restful/Rakefile +0 -25
  95. data/examples/restful/app.rb +0 -180
  96. data/examples/restful/webspicy/config.rb +0 -23
  97. data/examples/restful/webspicy/rack.rb +0 -7
  98. data/examples/restful/webspicy/real.rb +0 -8
  99. data/examples/restful/webspicy/schema.fio +0 -20
  100. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  101. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  102. data/examples/restful/webspicy/support/todo_removed.rb +0 -18
  103. data/examples/restful/webspicy/todo/deleteTodo.yml +0 -52
  104. data/examples/restful/webspicy/todo/getTodo.yml +0 -50
  105. data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +0 -46
  106. data/examples/restful/webspicy/todo/getTodos.yml +0 -36
  107. data/examples/restful/webspicy/todo/options.yml +0 -32
  108. data/examples/restful/webspicy/todo/patchTodo.yml +0 -66
  109. data/examples/restful/webspicy/todo/postCsv.yml +0 -43
  110. data/examples/restful/webspicy/todo/postFile.yml +0 -40
  111. data/examples/restful/webspicy/todo/postTodos.yml +0 -51
  112. data/examples/restful/webspicy/todo/putTodo.yml +0 -65
  113. data/examples/restful/webspicy/todo/todos.csv +0 -4
  114. data/examples/single_spec/spec.yml +0 -59
  115. data/examples/website/config.rb +0 -2
  116. data/examples/website/schema.fio +0 -1
  117. data/examples/website/specification/get-http.yml +0 -30
  118. data/examples/website/specification/get-https.yml +0 -30
  119. data/lib/webspicy/checker.rb +0 -25
  120. data/lib/webspicy/mocker.rb +0 -88
  121. data/lib/webspicy/openapi/generator.rb +0 -127
  122. data/lib/webspicy/tester/rspec_asserter.rb +0 -108
  123. data/lib/webspicy/tester/rspec_matchers.rb +0 -104
  124. data/spec/unit/mocker/test_mocker.rb +0 -32
  125. data/spec/unit/openapi/test_generator.rb +0 -28
@@ -1,5 +1,5 @@
1
1
  module Webspicy
2
- class Tester
2
+ module Web
3
3
  class RackTestClient < Client
4
4
 
5
5
  def self.for(app)
@@ -12,7 +12,7 @@ module Webspicy
12
12
  end
13
13
  attr_reader :api
14
14
 
15
- def call(test_case)
15
+ def _call(test_case)
16
16
  service, specification = test_case.service, test_case.specification
17
17
 
18
18
  # Instantiate the parameters
@@ -184,5 +184,5 @@ module Webspicy
184
184
  end # class Api
185
185
 
186
186
  end # class RackTestClient
187
- end # class Client
187
+ end # module Web
188
188
  end # module Webspicy
@@ -1,6 +1,6 @@
1
1
  module Webspicy
2
- class Tester
3
- class Client
2
+ module Web
3
+ class Client < Tester::Client
4
4
  module Support
5
5
  include Webspicy::Support::Colorize
6
6
 
@@ -0,0 +1,68 @@
1
+ module Webspicy
2
+ module Web
3
+ class Invocation < Tester::Invocation
4
+
5
+ ### Getters on response
6
+
7
+ def response_code
8
+ code = response.status
9
+ code = code.code unless code.is_a?(Integer)
10
+ code
11
+ end
12
+
13
+ ### Query methods
14
+
15
+ def done?
16
+ !response.nil?
17
+ end
18
+
19
+ def is_expected_success?
20
+ test_case.expected_status.to_i >= 200 && test_case.expected_status.to_i < 300
21
+ end
22
+
23
+ def is_success?
24
+ response_code >= 200 && response_code < 300
25
+ end
26
+
27
+ def is_empty_response?
28
+ response_code == 204
29
+ end
30
+
31
+ def is_redirect?
32
+ response_code >= 300 && response_code < 400
33
+ end
34
+
35
+ def raw_output
36
+ response.body.to_s
37
+ end
38
+
39
+ def is_structured_output?
40
+ ct = response.content_type || test_case.expected_content_type
41
+ ct = ct.mime_type if ct.respond_to?(:mime_type)
42
+ ct =~ /json/
43
+ end
44
+
45
+ def loaded_output
46
+ if is_structured_output?
47
+ raise "Body empty while expected" if raw_output.empty?
48
+ @loaded_output ||= ::JSON.parse(response.body)
49
+ else
50
+ raw_output
51
+ end
52
+ end
53
+ alias :loaded_body :loaded_output
54
+
55
+ def output
56
+ schema = is_expected_success? ? service.output_schema : service.error_schema
57
+ begin
58
+ schema.dress(loaded_output)
59
+ rescue Finitio::TypeError => ex
60
+ ex
61
+ end
62
+ end
63
+ alias :dressed_body :output
64
+ alias :error :output
65
+
66
+ end # class Invocation
67
+ end # module Web
68
+ end # module Webspicy
@@ -0,0 +1,90 @@
1
+ require "finitio/generation"
2
+ require "json"
3
+ module Webspicy
4
+ module Web
5
+ class Mocker
6
+
7
+ def initialize(config)
8
+ @config = Configuration.dress(config)
9
+ @generator = config.generator || Finitio::Generation.new
10
+ end
11
+ attr_reader :config, :generator
12
+
13
+ def call(env)
14
+ req = Rack::Request.new(env)
15
+ path = req.path
16
+ meth = req.request_method
17
+ if meth == "OPTIONS" && has_service?(path)
18
+ [204, {}, []]
19
+ elsif service = find_service(meth, path)
20
+ status = best_status_code(service)
21
+ body = status == 204 ? "" : random_body(service, req)
22
+ headers = generate_headers(service)
23
+ [ status, headers, [ body ].compact ]
24
+ else
25
+ [404, {}, []]
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def has_service?(path)
32
+ config.each_scope do |scope|
33
+ scope.each_specification do |specification|
34
+ next unless url_matches?(specification, path)
35
+ return true
36
+ end
37
+ end
38
+ return false
39
+ end
40
+
41
+ def find_service(method, path)
42
+ config.each_scope do |scope|
43
+ scope.each_specification do |specification|
44
+ next unless url_matches?(specification, path)
45
+ scope.each_service(specification) do |service|
46
+ return service if service.method == method
47
+ end
48
+ end
49
+ end
50
+ nil
51
+ end
52
+
53
+ def generate_headers(service)
54
+ {
55
+ "Content-Type" => best_content_type(service)
56
+ }.delete_if{|k,v| v.nil? }
57
+ end
58
+
59
+ def best_status_code(service)
60
+ if ex = service.examples.first
61
+ (ex.expected_status && ex.expected_status.to_i) || 200
62
+ else
63
+ 200
64
+ end
65
+ end
66
+
67
+ def best_content_type(service)
68
+ if ex = service.examples.first
69
+ ex.expected_content_type
70
+ else
71
+ "application/json"
72
+ end
73
+ end
74
+
75
+ def url_matches?(specification, path)
76
+ specification.url_pattern.match(path)
77
+ end
78
+
79
+ def random_body(service, request)
80
+ world = OpenStruct.new({
81
+ service: service,
82
+ request: request
83
+ })
84
+ data = generator.call(service.output_schema.main, world)
85
+ JSON.pretty_generate(data) if data
86
+ end
87
+
88
+ end # class Mocker
89
+ end # module Web
90
+ end # module Webspicy
@@ -1,5 +1,6 @@
1
1
  require "webspicy"
2
- require "webspicy/mocker"
2
+ require "webspicy/web"
3
+ require "webspicy/web/mocker"
3
4
  require 'rack'
4
5
  config = Webspicy::Configuration.dress(Path("/formalspec/"))
5
- run Webspicy::Mocker.new(config)
6
+ run Webspicy::Web::Mocker.new(config)
File without changes
@@ -0,0 +1,129 @@
1
+ require "finitio/generation"
2
+ require "finitio/json_schema"
3
+ module Webspicy
4
+ module Web
5
+ module Openapi
6
+ class Generator
7
+
8
+ def initialize(config)
9
+ @config = Configuration.dress(config)
10
+ @generator = config.generator || Finitio::Generation.new
11
+ end
12
+ attr_reader :config, :generator
13
+
14
+ def call
15
+ {
16
+ openapi: "3.0.2",
17
+ info: {
18
+ version: "1.0.0",
19
+ title: "Hello API"
20
+ },
21
+ paths: paths
22
+ }
23
+ end
24
+
25
+ private
26
+
27
+ def paths
28
+ config.each_scope.inject({}) do |paths,scope|
29
+ scope.each_specification.inject(paths) do |paths,specification|
30
+ paths.merge(path_for(specification)){|k,ps,qs|
31
+ ps.merge(qs)
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ def path_for(specification)
38
+ {
39
+ specification.url => {
40
+ summary: specification.name
41
+ }.merge(verbs_for(specification))
42
+ }
43
+ end
44
+
45
+ def verbs_for(specification)
46
+ specification.services.inject({}) do |verbs,service|
47
+ verb = service.method.downcase
48
+ verb_defn = {
49
+ description: service.description,
50
+ responses: responses_for(service)
51
+ }
52
+ unless ["get", "options", "delete", "head"].include?(verb)
53
+ verb_defn[:requestBody] = request_body_for(service)
54
+ end
55
+ verbs.merge({ verb => verb_defn })
56
+ end
57
+ end
58
+
59
+ def request_body_for(service)
60
+ schema = actual_input_schema(service)
61
+ {
62
+ required: true,
63
+ content: {
64
+ "application/json" => {
65
+ schema: schema.to_json_schema,
66
+ example: generator.call(schema, {})
67
+ }
68
+ }
69
+ }
70
+ end
71
+
72
+ def responses_for(service)
73
+ result = {}
74
+ service.examples.each_with_object(result) do |test_case, rs|
75
+ rs.merge!(response_for(test_case, false)){|k,r1,r2| r1 }
76
+ end
77
+ service.counterexamples.each_with_object(result) do |test_case, rs|
78
+ rs.merge!(response_for(test_case, true)){|k,r1,r2| r1 }
79
+ end
80
+ result
81
+ end
82
+
83
+ def response_for(test_case, counterexample)
84
+ res = {
85
+ description: test_case.description,
86
+ }
87
+ status = (test_case.expected_status && test_case.expected_status.to_int) || 200
88
+ if test_case.expected_content_type && status != 204
89
+ content = {
90
+ schema: schema_for(test_case, counterexample)
91
+ }
92
+ unless counterexample
93
+ content[:example] = example_for(test_case, counterexample)
94
+ end
95
+ res[:content] = {
96
+ test_case.expected_content_type => content
97
+ }
98
+ end
99
+ {
100
+ status => res
101
+ }
102
+ end
103
+
104
+ def schema_for(test_case, counterexample)
105
+ schema = actual_output_schema(test_case, counterexample)
106
+ schema.to_json_schema
107
+ end
108
+
109
+ def example_for(test_case, counterexample)
110
+ schema = actual_output_schema(test_case, counterexample)
111
+ generator.call(schema, {})
112
+ end
113
+
114
+ def actual_output_schema(test_case, counterexample)
115
+ if counterexample
116
+ test_case.service.error_schema['Main']
117
+ else
118
+ test_case.service.output_schema['Main']
119
+ end
120
+ end
121
+
122
+ def actual_input_schema(service)
123
+ service.input_schema['Main']
124
+ end
125
+
126
+ end # class Generator
127
+ end # module Openapi
128
+ end # module Web
129
+ end # module Webspicy
@@ -19,7 +19,7 @@ module Webspicy
19
19
 
20
20
  let(:configuration) {
21
21
  Configuration.new(restful_folder){|c|
22
- c.file_filter = /getTodo.yml/
22
+ c.file_filter = /get.yml/
23
23
  }
24
24
  }
25
25
 
@@ -32,7 +32,7 @@ module Webspicy
32
32
 
33
33
  let(:configuration) {
34
34
  Configuration.new(restful_folder){|c|
35
- c.file_filter = /getTodo.yml/
35
+ c.file_filter = /get.yml/
36
36
  c.service_filter = ->(s) {
37
37
  s.method == "POST"
38
38
  }
@@ -27,13 +27,13 @@ module Webspicy
27
27
  let(:configuration) {
28
28
  Configuration.new(restful_folder){|c|
29
29
  c.file_filter = ->(f) {
30
- f.basename.to_s == "getTodo.yml"
30
+ f.basename.to_s == "get.yml"
31
31
  }
32
32
  }
33
33
  }
34
34
 
35
- it 'returns only that file' do
36
- expect(subject.size).to eql(1)
35
+ it 'returns only that files' do
36
+ expect(subject.size).to eql(2)
37
37
  end
38
38
  end
39
39
 
@@ -41,12 +41,12 @@ module Webspicy
41
41
 
42
42
  let(:configuration) {
43
43
  Configuration.new(restful_folder){|c|
44
- c.file_filter = /getTodo.yml/
44
+ c.file_filter = /get.yml/
45
45
  }
46
46
  }
47
47
 
48
- it 'returns only that file' do
49
- expect(subject.size).to eql(1)
48
+ it 'returns only that files' do
49
+ expect(subject.size).to eql(2)
50
50
  end
51
51
  end
52
52
 
@@ -54,7 +54,7 @@ module Webspicy
54
54
 
55
55
  let(:configuration) {
56
56
  Configuration.new(restful_folder) do |c|
57
- c.folder 'todo'
57
+ c.folder 'formaldef/todo'
58
58
  end
59
59
  }
60
60
 
@@ -2,16 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  module Webspicy
4
4
  class Specification
5
- module Precondition
5
+ module Pre
6
6
  describe GlobalRequestHeaders do
7
7
  let(:gbr){
8
8
  GlobalRequestHeaders.new('Accept' => 'application/json')
9
9
  }
10
10
 
11
+ def instrument(tc)
12
+ t = OpenStruct.new(test_case: tc)
13
+ gbr.bind(t).instrument
14
+ end
15
+
11
16
  describe "instrument" do
12
17
  it 'injects the headers' do
13
18
  tc = TestCase.new({})
14
- gbr.instrument(tc, nil)
19
+ instrument(tc)
15
20
  expect(tc.headers['Accept']).to eql("application/json")
16
21
  end
17
22
 
@@ -21,7 +26,7 @@ module Webspicy
21
26
  'Content-Type' => 'text/plain'
22
27
  }
23
28
  })
24
- gbr.instrument(tc, nil)
29
+ instrument(tc)
25
30
  expect(tc.headers['Content-Type']).to eql("text/plain")
26
31
  expect(tc.headers['Accept']).to eql("application/json")
27
32
  end
@@ -32,7 +37,7 @@ module Webspicy
32
37
  'Accept' => 'text/plain'
33
38
  }
34
39
  })
35
- gbr.instrument(tc, nil)
40
+ instrument(tc)
36
41
  expect(tc.headers['Accept']).to eql("text/plain")
37
42
  end
38
43
  end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+ module Webspicy
3
+ class Specification
4
+ describe Condition do
5
+ describe "sooner_or_later" do
6
+ include Condition
7
+
8
+ it 'works when the block returns anything non falsy' do
9
+ x = sooner_or_later(max: 1){ 12 }
10
+ expect(x).to eql(12)
11
+ end
12
+
13
+ it 'returns nil otherwise' do
14
+ x = sooner_or_later(max: 1){ nil }
15
+ expect(x).to eql(nil)
16
+ end
17
+
18
+ it 'supports the block raising a Failure' do
19
+ expect {
20
+ sooner_or_later(max: 2){
21
+ raise Tester::Failure
22
+ }
23
+ }.to raise_error(Tester::Failure)
24
+ end
25
+
26
+ it 'catches Failure occuring before successes' do
27
+ seen = 0
28
+ x = sooner_or_later(max: 2){
29
+ seen += 1
30
+ raise Tester::Failure if seen == 1
31
+ 12
32
+ }
33
+ expect(x).to eql(12)
34
+ end
35
+
36
+ it 'can raise for us' do
37
+ expect{
38
+ sooner_or_later(max: 1, raise: true){ nil }
39
+ }.to raise_error(TimeoutError)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end