From 8111d523ce3325d4d67ca5210532345344cb5494 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 12 Sep 2015 12:51:31 +0200 Subject: [PATCH 1/3] adds Proposal's model spec --- spec/models/proposal_spec.rb | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/models/proposal_spec.rb diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb new file mode 100644 index 000000000..b4eaeeb56 --- /dev/null +++ b/spec/models/proposal_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +describe Proposal do + let(:proposal) { build(:proposal) } + + it "should be valid" do + expect(proposal).to be_valid + end + + it "should not be valid without an author" do + proposal.author = nil + expect(proposal).to_not be_valid + end + + it "should not be valid without a title" do + proposal.title = nil + expect(proposal).to_not be_valid + end + + describe "#description" do + it "should be mandatory" do + proposal.description = nil + expect(proposal).to_not be_valid + end + + it "should be sanitized" do + proposal.description = "" + proposal.valid? + expect(proposal.description).to eq("alert('danger');") + end + end + + it "should sanitize the tag list" do + proposal.tag_list = "user_id=1" + proposal.valid? + expect(proposal.tag_list).to eq(['user_id1']) + end + + it "should not be valid without accepting terms of service" do + proposal.terms_of_service = nil + expect(proposal).to_not be_valid + end +end \ No newline at end of file From 26113cac962f839698044acfa6faaad642ced127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 12 Sep 2015 12:53:22 +0200 Subject: [PATCH 2/3] adds setting value check in Proposal#editable? --- app/models/proposal.rb | 2 +- spec/models/proposal_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/proposal.rb b/app/models/proposal.rb index b92ab6ef3..8f59585e4 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -63,7 +63,7 @@ class Proposal < ActiveRecord::Base end def editable? - total_votes <= 1000 + total_votes <= Setting.value_for("max_votes_for_proposal_edit").to_i end def editable_by?(user) diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index b4eaeeb56..3200c7123 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -40,4 +40,26 @@ describe Proposal do proposal.terms_of_service = nil expect(proposal).to_not be_valid end + + describe "#editable?" do + let(:proposal) { create(:proposal) } + before(:each) {Setting.find_by(key: "max_votes_for_proposal_edit").update(value: 100)} + + it "should be true if proposal has no votes yet" do + expect(proposal.total_votes).to eq(0) + expect(proposal.editable?).to be true + end + + it "should be true if proposal has less than limit votes" do + create_list(:vote, 91, votable: proposal) + expect(proposal.total_votes).to eq(91) + expect(proposal.editable?).to be true + end + + it "should be false if proposal has more than limit votes" do + create_list(:vote, 102, votable: proposal) + expect(proposal.total_votes).to eq(102) + expect(proposal.editable?).to be false + end + end end \ No newline at end of file From 8df5ed0e02b4457e803992efe0303193a68dcb14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Sat, 12 Sep 2015 12:55:08 +0200 Subject: [PATCH 3/3] adds test for question presence --- spec/models/proposal_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index 3200c7123..52896e0da 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -12,6 +12,11 @@ describe Proposal do expect(proposal).to_not be_valid end + it "should not be valid without an question" do + proposal.question = nil + expect(proposal).to_not be_valid + end + it "should not be valid without a title" do proposal.title = nil expect(proposal).to_not be_valid