Skip to content

Commit f621ece

Browse files
committed
Personalized forms checks type before insert a field
1 parent 2fa781d commit f621ece

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

controllers/reviews/fields.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@
3030

3131
@campo_previo=@review.fields.where(:name=>name)
3232
if @campo_previo.empty?
33-
SrField.insert(:systematic_review_id=>rs_id, :order=>params['order'],:name=>name, :description=>params['description'], :type=>params['type'].chomp,:options=>params['options'])
33+
34+
type=params['type'].chomp
35+
36+
halt 500, "Not valid type #{type}" unless SrField.is_valid_type?(type)
37+
38+
SrField.insert(:systematic_review_id=>rs_id, :order=>params['order'],:name=>name, :description=>params['description'], :type=>type,:options=>params['options'])
3439
add_message(t('sr_new_sr_edit_field.doesnt_existfield.success', name:params['name']))
3540

3641
else

model/rs_campo.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
class SrField < Sequel::Model
3030
AVAILABLE_TYPES=[:text,:textarea,:select,:multiple]
31-
31+
def self.is_valid_type?(type)
32+
AVAILABLE_TYPES.include? type.to_s.chomp.to_sym
33+
end
3234
def self.types_a_sequel(campo)
3335
if campo[:type] == 'text'
3436
[campo[:name].to_sym, String, null: true]

spec/personalized_form_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
def create_field_1
1111
post '/review/1/new_field', name:'field_1', order:1, description:'First question', type:'text' ,options:''
1212
end
13+
1314
def create_field_2
1415
post '/review/1/new_field', name:'field_2', order:2, description:'Second question', type:'text' ,options:''
1516
end
17+
def bad_field_3
18+
post '/review/1/new_field', name:'field_3', order:3, description:'First question', type:'no_type' ,options:''
19+
end
20+
1621
def delete_all_fields
1722

1823
end
@@ -34,6 +39,21 @@ def delete_all_fields
3439
expect(SrField.where(:systematic_review_id=>1,:name=>'field_1').count).to eq(1)
3540
end
3641
end
42+
context "when we add a new text field with wrong type" do
43+
before(:context) do
44+
bad_field_3
45+
end
46+
it "should not be redirect" do
47+
expect(last_response).to_not be_redirect
48+
end
49+
it "should be status 500" do
50+
expect(last_response.status).to eq(500)
51+
end
52+
53+
it "should not add a new field on database" do
54+
expect(SrField.where(:systematic_review_id=>1,:name=>'field_3').count).to eq(0)
55+
end
56+
end
3757

3858
context "when we update the text field" do
3959
before(:context) do

0 commit comments

Comments
 (0)