This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from MITLibraries/106_submission_delete
106 submission delete
- Loading branch information
Showing
14 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class S3 | ||
# Returns a list of keys for content in S3 that has been abandoned | ||
# @api private | ||
# @return list of abandoned keys in the S3_BUCKET | ||
# @note Content can become abandoned if a Submission event is not completed. | ||
# For example, a user that comes to the form and attaches a document but | ||
# does not complete the form Submission will leave abandoned documents in | ||
# S3. This cleans those up. | ||
def self.abandoned_keys | ||
keys.reject { |k| Submission.local_document_keys.include?(k) } | ||
end | ||
|
||
# Returns a list of all keys in the S3_BUCKET | ||
# @api private | ||
# @return list of all keys in the S3_BUCKET | ||
def self.keys | ||
S3_BUCKET.objects.map(&:key) | ||
end | ||
|
||
# Delete all S3 content not associated with any Submission | ||
# @api private | ||
# @return nil | ||
def self.remove_abandoned_content | ||
abandoned_keys.map { |key| S3_BUCKET.object(key).delete } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace :submission do | ||
desc 'Delete Deposited Submissions older than One Month' | ||
task cleanup: :environment do | ||
logger = Logger.new(STDOUT) | ||
logger.level = Logger::INFO | ||
old_submissions = Submission.where(status: 'approved') | ||
.where('updated_at < ?', 1.month.ago) | ||
logger.debug("Deleting #{old_submissions.count} old Submissions") | ||
old_submissions.each(&:destroy) | ||
end | ||
|
||
desc 'Delete abandoned S3 content' | ||
task cleanup_abandoned: :environment do | ||
logger = Logger.new(STDOUT) | ||
logger.level = Logger::INFO | ||
logger.info("Deleting #{S3.abandoned_keys.count} abandoned files") | ||
S3.remove_abandoned_content | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require 'test_helper' | ||
|
||
class S3PagesTest < Capybara::Rails::TestCase | ||
def setup | ||
Capybara.current_driver = :poltergeist | ||
Capybara.server_port = 5000 | ||
Capybara.server_host = 'localhost' | ||
auth_setup | ||
FileUtils.rm_f('tmp/69b9156a124c96bbdb55cad753810e14.zip') | ||
FileUtils.rm_f('tmp/40550618d6b4d97792b0773c97207186.zip') | ||
end | ||
|
||
def teardown | ||
super | ||
Capybara.use_default_driver | ||
auth_teardown | ||
@sub.documents.map(&:remove!) if @sub | ||
end | ||
|
||
def base_valid_form | ||
Timecop.freeze(Time.zone.local(1999)) | ||
mock_auth(users(:one)) | ||
visit new_submission_path | ||
fill_in('Journal', with: 'Super Mega Journal') | ||
fill_in('Title', with: 'Alphabetical Order is Good Enough') | ||
check('Department of Energy (DOE)') | ||
select '1999', from: 'submission[pub_date(1i)]' | ||
select 'January', from: 'submission[pub_date(2i)]' | ||
Timecop.return | ||
end | ||
|
||
test 'attaching a file stores it in s3' do | ||
base_valid_form | ||
attach_file('submission[documents][]', | ||
File.absolute_path('./test/fixtures/a_pdf.pdf')) | ||
assert_text('a_pdf.pdf uploaded') | ||
click_on('Create Submission') | ||
@sub = Submission.last | ||
doc = @sub.documents.last | ||
doc_uri = @sub.document_uri(doc).split("#{ENV['S3_BUCKET']}/").last | ||
assert_equal(true, S3_BUCKET.object(doc_uri).exists?) | ||
end | ||
|
||
test 'deleting a Submission deletes files from s3' do | ||
base_valid_form | ||
attach_file('submission[documents][]', | ||
File.absolute_path('./test/fixtures/a_pdf.pdf')) | ||
assert_text('a_pdf.pdf uploaded') | ||
click_on('Create Submission') | ||
@sub = Submission.last | ||
doc = @sub.documents.last | ||
doc_uri = @sub.document_uri(doc).split("#{ENV['S3_BUCKET']}/").last | ||
assert_equal(true, S3_BUCKET.object(doc_uri).exists?) | ||
@sub.destroy | ||
assert_equal(false, S3_BUCKET.object(doc_uri).exists?) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'test_helper' | ||
require 'rake' | ||
|
||
class RakeTaskTestCase < ActiveSupport::TestCase | ||
def setup | ||
Rake::Task.define_task :environment | ||
QuickSubmit::Application.load_tasks | ||
|
||
Timecop.freeze(Time.zone.local(1999)) | ||
@sub_old_approved = create_subs('old and approved', 'approved') | ||
@sub_old_notapproved = create_subs('old and unapproved', '') | ||
Timecop.return | ||
|
||
@sub_new_approved = create_subs('new and approved', 'approved') | ||
@sub_new_notapproved = create_subs('new and unapproved', '') | ||
end | ||
|
||
def teardown | ||
Rake::Task.clear | ||
end | ||
|
||
def create_subs(title, status) | ||
Submission.create( | ||
title: title, | ||
documents: ['b_pdf.pdf'], | ||
user: users(:one), | ||
funders: ['Department of Energy (DOE)'], | ||
pub_date: 1.year.ago, | ||
status: status, | ||
handle: 'http://example.com') | ||
end | ||
|
||
test 'submissions with approved and older than 1 month are deleted' do | ||
subs = Submission.count | ||
Rake::Task['submission:cleanup'].invoke | ||
assert_raises(ActiveRecord::RecordNotFound) do | ||
@sub_old_approved.reload | ||
end | ||
assert_equal(subs - 1, Submission.count) | ||
end | ||
|
||
test 'submissions without approved and older than 1 month are not deleted' do | ||
subs = Submission.count | ||
Rake::Task['submission:cleanup'].invoke | ||
@sub_old_notapproved.reload | ||
assert_equal(subs - 1, Submission.count) | ||
end | ||
|
||
test 'submissions with approved and newer than one month are not deleted' do | ||
subs = Submission.count | ||
Rake::Task['submission:cleanup'].invoke | ||
@sub_new_approved.reload | ||
assert_equal(subs - 1, Submission.count) | ||
end | ||
|
||
test 'submissions without approved and newer than 1 month are not deleted' do | ||
subs = Submission.count | ||
Rake::Task['submission:cleanup'].invoke | ||
@sub_new_notapproved.reload | ||
assert_equal(subs - 1, Submission.count) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'test_helper' | ||
|
||
class S3Test < ActiveSupport::TestCase | ||
def setup | ||
S3_BUCKET.objects.map(&:delete) | ||
S3_BUCKET.put_object(key: 'uploads/12345') | ||
S3_BUCKET.put_object(key: 'uploads/45678') | ||
S3_BUCKET.put_object(key: 'uploads/asdfa') | ||
end | ||
|
||
def teardown | ||
S3_BUCKET.object('uploads/12345').delete | ||
S3_BUCKET.object('uploads/45678').delete | ||
S3_BUCKET.object('uploads/asdfa').delete | ||
end | ||
|
||
test 'keys' do | ||
assert_equal(['uploads/12345', 'uploads/45678', 'uploads/asdfa'], S3.keys) | ||
end | ||
|
||
test 'abandoned_keys' do | ||
Submission.create( | ||
title: 'title', | ||
documents: '//s3.amazonaws.com/mitquicksubmitdev/uploads/12345', | ||
user: users(:one), | ||
funders: ['Department of Energy (DOE)'], | ||
pub_date: 1.year.ago, | ||
status: 'status', | ||
handle: 'http://example.com') | ||
assert_equal(['uploads/45678', 'uploads/asdfa'], S3.abandoned_keys) | ||
end | ||
|
||
test 'remove_abandoned_content' do | ||
Submission.create( | ||
title: 'title', | ||
documents: '//s3.amazonaws.com/mitquicksubmitdev/uploads/12345', | ||
user: users(:one), | ||
funders: ['Department of Energy (DOE)'], | ||
pub_date: 1.year.ago, | ||
status: 'status', | ||
handle: 'http://example.com') | ||
assert_equal(['uploads/45678', 'uploads/asdfa'], S3.abandoned_keys) | ||
S3.remove_abandoned_content | ||
assert_equal([], S3.abandoned_keys) | ||
assert_equal(['uploads/12345'], S3.keys) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters