webspicy 0.16.1 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +11 -5
  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 +15 -4
  9. data/lib/webspicy/configuration.rb +57 -8
  10. data/lib/webspicy/configuration/scope.rb +22 -16
  11. data/lib/webspicy/formaldoc.fio +3 -1
  12. data/lib/webspicy/specification.rb +12 -10
  13. data/lib/webspicy/specification/condition.rb +23 -0
  14. data/lib/webspicy/specification/errcondition.rb +17 -0
  15. data/lib/webspicy/specification/postcondition.rb +1 -0
  16. data/lib/webspicy/specification/precondition.rb +1 -0
  17. data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +1 -1
  18. data/lib/webspicy/specification/service.rb +38 -25
  19. data/lib/webspicy/specification/test_case.rb +10 -17
  20. data/lib/webspicy/support.rb +22 -0
  21. data/lib/webspicy/support/data_object.rb +25 -0
  22. data/lib/webspicy/support/hooks.rb +65 -0
  23. data/lib/webspicy/support/world.rb +47 -0
  24. data/lib/webspicy/tester.rb +120 -61
  25. data/lib/webspicy/tester/asserter.rb +9 -4
  26. data/lib/webspicy/tester/assertions.rb +8 -9
  27. data/lib/webspicy/tester/client.rb +22 -42
  28. data/lib/webspicy/tester/failure.rb +6 -0
  29. data/lib/webspicy/tester/file_checker.rb +22 -0
  30. data/lib/webspicy/tester/invocation.rb +15 -196
  31. data/lib/webspicy/tester/reporter.rb +85 -0
  32. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  33. data/lib/webspicy/tester/reporter/documentation.rb +67 -0
  34. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  35. data/lib/webspicy/tester/reporter/exceptions.rb +60 -0
  36. data/lib/webspicy/tester/reporter/file_progress.rb +22 -0
  37. data/lib/webspicy/tester/reporter/file_summary.rb +42 -0
  38. data/lib/webspicy/tester/reporter/progress.rb +28 -0
  39. data/lib/webspicy/tester/reporter/summary.rb +62 -0
  40. data/lib/webspicy/tester/result.rb +139 -0
  41. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  42. data/lib/webspicy/tester/result/check.rb +33 -0
  43. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  44. data/lib/webspicy/tester/result/error_schema_met.rb +24 -0
  45. data/lib/webspicy/tester/result/output_schema_met.rb +24 -0
  46. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  47. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  48. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  49. data/lib/webspicy/version.rb +2 -2
  50. data/lib/webspicy/web.rb +4 -0
  51. data/lib/webspicy/web/client.rb +15 -0
  52. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  53. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  54. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  55. data/lib/webspicy/web/invocation.rb +65 -0
  56. data/lib/webspicy/web/mocker.rb +90 -0
  57. data/lib/webspicy/web/mocker/config.ru +6 -0
  58. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  59. data/lib/webspicy/web/openapi/generator.rb +129 -0
  60. data/spec/unit/configuration/scope/test_each_service.rb +2 -2
  61. data/spec/unit/configuration/scope/test_each_specification.rb +7 -7
  62. data/spec/unit/specification/test_condition.rb +26 -0
  63. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  64. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  65. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  66. data/spec/unit/support/world/fixtures/array.json +8 -0
  67. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  68. data/spec/unit/support/world/fixtures/single.json +11 -0
  69. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  70. data/spec/unit/support/world/test_world.rb +56 -0
  71. data/spec/unit/test_configuration.rb +50 -1
  72. data/spec/unit/tester/test_asserter.rb +198 -3
  73. data/spec/unit/tester/test_assertions.rb +8 -6
  74. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  75. data/spec/unit/web/openapi/test_generator.rb +31 -0
  76. metadata +72 -61
  77. data/examples/restful/Gemfile +0 -5
  78. data/examples/restful/Gemfile.lock +0 -105
  79. data/examples/restful/Rakefile +0 -25
  80. data/examples/restful/app.rb +0 -180
  81. data/examples/restful/webspicy/config.rb +0 -23
  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_removed.rb +0 -18
  88. data/examples/restful/webspicy/todo/deleteTodo.yml +0 -52
  89. data/examples/restful/webspicy/todo/getTodo.yml +0 -50
  90. data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +0 -46
  91. data/examples/restful/webspicy/todo/getTodos.yml +0 -36
  92. data/examples/restful/webspicy/todo/options.yml +0 -32
  93. data/examples/restful/webspicy/todo/patchTodo.yml +0 -66
  94. data/examples/restful/webspicy/todo/postCsv.yml +0 -43
  95. data/examples/restful/webspicy/todo/postFile.yml +0 -40
  96. data/examples/restful/webspicy/todo/postTodos.yml +0 -51
  97. data/examples/restful/webspicy/todo/putTodo.yml +0 -65
  98. data/examples/restful/webspicy/todo/todos.csv +0 -4
  99. data/examples/single_spec/spec.yml +0 -59
  100. data/examples/website/config.rb +0 -2
  101. data/examples/website/schema.fio +0 -1
  102. data/examples/website/specification/get-http.yml +0 -34
  103. data/examples/website/specification/get-https.yml +0 -34
  104. data/lib/webspicy/checker.rb +0 -25
  105. data/lib/webspicy/mocker.rb +0 -88
  106. data/lib/webspicy/openapi/generator.rb +0 -127
  107. data/lib/webspicy/tester/rspec_asserter.rb +0 -108
  108. data/lib/webspicy/tester/rspec_matchers.rb +0 -104
  109. data/spec/unit/mocker/test_mocker.rb +0 -32
  110. data/spec/unit/openapi/test_generator.rb +0 -28
@@ -0,0 +1,29 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class ErrconditionMet < Check
5
+
6
+ def initialize(result, post)
7
+ super(result)
8
+ @post = post
9
+ end
10
+ attr_reader :post
11
+
12
+ def behavior
13
+ post.to_s
14
+ end
15
+
16
+ def must?
17
+ true
18
+ end
19
+
20
+ def call
21
+ if err = post.check(invocation)
22
+ _! err
23
+ end
24
+ end
25
+
26
+ end # class ErrconditionMet
27
+ end # class Result
28
+ end # class Tester
29
+ end # module Webspicy
@@ -0,0 +1,24 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class ErrorSchemaMet < Check
5
+
6
+ def behavior
7
+ "Error data meets the expected error schema"
8
+ end
9
+
10
+ def must?
11
+ !test_case.is_expected_status?(204)
12
+ end
13
+
14
+ def call
15
+ output = invocation.loaded_body
16
+ service.error_schema.dress(output)
17
+ rescue Finitio::TypeError => ex
18
+ _! "Invalid error: #{ex.message}"
19
+ end
20
+
21
+ end # class ErrorSchemaMet
22
+ end # class Result
23
+ end # class Tester
24
+ end # module error
@@ -0,0 +1,24 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class OutputSchemaMet < Check
5
+
6
+ def behavior
7
+ "Its output meets the expected data schema"
8
+ end
9
+
10
+ def must?
11
+ !test_case.is_expected_status?(204)
12
+ end
13
+
14
+ def call
15
+ output = invocation.loaded_body
16
+ service.output_schema.dress(output)
17
+ rescue Finitio::TypeError => ex
18
+ _! "Invalid output: #{ex.message}"
19
+ end
20
+
21
+ end # class OutputSchemaMet
22
+ end # class Result
23
+ end # class Tester
24
+ end # module Webspicy
@@ -0,0 +1,29 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class PostconditionMet < Check
5
+
6
+ def initialize(result, post)
7
+ super(result)
8
+ @post = post
9
+ end
10
+ attr_reader :post
11
+
12
+ def behavior
13
+ post.to_s
14
+ end
15
+
16
+ def must?
17
+ true
18
+ end
19
+
20
+ def call
21
+ if err = post.check(invocation)
22
+ _! err
23
+ end
24
+ end
25
+
26
+ end # class PostconditionMet
27
+ end # class Result
28
+ end # class Tester
29
+ end # module Webspicy
@@ -0,0 +1,43 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class ResponseHeaderMet < Check
5
+
6
+ def initialize(result, header, expected, strategy = :eq)
7
+ unless [:eq, :start_with].include?(strategy)
8
+ raise ArgumentError, "Invalid strategy `#{strategy.inspect}`"
9
+ end
10
+ super(result)
11
+ @header = header
12
+ @expected = expected
13
+ @strategy = strategy
14
+ end
15
+ attr_reader :header, :expected, :strategy
16
+
17
+ def behavior
18
+ "It has a `#{header}: #{expected}` response header"
19
+ end
20
+
21
+ def must?
22
+ true
23
+ end
24
+
25
+ def call
26
+ got = response.headers[header]
27
+ if got.nil?
28
+ _! "Expected response header `#{header}` to be set"
29
+ else
30
+ msg = "Expected response header `#{header}` to be `#{expected}`, got `#{got}`"
31
+ case strategy
32
+ when :eq
33
+ _!(msg) unless expected == got
34
+ when :start_with
35
+ _!(msg) unless got.start_with?(expected)
36
+ end
37
+ end
38
+ end
39
+
40
+ end # class ResponseHeaderMet
41
+ end # class Result
42
+ end # class Tester
43
+ end # module Webspicy
@@ -0,0 +1,25 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class ResponseStatusMet < Check
5
+
6
+ def behavior
7
+ "It has a %d response status" % [ test_case.expected_status ]
8
+ end
9
+
10
+ def must?
11
+ test_case.has_expected_status?
12
+ end
13
+
14
+ def call
15
+ actual = response.status
16
+ expected = test_case.expected_status
17
+ unless expected === actual
18
+ _!("Expected response status to be #{expected}, got #{actual}")
19
+ end
20
+ end
21
+
22
+ end # class ResponseStatusMet
23
+ end # class Result
24
+ end # class Tester
25
+ end # module Webspicy
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 16
5
- TINY = 1
4
+ MINOR = 19
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
@@ -0,0 +1,4 @@
1
+ require_relative 'web/client'
2
+ require_relative 'web/invocation'
3
+ require_relative 'web/mocker'
4
+ require_relative 'web/openapi'
@@ -0,0 +1,15 @@
1
+ module Webspicy
2
+ module Web
3
+ class Client < Tester::Client
4
+
5
+ def call(test_case)
6
+ response = _call(test_case)
7
+ Invocation.new(test_case, response, self)
8
+ end
9
+
10
+ end # class Client
11
+ end # module Web
12
+ end # module Webspicy
13
+ require_relative 'client/support'
14
+ require_relative 'client/http_client'
15
+ require_relative 'client/rack_test_client'
@@ -1,5 +1,5 @@
1
1
  module Webspicy
2
- class Tester
2
+ module Web
3
3
  class HttpClient < Client
4
4
 
5
5
  class ::HTTP::Request
@@ -19,7 +19,7 @@ module Webspicy
19
19
  end
20
20
  attr_reader :api
21
21
 
22
- def call(test_case)
22
+ def _call(test_case)
23
23
  service, specification = test_case.service, test_case.specification
24
24
 
25
25
  # Instantiate the parameters
@@ -43,11 +43,10 @@ module Webspicy
43
43
  class Api
44
44
  include Client::Support
45
45
 
46
- attr_reader :last_response
47
-
48
46
  def initialize(scope)
49
47
  @scope = scope
50
48
  end
49
+ attr_reader :last_response
51
50
 
52
51
  def config
53
52
  @scope.config
@@ -57,7 +56,8 @@ module Webspicy
57
56
  info_request("OPTIONS", url, params, headers, body)
58
57
 
59
58
  params = querystring_params(params)
60
- @last_response = HTTP[headers || {}].options(url, params: params)
59
+ http_opts = http_options(params: params)
60
+ @last_response = HTTP[headers || {}].options(url, http_opts)
61
61
 
62
62
  debug_response(@last_response)
63
63
 
@@ -68,7 +68,8 @@ module Webspicy
68
68
  info_request("GET", url, params, headers, body)
69
69
 
70
70
  params = querystring_params(params)
71
- @last_response = HTTP[headers || {}].get(url, params: params)
71
+ http_opts = http_options(params: params)
72
+ @last_response = HTTP[headers || {}].get(url, http_opts)
72
73
 
73
74
  debug_response(@last_response)
74
75
 
@@ -85,18 +86,21 @@ module Webspicy
85
86
  case body
86
87
  when NilClass
87
88
  headers['Content-Type'] ||= 'application/json'
88
- @last_response = HTTP[headers].post(url, body: params.to_json)
89
+ http_opts = http_options(body: params.to_json)
90
+ @last_response = HTTP[headers].post(url, http_opts)
89
91
  when FileUpload
90
92
  file = HTTP::FormData::File.new(body.path.to_s, {
91
93
  content_type: body.content_type,
92
94
  filename: body.path.basename.to_s
93
95
  })
94
- @last_response = HTTP[headers].post(url, form: {
96
+ http_opts = http_options(form: {
95
97
  body.param_name.to_sym => file
96
98
  })
99
+ @last_response = HTTP[headers].post(url, http_opts)
97
100
  else
98
101
  headers['Content-Type'] ||= 'application/json'
99
- @last_response = HTTP[headers].post(url, body: body)
102
+ http_opts = http_options(body: body)
103
+ @last_response = HTTP[headers].post(url, http_opts)
100
104
  end
101
105
 
102
106
  debug_response(@last_response)
@@ -109,7 +113,8 @@ module Webspicy
109
113
 
110
114
  headers ||= {}
111
115
  headers['Content-Type'] ||= 'application/json'
112
- @last_response = HTTP[headers].patch(url, body: params.to_json)
116
+ http_opts = http_options(body: params.to_json)
117
+ @last_response = HTTP[headers].patch(url, http_opts)
113
118
 
114
119
  debug_response(@last_response)
115
120
 
@@ -121,7 +126,8 @@ module Webspicy
121
126
 
122
127
  headers ||= {}
123
128
  headers['Content-Type'] ||= 'application/json'
124
- @last_response = HTTP[headers].put(url, body: params.to_json)
129
+ http_opts = http_options(body: params.to_json)
130
+ @last_response = HTTP[headers].put(url, http_opts)
125
131
 
126
132
  debug_response(@last_response)
127
133
 
@@ -131,7 +137,8 @@ module Webspicy
131
137
  def post_form(url, params = {}, headers = nil, body = nil)
132
138
  info_request("POST", url, params, headers, body)
133
139
 
134
- @last_response = HTTP[headers || {}].post(url, form: params)
140
+ http_opts = http_options(form: params)
141
+ @last_response = HTTP[headers || {}].post(url, http_opts)
135
142
 
136
143
  debug_response(@last_response)
137
144
 
@@ -141,14 +148,27 @@ module Webspicy
141
148
  def delete(url, params = {}, headers = nil, body = nil)
142
149
  info_request("DELETE", url, params, headers, body)
143
150
 
144
- @last_response = HTTP[headers || {}].delete(url, body: params.to_json)
151
+ http_opts = http_options(body: params.to_json)
152
+ @last_response = HTTP[headers || {}].delete(url, http_opts)
145
153
 
146
154
  debug_response(@last_response)
147
155
 
148
156
  @last_response
149
157
  end
158
+
159
+ def http_options(extra)
160
+ if config.insecure
161
+ ctx = OpenSSL::SSL::SSLContext.new
162
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
163
+ {
164
+ :ssl_context => ctx
165
+ }.merge(extra)
166
+ else
167
+ extra
168
+ end
169
+ end
150
170
  end # class Api
151
171
 
152
172
  end # class HttpClient
153
- end # class Tester
173
+ end # module Web
154
174
  end # module Webspicy
@@ -1,5 +1,5 @@
1
1
  module Webspicy
2
- class Tester
2
+ module Web
3
3
  class RackTestClient < Client
4
4
 
5
5
  def self.for(app)
@@ -12,7 +12,7 @@ module Webspicy
12
12
  end
13
13
  attr_reader :api
14
14
 
15
- def call(test_case)
15
+ def _call(test_case)
16
16
  service, specification = test_case.service, test_case.specification
17
17
 
18
18
  # Instantiate the parameters
@@ -184,5 +184,5 @@ module Webspicy
184
184
  end # class Api
185
185
 
186
186
  end # class RackTestClient
187
- end # class Client
187
+ end # module Web
188
188
  end # module Webspicy
@@ -1,6 +1,6 @@
1
1
  module Webspicy
2
- class Tester
3
- class Client
2
+ module Web
3
+ class Client < Tester::Client
4
4
  module Support
5
5
  include Webspicy::Support::Colorize
6
6
 
@@ -0,0 +1,65 @@
1
+ module Webspicy
2
+ module Web
3
+ class Invocation < Tester::Invocation
4
+
5
+ ### Getters on response
6
+
7
+ def response_code
8
+ code = response.status
9
+ code = code.code unless code.is_a?(Integer)
10
+ code
11
+ end
12
+
13
+ ### Query methods
14
+
15
+ def done?
16
+ !response.nil?
17
+ end
18
+
19
+ def is_expected_success?
20
+ test_case.expected_status.to_i >= 200 && test_case.expected_status.to_i < 300
21
+ end
22
+
23
+ def is_success?
24
+ response_code >= 200 && response_code < 300
25
+ end
26
+
27
+ def is_empty_response?
28
+ response_code == 204
29
+ end
30
+
31
+ def is_redirect?
32
+ response_code >= 300 && response_code < 400
33
+ end
34
+
35
+ def raw_output
36
+ response.body.to_s
37
+ end
38
+
39
+ def loaded_output
40
+ ct = response.content_type || test_case.expected_content_type
41
+ ct = ct.mime_type if ct.respond_to?(:mime_type)
42
+ case ct
43
+ when %r{json}
44
+ raise "Body empty while expected" if raw_output.empty?
45
+ @loaded_output ||= ::JSON.parse(response.body)
46
+ else
47
+ raw_output
48
+ end
49
+ end
50
+ alias :loaded_body :loaded_output
51
+
52
+ def output
53
+ schema = is_expected_success? ? service.output_schema : service.error_schema
54
+ begin
55
+ schema.dress(loaded_output)
56
+ rescue Finitio::TypeError => ex
57
+ ex
58
+ end
59
+ end
60
+ alias :dressed_body :output
61
+ alias :error :output
62
+
63
+ end # class Invocation
64
+ end # module Web
65
+ end # module Webspicy