webspicy 0.17.0 → 0.20.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -0
  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 +17 -9
  9. data/lib/webspicy/configuration.rb +48 -8
  10. data/lib/webspicy/configuration/scope.rb +22 -8
  11. data/lib/webspicy/formaldoc.fio +5 -5
  12. data/lib/webspicy/specification.rb +5 -2
  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 +32 -0
  17. data/lib/webspicy/specification/{errcondition.rb → oldies/errcondition.rb} +6 -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 +34 -5
  27. data/lib/webspicy/specification/test_case.rb +11 -9
  28. data/lib/webspicy/support.rb +31 -0
  29. data/lib/webspicy/support/hooks.rb +65 -0
  30. data/lib/webspicy/support/world.rb +47 -0
  31. data/lib/webspicy/tester.rb +202 -3
  32. data/lib/webspicy/tester/assertions.rb +2 -2
  33. data/lib/webspicy/tester/client.rb +4 -50
  34. data/lib/webspicy/tester/fakeses.rb +41 -0
  35. data/lib/webspicy/tester/fakeses/email.rb +38 -0
  36. data/lib/webspicy/tester/fakesmtp.rb +39 -0
  37. data/lib/webspicy/tester/fakesmtp/email.rb +27 -0
  38. data/lib/webspicy/tester/file_checker.rb +22 -0
  39. data/lib/webspicy/tester/invocation.rb +15 -48
  40. data/lib/webspicy/tester/reporter.rb +85 -0
  41. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  42. data/lib/webspicy/tester/reporter/documentation.rb +74 -0
  43. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  44. data/lib/webspicy/tester/reporter/exceptions.rb +62 -0
  45. data/lib/webspicy/tester/reporter/file_progress.rb +25 -0
  46. data/lib/webspicy/tester/reporter/file_summary.rb +43 -0
  47. data/lib/webspicy/tester/reporter/progress.rb +30 -0
  48. data/lib/webspicy/tester/reporter/summary.rb +63 -0
  49. data/lib/webspicy/tester/result.rb +142 -0
  50. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  51. data/lib/webspicy/tester/result/check.rb +33 -0
  52. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  53. data/lib/webspicy/tester/result/error_schema_met.rb +25 -0
  54. data/lib/webspicy/tester/result/invocation_succeeded.rb +13 -0
  55. data/lib/webspicy/tester/result/output_schema_met.rb +25 -0
  56. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  57. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  58. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  59. data/lib/webspicy/version.rb +2 -2
  60. data/lib/webspicy/web.rb +4 -0
  61. data/lib/webspicy/web/client.rb +15 -0
  62. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  63. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  64. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  65. data/lib/webspicy/web/invocation.rb +69 -0
  66. data/lib/webspicy/web/mocker.rb +90 -0
  67. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  68. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  69. data/lib/webspicy/web/openapi/generator.rb +129 -0
  70. data/spec/unit/specification/{precondition → pre}/test_global_request_headers.rb +9 -4
  71. data/spec/unit/specification/test_condition.rb +44 -0
  72. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  73. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  74. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  75. data/spec/unit/support/world/fixtures/array.json +8 -0
  76. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  77. data/spec/unit/support/world/fixtures/single.json +11 -0
  78. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  79. data/spec/unit/support/world/test_world.rb +56 -0
  80. data/spec/unit/test_configuration.rb +49 -0
  81. data/spec/unit/tester/fakeses/test_email.rb +40 -0
  82. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  83. data/spec/unit/web/openapi/test_generator.rb +31 -0
  84. metadata +86 -72
  85. data/examples/restful/Gemfile +0 -5
  86. data/examples/restful/Gemfile.lock +0 -106
  87. data/examples/restful/Rakefile +0 -25
  88. data/examples/restful/app.rb +0 -180
  89. data/examples/restful/webspicy/config.rb +0 -24
  90. data/examples/restful/webspicy/formaldef/todo/_one/delete.yml +0 -55
  91. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +0 -46
  92. data/examples/restful/webspicy/formaldef/todo/_one/get.yml +0 -50
  93. data/examples/restful/webspicy/formaldef/todo/_one/patch.yml +0 -66
  94. data/examples/restful/webspicy/formaldef/todo/_one/put.yml +0 -65
  95. data/examples/restful/webspicy/formaldef/todo/get.yml +0 -36
  96. data/examples/restful/webspicy/formaldef/todo/options.yml +0 -32
  97. data/examples/restful/webspicy/formaldef/todo/post.csv.yml +0 -43
  98. data/examples/restful/webspicy/formaldef/todo/post.file.yml +0 -40
  99. data/examples/restful/webspicy/formaldef/todo/post.yml +0 -51
  100. data/examples/restful/webspicy/formaldef/todo/todos.csv +0 -4
  101. data/examples/restful/webspicy/rack.rb +0 -7
  102. data/examples/restful/webspicy/real.rb +0 -8
  103. data/examples/restful/webspicy/schema.fio +0 -20
  104. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  105. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  106. data/examples/restful/webspicy/support/todo_not_removed.rb +0 -21
  107. data/examples/restful/webspicy/support/todo_removed.rb +0 -20
  108. data/examples/single_spec/spec.yml +0 -59
  109. data/examples/website/config.rb +0 -2
  110. data/examples/website/schema.fio +0 -1
  111. data/examples/website/specification/get-http.yml +0 -30
  112. data/examples/website/specification/get-https.yml +0 -30
  113. data/lib/webspicy/checker.rb +0 -10
  114. data/lib/webspicy/mocker.rb +0 -88
  115. data/lib/webspicy/openapi/generator.rb +0 -127
  116. data/lib/webspicy/rspec/checker.rb +0 -2
  117. data/lib/webspicy/rspec/checker/rspec_checker.rb +0 -24
  118. data/lib/webspicy/rspec/support/rspec_runnable.rb +0 -27
  119. data/lib/webspicy/rspec/tester.rb +0 -4
  120. data/lib/webspicy/rspec/tester/rspec_asserter.rb +0 -121
  121. data/lib/webspicy/rspec/tester/rspec_matchers.rb +0 -114
  122. data/lib/webspicy/rspec/tester/rspec_tester.rb +0 -63
  123. data/spec/unit/mocker/test_mocker.rb +0 -32
  124. 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,10 @@ module Webspicy
93
93
  end # class Specification
94
94
  end # module Webspicy
95
95
  require_relative 'specification/service'
96
- require_relative 'specification/precondition'
97
- require_relative 'specification/postcondition'
96
+ require_relative 'specification/condition'
97
+ require_relative 'specification/pre'
98
+ require_relative 'specification/post'
99
+ require_relative 'specification/err'
100
+ require_relative 'specification/oldies'
98
101
  require_relative 'specification/test_case'
99
102
  require_relative 'specification/file_upload'
@@ -0,0 +1,48 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Condition
4
+ extend Forwardable
5
+
6
+ MATCH_ALL = "__all__"
7
+
8
+ attr_accessor :matching_description
9
+
10
+ # Given a service and a condition, returns a Pre instance of there is a
11
+ # match, nil otherwise.
12
+ def self.match(service, condition)
13
+ end
14
+
15
+ # Bind the condition instance to a current tester.
16
+ def bind(tester)
17
+ @tester = tester
18
+ self
19
+ end
20
+ attr_reader :tester
21
+
22
+ def_delegators :@tester, *[
23
+ :config, :scope, :client,
24
+ :specification, :spec_file,
25
+ :service, :test_case,
26
+ :invocation, :result,
27
+ :reporter
28
+ ]
29
+
30
+ def sooner_or_later(*args, &bl)
31
+ Webspicy::Support.sooner_or_later(*args, &bl)
32
+ end
33
+
34
+ def fail!(msg)
35
+ raise Tester::Failure, msg
36
+ end
37
+
38
+ def to_s
39
+ if matching_description == MATCH_ALL
40
+ self.class.name
41
+ else
42
+ matching_description
43
+ end
44
+ end
45
+
46
+ end # module Condition
47
+ end # class Specification
48
+ end # module Webspicy
@@ -0,0 +1,18 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Err
4
+ include Condition
5
+
6
+ # Instrument the current test_case so as to prepare for errcondition
7
+ # check later
8
+ def instrument
9
+ end
10
+
11
+ # Check that the errcondition is met on last invocation & result
12
+ # of an counterexample
13
+ def check!
14
+ end
15
+
16
+ end # module Err
17
+ end # module Specification
18
+ end # module Webspicy
@@ -0,0 +1,4 @@
1
+ require_relative 'oldies/precondition'
2
+ require_relative 'oldies/postcondition'
3
+ require_relative 'oldies/errcondition'
4
+ require_relative 'oldies/bridge'
@@ -0,0 +1,32 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Oldies
4
+ class Bridge
5
+ include Condition
6
+
7
+ def initialize(target)
8
+ @target = target
9
+ end
10
+ attr_reader :target
11
+
12
+ def instrument
13
+ return unless target.respond_to?(:instrument)
14
+ target.instrument(test_case, client)
15
+ end
16
+
17
+ def check!
18
+ return unless target.respond_to?(:check)
19
+ res = target.check(invocation)
20
+ res ? fail!(res) : nil
21
+ end
22
+
23
+ def to_s
24
+ "#{target} (backward compatibility bridge)"
25
+ end
26
+
27
+ end # class Bridge
28
+ end # module Errcondition
29
+ end # module Specification
30
+ Precondition = Specification::Precondition
31
+ Postcondition = Specification::Postcondition
32
+ end # module Webspicy
@@ -1,6 +1,12 @@
1
1
  module Webspicy
2
2
  class Specification
3
+ # Deprecated, use Err instead
3
4
  module Errcondition
5
+ include Condition
6
+
7
+ def bind(tester)
8
+ Oldies::Bridge.new(self).bind(tester)
9
+ end
4
10
 
5
11
  def self.match(service, descr)
6
12
  end
@@ -1,6 +1,12 @@
1
1
  module Webspicy
2
2
  class Specification
3
+ # Deprecated, use Post instead
3
4
  module Postcondition
5
+ include Condition
6
+
7
+ def bind(tester)
8
+ Oldies::Bridge.new(self).bind(tester)
9
+ end
4
10
 
5
11
  def self.match(service, descr)
6
12
  end
@@ -1,6 +1,12 @@
1
1
  module Webspicy
2
2
  class Specification
3
+ # Deprecated, use Pre instead
3
4
  module Precondition
5
+ include Condition
6
+
7
+ def bind(tester)
8
+ Oldies::Bridge.new(self).bind(tester)
9
+ end
4
10
 
5
11
  def self.match(service, pre)
6
12
  end
@@ -15,5 +21,3 @@ module Webspicy
15
21
  end # module Precondition
16
22
  end # class Specification
17
23
  end # module Webspicy
18
- require_relative 'precondition/global_request_headers'
19
- require_relative 'precondition/robust_to_invalid_input'
@@ -0,0 +1,20 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Post
4
+ include Condition
5
+
6
+ # Instrument the current test_case so as to prepare for postcondition
7
+ # check later
8
+ def instrument
9
+ end
10
+
11
+ # Check that the postcondition is met on last invocation & result
12
+ # of an example
13
+ def check!
14
+ end
15
+
16
+ end # module Post
17
+ end # module Specification
18
+ end # module Webspicy
19
+ require_relative "post/missing_condition_impl"
20
+ require_relative "post/unexpected_condition_impl"
@@ -0,0 +1,15 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Postcondition
4
+ class MissingConditionImpl
5
+ include Post
6
+
7
+ def check
8
+ msg = matching_description.gsub(/\(x\)/, "<!>")
9
+ raise "#{msg} (not instrumented)"
10
+ end
11
+
12
+ end # class MissingConditionImpl
13
+ end # module Postcondition
14
+ end # class Specification
15
+ end # module Webspicy
@@ -0,0 +1,15 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Postcondition
4
+ class UnexpectedConditionImpl
5
+ include Post
6
+
7
+ def check
8
+ msg = matching_description.gsub(/\( \)/, "<x>")
9
+ raise "#{msg} (is instrumented)"
10
+ end
11
+
12
+ end # class UnexpectedConditionImpl
13
+ end # module Postcondition
14
+ end # class Specification
15
+ end # module Webspicy
@@ -0,0 +1,19 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Pre
4
+ include Condition
5
+
6
+ # Instrument the current test_case so as to meet the precondition
7
+ def instrument
8
+ end
9
+
10
+ # Provide counterexamples of this precondition for a given service.
11
+ def counterexamples(service)
12
+ []
13
+ end
14
+
15
+ end # module Pre
16
+ end # class Specification
17
+ end # module Webspicy
18
+ require_relative 'pre/global_request_headers'
19
+ require_relative 'pre/robust_to_invalid_input'
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  class Specification
3
- module Precondition
3
+ module Pre
4
4
  class GlobalRequestHeaders
5
- include Precondition
5
+ include Pre
6
6
 
7
7
  DEFAULT_OPTIONS = {}
8
8
 
@@ -22,7 +22,7 @@ module Webspicy
22
22
  end
23
23
  end
24
24
 
25
- def instrument(test_case, client)
25
+ def instrument
26
26
  extra = headers.reject{|k|
27
27
  test_case.headers.has_key?(k)
28
28
  }
@@ -30,6 +30,6 @@ module Webspicy
30
30
  end
31
31
 
32
32
  end # class GlobalRequestHeaders
33
- end # module Precondition
33
+ end # module Pre
34
34
  end # class Specification
35
35
  end # module Webspicy
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  class Specification
3
- module Precondition
3
+ module Pre
4
4
  class RobustToInvalidInput
5
- include Precondition
5
+ include Pre
6
6
 
7
7
  def self.match(service, pre)
8
8
  self.new
@@ -49,7 +49,7 @@ module Webspicy
49
49
  status: Support::StatusRange.str("4xx")
50
50
  },
51
51
  :assert => []
52
- })]
52
+ })]
53
53
  else
54
54
  []
55
55
  end
@@ -63,6 +63,6 @@ module Webspicy
63
63
  end
64
64
 
65
65
  end # class RobustToInvalidInput
66
- end # module Precondition
66
+ end # module Pre
67
67
  end # class Specification
68
68
  end # module Webspicy
@@ -10,6 +10,9 @@ module Webspicy
10
10
  end
11
11
  attr_accessor :specification
12
12
 
13
+ # Deprecated
14
+ alias :resource :specification
15
+
13
16
  def self.info(raw)
14
17
  new(raw)
15
18
  end
@@ -111,12 +114,38 @@ module Webspicy
111
114
  def compile_conditions(descriptions, conditions)
112
115
  # Because we want pre & post to be able to match in all cases
113
116
  # 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
117
+ descriptions = [Condition::MATCH_ALL] if descriptions.empty?
118
+ mapping = {}
119
+ instances = conditions.map{|c|
120
+ instance = nil
121
+ descr = descriptions.find do |d|
122
+ instance = c.match(self, d)
123
+ end
124
+ instance.tap{|i|
125
+ mapping[descr] ||= i if i
126
+ i.matching_description = descr if i.respond_to?(:matching_description=)
127
+ }
128
+ }.compact
129
+ mapped = descriptions
130
+ .select{|d| mapping[d] }
131
+ .map{|d| mapping[d] }
132
+ unmapped = descriptions
133
+ .reject{|d| mapping[d] }
134
+ .select{|d| d.strip =~ /^(\(\w+\))?\(x\)/ }
135
+ .map{|d|
136
+ Postcondition::MissingConditionImpl.new.tap{|mc|
137
+ mc.matching_description = d
138
+ }
139
+ }
140
+ unexpected = descriptions
141
+ .select{|d| mapping[d] }
142
+ .select{|d| d.strip =~ /^(\(\w+\))?\( \)/ }
143
+ .map{|d|
144
+ Postcondition::UnexpectedConditionImpl.new.tap{|mc|
145
+ mc.matching_description = d
146
+ }
118
147
  }
119
- .flatten
148
+ mapped + unmapped + unexpected
120
149
  end
121
150
 
122
151
  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
@@ -24,6 +28,9 @@ module Webspicy
24
28
  service.specification
25
29
  end
26
30
 
31
+ # Deprecated
32
+ alias :resource :specification
33
+
27
34
  def self.info(raw)
28
35
  new(raw)
29
36
  end
@@ -85,6 +92,10 @@ module Webspicy
85
92
  expected_status === status
86
93
  end
87
94
 
95
+ def has_expected_status?
96
+ not expected[:status].nil?
97
+ end
98
+
88
99
  def expected_error
89
100
  expected[:error]
90
101
  end
@@ -109,15 +120,6 @@ module Webspicy
109
120
  !assert.empty?
110
121
  end
111
122
 
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
123
  def mutate(override)
122
124
  m = self.dup
123
125
  m.raw = self.raw.merge(override)