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,3 +1,34 @@
1
+ module Webspicy
2
+ module Support
3
+
4
+ SORL_OPTS = { max: 5, wait: 0.05, raise: false }
5
+
6
+ def sooner_or_later(opts = nil, &bl)
7
+ opts = SORL_OPTS.merge(opts || {})
8
+ left, wait_ms = opts[:max], opts[:wait]
9
+ r = _sooner_or_later(&bl)
10
+ until (r && !r.is_a?(Tester::Failure)) || left == 0
11
+ sleep(wait_ms)
12
+ wait_ms, left = wait_ms*2, left - 1
13
+ r = _sooner_or_later(&bl)
14
+ end
15
+ raise r if r.is_a?(Tester::Failure)
16
+ raise TimeoutError, "Timeout on sooner-or-later" if r.nil? && opts[:raise]
17
+ r
18
+ end
19
+ module_function :sooner_or_later
20
+
21
+ def _sooner_or_later(&bl)
22
+ bl.call
23
+ rescue Tester::Failure => ex
24
+ ex
25
+ end
26
+ module_function :_sooner_or_later
27
+
28
+ end # module Support
29
+ end # module Webspicy
1
30
  require_relative 'support/data_object'
2
31
  require_relative 'support/status_range'
3
32
  require_relative 'support/colorize'
33
+ require_relative 'support/world'
34
+ require_relative 'support/hooks'
@@ -0,0 +1,65 @@
1
+ module Webspicy
2
+ module Support
3
+ module Hooks
4
+
5
+ class Object
6
+ def initialize(config)
7
+ @config = config
8
+ end
9
+ attr_accessor :config
10
+ include Hooks
11
+ end
12
+
13
+ def self.for(config)
14
+ Object.new(config)
15
+ end
16
+
17
+ def fire_around(*args, &bl)
18
+ ls = config.listeners(:around_each)
19
+ if ls.size == 0
20
+ bl.call
21
+ elsif ls.size > 1
22
+ _fire_around(ls.first, ls[1..-1], args, &bl)
23
+ else
24
+ ls.first.call(*args, &bl)
25
+ end
26
+ end
27
+
28
+ def _fire_around(head, tail, args, &bl)
29
+ head.call(*args) do
30
+ if tail.empty?
31
+ bl.call
32
+ else
33
+ _fire_around(tail.first, tail[1..-1], args, &bl)
34
+ end
35
+ end
36
+ end
37
+ private :_fire_around
38
+
39
+ def fire_before_each(*args, &bl)
40
+ config.listeners(:before_each).each do |beach|
41
+ beach.call(*args, &bl)
42
+ end
43
+ end
44
+
45
+ def fire_after_each(*args, &bl)
46
+ config.listeners(:after_each).each do |aeach|
47
+ aeach.call(*args, &bl)
48
+ end
49
+ end
50
+
51
+ def fire_before_all(*args, &bl)
52
+ config.listeners(:before_all).each do |beach|
53
+ beach.call(*args, &bl)
54
+ end
55
+ end
56
+
57
+ def fire_after_all(*args, &bl)
58
+ config.listeners(:after_all).each do |aeach|
59
+ aeach.call(*args, &bl)
60
+ end
61
+ end
62
+
63
+ end # module Hooks
64
+ end # module Support
65
+ end # module Webspicy
@@ -0,0 +1,47 @@
1
+ module Webspicy
2
+ module Support
3
+ class World < OpenStruct
4
+
5
+ def initialize(folder, config = nil)
6
+ super({})
7
+ @folder = folder
8
+ @config = config
9
+ end
10
+ attr_reader :folder, :config
11
+
12
+ def method_missing(name, *args, &bl)
13
+ return super if name =~ /=$/
14
+ file = ['json', 'yml', 'yaml', 'rb']
15
+ .map{|e| folder/"#{name}.#{e}" }
16
+ .find{|f| f.file? }
17
+ data = case file && file.ext
18
+ when /json/
19
+ JSON.parse(file.read, object_class: OpenStruct)
20
+ when /ya?ml/
21
+ JSON.parse(file.load.to_json, object_class: OpenStruct)
22
+ when /rb/
23
+ ::Kernel.eval(file.read).tap{|x|
24
+ x.config = self.config if x.is_a?(Item)
25
+ }
26
+ end
27
+ self.send(:"#{name}=", data)
28
+ end
29
+
30
+ def to_data(which)
31
+ case which
32
+ when Hash
33
+ OpenStruct.new(which)
34
+ when Array
35
+ which.map{|x| to_data(x) }
36
+ else
37
+ which
38
+ end
39
+ end
40
+
41
+ module Item
42
+ attr_accessor :config
43
+ end # module Item
44
+
45
+ end # class World
46
+ end # module Support
47
+ end # module Webspicy
@@ -1,15 +1,214 @@
1
1
  module Webspicy
2
2
  class Tester
3
+ extend Forwardable
3
4
 
4
- def self.new(*args, &bl)
5
- require_relative 'rspec/tester'
6
- RSpecTester.new(*args, &bl)
5
+ class FailFast < Exception; end
6
+
7
+ def initialize(config)
8
+ @config = Configuration.dress(config)
9
+ @scope = nil
10
+ @hooks = nil
11
+ @client = nil
12
+ @spec_file = nil
13
+ @specification = nil
14
+ @service = nil
15
+ @test_case = nil
16
+ @invocation = nil
17
+ @invocation_error = nil
18
+ @reporter = default_reporter
19
+ end
20
+ attr_reader :config, :scope, :hooks, :client
21
+ attr_reader :specification, :spec_file
22
+ attr_reader :service, :test_case
23
+ attr_reader :invocation, :invocation_error, :result
24
+ attr_reader :reporter
25
+
26
+ def_delegators :@config, *[
27
+ :world
28
+ ]
29
+
30
+ def failfast?
31
+ config.failfast
32
+ end
33
+
34
+ def default_reporter
35
+ @reporter = Reporter::Composite.new
36
+ #@reporter << Reporter::Progress.new
37
+ @reporter << Reporter::Documentation.new
38
+ @reporter << Reporter::Exceptions.new
39
+ @reporter << Reporter::Summary.new
40
+ @reporter << Reporter::ErrorCount.new
41
+ end
42
+
43
+ def call
44
+ reporter.init(self)
45
+ begin
46
+ run_config
47
+ rescue FailFast
48
+ end
49
+ reporter.report
50
+ reporter.find(Reporter::ErrorCount).report
51
+ end
52
+
53
+ def find_and_call(method, url, mutation)
54
+ unless tc = scope.find_test_case(method, url)
55
+ raise Error, "No such service `#{method} #{url}`"
56
+ end
57
+ mutated = tc.mutate(mutation)
58
+ fork_tester(test_case: mutated) do |t|
59
+ instrumented = t.instrument_test_case
60
+ t.client.call(instrumented)
61
+ end
62
+ end
63
+
64
+ def bind_condition(c)
65
+ c = Specification::Oldies::Bridge.new(c) unless c.respond_to?(:bind)
66
+ c.bind(self)
67
+ end
68
+
69
+ protected
70
+
71
+ def run_config
72
+ config.each_scope do |scope|
73
+ @scope = scope
74
+ @hooks = Support::Hooks.for(scope.config)
75
+ @client = scope.get_client
76
+ reporter.before_all
77
+ @hooks.fire_before_all(self)
78
+ reporter.before_all_done
79
+ reporter.before_scope
80
+ run_scope
81
+ reporter.scope_done
82
+ reporter.after_all
83
+ @hooks.fire_after_all(self)
84
+ reporter.after_all_done
85
+ end
86
+ end
87
+
88
+ def run_scope
89
+ scope.each_specification_file do |spec_file|
90
+ @specification = load_specification(spec_file)
91
+ if @specification
92
+ reporter.before_specification
93
+ run_specification
94
+ reporter.specification_done
95
+ reporter.spec_file_done
96
+ elsif failfast?
97
+ raise FailFast
98
+ end
99
+ end
100
+ end
101
+
102
+ def load_specification(spec_file)
103
+ @spec_file = spec_file
104
+ reporter.before_spec_file
105
+ Webspicy.specification(spec_file.load, spec_file, scope)
106
+ rescue *PASSTHROUGH_EXCEPTIONS
107
+ raise
108
+ rescue Exception => e
109
+ reporter.spec_file_error(e)
110
+ nil
111
+ end
112
+
113
+ def run_specification
114
+ scope.each_service(specification) do |service|
115
+ @service = service
116
+ reporter.before_service
117
+ run_service
118
+ reporter.service_done
119
+ end
7
120
  end
8
121
 
122
+ def run_service
123
+ scope.each_testcase(service) do |test_case|
124
+ @test_case = test_case
125
+ reporter.before_test_case
126
+ run_test_case
127
+ reporter.test_case_done
128
+ end
129
+ end
130
+
131
+ def run_test_case
132
+ hooks.fire_around(self) do
133
+ reporter.before_each
134
+ hooks.fire_before_each(self)
135
+ reporter.before_each_done
136
+
137
+ reporter.before_instrument
138
+ instrument_test_case
139
+ reporter.instrument_done
140
+
141
+ call_test_case_target
142
+
143
+ reporter.before_assertions
144
+ check_invocation
145
+ reporter.assertions_done
146
+
147
+ reporter.after_each
148
+ hooks.fire_after_each(self)
149
+ reporter.after_each_done
150
+
151
+ raise FailFast if !result.success? and failfast?
152
+ end
153
+ end
154
+
155
+ def call_test_case_target
156
+ reporter.before_invocation
157
+ @invocation = client.call(test_case)
158
+ reporter.invocation_done
159
+ rescue *PASSTHROUGH_EXCEPTIONS
160
+ raise
161
+ rescue => ex
162
+ @invocation_error = ex
163
+ reporter.invocation_done
164
+ end
165
+
166
+ def instrument_test_case
167
+ service = test_case.service
168
+ service.preconditions.each do |pre|
169
+ instrument_one(pre)
170
+ end
171
+ service.postconditions.each do |post|
172
+ instrument_one(post)
173
+ end if test_case.example?
174
+ service.errconditions.each do |err|
175
+ instrument_one(err)
176
+ end if test_case.counterexample?
177
+ config.listeners(:instrument).each do |i|
178
+ i.call(self)
179
+ end
180
+ test_case
181
+ end
182
+
183
+ def instrument_one(condition)
184
+ bind_condition(condition).instrument
185
+ rescue ArgumentError
186
+ raise "#{condition.class} implements old PRE/POST contract"
187
+ end
188
+
189
+ def check_invocation
190
+ @result = Result.from(self)
191
+ end
192
+
193
+ def fork_tester(binding = {})
194
+ yield dup.tap{|t|
195
+ binding.each_pair do |k,v|
196
+ t.send(:"#{k}=", v)
197
+ end
198
+ }
199
+ end
200
+
201
+ private
202
+
203
+ attr_writer :test_case
204
+
9
205
  end # class Tester
10
206
  end # module Webspicy
207
+ require_relative 'tester/reporter'
11
208
  require_relative 'tester/client'
12
209
  require_relative 'tester/invocation'
210
+ require_relative 'tester/result'
13
211
  require_relative 'tester/failure'
14
212
  require_relative 'tester/assertions'
15
213
  require_relative 'tester/asserter'
214
+ require_relative 'tester/file_checker'
@@ -40,7 +40,7 @@ module Webspicy
40
40
  actual_size(target, path) == expected
41
41
  end
42
42
 
43
- def actual_size(target, path)
43
+ def actual_size(target, path)
44
44
  target = extract_path(target, path)
45
45
  respond_to!(target, :size).size
46
46
  end
@@ -68,7 +68,7 @@ module Webspicy
68
68
  an_array(target).find { |t| t[:id] == id }
69
69
  end
70
70
 
71
- def idFD(element, expected)
71
+ def idFD(element, expected)
72
72
  expected.keys.all? do |k|
73
73
  value_equal(expected[k], element[k])
74
74
  end
@@ -1,63 +1,17 @@
1
1
  module Webspicy
2
2
  class Tester
3
3
  class Client
4
+ extend Forwardable
4
5
 
5
6
  def initialize(scope)
6
7
  @scope = scope
7
8
  end
8
9
  attr_reader :scope
9
10
 
10
- def config
11
- scope.config
12
- end
13
-
14
- def around(*args, &bl)
15
- args << self
16
- ls = config.listeners(:around_each)
17
- if ls.size == 0
18
- bl.call
19
- elsif ls.size > 1
20
- _around(ls.first, ls[1..-1], args, &bl)
21
- else
22
- ls.first.call(*args, &bl)
23
- end
24
- end
25
-
26
- def _around(head, tail, args, &bl)
27
- head.call(*args) do
28
- if tail.empty?
29
- bl.call
30
- else
31
- _around(tail.first, tail[1..-1], args, &bl)
32
- end
33
- end
34
- end
35
- private :_around
36
-
37
- def instrument(*args, &bl)
38
- args << self
39
- config.listeners(:instrument).each do |i|
40
- i.call(*args, &bl)
41
- end
42
- end
43
-
44
- def before(*args, &bl)
45
- args << self
46
- config.listeners(:before_each).each do |beach|
47
- beach.call(*args, &bl)
48
- end
49
- end
50
-
51
- def after(*args, &bl)
52
- args << self
53
- config.listeners(:after_each).each do |aeach|
54
- aeach.call(*args, &bl)
55
- end
56
- end
11
+ def_delegators :@scope, *[
12
+ :config
13
+ ]
57
14
 
58
15
  end # class Client
59
16
  end # class Tester
60
17
  end # module Webspicy
61
- require_relative 'client/support'
62
- require_relative 'client/http_client'
63
- require_relative 'client/rack_test_client'