Skip to content

Commit

Permalink
feat: Adding Github Unittests workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifercr07 committed Jun 18, 2024
1 parent 84bcbc5 commit 7fd60c2
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 169 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
id: setup-python
uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pylint poetry
poetry install
- name: Run tests
run: poetry run pytest
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
A command-line tool designed to detect and purge any and all macros and dynamic content from commonly used office document formats (including MS Office Files, PDFs, etc.).

[![Super-Linter](https://github.com/Anti-Malware-Alliance/ms-office-macro-bleach/actions/workflows/pre-commit-actions.yml/badge.svg)](https://github.com/marketplace/actions/super-linter)
![Unit Tests](https://github.com/Anti-Malware-Alliance/ms-office-macro-bleach/actions/workflows/unit-tests.yml/badge.svg)


## Supported formats

Expand Down
120 changes: 69 additions & 51 deletions tests/test_file_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,75 @@
All tests are written for and conducted using pytest.
"""

from subprocess import check_output
from os import remove, rename
import unittest
from subprocess import check_output, CalledProcessError
import os
from shutil import copyfile

program_dir = "../docubleach/"


def test_valid_file_with_macros():
copyfile("test_files/valid_file_with_macros.docm", "test_files/valid_file_with_macros.bak")

output = check_output(f"python {program_dir}bleach.py test_files/valid_file_with_macros.docm", encoding='utf-8')

remove("test_files/valid_file_with_macros.docm")
rename("test_files/valid_file_with_macros.bak", "test_files/valid_file_with_macros.docm")

assert output == ""


def test_valid_file_with_macros_with_check():
copyfile("test_files/valid_file_with_macros_check.docm", "test_files/valid_file_with_macros_check.bak")

output = check_output(f"python {program_dir}bleach.py test_files/valid_file_with_macros_check.docm -c",
encoding='utf-8')

remove("test_files/valid_file_with_macros_check.docm")
rename("test_files/valid_file_with_macros_check.bak", "test_files/valid_file_with_macros_check.docm")

assert output == "Macros detected and removed.\n"


def test_valid_file_without_macros():
output = check_output(f"python {program_dir}bleach.py test_files/valid_file_without_macros.docx", encoding='utf-8')

assert output == ""


def test_invalid_file_type():
output = check_output(f"python {program_dir}bleach.py test_files/invalid_file_type.txt", encoding='utf-8')

assert output == "Unsupported file format.\n"


def test_invalid_file_size():

# Create temporary file exceeding 200MB limit
with open("test_files/invalid_file_size.docx", "wb") as out:
out.truncate(262144000)

output = check_output(f"python {program_dir}bleach.py test_files/invalid_file_size.docx", encoding='utf-8')

remove("test_files/invalid_file_size.docx")

assert output == "File exceeds size limit.\n"
class TestFileValidation(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cur_dir = os.getcwd()
cls.test_dir = os.path.join(cls.cur_dir, 'tests')
cls.test_resources_dir = os.path.join(cls.test_dir, 'test_files')
cls.program_dir = os.path.join(cls.cur_dir, "docubleach", "bleach.py")

# Verify the existence of the paths
if not os.path.exists(cls.program_dir):
raise FileNotFoundError(f"Program file not found: {cls.program_dir}")

def run_test(self, filename, expected_output, run_check=False):
try:
if run_check:
output = check_output(
["python", self.program_dir, os.path.join(self.test_resources_dir, filename), "-c"],
encoding='utf-8'
)
else:
output = check_output(
["python", self.program_dir, os.path.join(self.test_resources_dir, filename)],
encoding='utf-8'
)

self.assertEqual(output, expected_output)
except CalledProcessError as e:
self.fail(f"Subprocess failed with error: {e}")

def run_macro_test(self, filename, expected_output, run_check=False):
backup_file = os.path.join(self.test_resources_dir, f"{filename}.bak")
test_file = os.path.join(self.test_resources_dir, filename)

copyfile(test_file, backup_file)
try:
self.run_test(filename, expected_output, run_check)
finally:
os.remove(test_file)
os.rename(backup_file, test_file)

def test_valid_file_with_macros(self):
self.run_macro_test("valid_file_with_macros.docm", "")

def test_valid_file_with_macros_with_check(self):
self.run_macro_test("valid_file_with_macros_check.docm",
"Macros detected and removed.\n", True)

def test_valid_file_without_macros(self):
self.run_macro_test("valid_file_without_macros.docx", "")

def test_invalid_file_type(self):
self.run_macro_test("invalid_file_type.txt", "Unsupported file format.\n")

def test_invalid_file_size(self):
try:
# Create temporary file exceeding 200MB limit
with open(os.path.join(self.test_resources_dir, "invalid_file_size.docx"), "wb") as out:
out.truncate(262144000)

self.run_macro_test("invalid_file_size.docx", "File exceeds size limit.\n")
finally:
os.remove(os.path.join(self.test_resources_dir, "invalid_file_size.docx"))


if __name__ == '__main__':
unittest.main()
179 changes: 61 additions & 118 deletions tests/test_ooxml_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,144 +8,87 @@
All tests are written for and conducted using pytest.
"""
from subprocess import check_output
from os import remove, rename
import unittest
import os
from subprocess import check_output, CalledProcessError
from shutil import copyfile


program_dir = "../docubleach/"
class OOXMLFilesTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cur_dir = os.getcwd()
cls.test_dir = os.path.join(cls.cur_dir, 'tests')
cls.test_resources_dir = os.path.join(cls.test_dir, 'test_files')
cls.program_dir = os.path.join(cls.cur_dir, "docubleach", "bleach.py")

# Verify the existence of the paths
if not os.path.exists(cls.program_dir):
raise FileNotFoundError(f"Program file not found: {cls.program_dir}")

def test_word_document():
output = check_output(f"python {program_dir}bleach.py test_files/word_document.docx -c", encoding='utf-8')
def run_test(self, filename, expected_output):
try:
output = check_output(
["python", self.program_dir, os.path.join(self.test_resources_dir, filename), "-c"],
encoding='utf-8'
)
self.assertEqual(output, expected_output)
except CalledProcessError as e:
self.fail(f"Subprocess failed with error: {e}")

assert output == ""
def run_macro_test(self, filename, expected_output):
backup_file = os.path.join(self.test_resources_dir, f"{filename}.bak")
test_file = os.path.join(self.test_resources_dir, filename)

copyfile(test_file, backup_file)
try:
self.run_test(filename, expected_output)
finally:
os.remove(test_file)
os.rename(backup_file, test_file)

def test_word_document_with_macros():
copyfile("test_files/word_document_with_macros.docm", "test_files/word_document_with_macros.docm.bak")
def test_word_document(self):
self.run_test('word_document.docx', "")

output = check_output(f"python {program_dir}bleach.py test_files/word_document_with_macros.docm -c",
encoding='utf-8')
def test_word_document_with_macros(self):
self.run_macro_test('word_document_with_macros.docm', "Macros detected and removed.\n")

remove("test_files/word_document_with_macros.docm")
rename("test_files/word_document_with_macros.docm.bak", "test_files/word_document_with_macros.docm")
def test_word_template(self):
self.run_test('word_template.dotx', "")

assert output == "Macros detected and removed.\n"
def test_word_template_with_macros(self):
self.run_macro_test('word_template_with_macros.dotm', "Macros detected and removed.\n")

def test_powerpoint_presentation(self):
self.run_test('powerpoint_presentation.pptx', "")

def test_word_template():
output = check_output(f"python {program_dir}bleach.py test_files/word_template.dotx -c", encoding='utf-8')
def test_powerpoint_presentation_with_macros(self):
self.run_macro_test('powerpoint_presentation_with_macros.pptm', "Macros detected and removed.\n")

assert output == ""
def test_powerpoint_template(self):
self.run_test('powerpoint_template.potx', "")

def test_powerpoint_template_with_macros(self):
self.run_macro_test('powerpoint_template_with_macros.potm', "Macros detected and removed.\n")

def test_word_template_with_macros():
copyfile("test_files/word_template_with_macros.dotm", "test_files/word_template_with_macros.dotm.bak")
def test_powerpoint_show(self):
self.run_test('powerpoint_show.ppsx', "")

output = check_output(f"python {program_dir}bleach.py test_files/word_template_with_macros.dotm -c",
encoding='utf-8')
def test_powerpoint_show_with_macros(self):
self.run_macro_test('powerpoint_show_with_macros.ppsm', "Macros detected and removed.\n")

remove("test_files/word_template_with_macros.dotm")
rename("test_files/word_template_with_macros.dotm.bak", "test_files/word_template_with_macros.dotm")
def test_excel_spreadsheet(self):
self.run_test('excel_spreadsheet.xlsx', "")

assert output == "Macros detected and removed.\n"
def test_excel_spreadsheet_with_macros(self):
self.run_macro_test('excel_spreadsheet_with_macros.xlsm', "Macros detected and removed.\n")

def test_excel_template(self):
self.run_test('excel_template.xltx', "")

def test_powerpoint_presentation():
output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_presentation.pptx -c",
encoding='utf-8')
def test_excel_template_with_macros(self):
self.run_macro_test('excel_template_with_macros.xltm', "Macros detected and removed.\n")

assert output == ""


def test_powerpoint_presentation_with_macros():
copyfile("test_files/powerpoint_presentation_with_macros.pptm",
"test_files/powerpoint_presentation_with_macros.pptm.bak")

output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_presentation_with_macros.pptm -c",
encoding='utf-8')

remove("test_files/powerpoint_presentation_with_macros.pptm")
rename("test_files/powerpoint_presentation_with_macros.pptm.bak",
"test_files/powerpoint_presentation_with_macros.pptm")

assert output == "Macros detected and removed.\n"


def test_powerpoint_template():
output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_template.potx -c",
encoding='utf-8')

assert output == ""


def test_powerpoint_template_with_macros():
copyfile("test_files/powerpoint_template_with_macros.potm",
"test_files/powerpoint_template_with_macros.potm.bak")

output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_template_with_macros.potm -c",
encoding='utf-8')

remove("test_files/powerpoint_template_with_macros.potm")
rename("test_files/powerpoint_template_with_macros.potm.bak",
"test_files/powerpoint_template_with_macros.potm")

assert output == "Macros detected and removed.\n"


def test_powerpoint_show():
output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_show.ppsx -c",
encoding='utf-8')

assert output == ""


def test_powerpoint_show_with_macros():
copyfile("test_files/powerpoint_show_with_macros.ppsm", "test_files/powerpoint_show_with_macros.ppsm.bak")

output = check_output(f"python {program_dir}bleach.py test_files/powerpoint_show_with_macros.ppsm -c",
encoding='utf-8')

remove("test_files/powerpoint_show_with_macros.ppsm")
rename("test_files/powerpoint_show_with_macros.ppsm.bak", "test_files/powerpoint_show_with_macros.ppsm")

assert output == "Macros detected and removed.\n"


def test_excel_spreadsheet():
output = check_output(f"python {program_dir}bleach.py test_files/excel_spreadsheet.xlsx -c",
encoding='utf-8')

assert output == ""


def test_excel_spreadsheet_with_macros():
copyfile("test_files/excel_spreadsheet_with_macros.xlsm",
"test_files/excel_spreadsheet_with_macros.xlsm.bak")

output = check_output(f"python {program_dir}bleach.py test_files/excel_spreadsheet_with_macros.xlsm -c",
encoding='utf-8')

remove("test_files/excel_spreadsheet_with_macros.xlsm")
rename("test_files/excel_spreadsheet_with_macros.xlsm.bak", "test_files/excel_spreadsheet_with_macros.xlsm")

assert output == "Macros detected and removed.\n"


def test_excel_template():
output = check_output(f"python {program_dir}bleach.py test_files/excel_template.xltx -c", encoding='utf-8')

assert output == ""


def test_excel_template_with_macros():
copyfile("test_files/excel_template_with_macros.xltm", "test_files/excel_template_with_macros.xltm.bak")

output = check_output(f"python {program_dir}bleach.py test_files/excel_template_with_macros.xltm -c",
encoding='utf-8')

remove("test_files/excel_template_with_macros.xltm")
rename("test_files/excel_template_with_macros.xltm.bak", "test_files/excel_template_with_macros.xltm")

assert output == "Macros detected and removed.\n"
if __name__ == '__main__':
unittest.main()

0 comments on commit 7fd60c2

Please sign in to comment.