<commit_before>require "spec_helper"

describe PublishingAdapter do
  let(:publishing_api_schema_name_for_manual) {
    ManualPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
  }

  let(:publishing_api_document_type_for_manual) {
    ManualPublishingAPIExporter::PUBLISHING_API_DOCUMENT_TYPE
  }

  let(:publishing_api_schema_name_for_section) {
    SectionPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
  }

  let(:publishing_api_document_type_for_section) {
    SectionPublishingAPIExporter::PUBLISHING_API_DOCUMENT_TYPE
  }

  let(:timestamp) { Time.zone.parse("2017-01-01 00:00:00") }

  let(:publishing_api) { double(:publishing_api) }
  let(:organisations) { double(:organisations_adapter) }

  let(:manual_id) { "a55242ed-178f-4716-8bb3-5d4f82d38531" }

  let(:manual) {
    Manual.build(
      id: manual_id,
      slug: "manual-slug",
      organisation_slug: "organisation-slug",
      title: "manual-title",
      summary: "manual-summary",
      body: "manual-body",
    )
  }

  let(:section_uuid) { "64dbf396-b637-40b7-ada4-f19ce460e5e9" }

  let(:section) {
    Section.new(
      manual: manual,
      uuid: section_uuid,
      editions: [section_edition],
    )
  }

  let(:section_edition) {
    SectionEdition.new(
      slug: "manual-slug/section-slug",
      title: "section-title",
      summary: "section-summary",
      body: "section-body"
    )
  }

  let(:organisation_content_id) { "afa741e9-c68e-4ade-bc8f-ceb1fced23a6" }

  let(:organisation) {
    Organisation.new(
      title: "organisation-title",
      abbreviation: "organisation-abbreviation",
      content_id: organisation_content_id,
      web_url: "organisation-web-url",
    )
  }

  let(:publication_logs) { [] }

  before do
    allow(Services).to receive(:publishing_api).and_return(publishing_api)
    allow(Adapters).to receive(:organisations).and_return(organisations)

    allow(PublicationLog).to receive(:change_notes_for).with("manual-slug")
      .and_return(publication_logs)
  end

  describe "#save" do
    before do
      manual.sections = [section]

      allow(manual).to receive(:version_type).and_return(:new)
      allow(section).to receive(:needs_exporting?).and_return(true)

      allow(organisations).to receive(:find).with("organisation-slug").and_return(organisation)
      allow(publishing_api).to receive(:patch_links).with(anything, anything)
      allow(publishing_api).to receive(:put_content).with(anything, anything)
    end

    it "saves links for manual to Publishing API" do
      expect(publishing_api).to receive(:patch_links).with(
        manual_id,
        links: {
          organisations: [organisation_content_id],
          sections: [section_uuid]
        }
      )

      subject.save(manual)
    end

    it "saves links for manual to Publishing API with valid attributes" do
      expect(publishing_api).to receive(:patch_links).with(
        manual_id,
        be_valid_against_links_schema(
          ManualPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
        )
      )

      subject.save(manual)
    end

    it "saves content for manual to Publishing API" do
      expect(publishing_api).to receive(:put_content).with(
        manual_id,
        base_path: "/manual-slug",
        schema_name: publishing_api_schema_name_for_manual,
        document_type: publishing_api_document_type_for_manual,
        title: "manual-title",
        description: "manual-summary",
        update_type: "major",
        publishing_app: "manuals-publisher",
        rendering_app: "manuals-frontend",
        routes: [
          {
            path: "/manual-slug",
            type: "exact",
          },
          {
            path: "/manual-slug/updates",
            type: "exact",
          }
        ],
        details: {
          body: [
            {
              content_type: "text/govspeak",
              content: "manual-body"
            },
            {
              content_type: "text/html",
              content: "<p>manual-body</p>\n"
            }
          ],
          child_section_groups:  [
            {
              title: "Contents",
              child_sections: [
                {
                  title: "section-title",
                  description: "section-summary",
                  base_path: "/manual-slug/section-slug",
                }
              ],
            }
          ],
          change_notes: [],
          organisations: [
            {
              title: "organisation-title",
              abbreviation: "organisation-abbreviation",
              web_url: "organisation-web-url",
            }
          ]
        },
        locale: "en",
      )

      subject.save(manual)
    end

    it "saves links for all manual's sections to Publishing API" do
      expect(publishing_api).to receive(:patch_links).with(
        section_uuid,
        links: {
          organisations: [organisation_content_id],
          manual: [manual_id]
        }
      )

      subject.save(manual)
    end

    it "saves links for all manual's sections to Publishing API with valid attributes" do
      expect(publishing_api).to receive(:patch_links).with(
        section_uuid,
        be_valid_against_links_schema(
          SectionPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
        )
      )

      subject.save(manual)
    end

    it "saves content for all manual's sections to Publishing API" do
      expect(publishing_api).to receive(:put_content).with(
        section_uuid,
        base_path: "/manual-slug/section-slug",
        schema_name: publishing_api_schema_name_for_section,
        document_type: publishing_api_document_type_for_section,
        title: "section-title",
        description: "section-summary",
        update_type: "major",
        publishing_app: "manuals-publisher",
        rendering_app: "manuals-frontend",
        routes: [<commit_msg>DRY up constants in PublishingAdapter spec<commit_after>require "spec_helper"

describe PublishingAdapter do
  let(:publishing_api_schema_name_for_manual) {
    ManualPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
  }

  let(:publishing_api_document_type_for_manual) {
    ManualPublishingAPIExporter::PUBLISHING_API_DOCUMENT_TYPE
  }

  let(:publishing_api_schema_name_for_section) {
    SectionPublishingAPIExporter::PUBLISHING_API_SCHEMA_NAME
  }

  let(:publishing_api_document_type_for_section) {
    SectionPublishingAPIExporter::PUBLISHING_API_DOCUMENT_TYPE
  }

  let(:timestamp) { Time.zone.parse("2017-01-01 00:00:00") }

  let(:publishing_api) { double(:publishing_api) }
  let(:organisations) { double(:organisations_adapter) }

  let(:manual_id) { "a55242ed-178f-4716-8bb3-5d4f82d38531" }

  let(:manual) {
    Manual.build(
      id: manual_id,
      slug: "manual-slug",
      organisation_slug: "organisation-slug",
      title: "manual-title",
      summary: "manual-summary",
      body: "manual-body",
    )
  }

  let(:section_uuid) { "64dbf396-b637-40b7-ada4-f19ce460e5e9" }

  let(:section) {
    Section.new(
      manual: manual,
      uuid: section_uuid,
      editions: [section_edition],
    )
  }

  let(:section_edition) {
    SectionEdition.new(
      slug: "manual-slug/section-slug",
      title: "section-title",
      summary: "section-summary",
      body: "section-body"
    )
  }

  let(:organisation_content_id) { "afa741e9-c68e-4ade-bc8f-ceb1fced23a6" }

  let(:organisation) {
    Organisation.new(
      title: "organisation-title",
      abbreviation: "organisation-abbreviation",
      content_id: organisation_content_id,
      web_url: "organisation-web-url",
    )
  }

  let(:publication_logs) { [] }

  before do
    allow(Services).to receive(:publishing_api).and_return(publishing_api)
    allow(Adapters).to receive(:organisations).and_return(organisations)

    allow(PublicationLog).to receive(:change_notes_for).with("manual-slug")
      .and_return(publication_logs)
  end

  describe "#save" do
    before do
      manual.sections = [section]

      allow(manual).to receive(:version_type).and_return(:new)
      allow(section).to receive(:needs_exporting?).and_return(true)

      allow(organisations).to receive(:find).with("organisation-slug").and_return(organisation)
      allow(publishing_api).to receive(:patch_links).with(anything, anything)
      allow(publishing_api).to receive(:put_content).with(anything, anything)
    end

    it "saves links for manual to Publishing API" do
      expect(publishing_api).to receive(:patch_links).with(
        manual_id,
        links: {
          organisations: [organisation_content_id],
          sections: [section_uuid]
        }
      )

      subject.save(manual)
    end

    it "saves links for manual to Publishing API with valid attributes" do
      expect(publishing_api).to receive(:patch_links).with(
        manual_id,
        be_valid_against_links_schema(
          publishing_api_schema_name_for_manual
        )
      )

      subject.save(manual)
    end

    it "saves content for manual to Publishing API" do
      expect(publishing_api).to receive(:put_content).with(
        manual_id,
        base_path: "/manual-slug",
        schema_name: publishing_api_schema_name_for_manual,
        document_type: publishing_api_document_type_for_manual,
        title: "manual-title",
        description: "manual-summary",
        update_type: "major",
        publishing_app: "manuals-publisher",
        rendering_app: "manuals-frontend",
        routes: [
          {
            path: "/manual-slug",
            type: "exact",
          },
          {
            path: "/manual-slug/updates",
            type: "exact",
          }
        ],
        details: {
          body: [
            {
              content_type: "text/govspeak",
              content: "manual-body"
            },
            {
              content_type: "text/html",
              content: "<p>manual-body</p>\n"
            }
          ],
          child_section_groups:  [
            {
              title: "Contents",
              child_sections: [
                {
                  title: "section-title",
                  description: "section-summary",
                  base_path: "/manual-slug/section-slug",
                }
              ],
            }
          ],
          change_notes: [],
          organisations: [
            {
              title: "organisation-title",
              abbreviation: "organisation-abbreviation",
              web_url: "organisation-web-url",
            }
          ]
        },
        locale: "en",
      )

      subject.save(manual)
    end

    it "saves links for all manual's sections to Publishing API" do
      expect(publishing_api).to receive(:patch_links).with(
        section_uuid,
        links: {
          organisations: [organisation_content_id],
          manual: [manual_id]
        }
      )

      subject.save(manual)
    end

    it "saves links for all manual's sections to Publishing API with valid attributes" do
      expect(publishing_api).to receive(:patch_links).with(
        section_uuid,
        be_valid_against_links_schema(
          publishing_api_schema_name_for_section
        )
      )

      subject.save(manual)
    end

    it "saves content for all manual's sections to Publishing API" do
      expect(publishing_api).to receive(:put_content).with(
        section_uuid,
        base_path: "/manual-slug/section-slug",
        schema_name: publishing_api_schema_name_for_section,
        document_type: publishing_api_document_type_for_section,
        title: "section-title",
        description: "section-summary",
        update_type: "major",
        publishing_app: "manuals-publisher",
        rendering_app: "manuals-frontend",
        routes: [
{'fraction_non_alphanumeric': 0.106007669365147, 'fraction_numerical': 0.011589262888794206, 'mean_word_length': 2.3977996525767225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887437', 'n_tokens_mistral': 3786, 'n_tokens_neox': 3565, 'n_words': 563}