add new variable expansions for attachments

fixes PLAT-931

test-plan:
file_menu lti launches should now expand the following variables:
$Canvas.file.media.id
$Canvas.file.media.type
$Canvas.file.media.duration
$Canvas.file.media.size
$Canvas.file.media.title
$Canvas.file.usageRights.name
$Canvas.file.usageRights.url
$Canvas.file.usageRights.copyright_text

Change-Id: Ia358e8535aa31ad43c685323f38b26d9c3bce16b
Reviewed-on: https://gerrit.instructure.com/48606
Tested-by: Jenkins
Reviewed-by: Brad Humphrey <brad@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Nathan Mills <nathanm@instructure.com>
This commit is contained in:
Nathan Mills 2015-02-10 16:40:06 -07:00
parent 409020d3ce
commit ede725358f
5 changed files with 174 additions and 13 deletions

View File

@ -430,7 +430,7 @@ class ExternalToolsController < ApplicationController
end
protected :basic_lti_launch_request
def content_item_selection_response(tool, placement, content_item_response)
def content_item_selection_response(tool, placement, content_item_response, attachment = nil)
params = default_lti_params.merge(
{
#required params
@ -442,7 +442,7 @@ class ExternalToolsController < ApplicationController
context_title: @context.name,
tool_consumer_instance_name: @domain_root_account.name,
tool_consumer_instance_contact_email: HostUrl.outgoing_email_address,
}).merge(variable_expander(tool:tool).expand_variables!(tool.set_custom_fields(placement)))
}).merge(variable_expander(tool:tool, attachment:@file).expand_variables!(tool.set_custom_fields(placement)))
lti_launch = Lti::Launch.new
@ -477,23 +477,23 @@ class ExternalToolsController < ApplicationController
def content_item_for_file
#find the content title
file = Attachment.where(:id => params[:files].first).first
@file = Attachment.where(:id => params[:files].first).first
if @context.is_a?(Account)
raise ActiveRecord::RecordNotFound unless file.context == @current_user
elsif file.context.is_a?(Course)
raise ActiveRecord::RecordNotFound unless file.context == @context
elsif file.context.is_a?(Group)
raise ActiveRecord::RecordNotFound unless file.context.context == @context
raise ActiveRecord::RecordNotFound unless @file.context == @current_user
elsif @file.context.is_a?(Course)
raise ActiveRecord::RecordNotFound unless @file.context == @context
elsif @file.context.is_a?(Group)
raise ActiveRecord::RecordNotFound unless @file.context.context == @context
end
render_unauthorized_action if file.locked_for?(@current_user, check_policies: true)
render_unauthorized_action if @file.locked_for?(@current_user, check_policies: true)
{
"@type" => "ContentItemPlacement",
"placementOf" => {
"@type" => "FileItem",
"@id" => file_download_url(file, { :verifier => file.uuid, :download => '1', :download_frd => '1' }),
"mediaType" => file.content_type,
"title" => file.display_name
"@id" => file_download_url(@file, { :verifier => @file.uuid, :download => '1', :download_frd => '1' }),
"mediaType" => @file.content_type,
"title" => @file.display_name
}
}
end

View File

@ -46,4 +46,8 @@ class UsageRights < ActiveRecord::Base
self.class.licenses[license || 'private'][:readable_license]
end
def license_url
self.class.licenses[license || 'private'][:license_url]
end
end

View File

@ -25,7 +25,7 @@ module Lti
attr_reader :context, :root_account, :controller, :current_user
attr_accessor :current_pseudonym, :content_tag, :assignment,
:tool_setting_link_id, :tool_setting_binding_id, :tool_setting_proxy_id, :tool
:tool_setting_link_id, :tool_setting_binding_id, :tool_setting_proxy_id, :tool, :attachment
def self.register_expansion(name, permission_groups, proc, guard = -> { true })
@expansions ||= {}
@ -42,6 +42,9 @@ module Lti
ENROLLMENT_GUARD = -> { @current_user && @context.is_a?(Course) }
CONTENT_TAG_GUARD = -> { @content_tag }
ASSIGNMENT_GUARD = -> { @assignment }
MEDIA_OBJECT_GUARD = -> { @attachment && @attachment.media_object}
USAGE_RIGHTS_GUARD = -> { @attachment && @attachment.usage_rights}
MEDIA_OBJECT_ID_GUARD = -> {@attachment && (@attachment.media_object || @attachment.media_entry_id )}
def initialize(root_account, context, controller, opts = {})
@ -268,6 +271,38 @@ module Lti
-> { @controller.named_context_url(@tool.context, :context_tool_consumer_profile_url, "339b6700-e4cb-47c5-a54f-3ee0064921a9", include_host: true )},
-> { @tool }
register_expansion 'Canvas.file.media.id', [],
-> { (@attachment.media_object && @attachment.media_object.media_id) || @attachment.media_entry_id },
MEDIA_OBJECT_ID_GUARD
register_expansion 'Canvas.file.media.type', [],
-> {@attachment.media_object.media_type},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.duration', [],
-> {@attachment.media_object.duration},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.size', [],
-> {@attachment.media_object.total_size},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.media.title', [],
-> {@attachment.media_object.user_entered_title || @attachment.media_object.title},
MEDIA_OBJECT_GUARD
register_expansion 'Canvas.file.usageRights.name', [],
-> {@attachment.usage_rights.license_name},
USAGE_RIGHTS_GUARD
register_expansion 'Canvas.file.usageRights.url', [],
-> {@attachment.usage_rights.license_url},
USAGE_RIGHTS_GUARD
register_expansion 'Canvas.file.usageRights.copyrightText', [],
-> {@attachment.usage_rights.legal_copyright},
USAGE_RIGHTS_GUARD
private
def sis_pseudonym

View File

@ -362,6 +362,94 @@ module Lti
end
end
context 'attachment' do
let (:attachment) do
attachment = attachment_obj_with_context(course)
attachment.media_object = media_object
attachment.usage_rights = usage_rights
attachment
end
let(:media_object) do
mo = MediaObject.new
mo.media_id = '1234'
mo.media_type = 'video'
mo.duration = 555
mo.total_size = 444
mo.title = 'some title'
mo
end
let(:usage_rights) do
ur = UsageRights.new
ur.legal_copyright = 'legit'
ur
end
subject { described_class.new(root_account, account, controller, current_user: user, tool: tool, attachment: attachment) }
it 'has substitution for $Canvas.file.media.id when a media object is present' do
exp_hash = {test: '$Canvas.file.media.id'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq '1234'
end
it 'has substitution for $Canvas.file.media.id when a media object is present' do
exp_hash = {test: '$Canvas.file.media.id'}
attachment.media_object = nil
attachment.media_entry_id = '4567'
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq '4567'
end
it 'has substitution for $Canvas.file.media.type' do
exp_hash = {test: '$Canvas.file.media.type'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'video'
end
it 'has substitution for $Canvas.file.media.duration' do
exp_hash = {test: '$Canvas.file.media.duration'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 555
end
it 'has substitution for $Canvas.file.media.size' do
exp_hash = {test: '$Canvas.file.media.size'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 444
end
it 'has substitution for $Canvas.file.media.title' do
exp_hash = {test: '$Canvas.file.media.title'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'some title'
end
it 'uses user_entered_title for $Canvas.file.media.title if present' do
media_object.user_entered_title = 'user title'
exp_hash = {test: '$Canvas.file.media.title'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'user title'
end
it 'has substitution for $Canvas.file.usageRights.name' do
exp_hash = {test: '$Canvas.file.usageRights.name'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'Private (Copyrighted)'
end
it 'has substitution for $Canvas.file.usageRights.url' do
exp_hash = {test: '$Canvas.file.usageRights.url'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'http://en.wikipedia.org/wiki/Copyright'
end
it 'has substitution for $Canvas.file.usageRights.copyright_text' do
exp_hash = {test: '$Canvas.file.usageRights.copyrightText'}
subject.expand_variables!(exp_hash)
expect(exp_hash[:test]).to eq 'legit'
end
end
it 'has substitution for $Canvas.masqueradingUser.id' do
logged_in_user = User.new
logged_in_user.stubs(:id).returns(7878)

View File

@ -0,0 +1,34 @@
#
# Copyright (C) 2015 Instructure, Inc.
#
# 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/>.
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe UsageRights do
describe '#license_url' do
it 'returns the private license url if no license is specified' do
expect(subject.license_url).to eq 'http://en.wikipedia.org/wiki/Copyright'
end
it 'returns the url for the license' do
subject.license = 'cc_by_nc_nd'
expect(subject.license_url).to eq 'http://creativecommons.org/licenses/by-nc-nd/4.0/'
end
end
end