Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ def create
@submission = Submission.new(submission_params)
@submission.user = current_user
if @submission.save
# process_submission
# @submission.send_status_email
flash.notice = 'Your Submission is now in progress'
redirect_to submissions_path
else
Expand All @@ -46,36 +44,6 @@ def callback_uri
callback_submission_status_url(@submission)
end

def process_submission
@submission.to_sword_package(callback_uri)
sword = Sword.new(@submission)
begin
sword.deposit
read_sword_response(sword)
rescue RestClient::Unauthorized
@submission.status = 'failed'
flash.notice = 'There was a problem with your submission.'
end
@submission.save
end

def read_sword_response(sword)
deposited(sword) if sword.response.code == 201
queued if sword.response.code == 202
end

def deposited(sword)
@submission.status = 'deposited'
@submission.handle = sword.handle
flash.notice =
"Your Submission succeeded. Permanent URL: #{@submission.handle}"
end

def queued
@submission.status = 'in review queue'
flash.notice = 'Your Submission is now in progress.'
end

def submission_params
params.require(:submission).permit(:title, :agreed_to_license, :author,
:journal, :doi, :grant_number, :doe,
Expand Down
35 changes: 35 additions & 0 deletions app/jobs/sword_submit_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class SwordSubmitJob < ActiveJob::Base
queue_as :default

def perform(submission)
@submission = submission
process_submission
@submission.send_status_email
end

def process_submission
callback_uri = "http://example.com/callbacks/status/#{@submission.uuid}'"
sword = Sword.new(@submission, callback_uri)
begin
sword.deposit
read_sword_response(sword)
rescue RestClient::Unauthorized
@submission.status = 'failed'
end
@submission.save
end

def read_sword_response(sword)
deposited(sword) if sword.response.code == 201
queued if sword.response.code == 202
end

def deposited(sword)
@submission.status = 'deposited'
@submission.handle = sword.handle
end

def queued
@submission.status = 'in review queue'
end
end
5 changes: 3 additions & 2 deletions app/models/sword.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class Sword
attr_reader :response

def initialize(submission)
def initialize(submission, callback_uri)
@submission = submission
@callback_uri = callback_uri
@sword_server = RestClient::Resource.new(
Rails.application.secrets.sword_endpoint,
user: Rails.application.secrets.sword_username,
Expand All @@ -11,7 +12,7 @@ def initialize(submission)

def deposit
@response = @sword_server.post(
File.read(@submission.sword_path),
@submission.to_sword_package(@callback_uri),
content_type: 'application/zip',
x_packaging: 'http://purl.org/net/sword-types/METSDSpaceSIP')
end
Expand Down
33 changes: 33 additions & 0 deletions test/jobs/sword_submit_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'test_helper'

class SwordSubmitJobTest < ActiveJob::TestCase
test 'successful sword submission with workflow enabled' do
sub = submissions(:sub_one)
VCR.use_cassette('workflow_submission', preserve_exact_body_bytes: true) do
SwordSubmitJob.perform_now(sub)
end
sub.reload
assert_equal('in review queue', sub.status)
assert_nil(sub.handle)
end

test 'successful sword submission with no workflow enabled' do
sub = submissions(:sub_one)
VCR.use_cassette('deposit', preserve_exact_body_bytes: true) do
SwordSubmitJob.perform_now(sub)
end
sub.reload
assert_equal('deposited', sub.status)
assert_equal('http://hdl.handle.net/123456789/420', sub.handle)
end

test 'invalid sword credentials' do
sub = submissions(:sub_one)
VCR.use_cassette('invalid_credentials', preserve_exact_body_bytes: true) do
SwordSubmitJob.perform_now(sub)
end
sub.reload
assert_equal('failed', sub.status)
assert_nil(sub.handle)
end
end
12 changes: 6 additions & 6 deletions test/models/sword_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def cleaup_sword_files(sub)
VCR.use_cassette('workflow submission', preserve_exact_body_bytes: true) do
sub = submissions(:sub_one)
setup_sword_files(sub)
sub.to_sword_package('http://example.com/callback')
sword = Sword.new(sub)
callback_uri = 'http://example.com/callback'
sword = Sword.new(sub, callback_uri)
response = sword.deposit
cleaup_sword_files(sub)
assert_equal(response.code, 202)
Expand All @@ -29,8 +29,8 @@ def cleaup_sword_files(sub)
VCR.use_cassette('deposit', preserve_exact_body_bytes: true) do
sub = submissions(:sub_one)
setup_sword_files(sub)
sub.to_sword_package('http://example.com/callback')
sword = Sword.new(sub)
callback_uri = 'http://example.com/callback'
sword = Sword.new(sub, callback_uri)
response = sword.deposit
cleaup_sword_files(sub)
assert_equal(response.code, 201)
Expand All @@ -41,8 +41,8 @@ def cleaup_sword_files(sub)
VCR.use_cassette('invalid credentials', preserve_exact_body_bytes: true) do
sub = submissions(:sub_one)
setup_sword_files(sub)
sub.to_sword_package('http://example.com/callback')
sword = Sword.new(sub)
callback_uri = 'http://example.com/callback'
sword = Sword.new(sub, callback_uri)
assert_raises RestClient::Unauthorized do
sword.deposit
end
Expand Down