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,90 @@
1
+ require "finitio/generation"
2
+ require "json"
3
+ module Webspicy
4
+ module Web
5
+ class Mocker
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(env)
14
+ req = Rack::Request.new(env)
15
+ path = req.path
16
+ meth = req.request_method
17
+ if meth == "OPTIONS" && has_service?(path)
18
+ [204, {}, []]
19
+ elsif service = find_service(meth, path)
20
+ status = best_status_code(service)
21
+ body = status == 204 ? "" : random_body(service, req)
22
+ headers = generate_headers(service)
23
+ [ status, headers, [ body ].compact ]
24
+ else
25
+ [404, {}, []]
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def has_service?(path)
32
+ config.each_scope do |scope|
33
+ scope.each_specification do |specification|
34
+ next unless url_matches?(specification, path)
35
+ return true
36
+ end
37
+ end
38
+ return false
39
+ end
40
+
41
+ def find_service(method, path)
42
+ config.each_scope do |scope|
43
+ scope.each_specification do |specification|
44
+ next unless url_matches?(specification, path)
45
+ scope.each_service(specification) do |service|
46
+ return service if service.method == method
47
+ end
48
+ end
49
+ end
50
+ nil
51
+ end
52
+
53
+ def generate_headers(service)
54
+ {
55
+ "Content-Type" => best_content_type(service)
56
+ }.delete_if{|k,v| v.nil? }
57
+ end
58
+
59
+ def best_status_code(service)
60
+ if ex = service.examples.first
61
+ (ex.expected_status && ex.expected_status.to_i) || 200
62
+ else
63
+ 200
64
+ end
65
+ end
66
+
67
+ def best_content_type(service)
68
+ if ex = service.examples.first
69
+ ex.expected_content_type
70
+ else
71
+ "application/json"
72
+ end
73
+ end
74
+
75
+ def url_matches?(specification, path)
76
+ specification.url_pattern.match(path)
77
+ end
78
+
79
+ def random_body(service, request)
80
+ world = OpenStruct.new({
81
+ service: service,
82
+ request: request
83
+ })
84
+ data = generator.call(service.output_schema.main, world)
85
+ JSON.pretty_generate(data) if data
86
+ end
87
+
88
+ end # class Mocker
89
+ end # module Web
90
+ end # module Webspicy
@@ -1,5 +1,6 @@
1
1
  require "webspicy"
2
- require "webspicy/mocker"
2
+ require "webspicy/web"
3
+ require "webspicy/web/mocker"
3
4
  require 'rack'
4
5
  config = Webspicy::Configuration.dress(Path("/formalspec/"))
5
- run Webspicy::Mocker.new(config)
6
+ run Webspicy::Web::Mocker.new(config)
File without changes
@@ -0,0 +1,129 @@
1
+ require "finitio/generation"
2
+ require "finitio/json_schema"
3
+ module Webspicy
4
+ module Web
5
+ module Openapi
6
+ class Generator
7
+
8
+ def initialize(config)
9
+ @config = Configuration.dress(config)
10
+ @generator = config.generator || Finitio::Generation.new
11
+ end
12
+ attr_reader :config, :generator
13
+
14
+ def call
15
+ {
16
+ openapi: "3.0.2",
17
+ info: {
18
+ version: "1.0.0",
19
+ title: "Hello API"
20
+ },
21
+ paths: paths
22
+ }
23
+ end
24
+
25
+ private
26
+
27
+ def paths
28
+ config.each_scope.inject({}) do |paths,scope|
29
+ scope.each_specification.inject(paths) do |paths,specification|
30
+ paths.merge(path_for(specification)){|k,ps,qs|
31
+ ps.merge(qs)
32
+ }
33
+ end
34
+ end
35
+ end
36
+
37
+ def path_for(specification)
38
+ {
39
+ specification.url => {
40
+ summary: specification.name
41
+ }.merge(verbs_for(specification))
42
+ }
43
+ end
44
+
45
+ def verbs_for(specification)
46
+ specification.services.inject({}) do |verbs,service|
47
+ verb = service.method.downcase
48
+ verb_defn = {
49
+ description: service.description,
50
+ responses: responses_for(service)
51
+ }
52
+ unless ["get", "options", "delete", "head"].include?(verb)
53
+ verb_defn[:requestBody] = request_body_for(service)
54
+ end
55
+ verbs.merge({ verb => verb_defn })
56
+ end
57
+ end
58
+
59
+ def request_body_for(service)
60
+ schema = actual_input_schema(service)
61
+ {
62
+ required: true,
63
+ content: {
64
+ "application/json" => {
65
+ schema: schema.to_json_schema,
66
+ example: generator.call(schema, {})
67
+ }
68
+ }
69
+ }
70
+ end
71
+
72
+ def responses_for(service)
73
+ result = {}
74
+ service.examples.each_with_object(result) do |test_case, rs|
75
+ rs.merge!(response_for(test_case, false)){|k,r1,r2| r1 }
76
+ end
77
+ service.counterexamples.each_with_object(result) do |test_case, rs|
78
+ rs.merge!(response_for(test_case, true)){|k,r1,r2| r1 }
79
+ end
80
+ result
81
+ end
82
+
83
+ def response_for(test_case, counterexample)
84
+ res = {
85
+ description: test_case.description,
86
+ }
87
+ status = (test_case.expected_status && test_case.expected_status.to_int) || 200
88
+ if test_case.expected_content_type && status != 204
89
+ content = {
90
+ schema: schema_for(test_case, counterexample)
91
+ }
92
+ unless counterexample
93
+ content[:example] = example_for(test_case, counterexample)
94
+ end
95
+ res[:content] = {
96
+ test_case.expected_content_type => content
97
+ }
98
+ end
99
+ {
100
+ status => res
101
+ }
102
+ end
103
+
104
+ def schema_for(test_case, counterexample)
105
+ schema = actual_output_schema(test_case, counterexample)
106
+ schema.to_json_schema
107
+ end
108
+
109
+ def example_for(test_case, counterexample)
110
+ schema = actual_output_schema(test_case, counterexample)
111
+ generator.call(schema, {})
112
+ end
113
+
114
+ def actual_output_schema(test_case, counterexample)
115
+ if counterexample
116
+ test_case.service.error_schema['Main']
117
+ else
118
+ test_case.service.output_schema['Main']
119
+ end
120
+ end
121
+
122
+ def actual_input_schema(service)
123
+ service.input_schema['Main']
124
+ end
125
+
126
+ end # class Generator
127
+ end # module Openapi
128
+ end # module Web
129
+ end # module Webspicy
@@ -2,16 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  module Webspicy
4
4
  class Specification
5
- module Precondition
5
+ module Pre
6
6
  describe GlobalRequestHeaders do
7
7
  let(:gbr){
8
8
  GlobalRequestHeaders.new('Accept' => 'application/json')
9
9
  }
10
10
 
11
+ def instrument(tc)
12
+ t = OpenStruct.new(test_case: tc)
13
+ gbr.bind(t).instrument
14
+ end
15
+
11
16
  describe "instrument" do
12
17
  it 'injects the headers' do
13
18
  tc = TestCase.new({})
14
- gbr.instrument(tc, nil)
19
+ instrument(tc)
15
20
  expect(tc.headers['Accept']).to eql("application/json")
16
21
  end
17
22
 
@@ -21,7 +26,7 @@ module Webspicy
21
26
  'Content-Type' => 'text/plain'
22
27
  }
23
28
  })
24
- gbr.instrument(tc, nil)
29
+ instrument(tc)
25
30
  expect(tc.headers['Content-Type']).to eql("text/plain")
26
31
  expect(tc.headers['Accept']).to eql("application/json")
27
32
  end
@@ -32,7 +37,7 @@ module Webspicy
32
37
  'Accept' => 'text/plain'
33
38
  }
34
39
  })
35
- gbr.instrument(tc, nil)
40
+ instrument(tc)
36
41
  expect(tc.headers['Accept']).to eql("text/plain")
37
42
  end
38
43
  end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+ module Webspicy
3
+ class Specification
4
+ describe Condition do
5
+ describe "sooner_or_later" do
6
+ include Condition
7
+
8
+ it 'works when the block returns anything non falsy' do
9
+ x = sooner_or_later(max: 1){ 12 }
10
+ expect(x).to eql(12)
11
+ end
12
+
13
+ it 'returns nil otherwise' do
14
+ x = sooner_or_later(max: 1){ nil }
15
+ expect(x).to eql(nil)
16
+ end
17
+
18
+ it 'supports the block raising a Failure' do
19
+ expect {
20
+ sooner_or_later(max: 2){
21
+ raise Tester::Failure
22
+ }
23
+ }.to raise_error(Tester::Failure)
24
+ end
25
+
26
+ it 'catches Failure occuring before successes' do
27
+ seen = 0
28
+ x = sooner_or_later(max: 2){
29
+ seen += 1
30
+ raise Tester::Failure if seen == 1
31
+ 12
32
+ }
33
+ expect(x).to eql(12)
34
+ end
35
+
36
+ it 'can raise for us' do
37
+ expect{
38
+ sooner_or_later(max: 1, raise: true){ nil }
39
+ }.to raise_error(TimeoutError)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ module Support
4
+ describe Hooks, "fire_after_each" do
5
+
6
+ class Hooker
7
+ include Hooks
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+ attr_reader :config
12
+ end
13
+
14
+ it 'work with no after_each at all' do
15
+ config = Configuration.new
16
+ Hooker.new(config).fire_after_each(1, 2, 3)
17
+ end
18
+
19
+ it 'work with one after_each' do
20
+ config = Configuration.new
21
+
22
+ seen_args = nil
23
+ config.after_each do |*args|
24
+ seen_args = args
25
+ end
26
+
27
+ Hooker.new(config).fire_after_each(1, 2, 3)
28
+
29
+ expect(seen_args).to eql([1, 2, 3])
30
+ end
31
+
32
+ it 'works with two after_each' do
33
+ config = Configuration.new
34
+
35
+ seen = false
36
+
37
+ seen_args = []
38
+ config.after_each do |*args|
39
+ seen_args << args
40
+ end
41
+ config.after_each do |*args|
42
+ seen_args << args
43
+ end
44
+
45
+ Hooker.new(config).fire_after_each(1, 2, 3)
46
+
47
+ expect(seen_args.size).to eql(2)
48
+ expect(seen_args.all?{|sa| sa == [1,2,3] }).to be(true)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -1,7 +1,15 @@
1
1
  require 'spec_helper'
2
2
  module Webspicy
3
- class Tester
4
- describe Client, "around" do
3
+ module Support
4
+ describe Hooks, "fire_around" do
5
+
6
+ class Hooker
7
+ include Hooks
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+ attr_reader :config
12
+ end
5
13
 
6
14
  it 'work with no around at all' do
7
15
  config = Configuration.new
@@ -9,8 +17,7 @@ module Webspicy
9
17
  seen = false
10
18
  block = ->(){ seen = true }
11
19
 
12
- scope = Configuration::Scope.new(config)
13
- Client.new(scope).around(1, 2, 3, &block)
20
+ Hooker.new(config).fire_around(1, 2, 3, &block)
14
21
  expect(seen).to be(true)
15
22
  end
16
23
 
@@ -24,11 +31,10 @@ module Webspicy
24
31
  bl.call
25
32
  end
26
33
 
27
- scope = Configuration::Scope.new(config)
28
34
  block = ->(){ seen = true }
29
- Client.new(scope).around(1, 2, 3, &block)
35
+ Hooker.new(config).fire_around(1, 2, 3, &block)
30
36
 
31
- expect(seen_args[0...-1]).to eql([1, 2, 3])
37
+ expect(seen_args).to eql([1, 2, 3])
32
38
  expect(seen).to be(true)
33
39
  end
34
40
 
@@ -47,12 +53,11 @@ module Webspicy
47
53
  bl.call
48
54
  end
49
55
 
50
- scope = Configuration::Scope.new(config)
51
56
  block = ->(){ seen = true }
52
- Client.new(scope).around(1, 2, 3, &block)
57
+ Hooker.new(config).fire_around(1, 2, 3, &block)
53
58
 
54
59
  expect(seen_args.size).to eql(2)
55
- expect(seen_args.all?{|sa| sa[0...-1] == [1,2,3] }).to be(true)
60
+ expect(seen_args.all?{|sa| sa == [1,2,3] }).to be(true)
56
61
  expect(seen).to be(true)
57
62
  end
58
63
 
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ module Webspicy
3
+ module Support
4
+ describe Hooks, "fire_before_each" do
5
+
6
+ class Hooker
7
+ include Hooks
8
+ def initialize(config)
9
+ @config = config
10
+ end
11
+ attr_reader :config
12
+ end
13
+
14
+ it 'work with no before_each at all' do
15
+ config = Configuration.new
16
+ Hooker.new(config).fire_before_each(1, 2, 3)
17
+ end
18
+
19
+ it 'work with one before_each' do
20
+ config = Configuration.new
21
+
22
+ seen_args = nil
23
+ config.before_each do |*args|
24
+ seen_args = args
25
+ end
26
+
27
+ Hooker.new(config).fire_before_each(1, 2, 3)
28
+
29
+ expect(seen_args).to eql([1, 2, 3])
30
+ end
31
+
32
+ it 'works with two before_each' do
33
+ config = Configuration.new
34
+
35
+ seen = false
36
+
37
+ seen_args = []
38
+ config.before_each do |*args|
39
+ seen_args << args
40
+ end
41
+ config.before_each do |*args|
42
+ seen_args << args
43
+ end
44
+
45
+ Hooker.new(config).fire_before_each(1, 2, 3)
46
+
47
+ expect(seen_args.size).to eql(2)
48
+ expect(seen_args.all?{|sa| sa == [1,2,3] }).to be(true)
49
+ end
50
+
51
+ end
52
+ end
53
+ end