Skip to content

Commit 7783500

Browse files
committed
add more server side validations to observation model
1 parent b1095ae commit 7783500

4 files changed

Lines changed: 67 additions & 6 deletions

File tree

app/models/observation.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ class Observation < ApplicationRecord
172172

173173
validates :validation_status, presence: true
174174

175+
with_options unless: -> { Created? || Rejected? } do
176+
validates :subcategory, presence: true
177+
validates :severity, presence: true
178+
validates :observation_report, presence: true
179+
validates :evidence_type, presence: true
180+
end
181+
175182
before_validation :assign_observers_from_report, if: :observation_report_changed?
176183
before_validation :nullify_evidence_on_report, if: -> { evidence_type != "Evidence presented in the report" }
177184

spec/factories/observations.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@
4848
subcategory
4949
observation_report
5050
law
51-
user { build(:admin) }
51+
user { build(:ngo_manager) }
5252
severity { build(:severity, subcategory: subcategory) }
5353
operator { create(:operator, country: country) }
5454
observation_type { "operator" }
5555
is_active { true }
5656
validation_status { "Published (no comments)" }
57+
evidence_type { "No evidence" }
5758
is_physical_place { true }
5859
non_concession_activity { false }
5960
lng { 12.2222 }
@@ -67,19 +68,26 @@
6768
validation_status { "Created" }
6869
end
6970

70-
after(:create) do |doc, evaluator|
71-
doc.update(validation_status: evaluator.force_status) if evaluator.force_status
71+
after(:build) do |observation|
72+
# TODO: maybe that should be in the model?
73+
observation.observers = [observation.user.observer] if observation.observers.empty? || observation.observation_report.nil?
74+
end
75+
76+
after(:create) do |observation, evaluator|
77+
observation.update(validation_status: evaluator.force_status) if evaluator.force_status
7278
end
7379
end
7480

7581
factory :gov_observation, class: "Observation" do
76-
severity
7782
country
83+
subcategory { build(:subcategory) }
84+
severity { build(:severity, subcategory: subcategory) }
85+
observation_report { build(:observation_report) }
7886
governments { build_list(:government, 2) }
79-
observers { build_list(:observer, 1) }
8087
user { build(:admin) }
8188
observation_type { "government" }
8289
validation_status { "Published (no comments)" }
90+
evidence_type { "No evidence" }
8391
is_active { true }
8492
publication_date { DateTime.now.yesterday.to_date }
8593
end

spec/integration/v1/governments_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module V1
5050

5151
before do
5252
create_list(:government, 3, country: country) # those should not be returned
53-
create(:gov_observation, observers: [observer], governments: [gov])
53+
create(:gov_observation, observation_report: build(:observation_report, observers: [observer]), governments: [gov])
5454
end
5555

5656
it "returns only governments linked with observer observations" do

spec/models/observation_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,52 @@
267267
end
268268
end
269269
end
270+
271+
describe "government observation validations" do
272+
it "should be invalid when government observation has operator_id" do
273+
country = create(:country)
274+
observation = build(:gov_observation, country: country)
275+
observation.operator = create(:operator, country: country)
276+
277+
expect(observation.valid?).to eq(false)
278+
expect(observation.errors[:operator_id]).to include("must be blank")
279+
end
280+
end
281+
282+
describe "required fields for non-Created and non-Rejected statuses" do
283+
it "allows missing required fields when status is Created or Rejected" do
284+
created_obs = build(:observation,
285+
validation_status: "Created",
286+
subcategory: nil,
287+
severity: nil,
288+
observation_report: nil,
289+
evidence_type: nil)
290+
expect(created_obs).to be_valid
291+
292+
rejected_obs = build(:observation,
293+
validation_status: "Rejected",
294+
subcategory: nil,
295+
severity: nil,
296+
observation_report: nil,
297+
evidence_type: nil)
298+
expect(rejected_obs).to be_valid
299+
end
300+
301+
it "requires subcategory, severity, observation_report, and evidence_type for other statuses" do
302+
observation = build(:observation,
303+
validation_status: "Ready for QC2",
304+
subcategory: nil,
305+
severity: nil,
306+
observation_report: nil,
307+
evidence_type: nil)
308+
309+
expect(observation.valid?).to eq(false)
310+
expect(observation.errors[:subcategory]).to include("can't be blank")
311+
expect(observation.errors[:severity]).to include("can't be blank")
312+
expect(observation.errors[:observation_report]).to include("can't be blank")
313+
expect(observation.errors[:evidence_type]).to include("can't be blank")
314+
end
315+
end
270316
end
271317

272318
describe "Hooks" do

0 commit comments

Comments
 (0)