webspicy 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) 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 -7
  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 +2 -0
  12. data/lib/webspicy/specification/condition.rb +23 -0
  13. data/lib/webspicy/specification/errcondition.rb +1 -0
  14. data/lib/webspicy/specification/postcondition.rb +1 -0
  15. data/lib/webspicy/specification/precondition.rb +1 -0
  16. data/lib/webspicy/specification/service.rb +11 -6
  17. data/lib/webspicy/specification/test_case.rb +8 -9
  18. data/lib/webspicy/support.rb +21 -0
  19. data/lib/webspicy/support/hooks.rb +65 -0
  20. data/lib/webspicy/support/world.rb +47 -0
  21. data/lib/webspicy/tester.rb +136 -3
  22. data/lib/webspicy/tester/client.rb +22 -42
  23. data/lib/webspicy/tester/file_checker.rb +22 -0
  24. data/lib/webspicy/tester/invocation.rb +15 -48
  25. data/lib/webspicy/tester/reporter.rb +85 -0
  26. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  27. data/lib/webspicy/tester/reporter/documentation.rb +67 -0
  28. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  29. data/lib/webspicy/tester/reporter/exceptions.rb +60 -0
  30. data/lib/webspicy/tester/reporter/file_progress.rb +22 -0
  31. data/lib/webspicy/tester/reporter/file_summary.rb +42 -0
  32. data/lib/webspicy/tester/reporter/progress.rb +28 -0
  33. data/lib/webspicy/tester/reporter/summary.rb +62 -0
  34. data/lib/webspicy/tester/result.rb +139 -0
  35. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  36. data/lib/webspicy/tester/result/check.rb +33 -0
  37. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  38. data/lib/webspicy/tester/result/error_schema_met.rb +24 -0
  39. data/lib/webspicy/tester/result/output_schema_met.rb +24 -0
  40. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  41. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  42. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  43. data/lib/webspicy/version.rb +1 -1
  44. data/lib/webspicy/web.rb +4 -0
  45. data/lib/webspicy/web/client.rb +15 -0
  46. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  47. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  48. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  49. data/lib/webspicy/web/invocation.rb +65 -0
  50. data/lib/webspicy/web/mocker.rb +90 -0
  51. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  52. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  53. data/lib/webspicy/web/openapi/generator.rb +129 -0
  54. data/spec/unit/specification/test_condition.rb +26 -0
  55. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  56. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  57. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  58. data/spec/unit/support/world/fixtures/array.json +8 -0
  59. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  60. data/spec/unit/support/world/fixtures/single.json +11 -0
  61. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  62. data/spec/unit/support/world/test_world.rb +56 -0
  63. data/spec/unit/test_configuration.rb +49 -0
  64. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  65. data/spec/unit/web/openapi/test_generator.rb +31 -0
  66. metadata +61 -59
  67. data/examples/restful/Gemfile +0 -5
  68. data/examples/restful/Rakefile +0 -25
  69. data/examples/restful/app.rb +0 -180
  70. data/examples/restful/webspicy/config.rb +0 -24
  71. data/examples/restful/webspicy/formaldef/todo/_one/delete.yml +0 -55
  72. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +0 -46
  73. data/examples/restful/webspicy/formaldef/todo/_one/get.yml +0 -50
  74. data/examples/restful/webspicy/formaldef/todo/_one/patch.yml +0 -66
  75. data/examples/restful/webspicy/formaldef/todo/_one/put.yml +0 -65
  76. data/examples/restful/webspicy/formaldef/todo/get.yml +0 -36
  77. data/examples/restful/webspicy/formaldef/todo/options.yml +0 -32
  78. data/examples/restful/webspicy/formaldef/todo/post.csv.yml +0 -43
  79. data/examples/restful/webspicy/formaldef/todo/post.file.yml +0 -40
  80. data/examples/restful/webspicy/formaldef/todo/post.yml +0 -51
  81. data/examples/restful/webspicy/formaldef/todo/todos.csv +0 -4
  82. data/examples/restful/webspicy/rack.rb +0 -7
  83. data/examples/restful/webspicy/real.rb +0 -8
  84. data/examples/restful/webspicy/schema.fio +0 -20
  85. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  86. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  87. data/examples/restful/webspicy/support/todo_not_removed.rb +0 -21
  88. data/examples/restful/webspicy/support/todo_removed.rb +0 -20
  89. data/examples/single_spec/spec.yml +0 -59
  90. data/examples/website/config.rb +0 -2
  91. data/examples/website/schema.fio +0 -1
  92. data/examples/website/specification/get-http.yml +0 -30
  93. data/examples/website/specification/get-https.yml +0 -30
  94. data/lib/webspicy/checker.rb +0 -10
  95. data/lib/webspicy/mocker.rb +0 -88
  96. data/lib/webspicy/openapi/generator.rb +0 -127
  97. data/lib/webspicy/rspec/checker.rb +0 -2
  98. data/lib/webspicy/rspec/checker/rspec_checker.rb +0 -24
  99. data/lib/webspicy/rspec/support/rspec_runnable.rb +0 -27
  100. data/lib/webspicy/rspec/tester.rb +0 -4
  101. data/lib/webspicy/rspec/tester/rspec_asserter.rb +0 -121
  102. data/lib/webspicy/rspec/tester/rspec_matchers.rb +0 -114
  103. data/lib/webspicy/rspec/tester/rspec_tester.rb +0 -63
  104. data/spec/unit/mocker/test_mocker.rb +0 -32
  105. data/spec/unit/openapi/test_generator.rb +0 -28
@@ -12,25 +12,28 @@ module Webspicy
12
12
  ###
13
13
 
14
14
  # Yields each specification file in the current scope
15
- def each_specification_file(&bl)
16
- return enum_for(:each_specification_file) unless block_given?
17
- _each_specification_file(config, &bl)
15
+ def each_specification_file(apply_filter = true, &bl)
16
+ return enum_for(:each_specification_file, apply_filter) unless block_given?
17
+ _each_specification_file(config, apply_filter, &bl)
18
18
  end
19
19
 
20
20
  # Recursive implementation of `each_specification_file` for each
21
21
  # folder in the configuration.
22
- def _each_specification_file(config)
22
+ def _each_specification_file(config, apply_filter = true)
23
23
  folder = config.folder
24
- folder.glob("**/*.yml").select(&to_filter_proc(config.file_filter)).each do |file|
24
+ world = config.folder/"world"
25
+ fs = folder.glob("**/*.yml").reject{|f| f.to_s.start_with?(world.to_s) }
26
+ fs = fs.select(&to_filter_proc(config.file_filter)) if apply_filter
27
+ fs.each do |file|
25
28
  yield file, folder
26
29
  end
27
30
  end
28
31
  private :_each_specification_file
29
32
 
30
33
  # Yields each specification in the current scope in turn.
31
- def each_specification(&bl)
32
- return enum_for(:each_specification) unless block_given?
33
- each_specification_file do |file, folder|
34
+ def each_specification(apply_filter = true, &bl)
35
+ return enum_for(:each_specification, apply_filter) unless block_given?
36
+ each_specification_file(apply_filter) do |file, folder|
34
37
  yield Webspicy.specification(file.load, file, self)
35
38
  end
36
39
  end
@@ -68,6 +71,17 @@ module Webspicy
68
71
  each_generated_counterexamples(service, &bl)
69
72
  end
70
73
 
74
+ def find_test_case(method, url)
75
+ each_specification(false) do |spec|
76
+ next unless spec.url == url
77
+ spec.services.each do |service|
78
+ next unless service.method == method
79
+ return service.examples.first
80
+ end
81
+ end
82
+ nil
83
+ end
84
+
71
85
  ###
72
86
  ### Schemas -- For parsing input and output data schemas found in
73
87
  ### web service definitions
@@ -18,6 +18,11 @@ FileUpload =
18
18
  }
19
19
 
20
20
  Specification = .Webspicy::Specification
21
+ <info> {
22
+ name: String
23
+ url: String
24
+ services: [Service]
25
+ }
21
26
  <singleservice> {
22
27
  name :? String
23
28
  url : String
@@ -34,11 +39,6 @@ Specification = .Webspicy::Specification
34
39
  examples :? [TestCase]
35
40
  counterexamples :? [TestCase]
36
41
  }
37
- <info> {
38
- name: String
39
- url: String
40
- services: [Service]
41
- }
42
42
 
43
43
  Service =
44
44
  .Webspicy::Specification::Service <info> {
@@ -93,7 +93,9 @@ module Webspicy
93
93
  end # class Specification
94
94
  end # module Webspicy
95
95
  require_relative 'specification/service'
96
+ require_relative 'specification/condition'
96
97
  require_relative 'specification/precondition'
97
98
  require_relative 'specification/postcondition'
99
+ require_relative 'specification/errcondition'
98
100
  require_relative 'specification/test_case'
99
101
  require_relative 'specification/file_upload'
@@ -0,0 +1,23 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Condition
4
+
5
+ MATCH_ALL = "__all__"
6
+
7
+ attr_accessor :matching_description
8
+
9
+ def to_s
10
+ if matching_description == MATCH_ALL
11
+ self.class.name
12
+ else
13
+ matching_description
14
+ end
15
+ end
16
+
17
+ def sooner_or_later(*args, &bl)
18
+ Webspicy::Support.sooner_or_later(*args, &bl)
19
+ end
20
+
21
+ end # module Condition
22
+ end # class Specification
23
+ end # module Webspicy
@@ -1,6 +1,7 @@
1
1
  module Webspicy
2
2
  class Specification
3
3
  module Errcondition
4
+ include Condition
4
5
 
5
6
  def self.match(service, descr)
6
7
  end
@@ -1,6 +1,7 @@
1
1
  module Webspicy
2
2
  class Specification
3
3
  module Postcondition
4
+ include Condition
4
5
 
5
6
  def self.match(service, descr)
6
7
  end
@@ -1,6 +1,7 @@
1
1
  module Webspicy
2
2
  class Specification
3
3
  module Precondition
4
+ include Condition
4
5
 
5
6
  def self.match(service, pre)
6
7
  end
@@ -111,12 +111,17 @@ module Webspicy
111
111
  def compile_conditions(descriptions, conditions)
112
112
  # Because we want pre & post to be able to match in all cases
113
113
  # we need at least one condition
114
- descriptions = ["all"] if descriptions.empty?
115
- descriptions
116
- .map{|descr|
117
- conditions.map{|c| c.match(self, descr) }.compact
118
- }
119
- .flatten
114
+ descriptions = [Condition::MATCH_ALL] if descriptions.empty?
115
+ conditions.map{|c|
116
+ instance = nil
117
+ descr = descriptions.find do |d|
118
+ instance = c.match(self, d)
119
+ end
120
+ if instance && instance.respond_to?(:matching_description=)
121
+ instance.matching_description = descr
122
+ end
123
+ instance
124
+ }.compact
120
125
  end
121
126
 
122
127
  def bind_examples
@@ -16,6 +16,10 @@ module Webspicy
16
16
  self
17
17
  end
18
18
 
19
+ def example?
20
+ !@counterexample
21
+ end
22
+
19
23
  def counterexample?
20
24
  !!@counterexample
21
25
  end
@@ -85,6 +89,10 @@ module Webspicy
85
89
  expected_status === status
86
90
  end
87
91
 
92
+ def has_expected_status?
93
+ not expected[:status].nil?
94
+ end
95
+
88
96
  def expected_error
89
97
  expected[:error]
90
98
  end
@@ -109,15 +117,6 @@ module Webspicy
109
117
  !assert.empty?
110
118
  end
111
119
 
112
- def instrument(client)
113
- service.preconditions.each do |pre|
114
- pre.instrument(self, client) if pre.respond_to?(:instrument)
115
- end
116
- service.postconditions.each do |post|
117
- post.instrument(self, client) if post.respond_to?(:instrument)
118
- end
119
- end
120
-
121
120
  def mutate(override)
122
121
  m = self.dup
123
122
  m.raw = self.raw.merge(override)
@@ -1,3 +1,24 @@
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)
7
+ opts = SORL_OPTS.merge(opts || {})
8
+ left, wait_ms = opts[:max], opts[:wait]
9
+ until (r = yield) || left == 0
10
+ sleep(wait_ms)
11
+ wait_ms, left = wait_ms*2, left - 1
12
+ end
13
+ raise TimeoutError, "Timeout on sooner-or-later" if r.nil? && opts[:raise]
14
+ r
15
+ end
16
+ module_function :sooner_or_later
17
+
18
+ end # module Support
19
+ end # module Webspicy
1
20
  require_relative 'support/data_object'
2
21
  require_relative 'support/status_range'
3
22
  require_relative 'support/colorize'
23
+ require_relative 'support/world'
24
+ 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,148 @@
1
1
  module Webspicy
2
2
  class Tester
3
3
 
4
- def self.new(*args, &bl)
5
- require_relative 'rspec/tester'
6
- RSpecTester.new(*args, &bl)
4
+ class FailFast < Exception; end
5
+
6
+ def initialize(config)
7
+ @config = Configuration.dress(config)
8
+ @scope = nil
9
+ @hooks = nil
10
+ @client = nil
11
+ @spec_file = nil
12
+ @specification = nil
13
+ @service = nil
14
+ @test_case = nil
15
+ @invocation = nil
16
+ @reporter = default_reporter
17
+ end
18
+ attr_reader :config, :scope, :hooks, :client
19
+ attr_reader :specification, :spec_file
20
+ attr_reader :service, :test_case
21
+ attr_reader :invocation, :result
22
+ attr_reader :reporter
23
+
24
+ def failfast?
25
+ config.failfast
26
+ end
27
+
28
+ def default_reporter
29
+ @reporter = Reporter::Composite.new
30
+ #@reporter << Reporter::Progress.new
31
+ @reporter << Reporter::Documentation.new
32
+ @reporter << Reporter::Exceptions.new
33
+ @reporter << Reporter::Summary.new
34
+ @reporter << Reporter::ErrorCount.new
35
+ end
36
+
37
+ def call
38
+ reporter.init(self)
39
+ begin
40
+ run_config
41
+ rescue FailFast
42
+ end
43
+ reporter.report
44
+ reporter.find(Reporter::ErrorCount).report
45
+ end
46
+
47
+ private
48
+
49
+ def run_config
50
+ config.each_scope do |scope|
51
+ @scope = scope
52
+ @hooks = Support::Hooks.for(scope.config)
53
+ @client = scope.get_client
54
+ reporter.before_all
55
+ @hooks.fire_before_all(@scope, @client)
56
+ reporter.before_all_done
57
+ reporter.before_scope
58
+ run_scope
59
+ reporter.scope_done
60
+ reporter.after_all
61
+ @hooks.fire_after_all(@scope, @client)
62
+ reporter.after_all_done
63
+ end
64
+ end
65
+
66
+ def run_scope
67
+ scope.each_specification_file do |spec_file|
68
+ @specification = load_specification(spec_file)
69
+ if @specification
70
+ reporter.before_specification
71
+ run_specification
72
+ reporter.specification_done
73
+ reporter.spec_file_done
74
+ elsif failfast?
75
+ raise FailFast
76
+ end
77
+ end
78
+ end
79
+
80
+ def load_specification(spec_file)
81
+ @spec_file = spec_file
82
+ reporter.before_spec_file
83
+ Webspicy.specification(spec_file.load, spec_file, scope)
84
+ rescue *PASSTHROUGH_EXCEPTIONS
85
+ raise
86
+ rescue Exception => e
87
+ reporter.spec_file_error(e)
88
+ nil
89
+ end
90
+
91
+ def run_specification
92
+ scope.each_service(specification) do |service|
93
+ @service = service
94
+ reporter.before_service
95
+ run_service
96
+ reporter.service_done
97
+ end
98
+ end
99
+
100
+ def run_service
101
+ scope.each_testcase(service) do |test_case|
102
+ @test_case = test_case
103
+ reporter.before_test_case
104
+ run_test_case
105
+ reporter.test_case_done
106
+ end
107
+ end
108
+
109
+ def run_test_case
110
+ hooks.fire_around(test_case, client) do
111
+ reporter.before_each
112
+ hooks.fire_before_each(test_case, client)
113
+ reporter.before_each_done
114
+
115
+ reporter.before_instrument
116
+ client.instrument(test_case)
117
+ reporter.instrument_done
118
+
119
+ reporter.before_invocation
120
+ @invocation = client.call(test_case)
121
+ reporter.invocation_done
122
+
123
+ reporter.before_assertions
124
+ check_invocation
125
+ reporter.assertions_done
126
+
127
+ reporter.after_each
128
+ hooks.fire_after_each(test_case, @invocation, client)
129
+ reporter.after_each_done
130
+
131
+ raise FailFast if !result.success? and failfast?
132
+ end
133
+ end
134
+
135
+ def check_invocation
136
+ @result = Result.from(self)
7
137
  end
8
138
 
9
139
  end # class Tester
10
140
  end # module Webspicy
141
+ require_relative 'tester/reporter'
11
142
  require_relative 'tester/client'
12
143
  require_relative 'tester/invocation'
144
+ require_relative 'tester/result'
13
145
  require_relative 'tester/failure'
14
146
  require_relative 'tester/assertions'
15
147
  require_relative 'tester/asserter'
148
+ require_relative 'tester/file_checker'