webspicy 0.15.8 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +76 -24
  3. data/bin/webspicy +30 -14
  4. data/examples/restful/Gemfile.lock +40 -24
  5. data/examples/restful/Rakefile +0 -1
  6. data/examples/restful/app.rb +4 -1
  7. data/examples/restful/webspicy/config.rb +9 -0
  8. data/examples/restful/webspicy/{todo/deleteTodo.yml → formaldef/todo/_one/delete.yml} +7 -1
  9. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +46 -0
  10. data/examples/restful/webspicy/{todo/getTodo.yml → formaldef/todo/_one/get.yml} +0 -0
  11. data/examples/restful/webspicy/{todo/patchTodo.yml → formaldef/todo/_one/patch.yml} +3 -0
  12. data/examples/restful/webspicy/{todo/putTodo.yml → formaldef/todo/_one/put.yml} +0 -0
  13. data/examples/restful/webspicy/{todo/getTodos.yml → formaldef/todo/get.yml} +0 -0
  14. data/examples/restful/webspicy/{todo → formaldef/todo}/options.yml +1 -1
  15. data/examples/restful/webspicy/{todo/postCsv.yml → formaldef/todo/post.csv.yml} +0 -0
  16. data/examples/restful/webspicy/{todo/postFile.yml → formaldef/todo/post.file.yml} +1 -1
  17. data/examples/restful/webspicy/{todo/postTodos.yml → formaldef/todo/post.yml} +0 -0
  18. data/examples/restful/webspicy/{todo → formaldef/todo}/todos.csv +0 -0
  19. data/examples/restful/webspicy/rack.rb +1 -1
  20. data/examples/restful/webspicy/real.rb +1 -1
  21. data/examples/restful/webspicy/schema.fio +2 -2
  22. data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
  23. data/examples/restful/webspicy/support/todo_not_removed.rb +21 -0
  24. data/examples/restful/webspicy/support/todo_removed.rb +20 -0
  25. data/examples/single_spec/spec.yml +59 -0
  26. data/examples/website/config.rb +2 -0
  27. data/examples/website/schema.fio +1 -0
  28. data/examples/website/specification/get-http.yml +30 -0
  29. data/examples/website/specification/get-https.yml +30 -0
  30. data/lib/finitio/webspicy/scalars.fio +25 -0
  31. data/lib/webspicy.rb +49 -17
  32. data/lib/webspicy/checker.rb +5 -20
  33. data/lib/webspicy/configuration.rb +79 -14
  34. data/lib/webspicy/configuration/scope.rb +154 -0
  35. data/lib/webspicy/configuration/single_url.rb +58 -0
  36. data/lib/webspicy/configuration/single_yml_file.rb +30 -0
  37. data/lib/webspicy/formaldoc.fio +25 -8
  38. data/lib/webspicy/mocker.rb +8 -8
  39. data/lib/webspicy/mocker/config.ru +5 -0
  40. data/lib/webspicy/openapi.rb +1 -0
  41. data/lib/webspicy/openapi/generator.rb +127 -0
  42. data/lib/webspicy/rspec/checker.rb +2 -0
  43. data/lib/webspicy/rspec/checker/rspec_checker.rb +24 -0
  44. data/lib/webspicy/rspec/support/rspec_runnable.rb +27 -0
  45. data/lib/webspicy/rspec/tester.rb +4 -0
  46. data/lib/webspicy/rspec/tester/rspec_asserter.rb +121 -0
  47. data/lib/webspicy/rspec/tester/rspec_matchers.rb +114 -0
  48. data/lib/webspicy/rspec/tester/rspec_tester.rb +63 -0
  49. data/lib/webspicy/{resource.rb → specification.rb} +31 -10
  50. data/lib/webspicy/specification/errcondition.rb +16 -0
  51. data/lib/webspicy/specification/file_upload.rb +37 -0
  52. data/lib/webspicy/specification/postcondition.rb +16 -0
  53. data/lib/webspicy/specification/precondition.rb +19 -0
  54. data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
  55. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
  56. data/lib/webspicy/{resource → specification}/service.rb +38 -25
  57. data/lib/webspicy/specification/test_case.rb +133 -0
  58. data/lib/webspicy/support.rb +2 -0
  59. data/lib/webspicy/support/colorize.rb +28 -0
  60. data/lib/webspicy/support/data_object.rb +25 -0
  61. data/lib/webspicy/support/status_range.rb +6 -1
  62. data/lib/webspicy/tester.rb +8 -77
  63. data/lib/webspicy/tester/asserter.rb +11 -5
  64. data/lib/webspicy/tester/assertions.rb +13 -10
  65. data/lib/webspicy/tester/client.rb +63 -0
  66. data/lib/webspicy/tester/client/http_client.rb +154 -0
  67. data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
  68. data/lib/webspicy/tester/client/support.rb +65 -0
  69. data/lib/webspicy/tester/failure.rb +6 -0
  70. data/lib/webspicy/tester/invocation.rb +70 -0
  71. data/lib/webspicy/version.rb +2 -2
  72. data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
  73. data/spec/unit/configuration/scope/test_each_service.rb +49 -0
  74. data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
  75. data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
  76. data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
  77. data/spec/unit/openapi/test_generator.rb +28 -0
  78. data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
  79. data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
  80. data/spec/unit/specification/test_case/test_mutate.rb +24 -0
  81. data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
  82. data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
  83. data/spec/unit/test_configuration.rb +24 -7
  84. data/spec/unit/tester/client/test_around.rb +61 -0
  85. data/spec/unit/tester/test_asserter.rb +246 -0
  86. data/spec/unit/tester/test_assertions.rb +12 -10
  87. data/tasks/test.rake +3 -1
  88. metadata +106 -48
  89. data/LICENSE.md +0 -22
  90. data/lib/webspicy/client.rb +0 -61
  91. data/lib/webspicy/client/http_client.rb +0 -145
  92. data/lib/webspicy/client/rack_test_client.rb +0 -181
  93. data/lib/webspicy/client/support.rb +0 -48
  94. data/lib/webspicy/file_upload.rb +0 -35
  95. data/lib/webspicy/postcondition.rb +0 -14
  96. data/lib/webspicy/precondition.rb +0 -15
  97. data/lib/webspicy/resource/service/invocation.rb +0 -212
  98. data/lib/webspicy/resource/service/test_case.rb +0 -132
  99. data/lib/webspicy/scope.rb +0 -160
  100. data/spec/unit/client/test_around.rb +0 -59
  101. data/spec/unit/scope/test_each_resource.rb +0 -66
  102. data/spec/unit/scope/test_each_service.rb +0 -47
  103. data/spec/unit/scope/test_expand_example.rb +0 -63
  104. data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -9,31 +9,62 @@ module Webspicy
9
9
  @children = []
10
10
  @preconditions = []
11
11
  @postconditions = []
12
+ @errconditions = []
12
13
  @listeners = Hash.new{|h,k| h[k] = [] }
13
14
  @rspec_options = default_rspec_options
14
15
  @run_examples = default_run_examples
15
16
  @run_counterexamples = default_run_counterexamples
17
+ @run_generated_counterexamples = default_run_generated_counterexamples
16
18
  @file_filter = default_file_filter
17
19
  @service_filter = default_service_filter
18
20
  @test_case_filter = default_test_case_filter
19
- @client = HttpClient
21
+ @colors = {
22
+ :highlight => :cyan,
23
+ :error => :red,
24
+ :success => :green
25
+ }
26
+ @scope_factory = ->(config){ Scope.new(config) }
27
+ @client = Tester::HttpClient
20
28
  Path.require_tree(folder/'support') if (folder/'support').exists?
21
29
  yield(self) if block_given?
22
30
  end
23
31
  attr_accessor :folder
24
32
  protected :folder=
25
33
 
34
+ attr_accessor :colors
35
+
26
36
  def self.dress(arg, &bl)
27
- return arg if arg.is_a?(Configuration)
28
- arg = Path(arg)
29
- if arg.file?
30
- c = Kernel.instance_eval arg.read, arg.to_s
31
- yield(c) if block_given?
32
- c
33
- elsif (arg/'config.rb').file?
34
- dress(arg/'config.rb', &bl)
37
+ case arg
38
+ when Configuration
39
+ arg
40
+ when /^https?:\/\//
41
+ Configuration.new{|c|
42
+ c.scope_factory = SingleUrl.new(arg)
43
+ }
44
+ when ->(f){ Path(f).exists? }
45
+ arg = Path(arg)
46
+ if arg.file? && arg.ext == ".rb"
47
+ c = Kernel.instance_eval arg.read, arg.to_s
48
+ yield(c) if block_given?
49
+ c
50
+ elsif arg.file? && arg.ext == '.yml'
51
+ folder = arg.backfind("[config.rb]")
52
+ if folder && folder.exists?
53
+ dress(folder/"config.rb"){|c|
54
+ c.scope_factory = SingleYmlFile.new(arg)
55
+ }
56
+ else
57
+ Configuration.new{|c|
58
+ c.scope_factory = SingleYmlFile.new(arg)
59
+ }
60
+ end
61
+ elsif arg.directory? and (arg/'config.rb').file?
62
+ dress(arg/'config.rb', &bl)
63
+ else
64
+ raise ArgumentError, "Missing config.rb file"
65
+ end
35
66
  else
36
- raise ArgumentError, "Missing config.rb file"
67
+ raise ArgumentError, "Unable to turn `#{arg}` to a configuration"
37
68
  end
38
69
  end
39
70
 
@@ -51,10 +82,16 @@ module Webspicy
51
82
  config.each_scope(&bl)
52
83
  end
53
84
  else
54
- yield Scope.new(self)
85
+ yield factor_scope
55
86
  end
56
87
  end
57
88
 
89
+ attr_accessor :scope_factory
90
+
91
+ def factor_scope
92
+ @scope_factory.call(self)
93
+ end
94
+
58
95
  # Adds a folder to the list of folders where test case definitions are
59
96
  # to be found.
60
97
  def folder(folder = nil, &bl)
@@ -90,6 +127,13 @@ module Webspicy
90
127
  attr_accessor :postconditions
91
128
  protected :postconditions=
92
129
 
130
+ # Registers an errcondition matcher
131
+ def errcondition(clazz)
132
+ errconditions << clazz
133
+ end
134
+ attr_accessor :errconditions
135
+ protected :errconditions=
136
+
93
137
  # Returns whether this configuration has children configurations or not
94
138
  def has_children?
95
139
  !children.empty?
@@ -108,7 +152,7 @@ module Webspicy
108
152
 
109
153
  # Returns the defaut value for run_examples
110
154
  def default_run_examples
111
- ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'only')
155
+ ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'only' && ENV['ROBUST'] != 'generated')
112
156
  end
113
157
  private :default_run_examples
114
158
 
@@ -125,10 +169,27 @@ module Webspicy
125
169
 
126
170
  # Returns the defaut value for run_counterexamples
127
171
  def default_run_counterexamples
128
- ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'no')
172
+ ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'no' && ENV['ROBUST'] != 'generated')
129
173
  end
130
174
  private :default_run_counterexamples
131
175
 
176
+ # Sets whether generated counter examples have to be ran or not.
177
+ def run_generated_counterexamples=(run_generated_counterexamples)
178
+ @run_generated_counterexamples = run_generated_counterexamples
179
+ end
180
+ attr_reader :run_generated_counterexamples
181
+
182
+ # Whether generated counter examples must be ran or not.
183
+ def run_generated_counterexamples?
184
+ @run_generated_counterexamples
185
+ end
186
+
187
+ # Returns the defaut value for run_generated_counterexamples
188
+ def default_run_generated_counterexamples
189
+ ENV['ROBUST'].nil? || (ENV['ROBUST'] != 'no')
190
+ end
191
+ private :default_run_generated_counterexamples
192
+
132
193
  # Installs a host (resolver).
133
194
  #
134
195
  # The host resolver is responsible from transforming URLs found in
@@ -347,7 +408,7 @@ module Webspicy
347
408
  elsif not(self.parent.nil?)
348
409
  self.parent.data_system
349
410
  else
350
- Finitio::DEFAULT_SYSTEM
411
+ Webspicy::DEFAULT_SYSTEM
351
412
  end
352
413
  end
353
414
 
@@ -369,6 +430,7 @@ module Webspicy
369
430
  d.children = []
370
431
  d.preconditions = self.preconditions.dup
371
432
  d.postconditions = self.postconditions.dup
433
+ d.errconditions = self.errconditions.dup
372
434
  d.rspec_options = self.rspec_options.dup
373
435
  d.listeners = LISTENER_KINDS.inject({}){|ls,kind|
374
436
  ls.merge(kind => self.listeners(kind).dup)
@@ -379,3 +441,6 @@ module Webspicy
379
441
 
380
442
  end
381
443
  end
444
+ require_relative 'configuration/scope'
445
+ require_relative 'configuration/single_url'
446
+ require_relative 'configuration/single_yml_file'
@@ -0,0 +1,154 @@
1
+ module Webspicy
2
+ class Configuration
3
+ class Scope
4
+
5
+ def initialize(config)
6
+ @config = config
7
+ end
8
+ attr_reader :config
9
+
10
+ ###
11
+ ### Eachers -- Allow navigating the web service definitions
12
+ ###
13
+
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)
18
+ end
19
+
20
+ # Recursive implementation of `each_specification_file` for each
21
+ # folder in the configuration.
22
+ def _each_specification_file(config)
23
+ folder = config.folder
24
+ folder.glob("**/*.yml").select(&to_filter_proc(config.file_filter)).each do |file|
25
+ yield file, folder
26
+ end
27
+ end
28
+ private :_each_specification_file
29
+
30
+ # 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
+ yield Webspicy.specification(file.load, file, self)
35
+ end
36
+ end
37
+
38
+ def each_service(specification, &bl)
39
+ specification.services.select(&to_filter_proc(config.service_filter)).each(&bl)
40
+ end
41
+
42
+ def each_example(service, &bl)
43
+ service.examples
44
+ .map{|e| expand_example(service, e) }
45
+ .select(&to_filter_proc(config.test_case_filter))
46
+ .each(&bl) if config.run_examples?
47
+ end
48
+
49
+ def each_counterexamples(service, &bl)
50
+ service.counterexamples
51
+ .map{|e| expand_example(service, e) }
52
+ .select(&to_filter_proc(config.test_case_filter))
53
+ .each(&bl) if config.run_counterexamples?
54
+ end
55
+
56
+ def each_generated_counterexamples(service, &bl)
57
+ Webspicy.with_scope(self) do
58
+ service.generated_counterexamples
59
+ .map{|e| expand_example(service, e) }
60
+ .select(&to_filter_proc(config.test_case_filter))
61
+ .each(&bl) if config.run_generated_counterexamples?
62
+ end if config.run_generated_counterexamples?
63
+ end
64
+
65
+ def each_testcase(service, &bl)
66
+ each_example(service, &bl)
67
+ each_counterexamples(service, &bl)
68
+ each_generated_counterexamples(service, &bl)
69
+ end
70
+
71
+ ###
72
+ ### Schemas -- For parsing input and output data schemas found in
73
+ ### web service definitions
74
+ ###
75
+
76
+ # Parses a Finitio schema based on the data system.
77
+ def parse_schema(fio)
78
+ data_system.parse(fio)
79
+ end
80
+
81
+ # Returns the Data system to use for parsing schemas
82
+ def data_system
83
+ @data_system ||= config.data_system
84
+ end
85
+
86
+
87
+ ###
88
+ ### Service invocation: abstract the configuration about what client is
89
+ ### used and how to instantiate it
90
+ ###
91
+
92
+ # Returns an instance of the client to use to invoke web services
93
+ def get_client
94
+ config.client.new(self)
95
+ end
96
+
97
+ # Convert an instantiated URL found in a webservice definition
98
+ # to a real URL, using the configuration host.
99
+ #
100
+ # When no host resolved on the configuration and the url is not
101
+ # already an absolute URL, yields the block if given, or raise
102
+ # an exception.
103
+ def to_real_url(url, test_case = nil, &bl)
104
+ case config.host
105
+ when Proc
106
+ config.host.call(url, test_case)
107
+ when String
108
+ url =~ /^http/ ? url : "#{config.host}#{url}"
109
+ else
110
+ return url if url =~ /^http/
111
+ return yield(url) if block_given?
112
+ raise "Unable to resolve `#{url}` : no host resolver provided\nSee `Configuration#host="
113
+ end
114
+ end
115
+
116
+ ###
117
+ ### Private methods
118
+ ###
119
+
120
+ private
121
+
122
+ def expand_example(service, example)
123
+ return example unless service.default_example
124
+ h1 = service.default_example.to_info
125
+ h2 = example.to_info
126
+ ex = Specification::TestCase.new(merge_maps(h1, h2))
127
+ ex.bind(service, example.counterexample?)
128
+ end
129
+
130
+ def merge_maps(h1, h2)
131
+ h1.merge(h2) do |k,v1,v2|
132
+ case v1
133
+ when Hash then merge_maps(v1, v2)
134
+ when Array then v1 + v2
135
+ else v2
136
+ end
137
+ end
138
+ end
139
+
140
+ # Returns a proc that implements file_filter strategy according to the
141
+ # type of filter installed
142
+ def to_filter_proc(filter)
143
+ case ff = filter
144
+ when NilClass then ->(f){ true }
145
+ when Proc then ff
146
+ when Regexp then ->(f){ ff =~ f.to_s }
147
+ else
148
+ ->(f){ ff === f }
149
+ end
150
+ end
151
+
152
+ end # class Scope
153
+ end # class Configuration
154
+ end # module Webspicy
@@ -0,0 +1,58 @@
1
+ module Webspicy
2
+ class Configuration
3
+ class SingleUrl
4
+
5
+ class SingleUrlScope < Scope
6
+
7
+ def initialize(config, url)
8
+ super(config)
9
+ @url = url
10
+ end
11
+ attr_reader :url
12
+
13
+ def each_specification(&bl)
14
+ return enum_for(:each_specification) unless block_given?
15
+ spec = <<~YML
16
+ ---
17
+ name: |-
18
+ Default specification
19
+ url: |-
20
+ #{url}
21
+
22
+ services:
23
+ - method: |-
24
+ GET
25
+ description: |-
26
+ Getting #{url}
27
+
28
+ input_schema: |-
29
+ Any
30
+ output_schema: |-
31
+ Any
32
+ error_schema: |-
33
+ Any
34
+
35
+ examples:
36
+ - description: |-
37
+ it returns a 200
38
+ params: {}
39
+ expected:
40
+ status: 200
41
+ YML
42
+ Webspicy.debug(spec)
43
+ yield Webspicy.specification(spec, nil, self)
44
+ end
45
+
46
+ end # class SingleUrlScope
47
+
48
+ def initialize(url)
49
+ @url = url
50
+ end
51
+
52
+ def call(config)
53
+ SingleUrlScope.new(config, @url)
54
+ end
55
+
56
+ end # class SingleUrl
57
+ end # class Configuration
58
+ end # module Webspicy
@@ -0,0 +1,30 @@
1
+ module Webspicy
2
+ class Configuration
3
+ class SingleYmlFile
4
+
5
+ class SingleYmlFileScope < Scope
6
+
7
+ def initialize(config, file)
8
+ super(config)
9
+ @file = file
10
+ end
11
+ attr_reader :file
12
+
13
+ def each_specification(&bl)
14
+ return enum_for(:each_specification) unless block_given?
15
+ yield Webspicy.specification(file.read, nil, self)
16
+ end
17
+
18
+ end # class SingleYmlFileScope
19
+
20
+ def initialize(file)
21
+ @file = file
22
+ end
23
+
24
+ def call(config)
25
+ SingleYmlFileScope.new(config, @file)
26
+ end
27
+
28
+ end # class SingleYmlFile
29
+ end # class Configuration
30
+ end # module Webspicy
@@ -17,19 +17,36 @@ FileUpload =
17
17
  param_name :? String
18
18
  }
19
19
 
20
- Resource =
21
- .Webspicy::Resource <info> {
22
- name: String
23
- url: String
24
- services: [Service]
25
- }
20
+ Specification = .Webspicy::Specification
21
+ <singleservice> {
22
+ name :? String
23
+ url : String
24
+ method : Method
25
+ description : String
26
+ preconditions :? [String]|String
27
+ postconditions :? [String]|String
28
+ errconditions :? [String]|String
29
+ input_schema : Schema
30
+ output_schema : Schema
31
+ error_schema : Schema
32
+ blackbox :? String
33
+ default_example :? TestCase
34
+ examples :? [TestCase]
35
+ counterexamples :? [TestCase]
36
+ }
37
+ <info> {
38
+ name: String
39
+ url: String
40
+ services: [Service]
41
+ }
26
42
 
27
43
  Service =
28
- .Webspicy::Resource::Service <info> {
44
+ .Webspicy::Specification::Service <info> {
29
45
  method : Method
30
46
  description : String
31
47
  preconditions :? [String]|String
32
48
  postconditions :? [String]|String
49
+ errconditions :? [String]|String
33
50
  input_schema : Schema
34
51
  output_schema : Schema
35
52
  error_schema : Schema
@@ -40,7 +57,7 @@ Service =
40
57
  }
41
58
 
42
59
  TestCase =
43
- .Webspicy::Resource::Service::TestCase <info> {
60
+ .Webspicy::Specification::TestCase <info> {
44
61
  description :? String
45
62
  dress_params :? Boolean
46
63
  params :? Params