upload submissions to submissions folder if feature is enabled

test plan:
 - enable the submissions folder feature
 - create an assignment that accepts file submissions
 - submit the assignment by uploading a file
 - ensure it is uploaded to the read-only Submissions folder
 - repeat this test with a group assignment

 (NOTE: submitting an assignment by choosing an alredy-uploaded
        file will not yet copy the file into the Submissions
        folder; this is coming up in another commit)

closes CNVS-28138

Change-Id: I836b26324cfa87d9a3ea62ca80569bcdfbb3b671
Reviewed-on: https://gerrit.instructure.com/77922
Tested-by: Jenkins
Reviewed-by: James Williams  <jamesw@instructure.com>
QA-Review: Jahnavi Yetukuri <jyetukuri@instructure.com>
Product-Review: Jeremy Stanley <jeremy@instructure.com>
This commit is contained in:
Jeremy Stanley 2016-04-25 16:28:20 -06:00
parent 59821b6302
commit c378afc033
6 changed files with 54 additions and 2 deletions

View File

@ -750,6 +750,10 @@ class FilesController < ApplicationController
@folder = @context.folders.active.where(id: params[:attachment][:folder_id]).first
return unless authorized_action(@folder, @current_user, :manage_contents)
end
if intent == 'submit' && context.respond_to?(:submissions_folder) &&
@asset && @asset.context.root_account.feature_enabled?(:submissions_folder)
@folder ||= @context.submissions_folder(@asset.context)
end
@folder ||= Folder.unfiled_folder(@context)
@attachment.folder_id = @folder.id
end

View File

@ -457,7 +457,7 @@ class SubmissionsApiController < ApplicationController
permission = :nothing if @user != @current_user
# we don't check quota when uploading a file for assignment submission
if authorized_action(@assignment, @current_user, permission)
api_attachment_preflight(@user, request, :check_quota => false)
api_attachment_preflight(@user, request, :check_quota => false, :submission_context => @context)
end
end

View File

@ -733,7 +733,7 @@ class Group < ActiveRecord::Base
user.favorites.where(:context_type => 'Group', :context_id => self).exists?
end
def submissions_folder
def submissions_folder(_course = nil)
return @submissions_folder if @submissions_folder
Folder.unique_constraint_retry do
@submissions_folder = self.folders.where(parent_folder_id: Folder.root_folders(self).first, submission_context_code: 'root')

View File

@ -151,6 +151,8 @@ module Api::V1::Attachment
end
if @attachment.folder
return unless authorized_action(@attachment.folder, @current_user, :manage_contents)
elsif opts[:submission_context] && opts[:submission_context].root_account.feature_enabled?(:submissions_folder)
@attachment.folder = context.submissions_folder(opts[:submission_context]) if context.respond_to?(:submissions_folder)
end
duplicate_handling = check_duplicate_handling_option(params)
if opts[:check_quota]

View File

@ -2863,6 +2863,18 @@ describe 'Submissions API', type: :request do
json = api_call(:post, "/api/v1/courses/#{@course.id}/assignments/#{@assignment.id}/submissions/#{@student2.id}/files",
{ :controller => "submissions_api", :action => "create_file", :format => "json", :course_id => @course.to_param, :assignment_id => @assignment.to_param, :user_id => @student2.to_param }, {}, {}, { :expected_status => 401 })
end
it "should upload to a student's Submissions folder if the feature is enabled" do
@course.root_account.enable_feature! :submissions_folder
preflight(name: 'test.txt', size: 12345, content_type: 'text/plain')
f = Attachment.last.folder
expect(f.submission_context_code).to eq @course.asset_string
end
it "should not do so otherwise" do
preflight(name: 'test.txt', size: 12345, content_type: 'text/plain')
expect(Attachment.last.folder).not_to be_for_submissions
end
end
it "should reject invalid urls" do

View File

@ -920,6 +920,40 @@ describe FilesController do
expect(response.status).to eq 401
end
it "creates a file in the submissions folder if intent=='submit' and the feature is enabled" do
@course.root_account.enable_feature! :submissions_folder
user_session(@student)
assignment = @course.assignments.create!(:submission_types => 'online_upload')
post 'create_pending', {:attachment => {
:context_code => assignment.context_code,
:asset_string => assignment.asset_string,
:filename => 'test.txt',
:intent => 'submit'
}}
f = assigns[:attachment].folder
expect(f.submission_context_code).to eq @course.asset_string
end
it "uses a submissions folder for group assignments when the feature is enabled" do
@course.root_account.enable_feature! :submissions_folder
user_session(@student)
category = group_category
assignment = @course.assignments.create(:group_category => category, :submission_types => 'online_upload')
group = category.groups.create(:context => @course)
group.add_user(@student)
user_session(@student)
post 'create_pending', {:attachment => {
:context_code => @course.asset_string,
:asset_string => assignment.asset_string,
:intent => 'submit',
:filename => "bob.txt"
}}
expect(response).to be_success
expect(assigns[:attachment]).not_to be_nil
expect(assigns[:attachment].context).to eq group
expect(assigns[:attachment].folder).to be_for_submissions
end
context "sharding" do
specs_require_sharding