Skip to content

Commit

Permalink
document and test Hyrax::LeaseManager
Browse files Browse the repository at this point in the history
`LeaseManager` is substantially similar to `EmbargoManager`, and got in without
tests, with the idea that a refactor might quickly take place. it didn't and at
this point it needs a test harness if that's ever going to happen.

in the meanwhile. there are features we want to add to cover the remainder of
the lifecycle.
  • Loading branch information
tom johnson committed May 7, 2020
1 parent 8a784b3 commit a43faee
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
21 changes: 20 additions & 1 deletion app/services/hyrax/lease_manager.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

module Hyrax
##
# Provides utilities for managing the lifecycle of an `Hyrax::Lease` on a
# `Hyrax::Resource`.
class LeaseManager
##
# @!attribute [rw] resource
Expand Down Expand Up @@ -31,6 +34,12 @@ def lease_for(resource:, query_service: Hyrax.query_service)
end
end

##
# Copies and applies the lease to a new (target) resource.
#
# @param target [Hyrax::Resource]
#
# @return [Boolean]
def copy_lease_to(target:)
return false unless under_lease?

Expand All @@ -47,7 +56,14 @@ def apply
end

##
# @return [Valkyrie::Resource]
# @return [void]
# @raise [NotEnforcableError] when trying to apply an lease that isn't active
def apply!
apply || raise(NotEnforcableError)
end

##
# @return [Hyrax::Lease]
def lease
resource.lease || Lease.new
end
Expand All @@ -58,6 +74,9 @@ def under_lease?
lease.active?
end

class NotEnforcableError < RuntimeError; end
class NotReleasableError < RuntimeError; end

private

def clone_attributes
Expand Down
84 changes: 84 additions & 0 deletions spec/services/hyrax/lease_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# frozen_string_literal: true

RSpec.describe Hyrax::LeaseManager do
subject(:manager) { described_class.new(resource: resource) }
let(:resource) { Hyrax::Resource.new }

shared_context 'when under lease' do
let(:resource) { FactoryBot.build(:hyrax_resource, :under_lease) }
end

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

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

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

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

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

before { resource.visibility = 'restricted' }

it 'applies the active lease visibility' do
expect { manager.apply }
.to change { resource.visibility }
.from('restricted')
.to 'open'
end
end
end

describe '#lease' do
it 'gives an inactive lease' do
expect(manager.lease).not_to be_active
end

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

it 'gives an active lease' do
expect(manager.lease).to be_active
end

it 'has lease attributes' do
expect(manager.lease)
.to have_attributes visibility_after_lease: 'authenticated',
visibility_during_lease: 'open',
lease_expiration_date: an_instance_of(Date),
lease_history: be_empty
end
end
end

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

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

it { is_expected.to be_under_lease }
end

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

it { is_expected.not_to be_under_lease }
end
end
end

0 comments on commit a43faee

Please sign in to comment.