<commit_before>#
# Copyright (C) 2014 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__) + '/../../sharding_spec_helper')

describe "LTI integration tests" do
  let(:canvas_tool) {
    ContextExternalTool.new.tap do |canvas_tool|
      canvas_tool.context = root_account
      canvas_tool.url = 'http://launch/url'
      canvas_tool.name = 'tool'
      canvas_tool.consumer_key = '12345'
      canvas_tool.shared_secret = 'secret'
      canvas_tool.privacy_level = 'public'
      canvas_tool.settings[:custom_fields] = {
          'custom_variable_canvas_api_domain' => '$Canvas.api.domain',
          'custom_variable_canvas_assignment_id' => '$Canvas.assignment.id',
          'custom_variable_canvas_assignment_points_possible' => '$Canvas.assignment.pointsPossible',
          'custom_variable_canvas_assignment_title' => '$Canvas.assignment.title',
          'custom_variable_canvas_course_id' => '$Canvas.course.id',
          'custom_variable_canvas_enrollment_enrollment_state' => '$Canvas.enrollment.enrollmentState',
          'custom_variable_canvas_membership_concluded_roles' => '$Canvas.membership.concludedRoles',
          'custom_variable_canvas_user_id' => '$Canvas.user.id',
          'custom_variable_canvas_user_login_id' => '$Canvas.user.loginId',
          'custom_variable_person_address_timezone' => '$Person.address.timezone',
          'custom_variable_person_name_family' => '$Person.name.family',
          'custom_variable_person_name_full' => '$Person.name.full',
          'custom_variable_person_name_given' => '$Person.name.given',
      }
    end
  }

  let_once(:canvas_user) { user(name: '<NAME>') }

  let_once(:canvas_course) {
    course(active_course: true, course_name: 'my course').tap do |course|
      course.course_code = 'abc'
      course.sis_source_id = 'course_sis_id'
      course.root_account = root_account
      course.save!
    end
  }

  let_once(:root_account) {
    Account.new.tap do |account|
      account.name = 'root_account'
      account.save!
    end
  }

  let(:controller) do
    request_mock = mock('request')
    request_mock.stubs(:host).returns('/my/url')
    request_mock.stubs(:scheme).returns('https')
    m = mock('controller')
    m.stubs(:request).returns(request_mock)
    m.stubs(:logged_in_user).returns(@user || user)
    m
  end

  let(:return_url) { '/return/url' }

  it "generates the correct post payload" do
    canvas_user.email = '<EMAIL>'

    sub_account = Account.create!
    sub_account.root_account = root_account
    sub_account.save!
    pseudonym = pseudonym(canvas_user, account: root_account, username: 'login_id')

    teacher_in_course(user: canvas_user, course: canvas_course, active_enrollment: true)
    course_with_ta(user: canvas_user, course: canvas_course, active_enrollment: true)
    account_admin_user(user: canvas_user, account: sub_account)

    student_in_course(user: canvas_user, course: canvas_course, active_enrollment: true).conclude

    pseudonym.sis_user_id = 'sis id!'
    pseudonym.save!

    adapter = Lti::LtiOutboundAdapter.new(canvas_tool, canvas_user, canvas_course)

    variable_expander = Lti::VariableExpander.new(root_account, canvas_course, controller, {
                                                                  current_user: canvas_user,
                                                                  current_pseudonym: pseudonym})

    adapter.prepare_tool_launch(return_url, variable_expander)
    post_payload = adapter.generate_post_payload

    expect(post_payload['oauth_consumer_key']).to eq canvas_tool.consumer_key
    expect(post_payload['oauth_version']).to eq '1.0'
    expect(post_payload['context_id']).to eq canvas_tool.opaque_identifier_for(canvas_course)
    expect(post_payload['context_label']).to eq canvas_course.course_code
    expect(post_payload['context_title']).to eq canvas_course.name
    expect(post_payload['custom_canvas_enrollment_state']).to eq 'active'
    expect(post_payload['custom_canvas_api_domain']).to eq root_account.domain
    expect(post_payload['custom_canvas_course_id']).to eq canvas_course.id.to_s
    expect(post_payload['custom_canvas_user_id']).to eq canvas_user.id.to_s
    expect(post_payload['custom_canvas_user_login_id']).to eq 'login_id'
    expect(post_payload['custom_variable_canvas_api_domain']).to eq root_account.domain
    expect(post_payload['custom_variable_canvas_assignment_id']).to eq '$Canvas.assignment.id'
    expect(post_payload['custom_variable_canvas_assignment_points_possible']).to eq '$Canvas.assignment.pointsPossible'
    expect(post_payload['custom_variable_canvas_assignment_title']).to eq '$Canvas.assignment.title'
    expect(post_payload['custom_variable_canvas_course_id']).to eq canvas_course.id.to_s
    expect(post_payload['custom_variable_canvas_enrollment_enrollment_state']).to eq 'active'
    expect(post_payload['custom_variable_canvas_membership_concluded_roles']).to eq 'Learner'
    expect(post_payload['custom_variable_canvas_user_id']).to eq canvas_user.id.to_s
    expect(post_payload['custom_variable_canvas_user_login_id']).to eq 'login_id'
    expect(post_payload['launch_presentation_document_target']).to eq 'iframe'
    expect(post_payload['launch_presentation_locale']).to eq 'en'
    expect(post_payload['launch_presentation_return_url']).to eq '/return/url'
    expect(post_payload['lis_course_offering_sourcedid']).to eq 'course_sis_id'
    expect(post_payload['lis_person_contact_email_primary']).to eq canvas_user.email
    expect(post_payload['lis_person_name_family']).to eq canvas_user.last_name
    expect(post_payload['lis_person_name_full']).to eq canvas_user.name
    expect(post_payload['lis_person_name_given']).to eq canvas_user.first_name
    expect(post_payload['lis_person_sourcedid']).to eq 'sis id!'
    expect(post_payload['lti_message_type']).to eq 'basic-lti-launch-request'
    expect(post_payload['lti_version']).to eq 'LTI-1p0'
    expect(post_payload['oauth_callback']).to eq 'about:blank'
    expect(post_payload['resource_link_id']).to eq canvas_tool.opaque_identifier_for(canvas_course)
    expect(post_payload['resource_link_title']).to eq canvas_tool.name
    expect(post_payload['roles']).to eq 'urn:lti:role:ims/lis/TeachingAssistant,Instructor'
    expect(post_payload['ext_roles']).to eq "urn:lti:instrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Instructor,urn:lti:role:ims/lis/Instructor,urn:lti:role:ims/lis/TeachingAssistant,urn:lti:sysrole:ims/lis/User"
    expect(post_payload['tool_consumer_info_product_family_code']).to eq 'canvas'
    expect(post_payload['tool_consumer_info_version']).to eq 'cloud'
    expect(post_payload['tool_consumer_instance_contact_email']).to eq HostUrl.outgoing_email_address
    expect(post_payload['tool_consumer_instance_guid']).to eq root_account.lti_guid<commit_msg>spec: fix fragile spec<commit_after>#
# Copyright (C) 2014 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__) + '/../../sharding_spec_helper')

describe "LTI integration tests" do
  let(:canvas_tool) {
    ContextExternalTool.new.tap do |canvas_tool|
      canvas_tool.context = root_account
      canvas_tool.url = 'http://launch/url'
      canvas_tool.name = 'tool'
      canvas_tool.consumer_key = '12345'
      canvas_tool.shared_secret = 'secret'
      canvas_tool.privacy_level = 'public'
      canvas_tool.settings[:custom_fields] = {
          'custom_variable_canvas_api_domain' => '$Canvas.api.domain',
          'custom_variable_canvas_assignment_id' => '$Canvas.assignment.id',
          'custom_variable_canvas_assignment_points_possible' => '$Canvas.assignment.pointsPossible',
          'custom_variable_canvas_assignment_title' => '$Canvas.assignment.title',
          'custom_variable_canvas_course_id' => '$Canvas.course.id',
          'custom_variable_canvas_enrollment_enrollment_state' => '$Canvas.enrollment.enrollmentState',
          'custom_variable_canvas_membership_concluded_roles' => '$Canvas.membership.concludedRoles',
          'custom_variable_canvas_user_id' => '$Canvas.user.id',
          'custom_variable_canvas_user_login_id' => '$Canvas.user.loginId',
          'custom_variable_person_address_timezone' => '$Person.address.timezone',
          'custom_variable_person_name_family' => '$Person.name.family',
          'custom_variable_person_name_full' => '$Person.name.full',
          'custom_variable_person_name_given' => '$Person.name.given',
      }
    end
  }

  let_once(:canvas_user) { user(name: '<NAME>') }

  let_once(:canvas_course) {
    course(active_course: true, course_name: 'my course').tap do |course|
      course.course_code = 'abc'
      course.sis_source_id = 'course_sis_id'
      course.root_account = root_account
      course.save!
    end
  }

  let_once(:root_account) {
    Account.new.tap do |account|
      account.name = 'root_account'
      account.save!
    end
  }

  let(:controller) do
    request_mock = mock('request')
    request_mock.stubs(:host).returns('/my/url')
    request_mock.stubs(:scheme).returns('https')
    m = mock('controller')
    m.stubs(:request).returns(request_mock)
    m.stubs(:logged_in_user).returns(@user || user)
    m
  end

  let(:return_url) { '/return/url' }

  it "generates the correct post payload" do
    canvas_user.email = '<EMAIL>'

    sub_account = Account.create!
    sub_account.root_account = root_account
    sub_account.save!
    pseudonym = pseudonym(canvas_user, account: root_account, username: 'login_id')

    teacher_in_course(user: canvas_user, course: canvas_course, active_enrollment: true)
    course_with_ta(user: canvas_user, course: canvas_course, active_enrollment: true)
    account_admin_user(user: canvas_user, account: sub_account)

    student_in_course(user: canvas_user, course: canvas_course, active_enrollment: true).conclude

    pseudonym.sis_user_id = 'sis id!'
    pseudonym.save!

    adapter = Lti::LtiOutboundAdapter.new(canvas_tool, canvas_user, canvas_course)

    variable_expander = Lti::VariableExpander.new(root_account, canvas_course, controller, {
                                                                  current_user: canvas_user,
                                                                  current_pseudonym: pseudonym})

    adapter.prepare_tool_launch(return_url, variable_expander)
    post_payload = adapter.generate_post_payload

    expect(post_payload['oauth_consumer_key']).to eq canvas_tool.consumer_key
    expect(post_payload['oauth_version']).to eq '1.0'
    expect(post_payload['context_id']).to eq canvas_tool.opaque_identifier_for(canvas_course)
    expect(post_payload['context_label']).to eq canvas_course.course_code
    expect(post_payload['context_title']).to eq canvas_course.name
    expect(post_payload['custom_canvas_enrollment_state']).to eq 'active'
    expect(post_payload['custom_canvas_api_domain']).to eq root_account.domain
    expect(post_payload['custom_canvas_course_id']).to eq canvas_course.id.to_s
    expect(post_payload['custom_canvas_user_id']).to eq canvas_user.id.to_s
    expect(post_payload['custom_canvas_user_login_id']).to eq 'login_id'
    expect(post_payload['custom_variable_canvas_api_domain']).to eq root_account.domain
    expect(post_payload['custom_variable_canvas_assignment_id']).to eq '$Canvas.assignment.id'
    expect(post_payload['custom_variable_canvas_assignment_points_possible']).to eq '$Canvas.assignment.pointsPossible'
    expect(post_payload['custom_variable_canvas_assignment_title']).to eq '$Canvas.assignment.title'
    expect(post_payload['custom_variable_canvas_course_id']).to eq canvas_course.id.to_s
    expect(post_payload['custom_variable_canvas_enrollment_enrollment_state']).to eq 'active'
    expect(post_payload['custom_variable_canvas_membership_concluded_roles']).to eq 'Learner'
    expect(post_payload['custom_variable_canvas_user_id']).to eq canvas_user.id.to_s
    expect(post_payload['custom_variable_canvas_user_login_id']).to eq 'login_id'
    expect(post_payload['launch_presentation_document_target']).to eq 'iframe'
    expect(post_payload['launch_presentation_locale']).to eq 'en'
    expect(post_payload['launch_presentation_return_url']).to eq '/return/url'
    expect(post_payload['lis_course_offering_sourcedid']).to eq 'course_sis_id'
    expect(post_payload['lis_person_contact_email_primary']).to eq canvas_user.email
    expect(post_payload['lis_person_name_family']).to eq canvas_user.last_name
    expect(post_payload['lis_person_name_full']).to eq canvas_user.name
    expect(post_payload['lis_person_name_given']).to eq canvas_user.first_name
    expect(post_payload['lis_person_sourcedid']).to eq 'sis id!'
    expect(post_payload['lti_message_type']).to eq 'basic-lti-launch-request'
    expect(post_payload['lti_version']).to eq 'LTI-1p0'
    expect(post_payload['oauth_callback']).to eq 'about:blank'
    expect(post_payload['resource_link_id']).to eq canvas_tool.opaque_identifier_for(canvas_course)
    expect(post_payload['resource_link_title']).to eq canvas_tool.name
    expect(post_payload['roles'].split(',').sort).to eq ['Instructor', 'urn:lti:role:ims/lis/TeachingAssistant']
    expect(post_payload['ext_roles']).to eq "urn:lti:instrole:ims/lis/Administrator,urn:lti:instrole:ims/lis/Instructor,urn:lti:role:ims/lis/Instructor,urn:lti:role:ims/lis/TeachingAssistant,urn:lti:sysrole:ims/lis/User"
    expect(post_payload['tool_consumer_info_product_family_code']).to eq 'canvas'
    expect(post_payload['tool_consumer_info_version']).to eq 'cloud'
    expect(post_payload['tool_consumer_instance_contact_email']).to eq HostUrl.outgoing_email_address
    expect(post_payload['tool_consumer_instance_guid']).to eq root_account.lti_guid
{'fraction_non_alphanumeric': 0.11170640235735334, 'fraction_numerical': 0.0018751674256630056, 'mean_word_length': 4.849197023110067, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4725868', 'n_tokens_mistral': 4754, 'n_tokens_neox': 4636, 'n_words': 759}