Merge branch 'legislation-module-stable' of https://github.com/medialab-prado/consul into medialab-legislation

This commit is contained in:
decabeza
2017-05-22 18:43:49 +02:00
177 changed files with 10710 additions and 125 deletions

View File

@@ -12,6 +12,7 @@ describe "Abilities::Administrator" do
let(:debate) { create(:debate) }
let(:comment) { create(:comment) }
let(:proposal) { create(:proposal) }
let(:legislation_question) { create(:legislation_question) }
let(:hidden_debate) { create(:debate, :hidden) }
let(:hidden_comment) { create(:comment, :hidden) }
@@ -50,6 +51,9 @@ describe "Abilities::Administrator" do
it { should be_able_to(:comment_as_administrator, proposal) }
it { should_not be_able_to(:comment_as_moderator, proposal) }
it { should be_able_to(:comment_as_administrator, legislation_question) }
it { should_not be_able_to(:comment_as_moderator, legislation_question) }
it { should be_able_to(:manage, Annotation) }
it { should be_able_to(:read, SpendingProposal) }

View File

@@ -11,6 +11,7 @@ describe "Abilities::Moderator" do
let(:debate) { create(:debate) }
let(:comment) { create(:comment) }
let(:proposal) { create(:proposal) }
let(:legislation_question) { create(:legislation_question) }
let(:own_debate) { create(:debate, author: user) }
let(:own_comment) { create(:comment, author: user) }
@@ -101,7 +102,9 @@ describe "Abilities::Moderator" do
it { should be_able_to(:comment_as_moderator, debate) }
it { should be_able_to(:comment_as_moderator, proposal) }
it { should be_able_to(:comment_as_moderator, legislation_question) }
it { should_not be_able_to(:comment_as_administrator, debate) }
it { should_not be_able_to(:comment_as_administrator, proposal) }
it { should_not be_able_to(:comment_as_administrator, legislation_question) }
end
end

View File

@@ -0,0 +1,47 @@
require 'rails_helper'
RSpec.describe Legislation::Annotation, type: :model do
let(:draft_version) { create(:legislation_draft_version) }
let(:annotation) { create(:legislation_annotation, draft_version: draft_version) }
it "should be valid" do
expect(draft_version).to be_valid
expect(annotation).to be_valid
end
it "calculates the context for multinode annotations" do
annotation = create(:legislation_annotation,
draft_version: draft_version,
quote: "ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
Expetenda tincidunt in sed, ex partem placerat sea, porro commodo ex eam. His putant aeterno interesset at. Usu ea mundi tincidunt, omnium virtute aliquando ius ex. Ea aperiri sententiae duo. Usu nullam dolorum quaestio ei, sit vidit facilisis ea. Per ne impedit iracundia neglegentur. Consetetur neglegentur eum ut, vis animal legimus inimicus id.
His audiam",
ranges: [{"start"=>"/p[1]", "startOffset"=>6, "end"=>"/p[3]", "endOffset"=>11}]
)
expect(annotation.context).to eq("Lorem <span class=annotator-hl>ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\n\nExpetenda tincidunt in sed, ex partem placerat sea, porro commodo ex eam. His putant aeterno interesset at. Usu ea mundi tincidunt, omnium virtute aliquando ius ex. Ea aperiri sententiae duo. Usu nullam dolorum quaestio ei, sit vidit facilisis ea. Per ne impedit iracundia neglegentur. Consetetur neglegentur eum ut, vis animal legimus inimicus id.\n\nHis audiam</span>deserunt in, eum ubique voluptatibus te. In reque dicta usu. Ne rebum dissentiet eam, vim omnis deseruisse id. Ullum deleniti vituperata at quo, insolens complectitur te eos, ea pri dico munere propriae. Vel ferri facilis ut, qui paulo ridens praesent ad. Possim alterum qui cu. Accusamus consulatu ius te, cu decore soleat appareat usu.")
end
it "calculates the context for multinode annotations 2" do
annotation = create(:legislation_annotation,
draft_version: draft_version,
quote: "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\r\n\r\nExpetenda tincidunt in sed, ex partem placerat sea, porro commodo ex eam. His putant aeterno interesset at. Usu ea mundi tincidunt, omnium virtute aliquando ius ex. Ea aperiri sententiae duo",
ranges: [{"start"=>"/p[1]", "startOffset"=>273, "end"=>"/p[2]", "endOffset"=>190}]
)
expect(annotation.context).to eq("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. <span class=annotator-hl>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\r\n\r\nExpetenda tincidunt in sed, ex partem placerat sea, porro commodo ex eam. His putant aeterno interesset at. Usu ea mundi tincidunt, omnium virtute aliquando ius ex. Ea aperiri sententiae duo</span>. Usu nullam dolorum quaestio ei, sit vidit facilisis ea. Per ne impedit iracundia neglegentur. Consetetur neglegentur eum ut, vis animal legimus inimicus id.")
end
it "calculates the context for multinode annotations 3" do
draft_version = create(:legislation_draft_version, body: "The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software.\r\n\r\nThe licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.\r\n\r\nWhen we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.\r\n\r\nDevelopers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software.\r\n\r\nA secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public.\r\n\r\nThe GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version.\r\n\r\nAn older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license.")
annotation = create(:legislation_annotation,
draft_version: draft_version,
quote: "By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.\r\n\r\nWhen we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish)",
ranges: [{"start"=>"/p[2]", "startOffset"=>127, "end"=>"/p[3]", "endOffset"=>223}]
)
expect(annotation.context).to eq("The licenses for most software and other practical works are designed to take away your freedom to share and change the works. <span class=annotator-hl>By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.\r\n\r\nWhen we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish)</span>, that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.")
end
end

View File

@@ -0,0 +1,39 @@
require 'rails_helper'
RSpec.describe Legislation::Answer, type: :model do
let(:legislation_answer) { build(:legislation_answer) }
it "should be valid" do
expect(legislation_answer).to be_valid
end
it "counts answers" do
question = create(:legislation_question)
option_1 = create(:legislation_question_option, question: question, value: 'Yes')
option_2 = create(:legislation_question_option, question: question, value: 'No')
answer = create(:legislation_answer, question: question, question_option: option_2)
expect(answer).to be_valid
expect(question.answers_count).to eq 1
expect(option_2.answers_count).to eq 1
expect(option_1.answers_count).to eq 0
end
it "can't answer same question more than once" do
question = create(:legislation_question)
option_1 = create(:legislation_question_option, question: question, value: 'Yes')
option_2 = create(:legislation_question_option, question: question, value: 'No')
user = create(:user)
answer = create(:legislation_answer, question: question, question_option: option_2, user: user)
expect(answer).to be_valid
second_answer = build(:legislation_answer, question: question, question_option: option_1, user: user)
expect(second_answer).to be_invalid
expect(question.answers_count).to eq 1
expect(option_2.answers_count).to eq 1
expect(option_1.answers_count).to eq 0
end
end

View File

@@ -0,0 +1,80 @@
require 'rails_helper'
RSpec.describe Legislation::DraftVersion, type: :model do
let(:legislation_draft_version) { build(:legislation_draft_version) }
it "should be valid" do
expect(legislation_draft_version).to be_valid
end
it "renders and saves the html from the markdown body field" do
legislation_draft_version.body = body_markdown
legislation_draft_version.save!
expect(legislation_draft_version.body_html).to eq(body_html)
expect(legislation_draft_version.toc_html).to eq(toc_html)
end
def body_markdown
<<-BODY_MARKDOWN
# Title 1
Some paragraph.
A list:
- item 1
- item 2
## Subtitle
Another paragraph.
# Title 2
Something about this.
BODY_MARKDOWN
end
def body_html
<<-BODY_HTML
<h1 id="title-1">Title 1</h1>
<p>Some paragraph.</p>
<p>A list:</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
<h2 id="subtitle">Subtitle</h2>
<p>Another paragraph.</p>
<h1 id="title-2">Title 2</h1>
<p>Something about this.</p>
BODY_HTML
end
def toc_html
<<-TOC_HTML
<ul>
<li>
<a href="#title-1">Title 1</a>
<ul>
<li>
<a href="#subtitle">Subtitle</a>
</li>
</ul>
</li>
<li>
<a href="#title-2">Title 2</a>
</li>
</ul>
TOC_HTML
end
end

View File

@@ -0,0 +1,251 @@
require 'rails_helper'
RSpec.describe Legislation::Process, type: :model do
let(:process) { create(:legislation_process) }
it "should be valid" do
expect(process).to be_valid
end
describe "dates validations" do
it "is invalid if debate_start_date is present but debate_end_date is not" do
process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but debate_start_date is not" do
process = build(:legislation_process, debate_start_date: nil, debate_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:debate_start_date]).to include("can't be blank")
end
it "is invalid if allegations_start_date is present but debate_end_date is not" do
process = build(:legislation_process, allegations_start_date: Date.current, allegations_end_date: "")
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date]).to include("can't be blank")
end
it "is invalid if debate_end_date is present but allegations_start_date is not" do
process = build(:legislation_process, allegations_start_date: nil, allegations_end_date: Date.current)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_start_date]).to include("can't be blank")
end
end
describe "date ranges validations" do
it "is invalid if end_date is before start_date" do
process = build(:legislation_process, start_date: Date.current, end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:end_date]).to include("must be on or after the start date")
end
it "is valid if end_date is the same as start_date" do
process = build(:legislation_process, start_date: Date.current - 1.day, end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if debate_end_date is before debate start_date" do
process = build(:legislation_process, debate_start_date: Date.current, debate_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:debate_end_date]).to include("must be on or after the debate start date")
end
it "is valid if debate_end_date is the same as debate_start_date" do
process = build(:legislation_process, debate_start_date: Date.current - 1.day, debate_end_date: Date.current - 1.day)
expect(process).to be_valid
end
it "is invalid if allegations_end_date is before allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current, allegations_end_date: Date.current - 1.day)
expect(process).to be_invalid
expect(process.errors.messages[:allegations_end_date]).to include("must be on or after the allegations start date")
end
it "is valid if allegations_end_date is the same as allegations_start_date" do
process = build(:legislation_process, allegations_start_date: Date.current - 1.day, allegations_end_date: Date.current - 1.day)
expect(process).to be_valid
end
end
describe "filter scopes" do
before(:each) do
@process_1 = create(:legislation_process, start_date: Date.current - 2.days, end_date: Date.current + 1.day)
@process_2 = create(:legislation_process, start_date: Date.current + 1.days, end_date: Date.current + 3.days)
@process_3 = create(:legislation_process, start_date: Date.current - 4.days, end_date: Date.current - 3.days)
end
it "filters open" do
open_processes = ::Legislation::Process.open
expect(open_processes).to include(@process_1)
expect(open_processes).to_not include(@process_2)
expect(open_processes).to_not include(@process_3)
end
it "filters next" do
next_processes = ::Legislation::Process.next
expect(next_processes).to include(@process_2)
expect(next_processes).to_not include(@process_1)
expect(next_processes).to_not include(@process_3)
end
it "filters past" do
past_processes = ::Legislation::Process.past
expect(past_processes).to include(@process_3)
expect(past_processes).to_not include(@process_2)
expect(past_processes).to_not include(@process_1)
end
end
describe "#open_phase?" do
it "checks debate phase" do
# future
process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days)
expect(process.open_phase?(:debate)).to be false
# started
process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day)
expect(process.open_phase?(:debate)).to be true
# starts today
process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day)
expect(process.open_phase?(:debate)).to be true
# past
process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day)
expect(process.open_phase?(:debate)).to be false
end
it "checks allegations phase" do
# future
process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days)
expect(process.open_phase?(:allegations)).to be false
# started
process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day)
expect(process.open_phase?(:allegations)).to be true
# starts today
process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day)
expect(process.open_phase?(:allegations)).to be true
# past
process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day)
expect(process.open_phase?(:allegations)).to be false
end
it "checks draft publication phase" do
# future
process.update_attributes(draft_publication_date: Date.current + 2.days)
expect(process.open_phase?(:draft_publication)).to be false
# past
process.update_attributes(draft_publication_date: Date.current - 2.days)
expect(process.open_phase?(:draft_publication)).to be true
# starts today
process.update_attributes(draft_publication_date: Date.current)
expect(process.open_phase?(:draft_publication)).to be true
end
it "checks final version publication phase" do
# future
process.update_attributes(final_publication_date: Date.current + 2.days)
expect(process.open_phase?(:final_version_publication)).to be false
# past
process.update_attributes(final_publication_date: Date.current - 2.days)
expect(process.open_phase?(:final_version_publication)).to be true
# starts today
process.update_attributes(final_publication_date: Date.current)
expect(process.open_phase?(:final_version_publication)).to be true
end
end
describe "#show_phase?" do
it "checks debate phase" do
# future
process.update_attributes(debate_start_date: Date.current + 2.days, debate_end_date: Date.current + 3.days)
expect(process.show_phase?(:debate)).to be false
# started
process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current + 1.day)
expect(process.show_phase?(:debate)).to be true
# starts today
process.update_attributes(debate_start_date: Date.current, debate_end_date: Date.current + 1.day)
expect(process.show_phase?(:debate)).to be true
# past
process.update_attributes(debate_start_date: Date.current - 2.days, debate_end_date: Date.current - 1.day)
expect(process.show_phase?(:debate)).to be true
end
it "checks allegations phase" do
# future
process.update_attributes(allegations_start_date: Date.current + 2.days, allegations_end_date: Date.current + 3.days)
expect(process.show_phase?(:allegations)).to be false
# started
process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current + 1.day)
expect(process.show_phase?(:allegations)).to be true
# starts today
process.update_attributes(allegations_start_date: Date.current, allegations_end_date: Date.current + 1.day)
expect(process.show_phase?(:allegations)).to be true
# past
process.update_attributes(allegations_start_date: Date.current - 2.days, allegations_end_date: Date.current - 1.day)
expect(process.show_phase?(:allegations)).to be true
end
it "checks draft publication phase" do
# future
process.update_attributes(draft_publication_date: Date.current + 2.days)
expect(process.show_phase?(:draft_publication)).to be false
# past
process.update_attributes(draft_publication_date: Date.current - 2.days)
expect(process.show_phase?(:draft_publication)).to be true
# starts today
process.update_attributes(draft_publication_date: Date.current)
expect(process.show_phase?(:draft_publication)).to be true
end
it "checks final version publication phase" do
# future
process.update_attributes(final_publication_date: Date.current + 2.days)
expect(process.show_phase?(:final_version_publication)).to be false
# past
process.update_attributes(final_publication_date: Date.current - 2.days)
expect(process.show_phase?(:final_version_publication)).to be true
# starts today
process.update_attributes(final_publication_date: Date.current)
expect(process.show_phase?(:final_version_publication)).to be true
end
end
describe "#status" do
it "should detect planned phase" do
process.update_attributes(start_date: Date.current + 2.days)
expect(process.status).to eq(:planned)
end
it "should detect closed phase" do
process.update_attributes(end_date: Date.current - 2.days)
expect(process.status).to eq(:closed)
end
it "should detect open phase" do
expect(process.status).to eq(:open)
end
end
end

View File

@@ -0,0 +1,18 @@
require 'rails_helper'
RSpec.describe Legislation::QuestionOption, type: :model do
let(:legislation_question_option) { build(:legislation_question_option) }
it "should be valid" do
expect(legislation_question_option).to be_valid
end
it "should be unique per question" do
question = create(:legislation_question)
valid_question_option = create(:legislation_question_option, question: question, value: "uno")
invalid_question_option = build(:legislation_question_option, question: question, value: "uno")
expect(invalid_question_option).to_not be_valid
end
end

View File

@@ -0,0 +1,62 @@
require 'rails_helper'
RSpec.describe Legislation::Question, type: :model do
let(:question) { create(:legislation_question) }
it "should be valid" do
expect(question).to be_valid
end
context "can be deleted" do
example "when it has no options or answers" do
question = create(:legislation_question)
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
example "when it has options but no answers" do
create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_question_option, question: question, value: "No")
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
example "when it has options and answers" do
option_1 = create(:legislation_question_option, question: question, value: "Yes")
option_2 = create(:legislation_question_option, question: question, value: "No")
create(:legislation_answer, question: question, question_option: option_1)
create(:legislation_answer, question: question, question_option: option_2)
expect do
question.destroy
end.to change { Legislation::Question.count }.by(-1)
end
end
describe "#next_question_id" do
let!(:question1) { create(:legislation_question) }
let!(:question2) { create(:legislation_question, legislation_process_id: question1.legislation_process_id) }
it "should return the next question" do
expect(question1.next_question_id).to eq(question2.id)
end
it "should return nil" do
expect(question2.next_question_id).to be_nil
end
end
describe "#first_question_id" do
let!(:question1) { create(:legislation_question) }
let!(:question2) { create(:legislation_question, legislation_process_id: question1.legislation_process_id) }
it "should return the first question" do
expect(question1.first_question_id).to eq(question1.id)
expect(question2.first_question_id).to eq(question1.id)
end
end
end