Skip to content

feat(tests): add comprehensive tests for cleanup.py. Closes #699#705

Merged
regulartim merged 4 commits intointelowlproject:developfrom
opbot-xd:feat-test-cleanup-699
Jan 15, 2026
Merged

feat(tests): add comprehensive tests for cleanup.py. Closes #699#705
regulartim merged 4 commits intointelowlproject:developfrom
opbot-xd:feat-test-cleanup-699

Conversation

@opbot-xd
Copy link
Copy Markdown
Contributor

Description

This PR implements comprehensive unit tests for greedybear/cronjobs/cleanup.py as requested in issue #699. This logic was previously untested.

The tests ensure that the cleanup job correctly:

  • Initializes with default or injected repositories.
  • Calcluates the correct expiration dates for IOCs, Cowrie Sessions, and Command Sequences based on settings.
  • Calls the appropriate deletion methods on the IocRepository and CowrieSessionRepository with those dates.
  • Handles edge cases like zero deletions correctly.

Related issues

Closes #699

Type of change

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).

Checklist

  • I have read and understood the rules about how to Contribute to this project.
  • The pull request is for the branch develop.
  • I have added documentation of the new features.
  • Linter (Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.
  • I have added tests for the feature/bug I solved. All the tests (new and old ones) gave 0 errors.
  • If changes were made to an existing model/serializer/view, the docs were updated and regenerated (check CONTRIBUTE.md).
  • If the GUI has been modified:
    • I have a provided a screenshot of the result in the PR.
    • I have created new frontend tests for the new component or updated existing ones.

Code Coverage Report

Ran tests for greedybear/cronjobs/cleanup.py:

Name                             Stmts   Miss  Cover
----------------------------------------------------
greedybear/cronjobs/cleanup.py      29      0   100%
----------------------------------------------------
TOTAL                               29      0   100%

Copilot AI review requested due to automatic review settings January 13, 2026 18:41
@opbot-xd
Copy link
Copy Markdown
Contributor Author

Method used for coverage:

docker exec greedybear_uwsgi pip install coverage
docker exec greedybear_uwsgi coverage run --source=greedybear.cronjobs.cleanup manage.py test tests.greedybear.cronjobs.test_cleanup
docker exec greedybear_uwsgi coverage report

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive unit tests for the CleanUp cronjob class, which was previously untested. The tests verify that the cleanup job correctly initializes with repositories, calculates retention dates, and calls the appropriate deletion methods.

Changes:

  • Added three test methods covering initialization, normal operation with deletions, and edge case with zero deletions
  • Achieved 100% code coverage for greedybear/cronjobs/cleanup.py

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@opbot-xd
Copy link
Copy Markdown
Contributor Author

Following the Copilot suggestion, I updated line 78 in tests/greedybear/cronjobs/test_cleanup.py. I replaced the loose assertTrue(count >= 10) with a strict assertEqual(count, 10) to ensure we are verifying the exact number of expected log messages.

@opbot-xd
Copy link
Copy Markdown
Contributor Author

Extended Code Coverage Analysis

I ran a broader coverage check across the greedybear/cronjobs module to ensure my changes integrate well and to view the current state of testing.

Command:
coverage run --source=greedybear/cronjobs manage.py test tests.greedybear.cronjobs tests.test_mass_scanners --keepdb

Results:

File Coverage Status
greedybear/cronjobs/cleanup.py 100% This PR
greedybear/cronjobs/monitor_honeypots.py 100% Full coverage
greedybear/cronjobs/monitor_logs.py 100% Full coverage
greedybear/cronjobs/mass_scanners.py 97% High coverage
greedybear/cronjobs/firehol.py 94% High coverage
greedybear/cronjobs/extract.py 0% Needs attention (Out of scope)
greedybear/cronjobs/whatsmyip.py 0% Needs attention (Out of scope)

@regulartim
Copy link
Copy Markdown
Collaborator

Thanks for your coverage check. I'll open an issue for testing greedybear/cronjobs/whatsmyip.py.

Copy link
Copy Markdown
Collaborator

@regulartim regulartim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks! 👍

- Mock IOC_RETENTION, COMMAND_SEQUENCE_RETENTION, and COWRIE_SESSION_RETENTION
- Use fixed values (100, 90, 80 days) to ensure tests are environment-independent
- Remove dependency on settings module imports
- Addresses reviewer feedback about environment variable brittleness
Copilot AI review requested due to automatic review settings January 15, 2026 13:38
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

expected_ioc_date = datetime.now() - timedelta(days=100)
# Check that the date passed is approximately correct (within 1 second)
args, _ = ioc_repo.delete_old_iocs.call_args
self.assertAlmostEqual(args[0], expected_ioc_date, delta=timedelta(seconds=1))
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using assertAlmostEqual with datetime objects is problematic. The assertAlmostEqual method is designed for numeric comparisons, and while datetime objects can be subtracted to produce timedeltas, the comparison logic may not work correctly with timedelta delta values. Instead, manually calculate the difference and assert it's within an acceptable timedelta range, or convert to timestamps before comparison.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of that @opbot-xd ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! I completely agree with Copilot's assessment. I've updated the tests to manually calculate the time difference using .total_seconds() and then assert that it's within the acceptable range using assertLess.

This approach is more explicit and provides clearer error messages if the assertion fails. The updated implementation now shows exactly how many seconds the difference is, making debugging much easier.

All tests still pass with the new approach. ✅

- Replace assertAlmostEqual with manual timedelta calculations
- Use assertLess for clearer error messages on failure
- Convert to CustomTestCase for proper test fixture inheritance
- Addresses Copilot feedback about assertAlmostEqual with datetime objects
@opbot-xd opbot-xd requested a review from regulartim January 15, 2026 13:58
@regulartim regulartim merged commit f42b941 into intelowlproject:develop Jan 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants