diff --git a/app/controllers/sample_types_controller.rb b/app/controllers/sample_types_controller.rb index e5645ff3b4..7cb6b3ec82 100644 --- a/app/controllers/sample_types_controller.rb +++ b/app/controllers/sample_types_controller.rb @@ -227,6 +227,7 @@ def update_sample_json_metadata def check_if_locked @sample_type ||= SampleType.find(params[:id]) + @sample_type.reload return unless @sample_type&.locked? error_message = 'This sample type is locked and cannot be edited right now.' diff --git a/test/functional/sample_types_controller_test.rb b/test/functional/sample_types_controller_test.rb index 56482357e5..d45b6cae6e 100644 --- a/test/functional/sample_types_controller_test.rb +++ b/test/functional/sample_types_controller_test.rb @@ -41,7 +41,7 @@ class SampleTypesControllerTest < ActionController::TestCase assert_enqueued_with(job: SampleTypeUpdateJob) do assert_difference('ActivityLog.count') do assert_difference('SampleType.count') do - assert_difference('Task.count') do + assert_difference('Task.where(key: "template_generation").count') do post :create, params: { sample_type: { title: 'Hello!', project_ids: @project_ids, description: 'The description!!', @@ -787,8 +787,8 @@ class SampleTypesControllerTest < ActionController::TestCase assert_response :success end - # Locking the sample type - @sample_type.set_lock + # lock the sample type by adding a fake update task + UpdateSampleMetadataJob.perform_later(@sample_type, @person.user, []) assert @sample_type.locked? %i[edit manage].each do |action| diff --git a/test/functional/samples_controller_test.rb b/test/functional/samples_controller_test.rb index a19271f42f..94a06d3323 100644 --- a/test/functional/samples_controller_test.rb +++ b/test/functional/samples_controller_test.rb @@ -1591,10 +1591,9 @@ def rdf_test_object sample_type = FactoryBot.create(:simple_sample_type, contributor: person, project_ids: [project.id]) - get :new, params: { sample_type_id: sample_type.id } - assert_response :success + # lock the sample type by adding a fake update task + UpdateSampleMetadataJob.perform_later(sample_type, person.user, []) - sample_type.set_lock get :new, params: { sample_type_id: sample_type.id } assert_redirected_to sample_types_path(sample_type) assert_equal flash[:error], 'This sample type is locked. You cannot edit the sample.' diff --git a/test/unit/sample_test.rb b/test/unit/sample_test.rb index 37799b1ed6..3daf68c5d5 100644 --- a/test/unit/sample_test.rb +++ b/test/unit/sample_test.rb @@ -1456,8 +1456,11 @@ class SampleTest < ActiveSupport::TestCase end test 'add sample to a locked sample type' do - sample_type = FactoryBot.create(:simple_sample_type, project_ids: [FactoryBot.create(:project).id], contributor: FactoryBot.create(:person)) - sample_type.set_lock + person = FactoryBot.create(:person) + sample_type = FactoryBot.create(:simple_sample_type, project_ids: [FactoryBot.create(:project).id], contributor: person) + + # lock the sample type by adding a fake update task + UpdateSampleMetadataJob.perform_later(sample_type, person.user, []) assert sample_type.locked? assert_no_difference('Sample.count') do diff --git a/test/unit/sample_type_test.rb b/test/unit/sample_type_test.rb index c506a74453..f5b7f93817 100644 --- a/test/unit/sample_type_test.rb +++ b/test/unit/sample_type_test.rb @@ -6,11 +6,6 @@ def setup @person = FactoryBot.create(:person) @project = @person.projects.first @project_ids = [@project.id] - Rails.cache.clear - end - - def teardown - Rails.cache.clear end test 'validation' do @@ -695,7 +690,7 @@ def teardown assert sample_type.new_record? refute sample_type.template_generation_task.pending? - assert_difference('Task.count', 1) do + assert_difference('Task.where(key: "template_generation").count', 1) do assert_enqueued_with(job: SampleTemplateGeneratorJob, args: [sample_type]) do disable_authorization_checks { sample_type.save! } assert sample_type.reload.template_generation_task.pending? @@ -1063,25 +1058,15 @@ def teardown test 'sample type is locked?' do sample_type = FactoryBot.create(:simple_sample_type, project_ids: @project_ids, contributor: @person) refute sample_type.locked? - sample_type.set_lock + + # lock the sample type by adding a fake update task + UpdateSampleMetadataJob.perform_later(sample_type, @person.user, []) + assert sample_type.locked? refute sample_type.valid? assert sample_type.errors.added?(:base, 'This sample type is locked and cannot be edited right now.') end - test 'update locked sample type' do - sample_type = FactoryBot.create(:simple_sample_type, project_ids: @project_ids, contributor: @person) - (1..10).each do |i| - FactoryBot.create(:sample, sample_type: sample_type, project_ids: @project_ids, title: "Sample #{i}") - end - sample_type.with_lock do - # sample_type.samples << FactoryBot.create(:sample, sample_type: sample_type, project_ids: @project_ids, title: 'Sample 11') - # sample_type.title = 'Updated title' - sample_type.sample_attributes.first.title = 'Updated title' - sample_type.save! - end - end - private # sample type with 3 samples