test: Add unit tests for encrypt_pdf function#6
test: Add unit tests for encrypt_pdf function#6ambicuity wants to merge 2 commits intoSUPAIDEAS:mainfrom
Conversation
Add unit tests for encrypt_pdf function to verify functionality, including output file creation, encryption status, decryption with correct password, page count preservation, and handling of invalid input.
|
Thanks @ambicuity for the PR. Even otherwise, will review soon and merge if all looks good. |
|
@ambicuity , can you please resolve the conflicts? Also, check if CI goes Green, then we can merge |
There was a problem hiding this comment.
Pull request overview
This PR adds comprehensive unit tests for the encrypt_pdf() function to improve test coverage. The tests verify that the function correctly encrypts PDF files, preserves content during encryption, and handles error cases appropriately.
Changes:
- Added 5 new integration-style unit tests covering file creation, encryption verification, decryption, page count preservation, and error handling
- Renamed test class from
TestPdfUnitTeststoTestEncryptPdffor better clarity - Added poetry.lock file with dependency specifications
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/unittests/test_encryptpdf.py | Added 5 new unit tests for encrypt_pdf function with proper setup/teardown and docstrings |
| poetry.lock | Auto-generated lock file specifying exact versions of all dependencies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if os.path.exists(self.temp_dir): | ||
| os.rmdir(self.temp_dir) |
There was a problem hiding this comment.
The tearDown method uses os.rmdir() which will fail if the directory is not empty. If a test fails before removing the output_pdf file, or if multiple files are created during testing, the directory removal will raise an OSError. Consider using shutil.rmtree(self.temp_dir) instead, or wrap it in a try-except block for more robust cleanup.
| # Decrypt with correct password | ||
| decrypt_result = reader.decrypt(self.test_password) | ||
|
|
||
| self.assertNotEqual( | ||
| decrypt_result, | ||
| 0, |
There was a problem hiding this comment.
The assertion pattern here is inconsistent with the integration test at tests/integrationtests/test_encryptpdf_integration.py:40, which uses self.assertTrue(reader.decrypt(password)). While both approaches work (decrypt returns 0 for failure and non-zero for success), consider using assertTrue for consistency with the existing test pattern.
| # Decrypt with correct password | |
| decrypt_result = reader.decrypt(self.test_password) | |
| self.assertNotEqual( | |
| decrypt_result, | |
| 0, | |
| # Decrypt with correct password and assert success | |
| self.assertTrue( | |
| reader.decrypt(self.test_password), |
Summary
This PR adds comprehensive unit tests for the
encrypt_pdf()function to improve test coverage and ensure reliable PDF encryption functionality.Linked Issue
Fixes #2
Changes Made
Added 5 unit tests in
tests/unittests/test_encryptpdf.py:Testing
All tests use:
unittest.TestCaseframework (consistent with existing tests)tests/resources/Sample_PDF.pdfChecklist