1+ #
2+ # Copyright (C) 2014 Instructure, Inc.
3+ #
4+ # This file is part of Canvas.
5+ #
6+ # Canvas is free software: you can redistribute it and/or modify it under
7+ # the terms of the GNU Affero General Public License as published by the Free
8+ # Software Foundation, version 3 of the License.
9+ #
10+ # Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
11+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12+ # A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13+ # details.
14+ #
15+ # You should have received a copy of the GNU Affero General Public License along
16+ # with this program. If not, see <http://www.gnu.org/licenses/>.
17+ #
18+
19+ require File . expand_path ( File . dirname ( __FILE__ ) + '/../api_spec_helper' )
20+
21+ module Lti
22+ describe LtiAppsController , type : :request do
23+
24+ let ( :account ) { Account . create }
25+ let ( :product_family ) { ProductFamily . create ( vendor_code : '123' , product_code : 'abc' , vendor_name : 'acme' , root_account : account ) }
26+
27+ describe '#launch_definitions' do
28+
29+ before do
30+ tp = create_tool_proxy
31+ tp . bindings . create ( context : account )
32+ rh = create_resource_handler ( tp )
33+ @mh = create_message_handler ( rh )
34+ @external_tool = new_valid_external_tool ( account )
35+ end
36+
37+ it 'returns a list of launch definitions for a context and placements' do
38+ course_with_teacher ( active_all : true , user : user_with_pseudonym , account : account )
39+ json = api_call ( :get , "/api/v1/courses/#{ @course . id } /lti_apps/launch_definitions" ,
40+ { controller : 'lti/lti_apps' , action : 'launch_definitions' , format : 'json' ,
41+ placements : %w( module_item resource_selection ) , course_id : @course . id . to_s } )
42+ json . select { |j | j [ 'definition_type' ] == @mh . class . name && j [ 'definition_id' ] == @mh . id . to_s } . should_not be_nil
43+ json . select { |j | j [ 'definition_type' ] == @external_tool . class . name && j [ 'definition_id' ] == @external_tool . id . to_s } . should_not be_nil
44+ end
45+
46+ it 'paginates the launch definitions' do
47+ 5 . times { |_ | new_valid_external_tool ( account ) }
48+ course_with_teacher ( active_all : true , user : user_with_pseudonym , account : account )
49+ json = api_call ( :get , "/api/v1/courses/#{ @course . id } /lti_apps/launch_definitions?per_page=3" ,
50+ { controller : 'lti/lti_apps' , action : 'launch_definitions' , format : 'json' ,
51+ placements : %w( module_item resource_selection ) , course_id : @course . id . to_s , per_page : '3' } )
52+
53+ json_next = follow_pagination_link ( 'next' , :controller => 'lti/lti_apps' , :action => 'launch_definitions' )
54+ json . count . should == 3
55+ json_next . count . should == 3
56+ json
57+ end
58+
59+
60+ end
61+
62+
63+ def create_tool_proxy ( opts = { } )
64+ default_opts = {
65+ context : account ,
66+ shared_secret : 'shared_secret' ,
67+ guid : SecureRandom . uuid ,
68+ product_version : '1.0beta' ,
69+ lti_version : 'LTI-2p0' ,
70+ product_family : product_family ,
71+ workflow_state : 'active' ,
72+ raw_data : 'some raw data'
73+ }
74+ ToolProxy . create ( default_opts . merge ( opts ) )
75+ end
76+
77+ def create_resource_handler ( tool_proxy , opts = { } )
78+ default_opts = { resource_type_code : 'code' , name : ( 0 ...8 ) . map { ( 65 + rand ( 26 ) ) . chr } . join , tool_proxy : tool_proxy }
79+ ResourceHandler . create ( default_opts . merge ( opts ) )
80+ end
81+
82+ def create_message_handler ( resource_handler , opts = { } )
83+ default_ops = { message_type : 'basic-lti-launch-request' , launch_path : 'https://samplelaunch/blti' , resource_handler : resource_handler }
84+ MessageHandler . create ( default_ops . merge ( opts ) )
85+ end
86+
87+ def new_valid_external_tool ( context , resource_selection = false )
88+ tool = context . context_external_tools . new ( :name => ( 0 ...8 ) . map { ( 65 + rand ( 26 ) ) . chr } . join ,
89+ :consumer_key => "key" ,
90+ :shared_secret => "secret" )
91+ tool . url = "http://www.example.com/basic_lti"
92+ tool . resource_selection = { :url => "http://example.com/selection_test" , :selection_width => 400 , :selection_height => 400 } if resource_selection
93+ tool . save!
94+ tool
95+ end
96+
97+ end
98+ end
0 commit comments