Skip to content

Commit

Permalink
Have ActiveFedora respect read-only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcolvar committed Feb 4, 2025
1 parent 2d1b88a commit 59dc021
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/initializers/active_fedora_general.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,9 @@ def initialize(file)
get_values(:type) << self.class.type
end
end

ActiveFedora::Common.module_eval do
def readonly?
@readonly || Settings.repository_read_only_mode
end
end
41 changes: 41 additions & 0 deletions spec/lib/active_fedora_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2011-2024, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
# --- END LICENSE_HEADER BLOCK ---

require 'rails_helper'

describe 'ActiveFedora::Base' do
let(:new_obj) { ActiveFedora::Base.new }
let(:obj) { ActiveFedora::Base.create }

# This tests changes overrides that are in config/initializers/active_fedora_general.rb
context 'when read-only mode' do
before { allow(Settings).to receive(:repository_read_only_mode).and_return(true) }

it 'raises ReadOnlyRecord for any write operation' do
expect { ActiveFedora::Base.create }.to raise_error ActiveFedora::ReadOnlyRecord
expect { new_obj.save }.to raise_error ActiveFedora::ReadOnlyRecord
expect { new_obj.save! }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.save }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.save! }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.update }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.update! }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.delete }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.destroy }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.destroy! }.to raise_error ActiveFedora::ReadOnlyRecord
expect { obj.eradicate }.to raise_error ActiveFedora::ReadOnlyRecord
expect { ActiveFedora::Base.eradicate(obj.to_uri) }.to raise_error ActiveFedora::ReadOnlyRecord
expect { ActiveFedora::Base.delete_tombstone(obj.to_uri) }.to raise_error ActiveFedora::ReadOnlyRecord
end
end
end
3 changes: 2 additions & 1 deletion spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
end

describe 'repository read-only mode' do
let(:media_object) { FactoryBot.create(:media_object) }
# Next line is let! to ensure that it runs before the before block which would stop the object from being created
let!(:media_object) { FactoryBot.create(:media_object) }
let(:media_object_proxy) { SpeedyAF::Base.find(media_object.id) }
let(:collection) { media_object.collection }
let(:collection_proxy) { SpeedyAF::Base.find(collection.id) }
Expand Down

0 comments on commit 59dc021

Please sign in to comment.