Skip to content

Commit

Permalink
Refactor sword_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
fcd1 committed Feb 7, 2025
1 parent 5143053 commit ce78b99
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 44 deletions.
55 changes: 11 additions & 44 deletions app/controllers/sword_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,15 @@ def deposit
"Path to SWORD contents: #{@path_to_deposit_contents}"
)

# create Deposit instance to store deposit info in database
@deposit = Deposit.new
@deposit.depositor_user_id = @depositor_user_id
@deposit.collection_slug = @collection_slug
@deposit.deposit_files = @endpoint.documents_to_deposit
# fcd1, 9/6/18: use truncate_words here, but should also add a
# truncate(200, omission: '') at the model level to make sure we
# don't go beyond the 255 char limit for a string in MySql
# For now, just tack it on here.
@deposit.title =
@endpoint.deposit_title.truncate_words(20).truncate(200, omission: '')
@deposit.item_in_hyacinth = @endpoint.adapter_item_identifier
@deposit.asset_pids = @endpoint.asset_pids
@deposit.ingest_confirmed = @endpoint.confirm_ingest
@deposit.content_path = @path_to_deposit_contents
@deposit.save
# @depositor.deposits << @deposit
# @collection.deposits << @deposit
# create Deposit instance and store deposit info in database
@deposit = helpers.create_deposit(@depositor_user_id,
@collection_slug,
@endpoint.documents_to_deposit,
@endpoint.deposit_title.truncate_words(20).truncate(200, omission: ''),
@endpoint.adapter_item_identifier,
@endpoint.asset_pids,
@endpoint.confirm_ingest,
@path_to_deposit_contents)
response.status = 201
render json: { item_pid: @endpoint.adapter_item_identifier,
ingest_into_hyacinth: !(HYACINTH_CONFIG[:bypass_ingest] or COLLECTIONS[:slug][@endpoint.collection_slug][:bypass_hyacinth_ingest])}
Expand All @@ -63,19 +54,7 @@ def deposit
def service_document
# log basic essential info. Keep it terse! Gonna use :warn level, though not a warning.
Rails.logger.warn("Received Service Document request. Username: #{@depositor_user_id}")

content = HashWithIndifferentAccess.new
# For site-specific values, read from the config file:
content[:sword_version] = SWORD_CONFIG[:service_document][:sword_version]
content[:sword_verbose] = SWORD_CONFIG[:service_document][:sword_verbose]
content[:http_host] = request.env["HTTP_HOST"]
content[:collections] = []
COLLECTIONS[:slug].keys.each do |collection_slug|
if COLLECTIONS[:slug][collection_slug][:depositors].include? @depositor_user_id
content[:collections] << view_context.collection_info_for_service_document(collection_slug)
end
end
# @depositor.collections.each { |collection| content[:collections] << collection.info_for_service_document }
content = helpers.service_document_content
render xml: view_context.service_document_xml(content)
end

Expand All @@ -91,7 +70,7 @@ def check_for_valid_collection_slug

def check_basic_http_authentication
result = false
@depositor_user_id, @password = pull_credentials
@depositor_user_id, @password = helpers.pull_credentials(request)
unless DEPOSITORS[:basic_authentication_user_ids].key?(@depositor_user_id)
warn_msg_reason = "Unknown user/depositor: #{@depositor_user_id}"
# Rails.logger.warn "Unknown user/depositor: #{@depositor_user_id}"
Expand All @@ -108,18 +87,6 @@ def check_basic_http_authentication
end
end

# following is cut-and-paste of all the code in
# DepositRequest::pullCredentials
def pull_credentials
authorization = String.new(request.headers["Authorization"].to_s)
if(authorization.include? 'Basic ')
authorization['Basic '] = ''
authorization = Base64.decode64(authorization)
credentials = authorization.split(":")
[credentials[0] , credentials[1]]
end
end

def check_depositor_collection_permission
unless COLLECTIONS[:slug][@collection_slug][:depositors].include? @depositor_user_id
Rails.logger.warn "user/depositor #{@depositor_user_id} does not have access to collection slug #{@collection_slug}"
Expand Down
46 changes: 46 additions & 0 deletions app/helpers/sword_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,52 @@ def collection_info_for_service_document(collection_slug)
info
end

def pull_credentials(request)
authorization = String.new(request.headers["Authorization"].to_s)
if(authorization.include? 'Basic ')
authorization['Basic '] = ''
authorization = Base64.decode64(authorization)
credentials = authorization.split(":")
[credentials[0] , credentials[1]]
end
end

def create_deposit(depositor_user_id,
collection_slug,
documents_to_deposit,
title,
item_pid,
asset_pids,
ingest_confirmed,
content_path)
deposit = Deposit.new
deposit.depositor_user_id = depositor_user_id
deposit.collection_slug = collection_slug
deposit.deposit_files = documents_to_deposit
deposit.title = title
deposit.item_in_hyacinth = item_pid
deposit.asset_pids = asset_pids
deposit.ingest_confirmed = ingest_confirmed
deposit.content_path = content_path
deposit.save
deposit
end

def service_document_content
content = HashWithIndifferentAccess.new
# For site-specific values, read from the config file:
content[:sword_version] = SWORD_CONFIG[:service_document][:sword_version]
content[:sword_verbose] = SWORD_CONFIG[:service_document][:sword_verbose]
# content[:http_host] = request.env["HTTP_HOST"]
content[:collections] = []
COLLECTIONS[:slug].keys.each do |collection_slug|
if COLLECTIONS[:slug][collection_slug][:depositors].include? @depositor_user_id
content[:collections] << view_context.collection_info_for_service_document(collection_slug)
end
end
content
end

def service_document_xml(content)
xml = Builder::XmlMarkup.new( :indent => 2 )
xml.instruct! :xml, :encoding => "utf-8"
Expand Down
36 changes: 36 additions & 0 deletions spec/helpers/sword_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,42 @@ class DummyClass
end
end

describe 'pull_credentials' do
it 'returns array with Basic auth info' do
dc = DummyClass.new
encoded_auth = 'Basic Zmlyc3R0ZXN0ZGVwb3NpdG9yOmZpcnN0ZGVwb3NpdG9ycGFzc3dk'
request_double = instance_double(ActionDispatch::Request)
allow(request_double).to receive(:headers).and_return({'Authorization' => encoded_auth})
depositor, passwd = dc.pull_credentials request_double
expect(depositor).to eq('firsttestdepositor')
expect(passwd).to eq('firstdepositorpasswd')
end
end

describe 'create_deposit' do
it 'returns service document content' do
dc = DummyClass.new
deposit = create_deposit('test_user',
'test_collection_slug',
['a.txt','b.txt'],
'The test title',
'ac:12345',
['ac:54321', 'ac:0984'],
true,
'tmp/somewhere/')
expect(deposit).to be_an_instance_of(Deposit)
end
end

describe 'service_document_content' do
context 'with config info' do
it 'returns service document content' do
dc = DummyClass.new
expect(dc.service_document_content).to include 'sword_version'
end
end
end

describe 'service_document_xml' do
context 'with mime type and package type specified' do
it 'returns' do
Expand Down

0 comments on commit ce78b99

Please sign in to comment.