-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow processors to rename file attachments
Why are these changes being introduced: * Duplicate filenames attached to a thesis cannot be preserved * Sometimes we get duplicate filenames attached to a thesis * We did not have a way to rename filenames in the application Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/ETD-643 How does this address that need: * Adds a form that allows processors to rename a file attachment Document any side effects to this change: * ability model initialize class was auto updated by rubocop to use a guard clause
- Loading branch information
Showing
7 changed files
with
212 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class FileController < ApplicationController | ||
before_action :require_user | ||
before_action :authenticate_user! | ||
load_and_authorize_resource | ||
protect_from_forgery with: :exception | ||
|
||
def rename_form | ||
@thesis = Thesis.find(params[:thesis_id]) | ||
@attachment = ActiveStorage::Attachment.find(params[:attachment_id]) | ||
end | ||
|
||
def rename | ||
thesis = Thesis.find(params[:thesis_id]) | ||
attachment = ActiveStorage::Attachment.find(params[:attachment_id]) | ||
|
||
attachment.blob.filename = params[:attachment][:filename] | ||
|
||
if attachment.blob.save | ||
flash[:success] = "#{thesis.title} file #{attachment.filename} been updated." | ||
else | ||
flash[:error] = "#{thesis.title} file was unable to be updated" | ||
end | ||
|
||
redirect_to thesis_process_path(thesis) | ||
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,4 @@ | ||
<%= form_with model: @attachment, url: rename_file_path(thesis_id: @thesis, attachment_id: @attachment), method: 'post' do |form| %> | ||
<%= form.text_field :filename, value: @attachment.filename %> | ||
<%= form.submit %> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
require 'test_helper' | ||
|
||
class FilesControllerTest < ActionDispatch::IntegrationTest | ||
test 'anonymous users are prompted to log in when accessing rename form' do | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
get "/file/rename/#{@thesis.id}/#{@attachment.id}" | ||
|
||
assert_response :redirect | ||
assert_redirected_to '/login' | ||
end | ||
|
||
test 'basic can not access file rename form' do | ||
sign_in users(:basic) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
get "/file/rename/#{@thesis.id}/#{@attachment.id}" | ||
assert_redirected_to '/' | ||
follow_redirect! | ||
assert_select 'div.alert', text: 'Not authorized.', count: 1 | ||
end | ||
|
||
test 'transfer_submitter can not access file rename form' do | ||
sign_in users(:transfer_submitter) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
get "/file/rename/#{@thesis.id}/#{@attachment.id}" | ||
assert_redirected_to '/' | ||
follow_redirect! | ||
assert_select 'div.alert', text: 'Not authorized.', count: 1 | ||
end | ||
|
||
test 'thesis processors can access file rename form' do | ||
sign_in users(:processor) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
get "/file/rename/#{@thesis.id}/#{@attachment.id}" | ||
assert_response :success | ||
end | ||
|
||
test 'thesis admins can access file rename form' do | ||
sign_in users(:thesis_admin) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
get "/file/rename/#{@thesis.id}/#{@attachment.id}" | ||
assert_response :success | ||
end | ||
|
||
test 'thesis admins can rename blob filename' do | ||
sign_in users(:thesis_admin) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
follow_redirect! | ||
assert_equal thesis_process_path(theses(:publication_review)), path | ||
|
||
assert_select '.alert-banner.success', | ||
text: 'MyReadyThesis file renamed_a_pdf_too.pdf been updated.', count: 1 | ||
|
||
@attachment.reload | ||
|
||
assert_equal('renamed_a_pdf_too.pdf', @attachment.blob.filename.to_s) | ||
end | ||
|
||
test 'thesis processor can rename blob filename' do | ||
sign_in users(:processor) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
follow_redirect! | ||
assert_equal thesis_process_path(theses(:publication_review)), path | ||
|
||
assert_select '.alert-banner.success', | ||
text: 'MyReadyThesis file renamed_a_pdf_too.pdf been updated.', count: 1 | ||
|
||
@attachment.reload | ||
|
||
assert_equal('renamed_a_pdf_too.pdf', @attachment.blob.filename.to_s) | ||
end | ||
|
||
test 'basic user can not rename blob filename' do | ||
sign_in users(:basic) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
follow_redirect! | ||
assert_equal root_path, path | ||
|
||
assert_select 'div.alert', text: 'Not authorized.', count: 1 | ||
|
||
@attachment.reload | ||
|
||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
end | ||
|
||
test 'transfer_submitter user can not rename blob filename' do | ||
sign_in users(:transfer_submitter) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
follow_redirect! | ||
assert_equal root_path, path | ||
|
||
assert_select 'div.alert', text: 'Not authorized.', count: 1 | ||
|
||
@attachment.reload | ||
|
||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
end | ||
|
||
test 'anonymous user can not rename blob filename' do | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
assert_redirected_to '/login' | ||
|
||
@attachment.reload | ||
|
||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
end | ||
|
||
test 'renaming a file does not change the file checksum' do | ||
sign_in users(:thesis_admin) | ||
@thesis = theses(:publication_review) | ||
@attachment = @thesis.files.first | ||
assert_equal('a_pdf.pdf', @attachment.blob.filename.to_s) | ||
assert_equal('KADsjJnGD1sVUgvqyZOaRg==', @attachment.checksum) | ||
|
||
post rename_file_path(@thesis, @attachment), | ||
params: { attachment: { filename: 'renamed_a_pdf_too.pdf' } } | ||
follow_redirect! | ||
assert_equal thesis_process_path(theses(:publication_review)), path | ||
|
||
assert_select '.alert-banner.success', | ||
text: 'MyReadyThesis file renamed_a_pdf_too.pdf been updated.', count: 1 | ||
|
||
@attachment.reload | ||
|
||
assert_equal('renamed_a_pdf_too.pdf', @attachment.blob.filename.to_s) | ||
assert_equal('KADsjJnGD1sVUgvqyZOaRg==', @attachment.checksum) | ||
end | ||
end |