webspicy 0.15.4 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -24
  3. data/bin/webspicy +30 -13
  4. data/examples/restful/Gemfile +2 -2
  5. data/examples/restful/Gemfile.lock +53 -33
  6. data/examples/restful/Rakefile +0 -1
  7. data/examples/restful/app.rb +19 -1
  8. data/examples/restful/webspicy/config.rb +8 -0
  9. data/examples/restful/webspicy/rack.rb +1 -1
  10. data/examples/restful/webspicy/real.rb +1 -1
  11. data/examples/restful/webspicy/schema.fio +7 -2
  12. data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
  13. data/examples/restful/webspicy/support/todo_removed.rb +18 -0
  14. data/examples/restful/webspicy/todo/deleteTodo.yml +4 -1
  15. data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +46 -0
  16. data/examples/restful/webspicy/todo/options.yml +1 -1
  17. data/examples/restful/webspicy/todo/patchTodo.yml +3 -0
  18. data/examples/restful/webspicy/todo/postFile.yml +1 -1
  19. data/examples/restful/webspicy/todo/putTodo.yml +65 -0
  20. data/examples/single_spec/spec.yml +59 -0
  21. data/examples/website/config.rb +2 -0
  22. data/examples/website/schema.fio +1 -0
  23. data/examples/website/specification/get-http.yml +34 -0
  24. data/examples/website/specification/get-https.yml +34 -0
  25. data/lib/finitio/webspicy/scalars.fio +25 -0
  26. data/lib/webspicy.rb +48 -17
  27. data/lib/webspicy/checker.rb +2 -2
  28. data/lib/webspicy/configuration.rb +70 -14
  29. data/lib/webspicy/configuration/scope.rb +162 -0
  30. data/lib/webspicy/configuration/single_url.rb +58 -0
  31. data/lib/webspicy/configuration/single_yml_file.rb +30 -0
  32. data/lib/webspicy/formaldoc.fio +24 -9
  33. data/lib/webspicy/mocker.rb +8 -8
  34. data/lib/webspicy/openapi.rb +1 -0
  35. data/lib/webspicy/openapi/generator.rb +127 -0
  36. data/lib/webspicy/{resource.rb → specification.rb} +26 -5
  37. data/lib/webspicy/specification/file_upload.rb +37 -0
  38. data/lib/webspicy/specification/postcondition.rb +16 -0
  39. data/lib/webspicy/specification/precondition.rb +19 -0
  40. data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
  41. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
  42. data/lib/webspicy/{resource → specification}/service.rb +11 -6
  43. data/lib/webspicy/specification/test_case.rb +139 -0
  44. data/lib/webspicy/support.rb +1 -0
  45. data/lib/webspicy/support/colorize.rb +28 -0
  46. data/lib/webspicy/support/status_range.rb +9 -0
  47. data/lib/webspicy/tester.rb +16 -11
  48. data/lib/webspicy/tester/asserter.rb +3 -2
  49. data/lib/webspicy/tester/assertions.rb +5 -1
  50. data/lib/webspicy/tester/client.rb +63 -0
  51. data/lib/webspicy/tester/client/http_client.rb +154 -0
  52. data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
  53. data/lib/webspicy/tester/client/support.rb +65 -0
  54. data/lib/webspicy/tester/invocation.rb +218 -0
  55. data/lib/webspicy/tester/rspec_asserter.rb +108 -0
  56. data/lib/webspicy/tester/rspec_matchers.rb +104 -0
  57. data/lib/webspicy/version.rb +2 -2
  58. data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
  59. data/spec/unit/configuration/scope/test_each_service.rb +49 -0
  60. data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
  61. data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
  62. data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
  63. data/spec/unit/openapi/test_generator.rb +28 -0
  64. data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
  65. data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
  66. data/spec/unit/specification/test_case/test_mutate.rb +24 -0
  67. data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
  68. data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
  69. data/spec/unit/test_configuration.rb +24 -7
  70. data/spec/unit/tester/client/test_around.rb +61 -0
  71. data/spec/unit/tester/test_asserter.rb +51 -0
  72. data/spec/unit/tester/test_assertions.rb +4 -4
  73. data/tasks/test.rake +3 -1
  74. metadata +88 -44
  75. data/lib/webspicy/client.rb +0 -61
  76. data/lib/webspicy/client/http_client.rb +0 -133
  77. data/lib/webspicy/client/rack_test_client.rb +0 -168
  78. data/lib/webspicy/client/support.rb +0 -48
  79. data/lib/webspicy/file_upload.rb +0 -35
  80. data/lib/webspicy/postcondition.rb +0 -14
  81. data/lib/webspicy/precondition.rb +0 -15
  82. data/lib/webspicy/resource/service/invocation.rb +0 -212
  83. data/lib/webspicy/resource/service/test_case.rb +0 -132
  84. data/lib/webspicy/scope.rb +0 -157
  85. data/spec/unit/client/test_around.rb +0 -59
  86. data/spec/unit/scope/test_each_resource.rb +0 -66
  87. data/spec/unit/scope/test_each_service.rb +0 -47
  88. data/spec/unit/scope/test_expand_example.rb +0 -63
  89. data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -29,8 +29,8 @@ module Webspicy
29
29
 
30
30
  def has_service?(path)
31
31
  config.each_scope do |scope|
32
- scope.each_resource do |resource|
33
- next unless url_matches?(resource, path)
32
+ scope.each_specification do |specification|
33
+ next unless url_matches?(specification, path)
34
34
  return true
35
35
  end
36
36
  end
@@ -39,9 +39,9 @@ module Webspicy
39
39
 
40
40
  def find_service(method, path)
41
41
  config.each_scope do |scope|
42
- scope.each_resource do |resource|
43
- next unless url_matches?(resource, path)
44
- scope.each_service(resource) do |service|
42
+ scope.each_specification do |specification|
43
+ next unless url_matches?(specification, path)
44
+ scope.each_service(specification) do |service|
45
45
  return service if service.method == method
46
46
  end
47
47
  end
@@ -71,8 +71,8 @@ module Webspicy
71
71
  end
72
72
  end
73
73
 
74
- def url_matches?(resource, path)
75
- resource.url_pattern.match(path)
74
+ def url_matches?(specification, path)
75
+ specification.url_pattern.match(path)
76
76
  end
77
77
 
78
78
  def random_body(service, request)
@@ -85,4 +85,4 @@ module Webspicy
85
85
  end
86
86
 
87
87
  end # class Mocker
88
- end # module Webspicy
88
+ end # module Webspicy
@@ -0,0 +1 @@
1
+ require_relative 'openapi/generator'
@@ -0,0 +1,127 @@
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,5 +1,5 @@
1
1
  module Webspicy
2
- class Resource
2
+ class Specification
3
3
 
4
4
  def initialize(raw, location = nil)
5
5
  @raw = raw
@@ -12,6 +12,15 @@ module Webspicy
12
12
  new(raw)
13
13
  end
14
14
 
15
+ def self.singleservice(raw)
16
+ converted = {
17
+ name: raw.has_key?(:name) ? raw[:name] : "Unamed specification",
18
+ url: raw[:url],
19
+ services: [raw.reject{|k, _| k == :name || k == :url}]
20
+ }
21
+ self.info(raw)
22
+ end
23
+
15
24
  def located_at!(location)
16
25
  @location = Path(location)
17
26
  end
@@ -22,6 +31,10 @@ module Webspicy
22
31
  file
23
32
  end
24
33
 
34
+ def name
35
+ @raw[:name]
36
+ end
37
+
25
38
  def url
26
39
  @raw[:url]
27
40
  end
@@ -51,6 +64,10 @@ module Webspicy
51
64
  @raw
52
65
  end
53
66
 
67
+ def to_singleservice
68
+ raise NotImplementedError
69
+ end
70
+
54
71
  private
55
72
 
56
73
  def extract_placeholder_value(params, placeholder, split = nil)
@@ -69,10 +86,14 @@ module Webspicy
69
86
 
70
87
  def bind_services
71
88
  (@raw[:services] ||= []).each do |s|
72
- s.resource = self
89
+ s.specification = self
73
90
  end
74
91
  end
75
92
 
76
- end
77
- end
78
- require_relative 'resource/service'
93
+ end # class Specification
94
+ end # module Webspicy
95
+ require_relative 'specification/service'
96
+ require_relative 'specification/precondition'
97
+ require_relative 'specification/postcondition'
98
+ require_relative 'specification/test_case'
99
+ require_relative 'specification/file_upload'
@@ -0,0 +1,37 @@
1
+ module Webspicy
2
+ class Specification
3
+ class FileUpload
4
+
5
+ def initialize(raw)
6
+ @path = raw[:path]
7
+ @content_type = raw[:content_type]
8
+ @param_name = raw[:param_name] || "file"
9
+ end
10
+
11
+ attr_reader :path, :content_type, :param_name
12
+
13
+ def self.info(raw)
14
+ new(raw)
15
+ end
16
+
17
+ def locate(specification)
18
+ FileUpload.new({
19
+ path: specification.locate(path),
20
+ content_type: content_type
21
+ })
22
+ end
23
+
24
+ def to_info
25
+ { path: path.to_s,
26
+ content_type: content_type,
27
+ param_name: param_name }
28
+ end
29
+
30
+ def to_s
31
+ "FileUpload(#{to_info})"
32
+ end
33
+ alias :inspect :to_s
34
+
35
+ end # class FileUpload
36
+ end # class Specification
37
+ end # module Webspicy
@@ -0,0 +1,16 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Postcondition
4
+
5
+ def self.match(service, descr)
6
+ end
7
+
8
+ def instrument(test_case, client)
9
+ end
10
+
11
+ def check(invocation)
12
+ end
13
+
14
+ end # module Postcondition
15
+ end # module Specification
16
+ end # module Webspicy
@@ -0,0 +1,19 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Precondition
4
+
5
+ def self.match(service, pre)
6
+ end
7
+
8
+ def instrument(test_case, client)
9
+ end
10
+
11
+ def counterexamples(service)
12
+ []
13
+ end
14
+
15
+ end # module Precondition
16
+ end # class Specification
17
+ end # module Webspicy
18
+ require_relative 'precondition/global_request_headers'
19
+ require_relative 'precondition/robust_to_invalid_input'
@@ -0,0 +1,35 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Precondition
4
+ class GlobalRequestHeaders
5
+ include Precondition
6
+
7
+ DEFAULT_OPTIONS = {}
8
+
9
+ def initialize(headers, options = {}, &bl)
10
+ @headers = headers
11
+ @options = DEFAULT_OPTIONS.merge(options)
12
+ @matcher = bl
13
+ end
14
+ attr_reader :headers, :matcher
15
+
16
+ def match(service, pre)
17
+ if matcher
18
+ return self if matcher.call(service)
19
+ nil
20
+ else
21
+ self
22
+ end
23
+ end
24
+
25
+ def instrument(test_case, client)
26
+ extra = headers.reject{|k|
27
+ test_case.headers.has_key?(k)
28
+ }
29
+ test_case.headers.merge!(extra)
30
+ end
31
+
32
+ end # class GlobalRequestHeaders
33
+ end # module Precondition
34
+ end # class Specification
35
+ end # module Webspicy
@@ -0,0 +1,68 @@
1
+ module Webspicy
2
+ class Specification
3
+ module Precondition
4
+ class RobustToInvalidInput
5
+ include Precondition
6
+
7
+ def self.match(service, pre)
8
+ self.new
9
+ end
10
+
11
+ def match(service, pre)
12
+ self
13
+ end
14
+
15
+ def counterexamples(service)
16
+ spec = service.specification
17
+ first = service.examples.first
18
+ cexamples = []
19
+ cexamples += url_randomness_counterexamples(service, first) if first
20
+ cexamples += empty_input_counterexamples(service, first) if first
21
+ cexamples
22
+ end
23
+
24
+ protected
25
+
26
+ def url_randomness_counterexamples(service, first)
27
+ service.specification.url_placeholders.map{|p|
28
+ first.mutate({
29
+ :description => "it is robust to URL randomness on param `#{p}` (RobustToInvalidInput)",
30
+ :dress_params => false,
31
+ :params => first.params.merge(p => (SecureRandom.random_number * 100000000).to_i),
32
+ :expected => {
33
+ status: Support::StatusRange.str("4xx")
34
+ },
35
+ :assert => []
36
+ })
37
+ }
38
+ end
39
+
40
+ def empty_input_counterexamples(service, first)
41
+ placeholders = service.specification.url_placeholders
42
+ empty_input = first.params.reject{|k| !placeholders.include?(k) }
43
+ if !empty_input.empty? && invalid_input?(service, empty_input)
44
+ [first.mutate({
45
+ :description => "it is robust to an invalid empty input (RobustToInvalidInput)",
46
+ :dress_params => false,
47
+ :params => empty_input,
48
+ :expected => {
49
+ status: Support::StatusRange.str("4xx")
50
+ },
51
+ :assert => []
52
+ })]
53
+ else
54
+ []
55
+ end
56
+ end
57
+
58
+ def invalid_input?(service, empty_input)
59
+ service.input_schema.dress(empty_input)
60
+ false
61
+ rescue Finitio::Error
62
+ true
63
+ end
64
+
65
+ end # class RobustToInvalidInput
66
+ end # module Precondition
67
+ end # class Specification
68
+ end # module Webspicy
@@ -1,5 +1,5 @@
1
1
  module Webspicy
2
- class Resource
2
+ class Specification
3
3
  class Service
4
4
 
5
5
  def initialize(raw)
@@ -9,7 +9,7 @@ module Webspicy
9
9
  @preconditions = compile_preconditions
10
10
  @postconditions = compile_postconditions
11
11
  end
12
- attr_accessor :resource
12
+ attr_accessor :specification
13
13
 
14
14
  def self.info(raw)
15
15
  new(raw)
@@ -19,6 +19,10 @@ module Webspicy
19
19
  @raw[:method]
20
20
  end
21
21
 
22
+ def description
23
+ @raw[:description]
24
+ end
25
+
22
26
  def preconditions
23
27
  @preconditions
24
28
  end
@@ -77,7 +81,7 @@ module Webspicy
77
81
  end
78
82
 
79
83
  def to_s
80
- "#{method} #{resource.url}"
84
+ "#{method} #{specification.url}"
81
85
  end
82
86
 
83
87
  private
@@ -97,6 +101,9 @@ module Webspicy
97
101
  end
98
102
 
99
103
  def compile_conditions(descriptions, conditions)
104
+ # Because we want pre & post to be able to match in all cases
105
+ # we need at least one condition
106
+ descriptions = ["all"] if descriptions.empty?
100
107
  descriptions
101
108
  .map{|descr|
102
109
  conditions.map{|c| c.match(self, descr) }.compact
@@ -117,7 +124,5 @@ module Webspicy
117
124
  end
118
125
 
119
126
  end # class Service
120
- end # class Resource
127
+ end # class Specification
121
128
  end # module Webspicy
122
- require_relative 'service/test_case'
123
- require_relative 'service/invocation'