Metrics/LineLength: Line is too long. RSpec/InstanceVariable: Use let instead of an instance variable. Layout/TrailingBlankLines: Final newline missing. Style/StringLiterals: Prefer double-quoted strings.
168 lines
7.3 KiB
Ruby
168 lines
7.3 KiB
Ruby
require "rails_helper"
|
|
|
|
describe Legislation::AnnotationsController do
|
|
|
|
describe "POST create" do
|
|
|
|
let(:legal_process) { create(:legislation_process,
|
|
allegations_start_date: Date.current - 3.days,
|
|
allegations_end_date: Date.current + 2.days) }
|
|
let(:draft_version) { create(:legislation_draft_version, :published,
|
|
process: legal_process,
|
|
title: "Version 1") }
|
|
let(:final_version) { create(:legislation_draft_version, :published,
|
|
:final_version,
|
|
process: legal_process,
|
|
title: "Final version") }
|
|
let(:user) { create(:user, :level_two) }
|
|
|
|
|
|
it "creates an ahoy event" do
|
|
sign_in user
|
|
|
|
post :create, params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: draft_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges" => [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
expect(Ahoy::Event.where(name: :legislation_annotation_created).count).to eq 1
|
|
expect(Ahoy::Event.last.properties["legislation_annotation_id"]).to eq Legislation::Annotation.last.id
|
|
end
|
|
|
|
it "does not create an annotation if the draft version is a final version" do
|
|
sign_in user
|
|
|
|
post :create, params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: final_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges" => [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
|
|
expect(response).to have_http_status(:not_found)
|
|
end
|
|
|
|
it "creates an annotation if the process allegations phase is open" do
|
|
sign_in user
|
|
|
|
expect do
|
|
post :create, xhr: true,
|
|
params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: draft_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges" => [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
end.to change { draft_version.annotations.count }.by(1)
|
|
end
|
|
|
|
it "does not create an annotation if the process allegations phase is not open" do
|
|
sign_in user
|
|
legal_process.update_attribute(:allegations_end_date, Date.current - 1.day)
|
|
|
|
expect do
|
|
post :create, xhr: true,
|
|
params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: draft_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges"=> [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
end.to_not change { draft_version.annotations.count }
|
|
end
|
|
|
|
it "creates an annotation by parsing parameters in JSON" do
|
|
sign_in user
|
|
|
|
expect do
|
|
post :create, xhr: true,
|
|
params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: draft_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges" => [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}].to_json,
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
end.to change { draft_version.annotations.count }.by(1)
|
|
end
|
|
|
|
it "creates a new comment on an existing annotation when range is the same" do
|
|
annotation = create(:legislation_annotation, draft_version: draft_version,
|
|
text: "my annotation",
|
|
ranges: [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
range_start: "/p[1]",
|
|
range_start_offset: 6,
|
|
range_end: "/p[1]",
|
|
range_end_offset: 11)
|
|
sign_in user
|
|
|
|
expect do
|
|
post :create, xhr: true,
|
|
params: {
|
|
process_id: legal_process.id,
|
|
draft_version_id: draft_version.id,
|
|
legislation_annotation: {
|
|
"quote" => "ipsum",
|
|
"ranges" => [{
|
|
"start" => "/p[1]",
|
|
"startOffset" => 6,
|
|
"end" => "/p[1]",
|
|
"endOffset" => 11
|
|
}],
|
|
"text" => "una anotacion"
|
|
}
|
|
}
|
|
end.to_not change { draft_version.annotations.count }
|
|
|
|
expect(annotation.reload.comments_count).to eq(2)
|
|
expect(annotation.comments.last.body).to eq("una anotacion")
|
|
end
|
|
|
|
end
|
|
end
|