PDFUnlocker

Removing the password protection from a PDF file and adding a new file entry with the unlocked PDF.

python · Common Scripts

Details

IDPDFUnlocker
Languagepython
From Version5.0.0
Docker Imagedemisto/readpdf:1.0.0.9067966

README

Removes the password protection from a PDF file and adds a new file entry with the unlocked PDF.

Script Data


Name Description
Script Type python
Tags -

Inputs


Argument Name Description
entryID The entry ID with the PDF file.
password The password for the PDF file.

Outputs


There are no outputs for this script.

import demistomock as demisto


class TestPDFUnlocker:
    @staticmethod
    def mock_results(mocker):
        mocker.patch.object(demisto, "results")

    @staticmethod
    def mock_context(mocker, args_value=None):
        if not args_value:
            args_value = {"entryID": "entry_id", "password": "test"}
        mocker.patch.object(demisto, "args", return_value=args_value)

    @staticmethod
    def mock_file_path(mocker, path, name):
        mocker.patch.object(demisto, "getFilePath", return_value={"path": path, "name": name})

    @staticmethod
    def mock_demisto(mocker, args_value=None, file_obj=None):
        TestPDFUnlocker.mock_results(mocker)
        TestPDFUnlocker.mock_context(mocker, args_value)
        if file_obj:
            TestPDFUnlocker.mock_file_path(mocker, **file_obj)

    @staticmethod
    def get_demisto_results():
        return demisto.results.call_args[0][0]

    @staticmethod
    def create_file_object(file_path):
        return {"path": file_path, "name": file_path.split("/")[-1]}

    def test_parse_word_doc(self, mocker):
        """
        Given:
            - A PDF encrypted file

        When:
            - Run the PDFUnlocker script

        Then:
            - Verify that the pdf file was unlocked.

        """
        from PDFUnlocker import main

        self.mock_demisto(mocker, file_obj=self.create_file_object("./test_data/sample.pdf"))
        main()
        result = self.get_demisto_results()
        assert result.get("File") == "UNLOCKED_sample.pdf"

    def test_unlock_encrypted_with_quotation_mark(self, mocker):
        """
        Given:
            - A PDF file encrypted by a password that contains quotation mark.

        When:
            - Run the PDFUnlocker script.

        Then:
            - Verify that the pdf file was unlocked.

        """
        from PDFUnlocker import main

        self.mock_demisto(
            mocker,
            file_obj=self.create_file_object("./test_data/Testpdf_.pdf"),
            args_value={"entryID": "entry_id", "password": '"12345"'},
        )
        main()
        result = self.get_demisto_results()
        assert result.get("File") == "UNLOCKED_Testpdf_.pdf"

    def test_unlock_encrypted_with_incorrect_password(self, mocker):
        """
        Given:
            - A PDF file encrypted by a password, but incorrect password is provided.

        When:
            - Run the PDFUnlocker script.

        Then:
            - Verify that the expected error is thrown.

        """
        from PDFUnlocker import main

        self.mock_demisto(
            mocker,
            file_obj=self.create_file_object("./test_data/Testpdf_.pdf"),
            args_value={"entryID": "entry_id", "password": "123"},
        )
        return_error_mock = mocker.patch("PDFUnlocker.return_error")

        main()
        assert return_error_mock.call_count == 1
        assert return_error_mock.call_args[0][0] == "Incorrect password. Please provide the correct password."