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
@@ -36,6 +36,9 @@ services:
36
36
  params:
37
37
  id: 1
38
38
  description: 'hello world'
39
+ expected:
40
+ content_type: application/json
41
+ status: 200
39
42
  assert:
40
43
  - "pathFD('', description: 'hello world')"
41
44
 
@@ -17,7 +17,7 @@ services:
17
17
  }
18
18
 
19
19
  output_schema: |-
20
- .
20
+ Any
21
21
 
22
22
  error_schema: |-
23
23
  ErrorSchema
@@ -16,7 +16,7 @@ services:
16
16
  - Must be authenticated
17
17
 
18
18
  input_schema: |-
19
- .
19
+ Any
20
20
 
21
21
  output_schema: |-
22
22
  { count: Integer }
@@ -1,6 +1,6 @@
1
1
  require_relative 'config'
2
2
  webspicy_config do |c|
3
- c.client = Webspicy::RackTestClient.for(::Sinatra::Application)
3
+ c.client = Webspicy::Tester::RackTestClient.for(::Sinatra::Application)
4
4
  c.before_each do |_,client|
5
5
  client.api.post "/reset"
6
6
  end
@@ -1,7 +1,7 @@
1
1
  require_relative 'config'
2
2
  webspicy_config do |c|
3
3
  c.host = "http://127.0.0.1:4567"
4
- c.client = Webspicy::HttpClient
4
+ c.client = Webspicy::Tester::HttpClient
5
5
  c.before_each do |_,client|
6
6
  client.api.post "http://127.0.0.1:4567/reset"
7
7
  end
@@ -1,4 +1,4 @@
1
- @import finitio/data
1
+ @import webspicy/scalars
2
2
 
3
3
  Todo = {
4
4
  id : Integer
@@ -6,7 +6,7 @@ Todo = {
6
6
  }
7
7
 
8
8
  TodoPatch = {
9
- id :? Integer
9
+ id : Integer
10
10
  description :? String
11
11
  }
12
12
 
@@ -30,7 +30,7 @@ class MustBeAuthenticated
30
30
  def counterexample(description, role, expected, status = 401)
31
31
  YAML.load <<-YML.gsub(/^\s+[#][ ]/, "")
32
32
  # description: |-
33
- # (#{self.class.name}) #{description}
33
+ # #{description} (#{self.class.name} PRE)
34
34
  # params:
35
35
  # id: 1
36
36
  # dress_params:
@@ -41,7 +41,7 @@ class MustBeAuthenticated
41
41
  # content_type: application/json
42
42
  # status: #{status}
43
43
  # assert:
44
- # - "pathFD('', error: '#{expected}')"
44
+ # - "pathFD('', error: '#{expected}')"
45
45
  YML
46
46
  end
47
47
 
@@ -0,0 +1,21 @@
1
+ class TodoNotRemoved
2
+ include Webspicy::Specification::Postcondition
3
+
4
+ def self.match(service, descr)
5
+ return TodoNotRemoved.new if descr =~ /If it existed, the todo has not been removed/
6
+ end
7
+
8
+ def check(invocation)
9
+ client, scope, test_case = invocation.client,
10
+ invocation.client.scope,
11
+ invocation.test_case
12
+ return if invocation.response.status == 404
13
+ id = test_case.params['id']
14
+ url = scope.to_real_url("/todo/#{id}", test_case){|url| url }
15
+ response = client.api.get(url, {}, {
16
+ "Accept" => "application/json"
17
+ })
18
+ return nil if response.status == 200
19
+ "Todo `#{id}` was not supposed to be deleted, it was not found"
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ class TodoRemoved
2
+ include Webspicy::Specification::Postcondition
3
+
4
+ def self.match(service, descr)
5
+ return TodoRemoved.new if descr =~ /The todo has been removed/
6
+ end
7
+
8
+ def check(invocation)
9
+ client, scope, test_case = invocation.client,
10
+ invocation.client.scope,
11
+ invocation.test_case
12
+ id = test_case.params['id']
13
+ url = scope.to_real_url("/todo/#{id}", test_case){|url| url }
14
+ response = client.api.get(url, {}, {
15
+ "Accept" => "application/json"
16
+ })
17
+ return nil if response.status == 404
18
+ "Todo `#{id}` was not deleted, it has been found"
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ ---
2
+ name: |-
3
+ Todo
4
+
5
+ url: |-
6
+ http://127.0.0.1:4567/todo/{id}
7
+
8
+ services:
9
+ - method: |-
10
+ GET
11
+
12
+ description: |-
13
+ Returns a single todo item
14
+
15
+ input_schema: |-
16
+ {
17
+ id: Integer
18
+ }
19
+
20
+ output_schema: |-
21
+ {
22
+ id: Integer
23
+ description: String
24
+ }
25
+
26
+ error_schema: |-
27
+ {
28
+ error: String
29
+ }
30
+
31
+ examples:
32
+
33
+ - description: |-
34
+ when requested on an existing TODO
35
+ headers:
36
+ Accept: application/json
37
+ params:
38
+ id: 1
39
+ expected:
40
+ content_type: application/json
41
+ status: 200
42
+ assert:
43
+ - "pathFD('', id: 1)"
44
+ - "match('description', /Refactor/)"
45
+ - "notMatch('description', /Foo/)"
46
+
47
+ counterexamples:
48
+
49
+ - description: |-
50
+ when requested on an unexisting TODO
51
+ headers:
52
+ Accept: application/json
53
+ params:
54
+ id: 999254654
55
+ expected:
56
+ content_type: application/json
57
+ status: 404
58
+ assert:
59
+ - "pathFD('', error: 'No such todo')"
@@ -0,0 +1,2 @@
1
+ Webspicy::Configuration.new{|c|
2
+ }
@@ -0,0 +1 @@
1
+ @import webspicy/scalars
@@ -0,0 +1,30 @@
1
+ ---
2
+ url: |-
3
+ http://yourbackendisbroken.dev
4
+
5
+ method: |-
6
+ GET
7
+
8
+ description: |-
9
+ Redirects to the https version
10
+
11
+ input_schema: |-
12
+ Any
13
+
14
+ output_schema: |-
15
+ Any
16
+
17
+ error_schema: |-
18
+ Any
19
+
20
+ examples:
21
+
22
+ - description: |-
23
+ it works
24
+ params:
25
+ id: 1
26
+ expected:
27
+ content_type: text/html
28
+ status: 3xx
29
+ headers:
30
+ Location: https://yourbackendisbroken.dev/?id=1
@@ -0,0 +1,30 @@
1
+ ---
2
+ url: |-
3
+ https://yourbackendisbroken.dev
4
+
5
+ method: |-
6
+ GET
7
+
8
+ description: |-
9
+ Returns the web page
10
+
11
+ input_schema: |-
12
+ Any
13
+
14
+ output_schema: |-
15
+ Any
16
+
17
+ error_schema: |-
18
+ Any
19
+
20
+ examples:
21
+
22
+ - description: |-
23
+ it works
24
+ params:
25
+ id: 1
26
+ expected:
27
+ content_type: text/html
28
+ status: 200
29
+ assert:
30
+ - notEmpty
@@ -0,0 +1,25 @@
1
+ # Any
2
+ Any = .
3
+
4
+ # Booleans
5
+ Nil = .NilClass
6
+ True = .TrueClass
7
+ False = .FalseClass
8
+ Boolean = .TrueClass|.FalseClass
9
+
10
+ # Numbers
11
+ Numeric = .Numeric
12
+ Integer = .Integer
13
+ Float = .Float
14
+ Real = .Float
15
+
16
+ # String
17
+ String = .String
18
+
19
+ # Date, Time, DateTime
20
+ Date = .Date <iso8601> .String \( s | Date.iso8601(s) )
21
+ \( d | d.iso8601 )
22
+ Time = .Time <iso8601> .String \( s | Time.iso8601(s) )
23
+ \( t | t.iso8601 )
24
+ DateTime = .DateTime <iso8601> .String \( s | DateTime.iso8601(s) )
25
+ \( t | t.iso8601 )
@@ -7,25 +7,33 @@ require 'ostruct'
7
7
  require 'yaml'
8
8
  require 'rspec'
9
9
  require 'rack/test'
10
- require "mustermann"
10
+ require 'mustermann'
11
+ require 'colorized_string'
12
+ require 'securerandom'
11
13
  module Webspicy
12
14
 
13
15
  ###
14
16
  ### Load library
15
17
  ###
16
18
 
19
+ require 'webspicy/version'
17
20
  require 'webspicy/support'
21
+ require 'webspicy/specification'
18
22
  require 'webspicy/configuration'
19
- require 'webspicy/file_upload'
20
- require 'webspicy/scope'
21
- require 'webspicy/client'
22
- require 'webspicy/resource'
23
- require 'webspicy/precondition'
24
- require 'webspicy/postcondition'
25
23
  require 'webspicy/checker'
26
24
  require 'webspicy/tester'
27
- require 'webspicy/tester/assertions'
28
- require 'webspicy/tester/asserter'
25
+
26
+ ###
27
+ ### Backward compatibility
28
+ ###
29
+ Client = Tester::Client
30
+ HttpClient = Tester::HttpClient
31
+ RackTestClient = Tester::RackTestClient
32
+ Resource = Specification
33
+ Precondition = Specification::Precondition
34
+ Postcondition = Specification::Postcondition
35
+ FileUpload = Specification::FileUpload
36
+ Scope = Configuration::Scope
29
37
 
30
38
  ###
31
39
  ### About folders
@@ -36,30 +44,39 @@ module Webspicy
36
44
  EXAMPLES_FOLDER = ROOT_FOLDER/('examples')
37
45
 
38
46
  ###
39
- ### About formal doc and resources defined there
47
+ ### About formal doc and specifications defined there
40
48
  ###
41
-
49
+ Finitio.stdlib_path(Path.dir/"finitio")
50
+ DEFAULT_SYSTEM = Finitio.system(<<~FIO)
51
+ @import webspicy/scalars
52
+ FIO
42
53
  FORMALDOC = Finitio.system(Path.dir/("webspicy/formaldoc.fio"))
43
54
 
44
55
  # Returns a default scope instance.
45
56
  def default_scope
46
- Scope.new(Configuration.new)
57
+ Configuration::Scope.new(Configuration.new)
47
58
  end
48
59
  module_function :default_scope
49
60
 
50
- def resource(raw, file = nil, scope = default_scope)
61
+ def specification(raw, file = nil, scope = default_scope)
62
+ raw = YAML.load(raw) if raw.is_a?(String)
51
63
  with_scope(scope) do
52
- r = FORMALDOC["Resource"].dress(raw)
64
+ r = FORMALDOC["Specification"].dress(raw)
65
+ r.config = scope.config
53
66
  r.located_at!(file) if file
54
67
  r
55
68
  end
69
+ rescue Finitio::Error => ex
70
+ handle_finitio_error(ex, scope)
56
71
  end
57
- module_function :resource
72
+ module_function :specification
58
73
 
59
74
  def service(raw, scope = default_scope)
60
75
  with_scope(scope) do
61
76
  FORMALDOC["Service"].dress(raw)
62
77
  end
78
+ rescue Finitio::Error => ex
79
+ handle_finitio_error(ex)
63
80
  end
64
81
  module_function :service
65
82
 
@@ -67,9 +84,19 @@ module Webspicy
67
84
  with_scope(scope) do
68
85
  FORMALDOC["TestCase"].dress(raw)
69
86
  end
87
+ rescue Finitio::Error => ex
88
+ handle_finitio_error(ex)
70
89
  end
71
90
  module_function :test_case
72
91
 
92
+ def handle_finitio_error(ex, scope)
93
+ msg = "#{ex.message}:\n #{ex.root_cause.message}"
94
+ msg = Support::Colorize.colorize_error(msg, scope.config)
95
+ fatal(msg)
96
+ raise
97
+ end
98
+ module_function :handle_finitio_error
99
+
73
100
  #
74
101
  # Yields the block after having installed `scope` globally.
75
102
  #
@@ -120,7 +147,7 @@ module Webspicy
120
147
  if scope = Thread.current[:webspicy_scope]
121
148
  scope.parse_schema(fio)
122
149
  else
123
- Finitio.system(fio)
150
+ DEFAULT_SYSTEM.system(fio)
124
151
  end
125
152
  end
126
153
  module_function :schema
@@ -132,7 +159,7 @@ module Webspicy
132
159
  LOGGER = ::Logger.new(STDOUT)
133
160
  LOGGER.level = Logger.const_get(ENV['LOG_LEVEL'] || 'WARN')
134
161
  LOGGER.formatter = proc { |severity, datetime, progname, msg|
135
- " " + msg + "\n"
162
+ " " + msg + "\n"
136
163
  }
137
164
 
138
165
  def info(*args, &bl)
@@ -145,4 +172,9 @@ module Webspicy
145
172
  end
146
173
  module_function :debug
147
174
 
175
+ def fatal(*args, &bl)
176
+ LOGGER && LOGGER.fatal(*args, &bl)
177
+ end
178
+ module_function :fatal
179
+
148
180
  end
@@ -1,25 +1,10 @@
1
1
  module Webspicy
2
2
  class Checker
3
3
 
4
- def initialize(config)
5
- @config = Configuration.dress(config)
4
+ def self.new(*args, &bl)
5
+ require_relative 'rspec/checker'
6
+ RSpecChecker.new(*args, &bl)
6
7
  end
7
- attr_reader :config
8
8
 
9
- def call
10
- config.each_scope do |scope|
11
- scope.each_resource_file do |file, folder|
12
- RSpec.describe file.relative_to(folder).to_s do
13
-
14
- it 'meets the formal doc data schema' do
15
- Webspicy.resource(file.load, file, scope)
16
- end
17
-
18
- end
19
- end
20
- end
21
- RSpec::Core::Runner.run config.rspec_options
22
- end
23
-
24
- end
25
- end
9
+ end # class Checker
10
+ end # module Webspicy