Skip to content

Commit

Permalink
Merge pull request #22 from Anti-Malware-Alliance/lucifercr07/adding_…
Browse files Browse the repository at this point in the history
…unit_tests_pre_commit_hook

feat: Adding Github Unittests workflow
  • Loading branch information
Patterbear authored Jun 19, 2024
2 parents b538bd9 + 66ad502 commit fe8a59c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 74 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 --with test
- name: List directory structure
run: |
ls -R
- 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
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ optional = true
pytest = "^8.2.2"


[tool.pytest.ini_options]
pythonpath = ["."]


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
26 changes: 9 additions & 17 deletions tests/test_file_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,36 @@
"""

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


prog_dir = "docubleach/"
test_dir = "tests/test_files/"


def test_valid_file_with_macros():
copyfile(f"{test_dir}valid_file_with_macros.docm", f"{test_dir}valid_file_with_macros.bak")

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

remove(f"{test_dir}valid_file_with_macros.docm")
rename(f"{test_dir}valid_file_with_macros.bak", f"{test_dir}valid_file_with_macros.docm")
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros.docm"],
encoding='utf-8')

assert output == ""


def test_valid_file_with_macros_with_check():
copyfile(f"{test_dir}valid_file_with_macros_check.docm", f"{test_dir}valid_file_with_macros_check.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_with_macros_check.docm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_with_macros_check.docm", "-c"],
encoding='utf-8')

remove(f"{test_dir}valid_file_with_macros_check.docm")
rename(f"{test_dir}valid_file_with_macros_check.bak", f"{test_dir}valid_file_with_macros_check.docm")

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


def test_valid_file_without_macros():
output = check_output(f"python {prog_dir}bleach.py {test_dir}valid_file_without_macros.docx", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}valid_file_without_macros.docx"],
encoding='utf-8')

assert output == ""


def test_invalid_file_type():
output = check_output(f"python {prog_dir}bleach.py {test_dir}invalid_file_type.txt", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_type.txt"], encoding='utf-8')

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

Expand All @@ -64,7 +56,7 @@ def test_invalid_file_size():
with open(f"{test_dir}invalid_file_size.docx", "wb") as out:
out.truncate(262144000)

output = check_output(f"python {prog_dir}bleach.py {test_dir}invalid_file_size.docx", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}invalid_file_size.docx"], encoding='utf-8')

remove(f"{test_dir}invalid_file_size.docx")

Expand Down
72 changes: 15 additions & 57 deletions tests/test_ooxml_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,144 +9,102 @@
All tests are written for and conducted using pytest.
"""
from subprocess import check_output
from os import remove, rename
from shutil import copyfile


prog_dir = "docubleach/"
test_dir = "tests/test_files/"


def test_word_document():
output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document.docx -c", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document.docx", "-c"], encoding='utf-8')

assert output == ""


def test_word_document_with_macros():
copyfile(f"{test_dir}word_document_with_macros.docm", f"{test_dir}word_document_with_macros.docm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}word_document_with_macros.docm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_document_with_macros.docm", "-c"],
encoding='utf-8')

remove(f"{test_dir}word_document_with_macros.docm")
rename(f"{test_dir}word_document_with_macros.docm.bak", f"{test_dir}word_document_with_macros.docm")

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


def test_word_template():
output = check_output(f"python {prog_dir}bleach.py {test_dir}word_template.dotx -c", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template.dotx", "-c"], encoding='utf-8')

assert output == ""


def test_word_template_with_macros():
copyfile(f"{test_dir}word_template_with_macros.dotm", f"{test_dir}word_template_with_macros.dotm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}word_template_with_macros.dotm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}word_template_with_macros.dotm", "-c"],
encoding='utf-8')

remove(f"{test_dir}word_template_with_macros.dotm")
rename(f"{test_dir}word_template_with_macros.dotm.bak", f"{test_dir}word_template_with_macros.dotm")

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


def test_powerpoint_presentation():
output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_presentation.pptx -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation.pptx", "-c"],
encoding='utf-8')

assert output == ""


def test_powerpoint_presentation_with_macros():
copyfile(f"{test_dir}powerpoint_presentation_with_macros.pptm",
f"{test_dir}powerpoint_presentation_with_macros.pptm.bak")

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

remove(f"{test_dir}powerpoint_presentation_with_macros.pptm")
rename(f"{test_dir}powerpoint_presentation_with_macros.pptm.bak",
f"{test_dir}powerpoint_presentation_with_macros.pptm")
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_presentation_with_macros.pptm",
"-c"], encoding='utf-8')

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


def test_powerpoint_template():
output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_template.potx -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_template.potx", "-c"],
encoding='utf-8')

assert output == ""


def test_powerpoint_template_with_macros():
copyfile(f"{test_dir}powerpoint_template_with_macros.potm",
f"{test_dir}powerpoint_template_with_macros.potm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_template_with_macros.potm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_template_with_macros.potm", "-c"],
encoding='utf-8')

remove(f"{test_dir}powerpoint_template_with_macros.potm")
rename(f"{test_dir}powerpoint_template_with_macros.potm.bak",
f"{test_dir}powerpoint_template_with_macros.potm")

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


def test_powerpoint_show():
output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_show.ppsx -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_show.ppsx", "-c"],
encoding='utf-8')

assert output == ""


def test_powerpoint_show_with_macros():
copyfile(f"{test_dir}powerpoint_show_with_macros.ppsm", f"{test_dir}powerpoint_show_with_macros.ppsm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}powerpoint_show_with_macros.ppsm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}powerpoint_show_with_macros.ppsm", "-c"],
encoding='utf-8')

remove(f"{test_dir}powerpoint_show_with_macros.ppsm")
rename(f"{test_dir}powerpoint_show_with_macros.ppsm.bak", f"{test_dir}powerpoint_show_with_macros.ppsm")

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


def test_excel_spreadsheet():
output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_spreadsheet.xlsx -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_spreadsheet.xlsx", "-c"],
encoding='utf-8')

assert output == ""


def test_excel_spreadsheet_with_macros():
copyfile(f"{test_dir}excel_spreadsheet_with_macros.xlsm",
f"{test_dir}excel_spreadsheet_with_macros.xlsm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_spreadsheet_with_macros.xlsm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_spreadsheet_with_macros.xlsm", "-c"],
encoding='utf-8')

remove(f"{test_dir}excel_spreadsheet_with_macros.xlsm")
rename(f"{test_dir}excel_spreadsheet_with_macros.xlsm.bak", f"{test_dir}excel_spreadsheet_with_macros.xlsm")

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


def test_excel_template():
output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_template.xltx -c", encoding='utf-8')
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_template.xltx", "-c"], encoding='utf-8')

assert output == ""


def test_excel_template_with_macros():
copyfile(f"{test_dir}excel_template_with_macros.xltm", f"{test_dir}excel_template_with_macros.xltm.bak")

output = check_output(f"python {prog_dir}bleach.py {test_dir}excel_template_with_macros.xltm -c",
output = check_output(["python", f"{prog_dir}bleach.py", f"{test_dir}excel_template_with_macros.xltm", "-c"],
encoding='utf-8')

remove(f"{test_dir}excel_template_with_macros.xltm")
rename(f"{test_dir}excel_template_with_macros.xltm.bak", f"{test_dir}excel_template_with_macros.xltm")

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

0 comments on commit fe8a59c

Please sign in to comment.