webspicy 0.18.0 → 0.20.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +4 -4
  2. data/bin/webspicy +6 -1
  3. data/doc/1-black-box-scene.md +109 -0
  4. data/doc/2-black-box-testing.md +27 -0
  5. data/doc/3-specification-importance.md +41 -0
  6. data/doc/4-sequence-diagram.md +82 -0
  7. data/lib/webspicy.rb +17 -9
  8. data/lib/webspicy/configuration.rb +48 -8
  9. data/lib/webspicy/configuration/scope.rb +22 -8
  10. data/lib/webspicy/formaldoc.fio +5 -5
  11. data/lib/webspicy/specification.rb +5 -2
  12. data/lib/webspicy/specification/condition.rb +48 -0
  13. data/lib/webspicy/specification/err.rb +18 -0
  14. data/lib/webspicy/specification/oldies.rb +4 -0
  15. data/lib/webspicy/specification/oldies/bridge.rb +32 -0
  16. data/lib/webspicy/specification/{errcondition.rb → oldies/errcondition.rb} +6 -0
  17. data/lib/webspicy/specification/{postcondition.rb → oldies/postcondition.rb} +6 -0
  18. data/lib/webspicy/specification/{precondition.rb → oldies/precondition.rb} +6 -2
  19. data/lib/webspicy/specification/post.rb +20 -0
  20. data/lib/webspicy/specification/post/missing_condition_impl.rb +15 -0
  21. data/lib/webspicy/specification/post/unexpected_condition_impl.rb +15 -0
  22. data/lib/webspicy/specification/pre.rb +19 -0
  23. data/lib/webspicy/specification/{precondition → pre}/global_request_headers.rb +4 -4
  24. data/lib/webspicy/specification/{precondition → pre}/robust_to_invalid_input.rb +4 -4
  25. data/lib/webspicy/specification/service.rb +34 -5
  26. data/lib/webspicy/specification/test_case.rb +11 -9
  27. data/lib/webspicy/support.rb +31 -0
  28. data/lib/webspicy/support/hooks.rb +65 -0
  29. data/lib/webspicy/support/world.rb +47 -0
  30. data/lib/webspicy/tester.rb +202 -3
  31. data/lib/webspicy/tester/assertions.rb +2 -2
  32. data/lib/webspicy/tester/client.rb +4 -50
  33. data/lib/webspicy/tester/fakeses.rb +41 -0
  34. data/lib/webspicy/tester/fakeses/email.rb +38 -0
  35. data/lib/webspicy/tester/fakesmtp.rb +39 -0
  36. data/lib/webspicy/tester/fakesmtp/email.rb +27 -0
  37. data/lib/webspicy/tester/file_checker.rb +22 -0
  38. data/lib/webspicy/tester/invocation.rb +15 -48
  39. data/lib/webspicy/tester/reporter.rb +85 -0
  40. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  41. data/lib/webspicy/tester/reporter/documentation.rb +74 -0
  42. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  43. data/lib/webspicy/tester/reporter/exceptions.rb +62 -0
  44. data/lib/webspicy/tester/reporter/file_progress.rb +25 -0
  45. data/lib/webspicy/tester/reporter/file_summary.rb +43 -0
  46. data/lib/webspicy/tester/reporter/progress.rb +30 -0
  47. data/lib/webspicy/tester/reporter/summary.rb +63 -0
  48. data/lib/webspicy/tester/result.rb +142 -0
  49. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  50. data/lib/webspicy/tester/result/check.rb +33 -0
  51. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  52. data/lib/webspicy/tester/result/error_schema_met.rb +25 -0
  53. data/lib/webspicy/tester/result/invocation_succeeded.rb +13 -0
  54. data/lib/webspicy/tester/result/output_schema_met.rb +25 -0
  55. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  56. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  57. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  58. data/lib/webspicy/version.rb +2 -2
  59. data/lib/webspicy/web.rb +4 -0
  60. data/lib/webspicy/web/client.rb +15 -0
  61. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  62. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  63. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  64. data/lib/webspicy/web/invocation.rb +69 -0
  65. data/lib/webspicy/web/mocker.rb +90 -0
  66. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  67. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  68. data/lib/webspicy/web/openapi/generator.rb +129 -0
  69. data/spec/unit/specification/{precondition → pre}/test_global_request_headers.rb +9 -4
  70. data/spec/unit/specification/test_condition.rb +44 -0
  71. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  72. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  73. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  74. data/spec/unit/support/world/fixtures/array.json +8 -0
  75. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  76. data/spec/unit/support/world/fixtures/single.json +11 -0
  77. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  78. data/spec/unit/support/world/test_world.rb +56 -0
  79. data/spec/unit/test_configuration.rb +49 -0
  80. data/spec/unit/tester/fakeses/test_email.rb +40 -0
  81. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  82. data/spec/unit/web/openapi/test_generator.rb +31 -0
  83. metadata +80 -65
  84. data/examples/restful/Gemfile +0 -5
  85. data/examples/restful/Rakefile +0 -25
  86. data/examples/restful/app.rb +0 -180
  87. data/examples/restful/webspicy/config.rb +0 -24
  88. data/examples/restful/webspicy/formaldef/todo/_one/delete.yml +0 -55
  89. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +0 -46
  90. data/examples/restful/webspicy/formaldef/todo/_one/get.yml +0 -50
  91. data/examples/restful/webspicy/formaldef/todo/_one/patch.yml +0 -66
  92. data/examples/restful/webspicy/formaldef/todo/_one/put.yml +0 -65
  93. data/examples/restful/webspicy/formaldef/todo/get.yml +0 -36
  94. data/examples/restful/webspicy/formaldef/todo/options.yml +0 -32
  95. data/examples/restful/webspicy/formaldef/todo/post.csv.yml +0 -43
  96. data/examples/restful/webspicy/formaldef/todo/post.file.yml +0 -40
  97. data/examples/restful/webspicy/formaldef/todo/post.yml +0 -51
  98. data/examples/restful/webspicy/formaldef/todo/todos.csv +0 -4
  99. data/examples/restful/webspicy/rack.rb +0 -7
  100. data/examples/restful/webspicy/real.rb +0 -8
  101. data/examples/restful/webspicy/schema.fio +0 -20
  102. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  103. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  104. data/examples/restful/webspicy/support/todo_not_removed.rb +0 -21
  105. data/examples/restful/webspicy/support/todo_removed.rb +0 -20
  106. data/examples/single_spec/spec.yml +0 -59
  107. data/examples/website/config.rb +0 -2
  108. data/examples/website/schema.fio +0 -1
  109. data/examples/website/specification/get-http.yml +0 -30
  110. data/examples/website/specification/get-https.yml +0 -30
  111. data/lib/webspicy/checker.rb +0 -10
  112. data/lib/webspicy/mocker.rb +0 -88
  113. data/lib/webspicy/openapi/generator.rb +0 -127
  114. data/lib/webspicy/rspec/checker.rb +0 -2
  115. data/lib/webspicy/rspec/checker/rspec_checker.rb +0 -24
  116. data/lib/webspicy/rspec/support/rspec_runnable.rb +0 -27
  117. data/lib/webspicy/rspec/tester.rb +0 -4
  118. data/lib/webspicy/rspec/tester/rspec_asserter.rb +0 -121
  119. data/lib/webspicy/rspec/tester/rspec_matchers.rb +0 -114
  120. data/lib/webspicy/rspec/tester/rspec_tester.rb +0 -63
  121. data/spec/unit/mocker/test_mocker.rb +0 -32
  122. data/spec/unit/openapi/test_generator.rb +0 -28
@@ -1,2 +0,0 @@
1
- Webspicy::Configuration.new{|c|
2
- }
@@ -1 +0,0 @@
1
- @import webspicy/scalars
@@ -1,30 +0,0 @@
1
- ---
2
- url: |-
3
- http://yourbackendisbroken.dev
4
-
5
- method: |-
6
- GET
7
-
8
- description: |-
9
- Redirects to the https version
10
-
11
- input_schema: |-
12
- Any
13
-
14
- output_schema: |-
15
- Any
16
-
17
- error_schema: |-
18
- Any
19
-
20
- examples:
21
-
22
- - description: |-
23
- it works
24
- params:
25
- id: 1
26
- expected:
27
- content_type: text/html
28
- status: 3xx
29
- headers:
30
- Location: https://yourbackendisbroken.dev/?id=1
@@ -1,30 +0,0 @@
1
- ---
2
- url: |-
3
- https://yourbackendisbroken.dev
4
-
5
- method: |-
6
- GET
7
-
8
- description: |-
9
- Returns the web page
10
-
11
- input_schema: |-
12
- Any
13
-
14
- output_schema: |-
15
- Any
16
-
17
- error_schema: |-
18
- Any
19
-
20
- examples:
21
-
22
- - description: |-
23
- it works
24
- params:
25
- id: 1
26
- expected:
27
- content_type: text/html
28
- status: 200
29
- assert:
30
- - notEmpty
@@ -1,10 +0,0 @@
1
- module Webspicy
2
- class Checker
3
-
4
- def self.new(*args, &bl)
5
- require_relative 'rspec/checker'
6
- RSpecChecker.new(*args, &bl)
7
- end
8
-
9
- end # class Checker
10
- end # module Webspicy
@@ -1,88 +0,0 @@
1
- require "finitio/generation"
2
- require "json"
3
- module Webspicy
4
- class Mocker
5
-
6
- def initialize(config)
7
- @config = Configuration.dress(config)
8
- @generator = config.generator || Finitio::Generation.new
9
- end
10
- attr_reader :config, :generator
11
-
12
- def call(env)
13
- req = Rack::Request.new(env)
14
- path = req.path
15
- meth = req.request_method
16
- if meth == "OPTIONS" && has_service?(path)
17
- [204, {}, []]
18
- elsif service = find_service(meth, path)
19
- status = best_status_code(service)
20
- body = status == 204 ? "" : random_body(service, req)
21
- headers = generate_headers(service)
22
- [ status, headers, [ body ].compact ]
23
- else
24
- [404, {}, []]
25
- end
26
- end
27
-
28
- private
29
-
30
- def has_service?(path)
31
- config.each_scope do |scope|
32
- scope.each_specification do |specification|
33
- next unless url_matches?(specification, path)
34
- return true
35
- end
36
- end
37
- return false
38
- end
39
-
40
- def find_service(method, path)
41
- config.each_scope do |scope|
42
- scope.each_specification do |specification|
43
- next unless url_matches?(specification, path)
44
- scope.each_service(specification) do |service|
45
- return service if service.method == method
46
- end
47
- end
48
- end
49
- nil
50
- end
51
-
52
- def generate_headers(service)
53
- {
54
- "Content-Type" => best_content_type(service)
55
- }.delete_if{|k,v| v.nil? }
56
- end
57
-
58
- def best_status_code(service)
59
- if ex = service.examples.first
60
- (ex.expected_status && ex.expected_status.to_i) || 200
61
- else
62
- 200
63
- end
64
- end
65
-
66
- def best_content_type(service)
67
- if ex = service.examples.first
68
- ex.expected_content_type
69
- else
70
- "application/json"
71
- end
72
- end
73
-
74
- def url_matches?(specification, path)
75
- specification.url_pattern.match(path)
76
- end
77
-
78
- def random_body(service, request)
79
- world = OpenStruct.new({
80
- service: service,
81
- request: request
82
- })
83
- data = generator.call(service.output_schema.main, world)
84
- JSON.pretty_generate(data) if data
85
- end
86
-
87
- end # class Mocker
88
- end # module Webspicy
@@ -1,127 +0,0 @@
1
- require "finitio/generation"
2
- require "finitio/json_schema"
3
- module Webspicy
4
- module Openapi
5
- class Generator
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
14
- {
15
- openapi: "3.0.2",
16
- info: {
17
- version: "1.0.0",
18
- title: "Hello API"
19
- },
20
- paths: paths
21
- }
22
- end
23
-
24
- private
25
-
26
- def paths
27
- config.each_scope.inject({}) do |paths,scope|
28
- scope.each_specification.inject(paths) do |paths,specification|
29
- paths.merge(path_for(specification)){|k,ps,qs|
30
- ps.merge(qs)
31
- }
32
- end
33
- end
34
- end
35
-
36
- def path_for(specification)
37
- {
38
- specification.url => {
39
- summary: specification.name
40
- }.merge(verbs_for(specification))
41
- }
42
- end
43
-
44
- def verbs_for(specification)
45
- specification.services.inject({}) do |verbs,service|
46
- verb = service.method.downcase
47
- verb_defn = {
48
- description: service.description,
49
- responses: responses_for(service)
50
- }
51
- unless ["get", "options", "delete", "head"].include?(verb)
52
- verb_defn[:requestBody] = request_body_for(service)
53
- end
54
- verbs.merge({ verb => verb_defn })
55
- end
56
- end
57
-
58
- def request_body_for(service)
59
- schema = actual_input_schema(service)
60
- {
61
- required: true,
62
- content: {
63
- "application/json" => {
64
- schema: schema.to_json_schema,
65
- example: generator.call(schema, {})
66
- }
67
- }
68
- }
69
- end
70
-
71
- def responses_for(service)
72
- result = {}
73
- service.examples.each_with_object(result) do |test_case, rs|
74
- rs.merge!(response_for(test_case, false)){|k,r1,r2| r1 }
75
- end
76
- service.counterexamples.each_with_object(result) do |test_case, rs|
77
- rs.merge!(response_for(test_case, true)){|k,r1,r2| r1 }
78
- end
79
- result
80
- end
81
-
82
- def response_for(test_case, counterexample)
83
- res = {
84
- description: test_case.description,
85
- }
86
- status = (test_case.expected_status && test_case.expected_status.to_int) || 200
87
- if test_case.expected_content_type && status != 204
88
- content = {
89
- schema: schema_for(test_case, counterexample)
90
- }
91
- unless counterexample
92
- content[:example] = example_for(test_case, counterexample)
93
- end
94
- res[:content] = {
95
- test_case.expected_content_type => content
96
- }
97
- end
98
- {
99
- status => res
100
- }
101
- end
102
-
103
- def schema_for(test_case, counterexample)
104
- schema = actual_output_schema(test_case, counterexample)
105
- schema.to_json_schema
106
- end
107
-
108
- def example_for(test_case, counterexample)
109
- schema = actual_output_schema(test_case, counterexample)
110
- generator.call(schema, {})
111
- end
112
-
113
- def actual_output_schema(test_case, counterexample)
114
- if counterexample
115
- test_case.service.error_schema['Main']
116
- else
117
- test_case.service.output_schema['Main']
118
- end
119
- end
120
-
121
- def actual_input_schema(service)
122
- service.input_schema['Main']
123
- end
124
-
125
- end # class Generator
126
- end # module Openapi
127
- end # module Webspicy
@@ -1,2 +0,0 @@
1
- require_relative 'support/rspec_runnable'
2
- require_relative 'checker/rspec_checker'
@@ -1,24 +0,0 @@
1
- module Webspicy
2
- class Checker
3
- class RSpecChecker
4
- include Webspicy::Support::RSpecRunnable
5
-
6
- protected
7
-
8
- def load_rspec_examples
9
- config.each_scope do |scope|
10
- scope.each_specification_file do |file, folder|
11
- RSpec.describe file.relative_to(folder).to_s do
12
-
13
- it 'meets the formal doc data schema' do
14
- Webspicy.specification(file.load, file, scope)
15
- end
16
-
17
- end
18
- end
19
- end
20
- end
21
-
22
- end # class RSpecChecker
23
- end # class Checker
24
- end # module Webspicy
@@ -1,27 +0,0 @@
1
- module Webspicy
2
- module Support
3
- module RSpecRunnable
4
-
5
- def initialize(config)
6
- @config = Configuration.dress(config)
7
- end
8
- attr_reader :config
9
-
10
- def reset_rspec!
11
- RSpec.reset
12
- RSpec.configure do |c|
13
- c.filter_gems_from_backtrace "rake"
14
- end
15
- load_rspec_examples
16
- end
17
-
18
- def call(err=$stderr, out=$stdout)
19
- reset_rspec!
20
- options = RSpec::Core::ConfigurationOptions.new(config.rspec_options)
21
- conf = RSpec::Core::Configuration.new
22
- RSpec::Core::Runner.new(options, conf).run(err, out)
23
- end
24
-
25
- end # module RSpecRunnable
26
- end # module Support
27
- end # module Webspicy
@@ -1,4 +0,0 @@
1
- require_relative 'support/rspec_runnable'
2
- require_relative 'tester/rspec_matchers'
3
- require_relative 'tester/rspec_asserter'
4
- require_relative 'tester/rspec_tester'
@@ -1,121 +0,0 @@
1
- module Webspicy
2
- class Tester
3
- class RSpecAsserter
4
-
5
- def initialize(rspec, invocation)
6
- @rspec = rspec
7
- @invocation = invocation
8
- end
9
- attr_reader :rspec, :invocation
10
-
11
- def self.call(rspec, invocation)
12
- new(rspec, invocation).send(:assert!)
13
- end
14
-
15
- def response
16
- invocation.response
17
- end
18
-
19
- def test_case
20
- invocation.test_case
21
- end
22
-
23
- def service
24
- test_case.service
25
- end
26
-
27
- protected
28
-
29
- def assert!
30
- rspec.aggregate_failures do
31
- assert_status_met
32
- assert_content_type_met
33
- assert_expected_headers
34
- end
35
- assert_output_schema_met
36
- rspec.aggregate_failures do
37
- assert_assertions_met
38
- assert_postconditions_met
39
- assert_errconditions_met
40
- end
41
- end
42
-
43
- def assert_status_met
44
- got = response.status
45
- expected = test_case.expected_status
46
- rspec.expect(got).to rspec.match_response_status(expected)
47
- end
48
-
49
- def assert_content_type_met
50
- return if test_case.is_expected_status?(204)
51
- return unless ect = test_case.expected_content_type
52
- got = response.content_type
53
- got = got.mime_type if got.respond_to?(:mime_type)
54
- if ect.nil?
55
- rspec.expect(ect).to rspec.have_no_response_type
56
- else
57
- rspec.expect(got).to rspec.match_content_type(ect)
58
- end
59
- end
60
-
61
- def assert_expected_headers
62
- return unless test_case.has_expected_headers?
63
- test_case.expected_headers.each_pair do |k,v|
64
- got = response.headers[k]
65
- if got.nil?
66
- rspec.expect(got).to rspec.be_in_response_headers(k)
67
- else
68
- rspec.expect(got).to rspec.match_response_header(k, v)
69
- end
70
- end
71
- end
72
-
73
- def assert_output_schema_met
74
- return if test_case.is_expected_status?(204)
75
- return if invocation.is_redirect?
76
- if invocation.is_empty_response?
77
- body = response.body.to_s.strip
78
- rspec.expect(body).to rspec.be_an_empty_response_body
79
- else
80
- b = invocation.dressed_body
81
- if invocation.is_expected_success?
82
- rspec.expect(b).to rspec.meet_output_schema
83
- else
84
- rspec.expect(b).to rspec.meet_error_schema
85
- end
86
- end
87
- end
88
-
89
- def assert_assertions_met
90
- return unless test_case.has_assertions?
91
- asserter = Tester::Asserter.new(invocation.dressed_body)
92
- test_case.assert.each do |assert|
93
- begin
94
- asserter.instance_eval(assert)
95
- rescue => ex
96
- rspec.expect(ex).to rspec.meet_assertion(assert)
97
- end
98
- end
99
- end
100
-
101
- def assert_postconditions_met
102
- return unless service.has_postconditions?
103
- return if test_case.counterexample?
104
- service.postconditions.each do |post|
105
- msg = post.check(invocation)
106
- rspec.expect(msg).to rspec.meet_postcondition(post)
107
- end
108
- end
109
-
110
- def assert_errconditions_met
111
- return unless service.has_errconditions?
112
- return unless test_case.counterexample?
113
- service.errconditions.each do |post|
114
- msg = post.check(invocation)
115
- rspec.expect(msg).to rspec.meet_errcondition(post)
116
- end
117
- end
118
-
119
- end # class RSpecAsserter
120
- end # class Tester
121
- end # module Webspicy