diff --git a/app/models/log.rb b/app/models/log.rb index 452aa67c6..d10e529e3 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -9,7 +9,8 @@ class Log < ApplicationRecord belongs_to :bulk_upload, optional: true before_save :update_status! - before_validation :verify_data_protection_confirmation, on: :create + before_validation :data_protection_confirmation_signed?, on: :create + validate :data_protection_confirmation_signed?, on: :update, if: :owning_organisation_id_changed? STATUS = { "not_started" => 0, @@ -178,12 +179,12 @@ class Log < ApplicationRecord private - def verify_data_protection_confirmation + def data_protection_confirmation_signed? return unless FeatureToggle.new_data_protection_confirmation? return unless owning_organisation return if owning_organisation.data_protection_confirmed? - errors.add :owning_organisation, I18n.t("validations.organisation.data_sharing_agreement_not_signed") + errors.add :owning_organisation_id, I18n.t("validations.organisation.data_sharing_agreement_not_signed") end # Handle logs that are older than previous collection start date diff --git a/config/locales/en.yml b/config/locales/en.yml index f5d8a56b7..35b602c4d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -177,7 +177,7 @@ en: validations: organisation: - data_sharing_agreement_not_signed: Your organisation must accept the Data Sharing Agreement before you can create any logs. + data_sharing_agreement_not_signed: The organisation must accept the Data Sharing Agreement before it can be selected as the managing organisation. name_missing: "Enter the name of the organisation" provider_type_missing: "Select the organisation type" stock_owner: diff --git a/spec/shared/shared_log_examples.rb b/spec/shared/shared_log_examples.rb index 1a2629ba2..9d6805150 100644 --- a/spec/shared/shared_log_examples.rb +++ b/spec/shared/shared_log_examples.rb @@ -105,12 +105,12 @@ RSpec.shared_examples "shared log examples" do |log_type| end end - describe "#verify_data_protection_confirmation" do + describe "#data_protection_confirmation_signed?" do before do allow(FeatureToggle).to receive(:new_data_protection_confirmation?).and_return(false) end - it "is valid if the DSA is signed" do + it "is valid if the Data Protection Confirmation is signed" do log = build(log_type, :in_progress, owning_organisation: create(:organisation)) expect(log).to be_valid @@ -122,7 +122,7 @@ RSpec.shared_examples "shared log examples" do |log_type| expect(log).to be_valid end - it "is not valid if the DSA is not signed" do + it "is not valid if the Data Protection Confirmation is not signed" do log = build(log_type, owning_organisation: create(:organisation, :without_dpc)) expect(log).to be_valid @@ -134,7 +134,7 @@ RSpec.shared_examples "shared log examples" do |log_type| allow(FeatureToggle).to receive(:new_data_protection_confirmation?).and_return(true) end - it "is valid if the DSA is signed" do + it "is valid if the Data Protection Confirmation is signed" do log = build(log_type, :in_progress, owning_organisation: create(:organisation)) expect(log).to be_valid @@ -146,11 +146,25 @@ RSpec.shared_examples "shared log examples" do |log_type| expect(log).to be_valid end - it "is not valid if the DSA is not signed" do + it "is not valid if the Data Protection Confirmation is not signed" do log = build(log_type, owning_organisation: create(:organisation, :without_dpc)) expect(log).not_to be_valid - expect(log.errors[:owning_organisation]).to eq(["Your organisation must accept the Data Sharing Agreement before you can create any logs."]) + expect(log.errors[:owning_organisation_id]).to eq(["The organisation must accept the Data Sharing Agreement before it can be selected as the managing organisation."]) + end + + context "when updating" do + let(:log) { create(log_type, :in_progress) } + let(:org_with_dpc) { create(:organisation) } + let(:org_without_dpc) { create(:organisation, :without_dpc) } + + it "is valid when changing to another org with a signed Data Protection Confirmation" do + expect { log.owning_organisation = org_with_dpc }.not_to change(log, :valid?) + end + + it "invalid when changing to another org without a signed Data Protection Confirmation" do + expect { log.owning_organisation = org_without_dpc }.to change(log, :valid?).from(true).to(false) + end end end end