2020-10-27 00:51:19 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-02-22 04:35:39 +08:00
|
|
|
#
|
2017-04-28 12:06:26 +08:00
|
|
|
# Copyright (C) 2012 - present Instructure, Inc.
|
2012-02-22 04:35:39 +08:00
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
describe FileInContext do
|
|
|
|
before do
|
|
|
|
course_model
|
|
|
|
folder_model(:name => 'course files')
|
|
|
|
@course.folders << @folder
|
|
|
|
@course.save!
|
|
|
|
@course.reload
|
|
|
|
end
|
2015-06-26 06:50:24 +08:00
|
|
|
|
2012-02-22 04:35:39 +08:00
|
|
|
context "#attach" do
|
2021-09-23 22:17:23 +08:00
|
|
|
it "creates files with the supplied filename escaped for s3" do
|
2012-02-22 04:35:39 +08:00
|
|
|
# This horrible hack is because we need Attachment to behave like S3 in this case, as far as filename
|
|
|
|
# escaping goes. With attachment_fu, the filename is escaped, without it it is not. Because we're not
|
|
|
|
# able to dynamically switch out the S3 status during specs (see the selenium specs that fork a new process
|
|
|
|
# to test S3), we fake out just the part we care about. Also, we can't use Mocha because we need the
|
|
|
|
# argument of the method. This will be fixed when we've refactored Attachment to allow dynamically
|
|
|
|
# switching between S3 and local.
|
2015-06-26 06:50:24 +08:00
|
|
|
s3_storage!
|
2013-08-27 04:12:17 +08:00
|
|
|
unbound_method = Attachment.instance_method(:filename=)
|
2012-02-22 04:35:39 +08:00
|
|
|
class Attachment; def filename=(new_name); write_attribute :filename, sanitize_filename(new_name); end; end
|
|
|
|
filename = File.expand_path(File.join(File.dirname(__FILE__), %w(.. fixtures files escaping_test[0].txt)))
|
2020-09-25 03:26:13 +08:00
|
|
|
attachment = FileInContext.attach(@course, filename, folder: @folder)
|
2014-10-14 01:32:22 +08:00
|
|
|
expect(attachment.filename).to eq 'escaping_test%5B0%5D.txt'
|
2015-04-18 04:04:52 +08:00
|
|
|
expect(attachment).to be_published
|
2013-08-27 04:12:17 +08:00
|
|
|
Attachment.send(:define_method, :filename=, unbound_method)
|
2012-02-22 04:35:39 +08:00
|
|
|
end
|
2015-04-18 04:04:52 +08:00
|
|
|
|
|
|
|
describe "usage rights required" do
|
|
|
|
before do
|
2019-08-16 00:17:07 +08:00
|
|
|
@course.usage_rights_required = true
|
|
|
|
@course.save!
|
2015-04-18 04:04:52 +08:00
|
|
|
@filename = File.expand_path(File.join(File.dirname(__FILE__), %w(.. fixtures files a_file.txt)))
|
|
|
|
end
|
|
|
|
|
2021-09-23 22:17:23 +08:00
|
|
|
it "creates files in unpublished state" do
|
2015-04-18 04:04:52 +08:00
|
|
|
attachment = FileInContext.attach(@course, @filename)
|
|
|
|
expect(attachment).not_to be_published
|
|
|
|
end
|
|
|
|
|
2021-09-23 22:17:23 +08:00
|
|
|
it "creates files as published in non-course context" do
|
2015-04-18 04:04:52 +08:00
|
|
|
assignment = @course.assignments.create!
|
|
|
|
attachment = FileInContext.attach(assignment, @filename)
|
|
|
|
expect(attachment).to be_published
|
|
|
|
end
|
|
|
|
end
|
2012-02-22 04:35:39 +08:00
|
|
|
end
|
|
|
|
end
|