Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding Github Unittests workflow #22

Merged
merged 46 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
704f040
feat: Adding Github Unittests workflow
lucifercr07 Jun 13, 2024
db0e0a9
Using pytest for workflow
lucifercr07 Jun 18, 2024
81d9431
Reverting tests refactoring
lucifercr07 Jun 19, 2024
1f4a9af
fix: add setup/teardown to tests and test dependencies to workflow
Patterbear Jun 19, 2024
fda99cb
fix: edit path slashes to work with linux
Patterbear Jun 19, 2024
1c21160
revert: path slashes
Patterbear Jun 19, 2024
c695375
fix: restore all path slashes
Patterbear Jun 19, 2024
0ab3722
fix: add pythonpath to .toml
Patterbear Jun 19, 2024
1f9f590
fix: command format for validation tests
Patterbear Jun 19, 2024
a002fd1
fix: command format for all tests
Patterbear Jun 19, 2024
a30f3fb
fix: remove pyproject changes
Patterbear Jun 19, 2024
965771d
revert: restore pyproject changes
Patterbear Jun 19, 2024
b87c77f
fix: alter pytest pythonpath
Patterbear Jun 19, 2024
9673014
fix: comment out erroneous code
Patterbear Jun 19, 2024
b171bfe
revert: uncomment code
Patterbear Jun 19, 2024
24e6607
fix: change file size check method
Patterbear Jun 19, 2024
6742839
revert: getsize method re-added
Patterbear Jun 19, 2024
9c119ec
fix: add testpaths to pyproject.toml
Patterbear Jun 19, 2024
1c2cb53
fix: remove pythonpath from pyproject.toml
Patterbear Jun 19, 2024
7cfc24a
revert: return pythonpath to pyproject.toml
Patterbear Jun 19, 2024
3870701
fix: add absolute path detection
Patterbear Jun 19, 2024
5a9cad5
fix: file backup and restore bug
Patterbear Jun 19, 2024
0229a86
fix: comment out size check for bug fixing
Patterbear Jun 19, 2024
7fe6987
fix: remove setup and teardown
Patterbear Jun 19, 2024
8074d1f
fix: restore setup and teardown
Patterbear Jun 19, 2024
d2dc333
fix: add ls statement to workflow for debug
Patterbear Jun 19, 2024
393ad24
fix: add working directory to yml
Patterbear Jun 19, 2024
01957a3
fix: add absolute path to test scripts
Patterbear Jun 19, 2024
121d54b
fix: add cwd test
Patterbear Jun 19, 2024
c369dfe
fix: add function setup
Patterbear Jun 19, 2024
47f87fd
fix: add function teardown
Patterbear Jun 19, 2024
0a15f6c
fix: add set cwd to setup module function
Patterbear Jun 19, 2024
557e86a
revert: remove attempts to set cwd as problem is elsewhere
Patterbear Jun 19, 2024
9629d10
revert: add absolute paths for files (again)
Patterbear Jun 19, 2024
989d382
revert: add ls command to workflow (again)
Patterbear Jun 19, 2024
e62e3c4
revert: remove absolute paths
Patterbear Jun 19, 2024
0c19456
revert: remove erroneous path characters
Patterbear Jun 19, 2024
0f35586
revert: re-add pythonpath
Patterbear Jun 19, 2024
91be6cd
fix: attempt absolute path in test function
Patterbear Jun 19, 2024
24004bf
fix: change test order
Patterbear Jun 19, 2024
956284d
fix: comment out setup/teardown
Patterbear Jun 19, 2024
c508c05
revert: uncomment setup/teardown
Patterbear Jun 19, 2024
ad6f49e
fix: move function into working file
Patterbear Jun 19, 2024
ec0bba2
fix: remove first test file
Patterbear Jun 19, 2024
4db3246
revert: restore test file
Patterbear Jun 19, 2024
66ad502
fix: remove error-causing setup/teardown
Patterbear Jun 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"