Skip to content

Commit

Permalink
Extract a workspaces controller from objects controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed May 29, 2019
1 parent ece5f54 commit 1e2e832
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 68 deletions.
23 changes: 9 additions & 14 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-05-17 14:52:28 -0500 using RuboCop version 0.65.0.
# on 2019-05-29 09:08:03 -0500 using RuboCop version 0.65.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -29,7 +29,7 @@ Lint/UriEscapeUnescape:
- 'app/controllers/sdr_controller.rb'
- 'spec/controllers/sdr_controller_spec.rb'

# Offense count: 24
# Offense count: 23
Metrics/AbcSize:
Max: 94

Expand All @@ -38,11 +38,11 @@ Metrics/AbcSize:
Metrics/ClassLength:
Max: 124

# Offense count: 5
# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 11

# Offense count: 27
# Offense count: 26
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 43
Expand Down Expand Up @@ -101,7 +101,7 @@ RSpec/BeforeAfterAll:
- 'spec/support/**/*.rb'
- 'spec/dor/update_marc_record_service_spec.rb'

# Offense count: 25
# Offense count: 22
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Expand All @@ -113,7 +113,6 @@ RSpec/ContextWording:
- 'spec/services/public_xml_service_spec.rb'
- 'spec/services/publish_metadata_service_spec.rb'
- 'spec/services/registration_service_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 4
RSpec/DescribeClass:
Expand Down Expand Up @@ -148,34 +147,31 @@ RSpec/EmptyLineAfterSubject:
Exclude:
- 'spec/models/symphony_reader_spec.rb'

# Offense count: 21
# Offense count: 20
RSpec/ExpectInHook:
Exclude:
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/metadata_service_spec.rb'
- 'spec/services/public_desc_metadata_service_spec.rb'
- 'spec/services/publish_metadata_service_spec.rb'
- 'spec/services/registration_service_spec.rb'

# Offense count: 121
# Offense count: 111
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
- 'spec/dor/registration_response_spec.rb'
- 'spec/dor/service_item_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/metadata_service_spec.rb'
- 'spec/services/registration_service_spec.rb'

# Offense count: 50
# Offense count: 40
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Exclude:
- 'spec/controllers/objects_controller_spec.rb'
- 'spec/controllers/workflows_controller_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/metadata_service_spec.rb'
- 'spec/services/public_desc_metadata_service_spec.rb'
- 'spec/services/registration_service_spec.rb'
- 'spec/services/version_service_spec.rb'
Expand Down Expand Up @@ -220,13 +216,12 @@ RSpec/SubjectStub:
Exclude:
- 'spec/dor/update_marc_record_service_spec.rb'

# Offense count: 11
# Offense count: 10
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/dor/service_item_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/metadata_service_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 3
Expand Down
14 changes: 0 additions & 14 deletions app/controllers/objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ class ObjectsController < ApplicationController
render status: 409, plain: e.message, location: object_location(e.pid)
end

rescue_from(DruidTools::SameContentExistsError, DruidTools::DifferentContentExistsError) do |e|
render status: 409, plain: e.message
end

# Register new objects in DOR
def create
Rails.logger.info(params.inspect)
Expand All @@ -32,16 +28,6 @@ def create
end
end

# The param, source, can be passed as appended parameter to url:
# http://lyberservices-dev/dor/v1/objects/{druid}/initialize_workspace?source=/path/to/content/dir/for/druid
# or
# It can be passed in the body of the request as application/x-www-form-urlencoded parameters, as if submitted from a form
# TODO: We could get away with loading a simple object that mixes in Dor::Assembleable. It just needs to implement #pid
def initialize_workspace
WorkspaceService.create(@item, params[:source])
head :created
end

def publish
PublishMetadataService.publish(@item)
head :created
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/workspaces_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Handles API routes for managing the DOR workspace
class WorkspacesController < ApplicationController
before_action :load_item

rescue_from(DruidTools::SameContentExistsError, DruidTools::DifferentContentExistsError) do |e|
render status: 409, plain: e.message
end

# POST /v1/objects/:druid/workspace
# and the deprecated:
# POST /v1/objects/:druid/initialize_workspace
def create
WorkspaceService.create(@item, params[:source])
head :created
end
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

resources :objects, only: [:create] do
member do
post 'initialize_workspace'
post 'initialize_workspace', to: 'workspaces#create' # deprecated
post 'publish'
post 'update_marc_record'
post 'notify_goobi'
Expand All @@ -39,6 +39,8 @@
get 'contents/*path', to: 'content#read', format: false, as: :read_content
end

resource :workspace, only: [:create]

resources :metadata, only: [] do
collection do
get 'dublin_core'
Expand Down
39 changes: 0 additions & 39 deletions spec/controllers/objects_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,6 @@
end
end

describe 'initialize_workspace' do
before do
clean_workspace
end

after do
clean_workspace
end

it 'creates a druid tree in the dor workspace for the passed in druid' do
post 'initialize_workspace', params: { id: item.pid }
expect(File).to be_directory(TEST_WORKSPACE + '/aa/123/bb/4567')
end

it 'creates a link in the dor workspace to the path passed in as source' do
post 'initialize_workspace', params: { id: item.pid, source: '/some/path' }
expect(File).to be_symlink(TEST_WORKSPACE + '/aa/123/bb/4567/aa123bb4567')
end

context 'error handling' do
before do
druid = DruidTools::Druid.new(item.pid, TEST_WORKSPACE)
druid.mkdir
end

it 'returns a 409 Conflict http status code when the link/directory already exists' do
post 'initialize_workspace', params: { id: item.pid }
expect(response.status).to eq(409)
expect(response.body).to match(/The directory already exists/)
end

it 'returns a 409 Conflict http status code when the workspace already exists with different content' do
post 'initialize_workspace', params: { id: item.pid, source: '/some/path' }
expect(response.status).to eq(409)
expect(response.body).to match(/Unable to create link, directory already exists/)
end
end
end

describe '/publish' do
it 'calls publish metadata' do
expect(PublishMetadataService).to receive(:publish).with(item)
Expand Down
67 changes: 67 additions & 0 deletions spec/controllers/workspaces_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe WorkspacesController do
let(:item) { Dor::Item.new(pid: 'druid:aa123bb4567') }

before do
login
allow(Dor).to receive(:find).and_return(item)
rights_metadata_xml = Dor::RightsMetadataDS.new
allow(rights_metadata_xml).to receive_messages(ng_xml: Nokogiri::XML('<xml/>'))
allow(item).to receive_messages(
id: 'druid:aa123bb4567',
datastreams: { 'rightsMetadata' => rights_metadata_xml },
rightsMetadata: rights_metadata_xml,
remove_druid_prefix: 'aa123bb4567'
)
allow(rights_metadata_xml).to receive(:dra_object).and_return(Dor::RightsAuth.parse(Nokogiri::XML('<xml/>'), true))
end

describe '#create' do
before do
clean_workspace
end

after do
clean_workspace
end

it 'creates a druid tree in the dor workspace for the passed in druid' do
post :create, params: { id: item.pid }
expect(File).to be_directory(TEST_WORKSPACE + '/aa/123/bb/4567')
end

it 'creates a link in the dor workspace to the path passed in as source' do
post :create, params: { id: item.pid, source: '/some/path' }
expect(File).to be_symlink(TEST_WORKSPACE + '/aa/123/bb/4567/aa123bb4567')
end

context 'when the link/directory already exists' do
before do
druid = DruidTools::Druid.new(item.pid, TEST_WORKSPACE)
druid.mkdir
end

it 'returns a 409 Conflict http status code' do
post :create, params: { id: item.pid }
expect(response.status).to eq(409)
expect(response.body).to match(/The directory already exists/)
end
end

context 'when the workspace already exists with different content' do
before do
druid = DruidTools::Druid.new(item.pid, TEST_WORKSPACE)
druid.mkdir
end

it 'returns a 409 Conflict http status code' do
post :create, params: { id: item.pid, source: '/some/path' }
expect(response.status).to eq(409)
expect(response.body).to match(/Unable to create link, directory already exists/)
end
end
end
end

0 comments on commit 1e2e832

Please sign in to comment.