Skip to content

Commit

Permalink
expand the LeaseManager interface to handle releasing leases
Browse files Browse the repository at this point in the history
  • Loading branch information
tom johnson authored and Tom Johnson committed May 12, 2020
1 parent 9bb1c55 commit 8d8210e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/services/hyrax/lease_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,33 @@ def apply!
apply || raise(NotEnforcableError)
end

##
# @return [Boolean]
def enforced?
lease.visibility_during_lease.to_s == resource.visibility
end

##
# @return [Hyrax::Lease]
def lease
resource.lease || Lease.new
end

##
# @return [Boolean]
def release
return false if under_lease?
return true if lease.visibility_after_lease.nil?

resource.visibility = lease.visibility_after_lease
end

##
# @return [void]
def release!
release || raise(NotReleasableError)
end

##
# @return [Boolean]
def under_lease?
Expand Down
70 changes: 69 additions & 1 deletion spec/services/hyrax/lease_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

shared_context 'with expired lease' do
let(:resource) { FactoryBot.build(:hyrax_resource, lease: lease) }
let(:lease) { FactoryBot.build(:hyrax_lease, :expired) }
let(:lease) { FactoryBot.build(:hyrax_lease, :expired) }
end

describe '#apply' do
Expand Down Expand Up @@ -44,6 +44,36 @@
end
end

describe '#enforced' do
it { is_expected.not_to be_enforced }

context 'when under lease' do
include_context 'when under lease'

it { is_expected.not_to be_enforced }

context 'and it is applied' do
before { manager.apply! }

it { is_expected.to be_enforced }
end
end

context 'with expired lease' do
include_context 'with expired lease'

it { is_expected.not_to be_enforced }
end

context 'with an lease that is in force, but expired' do
include_context 'with expired lease'

before { resource.visibility = lease.visibility_during_lease }

it { is_expected.to be_enforced }
end
end

describe '#lease' do
it 'gives an inactive lease' do
expect(manager.lease).not_to be_active
Expand All @@ -66,6 +96,44 @@
end
end

describe '#release' do
context 'with no lease' do
it 'is a no-op' do
expect { manager.release }
.not_to change { resource.visibility }
end
end

context 'with expired lease' do
include_context 'with expired lease'

it 'ensures the post-lease visibility is set' do
manager.release
expect(resource.visibility).to eq lease.visibility_after_lease
end

context 'and lease was applied' do
before { resource.visibility = lease.visibility_during_lease }

it 'ensures the post-lease visibility is set' do
expect { manager.release }
.to change { resource.visibility }
.from(lease.visibility_during_lease)
.to lease.visibility_after_lease
end
end
end

context 'when under lease' do
include_context 'when under lease'

it 'is a no-op' do
expect { manager.release }
.not_to change { resource.visibility }
end
end
end

describe '#under_lease?' do
it { is_expected.not_to be_under_lease }

Expand Down

0 comments on commit 8d8210e

Please sign in to comment.