<commit_before># frozen_string_literal: true

require "test_helper"
require "base64"

module GobiertoAdmin
  module GobiertoAttachments
    class AttachmentTest < ActionDispatch::IntegrationTest
      def site
        @site ||= sites(:madrid)
      end

      def collection
        @collection ||= gobierto_common_collections(:site_attachments)
      end

      def admin
        @admin ||= gobierto_admin_admins(:nick)
      end

      def attachment_attributes
        @attachment_attributes ||= %w(id site_id name description file_name file_digest url human_readable_url human_readable_path file_size current_version created_at)
      end

      def pdf_file
        @pdf_file ||= file_fixture("gobierto_attachments/attachment/pdf-attachment.pdf")
      end

      def new_pdf_file
        @new_pdf_file ||= file_fixture("gobierto_attachments/attachment/new-pdf-attachment.pdf")
      end

      def xlsx_file
        @xlsx_file ||= file_fixture("gobierto_attachments/attachment/xlsx-attachment.xlsx")
      end

      def pdf_attachment
        gobierto_attachments_attachments(:pdf_attachment)
      end

      def attachable
        @attachable ||= gobierto_cms_pages(:consultation_faq)
      end

      def test_requests_need_authentication
        get admin_attachments_api_attachments_path(site_id: site.id)

        assert_response :unauthorized
      end

      def test_attachments_index
        login_admin_for_api(admin)

        get admin_attachments_api_attachments_path

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]
        attachment = attachments.first

        assert_response :success

        assert_equal 8, attachments.size
        assert array_match(attachment_attributes, attachment.keys)

        assert_equal "Attachment Name", attachment["name"]
        assert_equal 49, attachment["file_size"]
        assert_equal "attachment", attachment["file_name"]
        assert_equal "b6db2818dffd2fb2fc20836bebd59b87", attachment["file_digest"]
        assert_equal "http://host.com/attachments/super-long-and-ugly-aws-id/attachment", attachment["url"]
        assert_equal "Description of attachment without extension", attachment["description"]
        assert_equal 3, attachment["current_version"]
        assert_equal site.id, attachment["site_id"]
      end

      def test_attachments_index_for_attachable
        login_admin_for_api(admin)

        payload = {
          attachable_id: attachable.id,
          attachable_type: attachable.class.to_s
        }

        get admin_attachments_api_attachments_path(payload)

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]
        attachment = attachments.first

        assert_response :success

        assert_equal 1, json_response.size
        assert_equal "XLSX Attachment Name", attachment["name"]
      end

      def test_attachment_index_search
        login_admin_for_api(admin)

        payload = {
          search_string: "PDF"
        }

        ::GobiertoAttachments::Attachment.stubs(:search).returns([pdf_attachment])

        get admin_attachments_api_attachments_path(payload)

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]

        assert_response :success

        assert_equal 1, attachments.size
        assert_equal "PDF Attachment Name", attachments.first["name"]
      end

      def test_attachments_show_success
        login_admin_for_api(admin)

        get admin_attachments_api_attachment_path(pdf_attachment.id)

        json_response = JSON.parse(response.body)
        attachment = json_response["attachment"]

        assert_response :success

        assert array_match(attachment_attributes, attachment.keys)
        assert_equal "PDF Attachment Name", attachment["name"]
        assert_equal "http://host.com/attachments/super-long-and-ugly-aws-id/pdf-attachment.pdf", attachment["url"]
      end

      def test_attachments_show_error
        login_admin_for_api(admin)

        unexistent_id = 666

        get admin_attachments_api_attachment_path(unexistent_id)<commit_msg>Fix test<commit_after># frozen_string_literal: true

require "test_helper"
require "base64"

module GobiertoAdmin
  module GobiertoAttachments
    class AttachmentTest < ActionDispatch::IntegrationTest
      def setup
        super

        PgSearch::Multisearch.rebuild(::GobiertoAttachments::Attachment)
      end

      def site
        @site ||= sites(:madrid)
      end

      def collection
        @collection ||= gobierto_common_collections(:site_attachments)
      end

      def admin
        @admin ||= gobierto_admin_admins(:nick)
      end

      def attachment_attributes
        @attachment_attributes ||= %w(id site_id name description file_name file_digest url human_readable_url human_readable_path file_size current_version created_at)
      end

      def pdf_file
        @pdf_file ||= file_fixture("gobierto_attachments/attachment/pdf-attachment.pdf")
      end

      def new_pdf_file
        @new_pdf_file ||= file_fixture("gobierto_attachments/attachment/new-pdf-attachment.pdf")
      end

      def xlsx_file
        @xlsx_file ||= file_fixture("gobierto_attachments/attachment/xlsx-attachment.xlsx")
      end

      def pdf_attachment
        gobierto_attachments_attachments(:pdf_attachment)
      end

      def attachable
        @attachable ||= gobierto_cms_pages(:consultation_faq)
      end

      def test_requests_need_authentication
        get admin_attachments_api_attachments_path(site_id: site.id)

        assert_response :unauthorized
      end

      def test_attachments_index
        login_admin_for_api(admin)

        get admin_attachments_api_attachments_path

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]
        attachment = attachments.first

        assert_response :success

        assert_equal 8, attachments.size
        assert array_match(attachment_attributes, attachment.keys)

        assert_equal "Attachment Name", attachment["name"]
        assert_equal 49, attachment["file_size"]
        assert_equal "attachment", attachment["file_name"]
        assert_equal "b6db2818dffd2fb2fc20836bebd59b87", attachment["file_digest"]
        assert_equal "http://host.com/attachments/super-long-and-ugly-aws-id/attachment", attachment["url"]
        assert_equal "Description of attachment without extension", attachment["description"]
        assert_equal 3, attachment["current_version"]
        assert_equal site.id, attachment["site_id"]
      end

      def test_attachments_index_for_attachable
        login_admin_for_api(admin)

        payload = {
          attachable_id: attachable.id,
          attachable_type: attachable.class.to_s
        }

        get admin_attachments_api_attachments_path(payload)

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]
        attachment = attachments.first

        assert_response :success

        assert_equal 1, json_response.size
        assert_equal "XLSX Attachment Name", attachment["name"]
      end

      def test_attachment_index_search
        login_admin_for_api(admin)

        payload = {
          search_string: "PDF attachment available in AWS"
        }

        get admin_attachments_api_attachments_path(payload)

        json_response = JSON.parse(response.body)
        attachments = json_response["attachments"]

        assert_response :success

        assert_equal 1, attachments.size
        assert_equal "PDF attachment available in AWS", attachments.first["name"]
      end

      def test_attachments_show_success
        login_admin_for_api(admin)

        get admin_attachments_api_attachment_path(pdf_attachment.id)

        json_response = JSON.parse(response.body)
        attachment = json_response["attachment"]

        assert_response :success

        assert array_match(attachment_attributes, attachment.keys)
        assert_equal "PDF Attachment Name", attachment["name"]
        assert_equal "http://host.com/attachments/super-long-and-ugly-aws-id/pdf-attachment.pdf", attachment["url"]
      end

      def test_attachments_show_error
        login_admin_for_api(admin)

        unexistent_id = 666

        get admin_attachments_api_attachment_path(unexistent_id)
{'fraction_non_alphanumeric': 0.07304846025304369, 'fraction_numerical': 0.00644545237526856, 'mean_word_length': 3.0993150684931505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '3405625', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2311, 'n_words': 424}