# frozen_string_literal: true

#
# Copyright (C) 2021 - present 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_relative "../../../../import_helper"

describe CC::Importer::Canvas::LtiResourceLinkConverter do
  subject do
    Class.new do
      include CC::Importer::Canvas::LtiResourceLinkConverter

      def initialize(manifest, path)
        @manifest = manifest
        @package_root = PackageRoot.new(path)
      end
    end.new(manifest, path)
  end

  let(:manifest) { ImportHelper.get_import_data_xml("unzipped", "imsmanifest") }
  let(:path) { File.expand_path(File.dirname(__FILE__) + "/../../../../fixtures/importer/unzipped") }
  let(:lti_resource_links) { subject.convert_lti_resource_links }

  describe "#convert_lti_resource_links" do
    describe "with import_numeric_lti_custom_params_as_string FF on" do
      before do
        Account.site_admin.enable_feature!(:import_numeric_lti_custom_params_as_string)
      end

      it "extract custom params and lookup_uuid" do
        expect(lti_resource_links).to include(
          a_hash_including(
            custom: {
              param1: "some string",
              param2: "1",
              param3: "2.56",
              param4: true,
              param5: false,
              param6: "a12.5",
              param7: "5d781f15-c6b0-4901-a1f7-2a77e7bf4982",
              param8: "+1(855)552-2338",
              param9: "0011",
            },
            lookup_uuid: "1b302c1e-c0a2-42dc-88b6-c029699a7c7a",
            launch_url: "http://lti13testtool.docker/launch"
          )
        )
      end
    end

    describe "with import_numeric_lti_custom_params_as_string FF off" do
      before do
        Account.site_admin.disable_feature!(:import_numeric_lti_custom_params_as_string)
      end

      it "extract custom params and lookup_uuid" do
        expect(lti_resource_links).to include(
          a_hash_including(
            custom: {
              param1: "some string",
              param2: 1,
              param3: 2.56,
              param4: true,
              param5: false,
              param6: "a12.5",
              param7: "5d781f15-c6b0-4901-a1f7-2a77e7bf4982",
              param8: "+1(855)552-2338",
              param9: "0011",
            },
            lookup_uuid: "1b302c1e-c0a2-42dc-88b6-c029699a7c7a",
            launch_url: "http://lti13testtool.docker/launch"
          )
        )
      end
    end

    context 'with a "secure_launch_url"' do
      it "creates a resource link with the secure launch URL" do
        expect(lti_resource_links).to include(
          a_hash_including(
            custom: {
              param1: "some string",
              param2: "1",
              param3: "2.56",
              param4: true,
              param5: false,
              param6: "a12.5",
              param7: "5d781f15-c6b0-4901-a1f7-2a77e7bf4982",
              param8: "+1(855)552-2338"
            },
            lookup_uuid: "123412345c1e-c0a2-42dc-88b6-c029699a7c7a",
            launch_url: "https://lti13testtool.docker/launch/secure"
          )
        )
      end
    end
  end

  it "ignores resource links without a URL" do
    expect(lti_resource_links).not_to include(
      a_hash_including(
        lookup_uuid: "567812345c1e-c0a2-42dc-88b6-c029699a7c7a"
      )
    )
  end

  it "retrieves the assignment_migration_id from the resource link" do
    expect(lti_resource_links).to include(
      a_hash_including(
        assignment_migration_id: "76cf9398-0f63-4be2-a196-8e21d44716de"
      )
    )
  end
end
