weak_parameters 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4a10d28d1fedc2265c21778bc396a9290d873634
4
- data.tar.gz: 10443cf7641573095ca36b799a09bb460740b428
3
+ metadata.gz: cc43dbf15b3f5fe477fe58c0788a80aa036317f5
4
+ data.tar.gz: d6a24136ebea5ade178a61d4555e376128d95432
5
5
  SHA512:
6
- metadata.gz: f63c30ce3c389a79648fc265591e0976fd2f3d45b6568f190fff16068f3b0c26ad3edca2fcbea87f2ea7ccd6de97a4e27ea436f06d07b97c9b7dd9f8e8eb9ef1
7
- data.tar.gz: 58530920dd4a349f99f19683551bab09b9a8a5a58b247317a7e785e7df1774b97edc2edb83735cdbbeffa5395d486121ac83e61e37dddd62446941a4682f4b86
6
+ metadata.gz: 7b716fa12192074d863010d6b25369e1d00ec03571ae7e4872f40bba877373f1964f670790518f51a640ccac0bb742e53206625a77f9ea0a639de67b49a7452c
7
+ data.tar.gz: 6f8a71f1741499f9f0acde74dc8f0ea6a1fbb673679beaf8101c7a314f674d5c90f87a3731b83bd37994994d936d8d5bc85b623d2f0d61ed88d4b505dc862f3d
@@ -1,3 +1,6 @@
1
+ ## 0.4.1
2
+ - Support rails-api
3
+
1
4
  ## 0.4.0
2
5
  - Support Rails 5
3
6
 
@@ -57,6 +57,7 @@ module WeakParameters
57
57
  initializer 'weak_parameters' do
58
58
  ActiveSupport.on_load :action_controller do
59
59
  ActionController::Base.extend WeakParameters::Controller
60
+ ActionController::API.extend WeakParameters::Controller if Object.const_defined?('ActionController::API')
60
61
  end
61
62
  end
62
63
  end
@@ -1,3 +1,3 @@
1
1
  module WeakParameters
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -0,0 +1,5 @@
1
+ module API
2
+ class RecipesController < APIController
3
+ include WithRecipe
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module API
2
+ class StrongsController < APIController
3
+ include WithStrong
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class APIController < ActionController::API
2
+ rescue_from WeakParameters::ValidationError do
3
+ head 400
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ module WithRecipe
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ validates :create do
6
+ any :object
7
+ string :name, required: true, except: %w[invalid wrong]
8
+ integer :type, only: 0..3
9
+ integer :number, only: [0, 1]
10
+ boolean :flag
11
+ hash :config
12
+ array :tags
13
+ float :rate
14
+ file :attachment
15
+ integer :custom, only: 0..1, handler: :render_error
16
+ string :zip_code do |value|
17
+ zip_pattern(value)
18
+ end
19
+ object :nested, required: true do
20
+ integer :number, only: [0, 1]
21
+ end
22
+
23
+ list :numbers, :integer, description: 'some numbers'
24
+
25
+ object :body do
26
+ list :items, :object, description: 'some items' do
27
+ string :name
28
+ integer :price
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ def create
35
+ head 201
36
+ end
37
+
38
+ private
39
+
40
+ def render_error
41
+ head 403
42
+ end
43
+
44
+ def zip_pattern(value)
45
+ value =~ /\A\d{3}-\d{4}\z/
46
+ end
47
+ end
@@ -0,0 +1,61 @@
1
+ module WithStrong
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ validates :create do
6
+ any :object
7
+ any :strong_object, strong: true
8
+ string :name, required: true, except: %w[invalid wrong]
9
+ string :strong_name, required: true, strong: true, except: %w[invalid wrong]
10
+ integer :type, only: 0..3
11
+ integer :strong_type, strong: true, only: 0..3
12
+ integer :number, only: [0, 1]
13
+ integer :strong_number, strong: true, only: [0, 1]
14
+ boolean :flag
15
+ boolean :strong_flag, strong: true
16
+ hash :config
17
+ hash :strong_config, strong: true
18
+ array :tags
19
+ array :strong_tags, strong: true
20
+ float :rate
21
+ float :strong_rate, strong: true
22
+ file :attachment
23
+ file :strong_attachment, strong: true
24
+ integer :custom, only: 0..1, handler: :render_error
25
+ integer :strong_custom, strong: true, only: 0..1, handler: :render_error
26
+ string :zip_code do |value|
27
+ value =~ /\A\d{3}-\d{4}\z/
28
+ end
29
+ string :strong_zip_code, strong: true do |value|
30
+ value =~ /\A\d{3}-\d{4}\z/
31
+ end
32
+ object :nested, required: true do
33
+ integer :number, only: [0, 1]
34
+ end
35
+ object :strong_nested, required: true do
36
+ integer :strong_number, strong: true, only: [0, 1]
37
+ end
38
+
39
+ list :numbers, :integer, description: 'some numbers'
40
+
41
+ list :strong_numbers, :integer, strong: true, description: 'some numbers'
42
+
43
+ object :strong_body do
44
+ list :items, :object, description: 'some items' do
45
+ string :name
46
+ integer :strong_price, strong: true
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ def create
53
+ head 201
54
+ end
55
+
56
+ private
57
+
58
+ def render_error
59
+ head 403
60
+ end
61
+ end
@@ -1,43 +1,3 @@
1
1
  class RecipesController < ApplicationController
2
- validates :create do
3
- any :object
4
- string :name, required: true, except: %w[invalid wrong]
5
- integer :type, only: 0..3
6
- integer :number, only: [0, 1]
7
- boolean :flag
8
- hash :config
9
- array :tags
10
- float :rate
11
- file :attachment
12
- integer :custom, only: 0..1, handler: :render_error
13
- string :zip_code do |value|
14
- zip_pattern(value)
15
- end
16
- object :nested, required: true do
17
- integer :number, only: [0, 1]
18
- end
19
-
20
- list :numbers, :integer, description: 'some numbers'
21
-
22
- object :body do
23
- list :items, :object, description: 'some items' do
24
- string :name
25
- integer :price
26
- end
27
- end
28
- end
29
-
30
- def create
31
- head 201
32
- end
33
-
34
- private
35
-
36
- def render_error
37
- head 403
38
- end
39
-
40
- def zip_pattern(value)
41
- value =~ /\A\d{3}-\d{4}\z/
42
- end
2
+ include WithRecipe
43
3
  end
@@ -1,57 +1,3 @@
1
1
  class StrongsController < ApplicationController
2
- validates :create do
3
- any :object
4
- any :strong_object, strong: true
5
- string :name, required: true, except: %w[invalid wrong]
6
- string :strong_name, required: true, strong: true, except: %w[invalid wrong]
7
- integer :type, only: 0..3
8
- integer :strong_type, strong: true, only: 0..3
9
- integer :number, only: [0, 1]
10
- integer :strong_number, strong: true, only: [0, 1]
11
- boolean :flag
12
- boolean :strong_flag, strong: true
13
- hash :config
14
- hash :strong_config, strong: true
15
- array :tags
16
- array :strong_tags, strong: true
17
- float :rate
18
- float :strong_rate, strong: true
19
- file :attachment
20
- file :strong_attachment, strong: true
21
- integer :custom, only: 0..1, handler: :render_error
22
- integer :strong_custom, strong: true, only: 0..1, handler: :render_error
23
- string :zip_code do |value|
24
- value =~ /\A\d{3}-\d{4}\z/
25
- end
26
- string :strong_zip_code, strong: true do |value|
27
- value =~ /\A\d{3}-\d{4}\z/
28
- end
29
- object :nested, required: true do
30
- integer :number, only: [0, 1]
31
- end
32
- object :strong_nested, required: true do
33
- integer :strong_number, strong: true, only: [0, 1]
34
- end
35
-
36
- list :numbers, :integer, description: 'some numbers'
37
-
38
- list :strong_numbers, :integer, strong: true, description: 'some numbers'
39
-
40
- object :strong_body do
41
- list :items, :object, description: 'some items' do
42
- string :name
43
- integer :strong_price, strong: true
44
- end
45
- end
46
- end
47
-
48
- def create
49
- head 201
50
- end
51
-
52
- private
53
-
54
- def render_error
55
- head 403
56
- end
2
+ include WithStrong
57
3
  end
@@ -1,4 +1,9 @@
1
1
  Dummy::Application.routes.draw do
2
2
  resources :recipes, only: :create
3
3
  resources :strongs, only: :create
4
+
5
+ scope :api do
6
+ resources :recipes, only: :create
7
+ resources :strongs, only: :create
8
+ end
4
9
  end
@@ -0,0 +1,188 @@
1
+ require "spec_helper"
2
+
3
+ describe "Recipes with rails-api", type: :request do
4
+ let(:params) do
5
+ {
6
+ object: [],
7
+ name: "name",
8
+ number: 0,
9
+ type: 1,
10
+ flag: true,
11
+ config: {},
12
+ tags: [],
13
+ attachment: Rack::Test::UploadedFile.new(__FILE__),
14
+ zip_code: "123-4567",
15
+ custom: 0,
16
+ nested: {
17
+ number: 0
18
+ },
19
+ numbers: [1, 2, 3],
20
+ body: {
21
+ items: [
22
+ { name: "foo", price: 100 },
23
+ { name: "bar", price: 100 }
24
+ ]
25
+ }
26
+ }
27
+ end
28
+
29
+ shared_examples_for "400" do
30
+ it "returns 400" do
31
+ post "/api/recipes", params: params
32
+ expect(response.status).to eq(400)
33
+ end
34
+ end
35
+
36
+ shared_examples_for "201" do
37
+ it "creates a new recipe" do
38
+ post "/api/recipes", params: params
39
+ expect(response.status).to eq(201)
40
+ end
41
+ end
42
+
43
+ describe "POST /api/recipes" do
44
+ context "without required param" do
45
+ before do
46
+ params.delete(:name)
47
+ end
48
+ include_examples "400"
49
+ end
50
+
51
+ context "with wrong string param" do
52
+ before do
53
+ params[:name] = ["x"]
54
+ end
55
+ include_examples "400"
56
+ end
57
+
58
+ context "with wrong integer param" do
59
+ before do
60
+ params[:type] = "x"
61
+ end
62
+ include_examples "400"
63
+ end
64
+
65
+ context "with exceptional interger param" do
66
+ before do
67
+ params[:number] = [1]
68
+ end
69
+ include_examples "400"
70
+ end
71
+
72
+ context "with wrong boolean param" do
73
+ before do
74
+ params[:flag] = "x"
75
+ end
76
+ include_examples "400"
77
+ end
78
+
79
+ context "with wrong array param" do
80
+ before do
81
+ params[:tags] = "x"
82
+ end
83
+ include_examples "400"
84
+ end
85
+
86
+ context "with wrong hash param" do
87
+ before do
88
+ params[:config] = "x"
89
+ end
90
+ include_examples "400"
91
+ end
92
+
93
+ context "with wrong float param" do
94
+ before do
95
+ params[:rate] = "-x"
96
+ end
97
+ include_examples "400"
98
+ end
99
+
100
+ context "with wrong file param" do
101
+ before do
102
+ params[:attachment] = "x"
103
+ end
104
+ include_examples "400"
105
+ end
106
+
107
+ context "with block failure" do
108
+ before do
109
+ params[:zip_code] = "123-456"
110
+ end
111
+ include_examples "400"
112
+ end
113
+
114
+ context "with invalid param to :only condition" do
115
+ before do
116
+ params[:type] = 4
117
+ end
118
+ include_examples "400"
119
+ end
120
+
121
+ context "with invalid param to :except condition" do
122
+ before do
123
+ params[:name] = "invalid"
124
+ end
125
+ include_examples "400"
126
+ end
127
+
128
+ context "without non-required param" do
129
+ before do
130
+ params.delete(:type)
131
+ end
132
+ include_examples "201"
133
+ end
134
+
135
+ context "with valid condition", :autodoc do
136
+ include_examples "201"
137
+ end
138
+
139
+ context "with custom handler option" do
140
+ before do
141
+ params[:custom] = "invalid"
142
+ end
143
+ it "delegates to specified method" do
144
+ post "/recipes", params: params
145
+ expect(response.status).to eq(403)
146
+ end
147
+ end
148
+
149
+ context "with wrong nested params" do
150
+ before do
151
+ params[:nested][:number] = true
152
+ end
153
+ include_examples "400"
154
+ end
155
+
156
+ context "with wrong repeated params" do
157
+ describe 'scalar' do
158
+ before do
159
+ params[:numbers] = 1
160
+ end
161
+ include_examples "400"
162
+ end
163
+
164
+ describe 'wrong type' do
165
+ before do
166
+ params[:numbers] = ["foo"]
167
+ end
168
+ include_examples "400"
169
+ end
170
+ end
171
+
172
+ context "with complex params" do
173
+ describe 'invalid parameter' do
174
+ before do
175
+ params[:body][:items] << { price: "xxx" }
176
+ end
177
+ include_examples "400"
178
+ end
179
+
180
+ describe 'missing parameter' do
181
+ before do
182
+ params.delete :body
183
+ end
184
+ include_examples "201"
185
+ end
186
+ end
187
+ end
188
+ end
@@ -0,0 +1,100 @@
1
+ require "spec_helper"
2
+
3
+ describe "Strong with rails-api", type: :request do
4
+ let(:params) do
5
+ {
6
+ object: [1],
7
+ strong_object: [1],
8
+ name: "name",
9
+ strong_name: "name",
10
+ number: 0,
11
+ strong_number: 0,
12
+ type: 1,
13
+ strong_type: 1,
14
+ flag: true,
15
+ strong_flag: true,
16
+ config: { a: 1 },
17
+ strong_config: { a: 1, b: { c: 2 } },
18
+ tags: [1],
19
+ strong_tags: [1],
20
+ attachment: Rack::Test::UploadedFile.new(__FILE__),
21
+ strong_attachment: Rack::Test::UploadedFile.new(__FILE__),
22
+ zip_code: "123-4567",
23
+ strong_zip_code: "123-4567",
24
+ custom: 0,
25
+ strong_custom: 0,
26
+ nested: {
27
+ number: 0
28
+ },
29
+ strong_nested: {
30
+ strong_number: 0
31
+ },
32
+ numbers: [1, 2, 3],
33
+ strong_numbers: [1, 2, 3],
34
+ body: {
35
+ items: [
36
+ { name: "foo", price: 100 },
37
+ { name: "bar", price: 100 }
38
+ ]
39
+ },
40
+ strong_body: {
41
+ items: [
42
+ { name: "foo", strong_price: 100 },
43
+ { name: "bar", strong_price: 100 }
44
+ ]
45
+ }
46
+ }
47
+ end
48
+
49
+ describe "#permitted_params" do
50
+ it "returns permitted_params" do
51
+ post "/api/strongs", params: params
52
+ expect(controller.permitted_params).to have_key "strong_object"
53
+ expect(controller.permitted_params).not_to have_key "object"
54
+
55
+ expect(controller.permitted_params).to have_key "strong_name"
56
+ expect(controller.permitted_params).not_to have_key "name"
57
+
58
+ expect(controller.permitted_params).to have_key "strong_number"
59
+ expect(controller.permitted_params).not_to have_key "number"
60
+
61
+ expect(controller.permitted_params).to have_key "strong_type"
62
+ expect(controller.permitted_params).not_to have_key "type"
63
+
64
+ expect(controller.permitted_params).to have_key "strong_flag"
65
+ expect(controller.permitted_params).not_to have_key "flag"
66
+
67
+ expect(controller.permitted_params).to have_key "strong_config"
68
+ expect(controller.permitted_params).not_to have_key "config"
69
+
70
+ expect(controller.permitted_params).to have_key "strong_tags"
71
+ expect(controller.permitted_params).not_to have_key "tags"
72
+
73
+ expect(controller.permitted_params).not_to have_key "strong_rate"
74
+ expect(controller.permitted_params).not_to have_key "rate"
75
+
76
+ expect(controller.permitted_params).to have_key "strong_attachment"
77
+ expect(controller.permitted_params).not_to have_key "attachment"
78
+
79
+ expect(controller.permitted_params).to have_key "strong_zip_code"
80
+ expect(controller.permitted_params).not_to have_key "zip_code"
81
+
82
+ expect(controller.permitted_params).to have_key "strong_custom"
83
+ expect(controller.permitted_params).not_to have_key "custom"
84
+
85
+ expect(controller.permitted_params).to have_key "strong_nested"
86
+ expect(controller.permitted_params).not_to have_key "nested"
87
+
88
+ expect(controller.permitted_params).to have_key "strong_numbers"
89
+ expect(controller.permitted_params).not_to have_key "numbers"
90
+
91
+ expect(controller.permitted_params).to have_key "strong_body"
92
+ expect(controller.permitted_params).not_to have_key "body"
93
+
94
+ expect(controller.permitted_params[:strong_body]).to have_key "items"
95
+
96
+ expect(controller.permitted_params[:strong_body][:items].first).to have_key "strong_price"
97
+ expect(controller.permitted_params[:strong_body][:items].first).not_to have_key "name"
98
+ end
99
+ end
100
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weak_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryo Nakamura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-02 00:00:00.000000000 Z
11
+ date: 2016-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -155,7 +155,12 @@ files:
155
155
  - spec/dummy/Rakefile
156
156
  - spec/dummy/app/assets/javascripts/application.js
157
157
  - spec/dummy/app/assets/stylesheets/application.css
158
+ - spec/dummy/app/controllers/api/recipes_controller.rb
159
+ - spec/dummy/app/controllers/api/strongs_controller.rb
160
+ - spec/dummy/app/controllers/api_controller.rb
158
161
  - spec/dummy/app/controllers/application_controller.rb
162
+ - spec/dummy/app/controllers/concerns/with_recipe.rb
163
+ - spec/dummy/app/controllers/concerns/with_strong.rb
159
164
  - spec/dummy/app/controllers/recipes_controller.rb
160
165
  - spec/dummy/app/controllers/strongs_controller.rb
161
166
  - spec/dummy/app/helpers/application_helper.rb
@@ -188,6 +193,8 @@ files:
188
193
  - spec/dummy/public/500.html
189
194
  - spec/dummy/public/favicon.ico
190
195
  - spec/dummy/script/rails
196
+ - spec/requests/api/recipes_spec.rb
197
+ - spec/requests/api/strong_spec.rb
191
198
  - spec/requests/recipes_spec.rb
192
199
  - spec/requests/strong_spec.rb
193
200
  - spec/spec_helper.rb
@@ -220,7 +227,12 @@ test_files:
220
227
  - spec/dummy/Rakefile
221
228
  - spec/dummy/app/assets/javascripts/application.js
222
229
  - spec/dummy/app/assets/stylesheets/application.css
230
+ - spec/dummy/app/controllers/api/recipes_controller.rb
231
+ - spec/dummy/app/controllers/api/strongs_controller.rb
232
+ - spec/dummy/app/controllers/api_controller.rb
223
233
  - spec/dummy/app/controllers/application_controller.rb
234
+ - spec/dummy/app/controllers/concerns/with_recipe.rb
235
+ - spec/dummy/app/controllers/concerns/with_strong.rb
224
236
  - spec/dummy/app/controllers/recipes_controller.rb
225
237
  - spec/dummy/app/controllers/strongs_controller.rb
226
238
  - spec/dummy/app/helpers/application_helper.rb
@@ -253,6 +265,8 @@ test_files:
253
265
  - spec/dummy/public/500.html
254
266
  - spec/dummy/public/favicon.ico
255
267
  - spec/dummy/script/rails
268
+ - spec/requests/api/recipes_spec.rb
269
+ - spec/requests/api/strong_spec.rb
256
270
  - spec/requests/recipes_spec.rb
257
271
  - spec/requests/strong_spec.rb
258
272
  - spec/spec_helper.rb