webspicy 0.18.0 → 0.20.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) 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 -9
  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 +5 -2
  12. data/lib/webspicy/specification/condition.rb +48 -0
  13. data/lib/webspicy/specification/err.rb +18 -0
  14. data/lib/webspicy/specification/oldies.rb +4 -0
  15. data/lib/webspicy/specification/oldies/bridge.rb +32 -0
  16. data/lib/webspicy/specification/{errcondition.rb → oldies/errcondition.rb} +6 -0
  17. data/lib/webspicy/specification/{postcondition.rb → oldies/postcondition.rb} +6 -0
  18. data/lib/webspicy/specification/{precondition.rb → oldies/precondition.rb} +6 -2
  19. data/lib/webspicy/specification/post.rb +20 -0
  20. data/lib/webspicy/specification/post/missing_condition_impl.rb +15 -0
  21. data/lib/webspicy/specification/post/unexpected_condition_impl.rb +15 -0
  22. data/lib/webspicy/specification/pre.rb +19 -0
  23. data/lib/webspicy/specification/{precondition → pre}/global_request_headers.rb +4 -4
  24. data/lib/webspicy/specification/{precondition → pre}/robust_to_invalid_input.rb +4 -4
  25. data/lib/webspicy/specification/service.rb +34 -5
  26. data/lib/webspicy/specification/test_case.rb +11 -9
  27. data/lib/webspicy/support.rb +31 -0
  28. data/lib/webspicy/support/hooks.rb +65 -0
  29. data/lib/webspicy/support/world.rb +47 -0
  30. data/lib/webspicy/tester.rb +202 -3
  31. data/lib/webspicy/tester/assertions.rb +2 -2
  32. data/lib/webspicy/tester/client.rb +4 -50
  33. data/lib/webspicy/tester/fakeses.rb +41 -0
  34. data/lib/webspicy/tester/fakeses/email.rb +38 -0
  35. data/lib/webspicy/tester/fakesmtp.rb +39 -0
  36. data/lib/webspicy/tester/fakesmtp/email.rb +27 -0
  37. data/lib/webspicy/tester/file_checker.rb +22 -0
  38. data/lib/webspicy/tester/invocation.rb +15 -48
  39. data/lib/webspicy/tester/reporter.rb +85 -0
  40. data/lib/webspicy/tester/reporter/composite.rb +38 -0
  41. data/lib/webspicy/tester/reporter/documentation.rb +74 -0
  42. data/lib/webspicy/tester/reporter/error_count.rb +25 -0
  43. data/lib/webspicy/tester/reporter/exceptions.rb +62 -0
  44. data/lib/webspicy/tester/reporter/file_progress.rb +25 -0
  45. data/lib/webspicy/tester/reporter/file_summary.rb +43 -0
  46. data/lib/webspicy/tester/reporter/progress.rb +30 -0
  47. data/lib/webspicy/tester/reporter/summary.rb +63 -0
  48. data/lib/webspicy/tester/result.rb +142 -0
  49. data/lib/webspicy/tester/result/assert_met.rb +29 -0
  50. data/lib/webspicy/tester/result/check.rb +33 -0
  51. data/lib/webspicy/tester/result/errcondition_met.rb +29 -0
  52. data/lib/webspicy/tester/result/error_schema_met.rb +25 -0
  53. data/lib/webspicy/tester/result/invocation_succeeded.rb +13 -0
  54. data/lib/webspicy/tester/result/output_schema_met.rb +25 -0
  55. data/lib/webspicy/tester/result/postcondition_met.rb +29 -0
  56. data/lib/webspicy/tester/result/response_header_met.rb +43 -0
  57. data/lib/webspicy/tester/result/response_status_met.rb +25 -0
  58. data/lib/webspicy/version.rb +2 -2
  59. data/lib/webspicy/web.rb +4 -0
  60. data/lib/webspicy/web/client.rb +15 -0
  61. data/lib/webspicy/{tester → web}/client/http_client.rb +34 -14
  62. data/lib/webspicy/{tester → web}/client/rack_test_client.rb +3 -3
  63. data/lib/webspicy/{tester → web}/client/support.rb +2 -2
  64. data/lib/webspicy/web/invocation.rb +69 -0
  65. data/lib/webspicy/web/mocker.rb +90 -0
  66. data/lib/webspicy/{mocker → web/mocker}/config.ru +3 -2
  67. data/lib/webspicy/{openapi.rb → web/openapi.rb} +0 -0
  68. data/lib/webspicy/web/openapi/generator.rb +129 -0
  69. data/spec/unit/specification/{precondition → pre}/test_global_request_headers.rb +9 -4
  70. data/spec/unit/specification/test_condition.rb +44 -0
  71. data/spec/unit/support/hooks/test_fire_after_each.rb +53 -0
  72. data/spec/unit/{tester/client/test_around.rb → support/hooks/test_fire_around.rb} +15 -10
  73. data/spec/unit/support/hooks/test_fire_before_each.rb +53 -0
  74. data/spec/unit/support/world/fixtures/array.json +8 -0
  75. data/spec/unit/support/world/fixtures/queue.rb +1 -0
  76. data/spec/unit/support/world/fixtures/single.json +11 -0
  77. data/spec/unit/support/world/fixtures/yaml.yml +3 -0
  78. data/spec/unit/support/world/test_world.rb +56 -0
  79. data/spec/unit/test_configuration.rb +49 -0
  80. data/spec/unit/tester/fakeses/test_email.rb +40 -0
  81. data/spec/unit/web/mocker/test_mocker.rb +35 -0
  82. data/spec/unit/web/openapi/test_generator.rb +31 -0
  83. metadata +80 -65
  84. data/examples/restful/Gemfile +0 -5
  85. data/examples/restful/Rakefile +0 -25
  86. data/examples/restful/app.rb +0 -180
  87. data/examples/restful/webspicy/config.rb +0 -24
  88. data/examples/restful/webspicy/formaldef/todo/_one/delete.yml +0 -55
  89. data/examples/restful/webspicy/formaldef/todo/_one/get.simpler.yml +0 -46
  90. data/examples/restful/webspicy/formaldef/todo/_one/get.yml +0 -50
  91. data/examples/restful/webspicy/formaldef/todo/_one/patch.yml +0 -66
  92. data/examples/restful/webspicy/formaldef/todo/_one/put.yml +0 -65
  93. data/examples/restful/webspicy/formaldef/todo/get.yml +0 -36
  94. data/examples/restful/webspicy/formaldef/todo/options.yml +0 -32
  95. data/examples/restful/webspicy/formaldef/todo/post.csv.yml +0 -43
  96. data/examples/restful/webspicy/formaldef/todo/post.file.yml +0 -40
  97. data/examples/restful/webspicy/formaldef/todo/post.yml +0 -51
  98. data/examples/restful/webspicy/formaldef/todo/todos.csv +0 -4
  99. data/examples/restful/webspicy/rack.rb +0 -7
  100. data/examples/restful/webspicy/real.rb +0 -8
  101. data/examples/restful/webspicy/schema.fio +0 -20
  102. data/examples/restful/webspicy/support/must_be_an_admin.rb +0 -20
  103. data/examples/restful/webspicy/support/must_be_authenticated.rb +0 -48
  104. data/examples/restful/webspicy/support/todo_not_removed.rb +0 -21
  105. data/examples/restful/webspicy/support/todo_removed.rb +0 -20
  106. data/examples/single_spec/spec.yml +0 -59
  107. data/examples/website/config.rb +0 -2
  108. data/examples/website/schema.fio +0 -1
  109. data/examples/website/specification/get-http.yml +0 -30
  110. data/examples/website/specification/get-https.yml +0 -30
  111. data/lib/webspicy/checker.rb +0 -10
  112. data/lib/webspicy/mocker.rb +0 -88
  113. data/lib/webspicy/openapi/generator.rb +0 -127
  114. data/lib/webspicy/rspec/checker.rb +0 -2
  115. data/lib/webspicy/rspec/checker/rspec_checker.rb +0 -24
  116. data/lib/webspicy/rspec/support/rspec_runnable.rb +0 -27
  117. data/lib/webspicy/rspec/tester.rb +0 -4
  118. data/lib/webspicy/rspec/tester/rspec_asserter.rb +0 -121
  119. data/lib/webspicy/rspec/tester/rspec_matchers.rb +0 -114
  120. data/lib/webspicy/rspec/tester/rspec_tester.rb +0 -63
  121. data/spec/unit/mocker/test_mocker.rb +0 -32
  122. 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!
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,25 @@
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
+ return unless invocation.is_structured_output?
16
+ output = invocation.loaded_body
17
+ service.error_schema.dress(output)
18
+ rescue Finitio::TypeError => ex
19
+ _! "Invalid error: #{ex.message}"
20
+ end
21
+
22
+ end # class ErrorSchemaMet
23
+ end # class Result
24
+ end # class Tester
25
+ end # module error
@@ -0,0 +1,13 @@
1
+ module Webspicy
2
+ class Tester
3
+ class Result
4
+ class InvocationSuceeded < Check
5
+
6
+ def initialize(result)
7
+ super(result)
8
+ end
9
+
10
+ end # class InvocationSuceeded
11
+ end # class Result
12
+ end # class Tester
13
+ end # module Webspicy
@@ -0,0 +1,25 @@
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
+ return unless invocation.is_structured_output?
16
+ output = invocation.loaded_body
17
+ service.output_schema.dress(output)
18
+ rescue Finitio::TypeError => ex
19
+ _! "Invalid output: #{ex.message}"
20
+ end
21
+
22
+ end # class OutputSchemaMet
23
+ end # class Result
24
+ end # class Tester
25
+ 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!
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 = 18
5
- TINY = 0
4
+ MINOR = 20
5
+ TINY = 3
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,69 @@
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 is_structured_output?
40
+ ct = response.content_type || test_case.expected_content_type
41
+ ct = ct.mime_type if ct.respond_to?(:mime_type)
42
+ ct =~ /json/
43
+ end
44
+
45
+ def loaded_output
46
+ if is_structured_output?
47
+ raise "Body empty while expected" if raw_output.empty?
48
+ @loaded_output ||= ::JSON.parse(response.body)
49
+ else
50
+ raw_output
51
+ end
52
+ end
53
+ alias :loaded_body :loaded_output
54
+
55
+ def output
56
+ return loaded_output unless is_structured_output?
57
+ schema = is_expected_success? ? service.output_schema : service.error_schema
58
+ begin
59
+ schema.dress(loaded_output)
60
+ rescue Finitio::TypeError => ex
61
+ ex
62
+ end
63
+ end
64
+ alias :dressed_body :output
65
+ alias :error :output
66
+
67
+ end # class Invocation
68
+ end # module Web
69
+ end # module Webspicy