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
|
2015-06-26 06:50:24 +08:00
|
|
|
s3_storage!
|
2021-11-03 01:43:16 +08:00
|
|
|
|
|
|
|
filename = File.expand_path(File.join(__dir__, "../fixtures/files/escaping_test[0].txt"))
|
2020-09-25 03:26:13 +08:00
|
|
|
attachment = FileInContext.attach(@course, filename, folder: @folder)
|
2021-11-03 01:43:16 +08:00
|
|
|
allow(attachment).to receive(:filename=) do |new_name|
|
|
|
|
write_attribute(:filename, sanitize_filename(new_name))
|
|
|
|
end
|
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
|
2012-02-22 04:35:39 +08:00
|
|
|
end
|
2015-04-18 04:04:52 +08:00
|
|
|
|
2022-01-28 00:06:24 +08:00
|
|
|
describe "duplication handling" do
|
|
|
|
before do
|
|
|
|
@filename = File.expand_path(File.join(__dir__, "../fixtures/files/a_file.txt"))
|
|
|
|
@md5 = "2b00042f7481c7b056c4b410d28f33cf"
|
|
|
|
@sha512 = "8d3fffddf79e9a232ffd19f9ccaa4d6b37a6a243dbe0f23137b108a043d9da13121a9b505c804956b22e93c7f93969f4a7ba8ddea45bf4aab0bebc8f814e0991"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't duplicate attachments if the right hash is provided" do
|
|
|
|
FileInContext.attach(@course, @filename, folder: @folder)
|
|
|
|
FileInContext.attach(@course, @filename, folder: @folder, md5: @md5)
|
|
|
|
expect(@course.attachments.count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't duplicate attachments by comparing md5 hashes when sha512 finds nothing" do
|
|
|
|
attachment = FileInContext.attach(@course, @filename, folder: @folder)
|
|
|
|
attachment.update(md5: @md5)
|
|
|
|
|
|
|
|
# Making sure no additions to before blocks mess this up
|
|
|
|
expect(@course.attachments.where(md5: @sha512).take).to be_falsey
|
|
|
|
|
|
|
|
FileInContext.attach(@course, @filename, folder: @folder, md5: @sha512)
|
|
|
|
expect(@course.attachments.count).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't duplicate attachments by comparing sha512 hashes when md5 finds nothing" do
|
|
|
|
attachment = FileInContext.attach(@course, @filename, folder: @folder)
|
|
|
|
attachment.update(md5: @sha512)
|
|
|
|
|
|
|
|
# Making sure no additions to before blocks mess this up
|
|
|
|
expect(@course.attachments.where(md5: @md5).take).to be_falsey
|
|
|
|
|
|
|
|
FileInContext.attach(@course, @filename, folder: @folder, md5: @md5)
|
|
|
|
expect(@course.attachments.count).to eq 1
|
|
|
|
end
|
|
|
|
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
|