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 committed May 7, 2020
1 parent a43faee commit 146743a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
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
68 changes: 68 additions & 0 deletions spec/services/hyrax/lease_manager_spec.rb
Original file line number Diff line number Diff line change
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 146743a

Please sign in to comment.