FileToBase64List

Encode a file as base64 and store it in a Demisto list.

python · Common Scripts

Details

IDFileToBase64List
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility list

README

Encode a file as base64 and store it in a XSOAR list.

Script Data


Name Description
Script Type python2
Tags Utility, list
Cortex XSOAR Version 5.0.0

Used In


This script is used in the following playbooks and scripts.

  • Get Mails By Folder Pathes
  • Get Mails By Folder Paths

Inputs


Argument Name Description
entryId The file entry ID.
listName The list name in which to store the file. The list must exist in Demisto.
zipFile Whether to zip the file before storing it in the list.

Outputs


There are no outputs for this script.

import base64
import os
import zlib

import demistomock as demisto
from CommonServerPython import *
from FileToBase64List import get_file_data, main

from CommonServerUserPython import *

TEST_FILE_PATH = os.path.join("test_data", "file.txt")


def executeCommand(command, args=None):
    if command == "createList":
        return [{"Type": entryTypes["note"], "Contents": "List created successfully"}]
    raise ValueError(f"Unimplemented command called: {command}")


def test_file_to_base64_list(mocker):
    args = {"listName": "test", "zipFile": "no", "entryId": 1}
    mocker.patch.object(demisto, "args", return_value=args)
    mocker.patch.object(demisto, "getFilePath", return_value={"path": TEST_FILE_PATH})
    mocker.patch.object(demisto, "executeCommand", side_effect=executeCommand)
    mocker.patch.object(demisto, "results")
    result_entry = main()
    assert result_entry["HumanReadable"] == (
        "### File successfully stored in list\n|File Entry ID|List Name|Size|\n|---|---|---|\n| 1 | test | 28 |\n"
    )
    assert len(result_entry["Contents"]) > 0


def test_get_file_data(mocker):
    data = get_file_data(TEST_FILE_PATH)
    assert base64.b64decode(data).strip() == b"this is a test file"
    data = get_file_data(TEST_FILE_PATH, True)
    assert zlib.decompress(base64.b64decode(data)).strip() == b"this is a test file"