committee 0.4.10 → 0.4.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ module Committee::Middleware
2
2
  class RequestValidation < Base
3
3
  def initialize(app, options={})
4
4
  super
5
+ @allow_extra = options[:allow_extra]
5
6
  @prefix = options[:prefix]
6
7
  end
7
8
 
@@ -10,7 +11,12 @@ module Committee::Middleware
10
11
  env[@params_key] = Committee::RequestUnpacker.new(request).call
11
12
  link, _ = @router.routes_request?(request, prefix: @prefix)
12
13
  if link
13
- Committee::ParamValidator.new(env[@params_key], @schema, link).call
14
+ Committee::ParamValidator.new(
15
+ env[@params_key],
16
+ @schema,
17
+ link,
18
+ allow_extra: @allow_extra
19
+ ).call
14
20
  end
15
21
  @app.call(env)
16
22
  rescue Committee::BadRequest
@@ -2,15 +2,16 @@ module Committee
2
2
  class ParamValidator
3
3
  include Validation
4
4
 
5
- def initialize(params, schema, link_schema)
5
+ def initialize(params, schema, link_schema, options = {})
6
6
  @params = params
7
7
  @schema = schema
8
8
  @link_schema = link_schema
9
+ @allow_extra = options[:allow_extra]
9
10
  end
10
11
 
11
12
  def call
12
13
  detect_missing!
13
- detect_extra!
14
+ detect_extra! if !@allow_extra
14
15
  check_data!
15
16
  end
16
17
 
@@ -46,6 +46,18 @@ describe Committee::Middleware::RequestValidation do
46
46
  assert_match /unknown params/i, last_response.body
47
47
  end
48
48
 
49
+ it "doesn't error on an extra parameter with allow_extra" do
50
+ @app = new_rack_app(allow_extra: true)
51
+ params = {
52
+ "app" => "heroku-api",
53
+ "cloud" => "production",
54
+ "recipient" => "owner@heroku.com",
55
+ }
56
+ header "Content-Type", "application/json"
57
+ post "/account/app-transfers", MultiJson.encode(params)
58
+ assert_equal 200, last_response.status
59
+ end
60
+
49
61
  it "rescues JSON errors" do
50
62
  @app = new_rack_app
51
63
  header "Content-Type", "application/json"
@@ -12,28 +12,24 @@ describe Committee::ParamValidator do
12
12
  "app" => "heroku-api",
13
13
  "recipient" => "owner@heroku.com",
14
14
  }
15
- Committee::ParamValidator.new(params, @schema, @link_schema).call
15
+ validate(params, @schema, @link_schema)
16
16
  end
17
17
 
18
18
  it "detects a missing parameter" do
19
19
  e = assert_raises(Committee::InvalidParams) do
20
- Committee::ParamValidator.new({}, @schema, @link_schema).call
20
+ validate({}, @schema, @link_schema)
21
21
  end
22
22
  message = "Require params: app, recipient."
23
23
  assert_equal message, e.message
24
24
  end
25
25
 
26
- it "detects an extraneous parameter" do
26
+ it "doesn't error on an extraneous parameter with allow_extra" do
27
27
  params = {
28
28
  "app" => "heroku-api",
29
29
  "cloud" => "production",
30
30
  "recipient" => "owner@heroku.com",
31
31
  }
32
- e = assert_raises(Committee::InvalidParams) do
33
- Committee::ParamValidator.new(params, @schema, @link_schema).call
34
- end
35
- message = "Unknown params: cloud."
36
- assert_equal message, e.message
32
+ validate(params, @schema, @link_schema, allow_extra: true)
37
33
  end
38
34
 
39
35
  it "detects a parameter of the wrong type" do
@@ -42,7 +38,7 @@ describe Committee::ParamValidator do
42
38
  "recipient" => 123,
43
39
  }
44
40
  e = assert_raises(Committee::InvalidType) do
45
- Committee::ParamValidator.new(params, @schema, @link_schema).call
41
+ validate(params, @schema, @link_schema)
46
42
  end
47
43
  message = %{Invalid type for key "recipient": expected 123 to be ["string"].}
48
44
  assert_equal message, e.message
@@ -54,7 +50,7 @@ describe Committee::ParamValidator do
54
50
  "recipient" => "not-email",
55
51
  }
56
52
  e = assert_raises(Committee::InvalidFormat) do
57
- Committee::ParamValidator.new(params, @schema, @link_schema).call
53
+ validate(params, @schema, @link_schema)
58
54
  end
59
55
  message = %{Invalid format for key "recipient": expected "not-email" to be "email".}
60
56
  assert_equal message, e.message
@@ -66,7 +62,7 @@ describe Committee::ParamValidator do
66
62
  }
67
63
  link_schema = @schema["app"]["links"][0]
68
64
  e = assert_raises(Committee::InvalidPattern) do
69
- Committee::ParamValidator.new(params, @schema, link_schema).call
65
+ validate(params, @schema, link_schema)
70
66
  end
71
67
  message = %{Invalid pattern for key "name": expected %@! to match "(?-mix:^[a-z][a-z0-9-]{3,30}$)".}
72
68
  assert_equal message, e.message
@@ -81,7 +77,7 @@ describe Committee::ParamValidator do
81
77
  ]
82
78
  }
83
79
  link_schema = @schema["stack"]["links"][2]
84
- Committee::ParamValidator.new(params, @schema, link_schema).call
80
+ validate(params, @schema, link_schema)
85
81
  end
86
82
 
87
83
  it "detects an array item with a parameter of the wrong type" do
@@ -92,7 +88,7 @@ describe Committee::ParamValidator do
92
88
  }
93
89
  link_schema = @schema["stack"]["links"][2]
94
90
  e = assert_raises(Committee::InvalidType) do
95
- Committee::ParamValidator.new(params, @schema, link_schema).call
91
+ validate(params, @schema, link_schema)
96
92
  end
97
93
  message = %{Invalid type for key "state": expected 123 to be ["string"].}
98
94
  assert_equal message, e.message
@@ -106,7 +102,7 @@ describe Committee::ParamValidator do
106
102
  "flags" => [ "vip", "customer" ]
107
103
  }
108
104
  link_schema = @schema["account"]["links"][1]
109
- Committee::ParamValidator.new(params, @schema, link_schema).call
105
+ validate(params, @schema, link_schema)
110
106
  end
111
107
 
112
108
  it "detects an array item with a parameter of the wrong type" do
@@ -116,10 +112,16 @@ describe Committee::ParamValidator do
116
112
  }
117
113
  link_schema = @schema["account"]["links"][1]
118
114
  e = assert_raises(Committee::InvalidType) do
119
- Committee::ParamValidator.new(params, @schema, link_schema).call
115
+ validate(params, @schema, link_schema)
120
116
  end
121
117
  message = %{Invalid type for key "flags": expected 999 to be ["string"].}
122
118
  assert_equal message, e.message
123
119
  end
124
120
  end
121
+
122
+ private
123
+
124
+ def validate(params, schema, link_schema, options = {})
125
+ Committee::ParamValidator.new(params, schema, link_schema, options).call
126
+ end
125
127
  end
data/test/router_test.rb CHANGED
@@ -8,15 +8,19 @@ describe Committee::Router do
8
8
  end
9
9
 
10
10
  it "builds routes without parameters" do
11
- assert @router.routes?("GET", "/apps")
11
+ refute_nil @router.routes?("GET", "/apps")[0]
12
12
  end
13
13
 
14
14
  it "builds routes with parameters" do
15
- assert @router.routes?("GET", "/apps/123")
15
+ refute_nil @router.routes?("GET", "/apps/123")[0]
16
+ end
17
+
18
+ it "doesn't match anything on a /" do
19
+ assert_nil @router.routes?("GET", "/")[0]
16
20
  end
17
21
 
18
22
  it "takes a prefix" do
19
23
  # this is a sociopathic example
20
- assert @router.routes?("GET", "/kpi/apps/123", prefix: "/kpi")
24
+ refute_nil @router.routes?("GET", "/kpi/apps/123", prefix: "/kpi")[0]
21
25
  end
22
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.10
4
+ version: 0.4.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-04-26 00:00:00.000000000 Z
13
+ date: 2014-04-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -44,22 +44,6 @@ dependencies:
44
44
  - - ! '>'
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0.0'
47
- - !ruby/object:Gem::Dependency
48
- name: minitest
49
- requirement: !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- type: :development
56
- prerelease: false
57
- version_requirements: !ruby/object:Gem::Requirement
58
- none: false
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: '0'
63
47
  - !ruby/object:Gem::Dependency
64
48
  name: rack-test
65
49
  requirement: !ruby/object:Gem::Requirement
@@ -76,22 +60,6 @@ dependencies:
76
60
  - - ! '>='
77
61
  - !ruby/object:Gem::Version
78
62
  version: '0'
79
- - !ruby/object:Gem::Dependency
80
- name: rake
81
- requirement: !ruby/object:Gem::Requirement
82
- none: false
83
- requirements:
84
- - - ! '>='
85
- - !ruby/object:Gem::Version
86
- version: '0'
87
- type: :development
88
- prerelease: false
89
- version_requirements: !ruby/object:Gem::Requirement
90
- none: false
91
- requirements:
92
- - - ! '>='
93
- - !ruby/object:Gem::Version
94
- version: '0'
95
63
  description:
96
64
  email:
97
65
  - brandur@mutelight.org