Files
grecia/spec/controllers/legislation/annotations_controller_spec.rb
Javi Martín 97e826f2a4 Don't use update_attribute
This method is ambiguous. Sometimes we use it to set invalid data in
tests (which can usually be done with `update_column`), and other times
we use it instead of `update!`.

I'm removing it because, even if sometimes it could make sense to use
it, it's too similar to `update_attributes` (which is an alias for
`update` and runs validations), making it confusing.

However, there's one case where we're still using it: in the
ActsAsParanoidAliases module, we need to invoke the callbacks, which
`update_column` skips, but tests related to translations fail if we use
`update!`. The reason for this is the tests check what happens if we
restore a record without restoring its translations. But that will make
the record invalid, since there's a validation rule checking it has at
least one translation.

I'm not blacklisting any other method which skips validations because we
know they skip validations and use them anyway (hopefully with care).
2019-10-25 23:17:50 +02:00

165 lines
6.9 KiB
Ruby

require "rails_helper"
describe Legislation::AnnotationsController do
describe "POST create" do
let(:legal_process) do
create(:legislation_process, allegations_start_date: Date.current - 3.days,
allegations_end_date: Date.current + 2.days)
end
let(:draft_version) do
create(:legislation_draft_version, :published, process: legal_process, title: "Version 1")
end
let(:final_version) do
create(:legislation_draft_version, :published, :final_version,
process: legal_process, title: "Final version")
end
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!(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.not_to 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.not_to 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