Description
While working with the connector tests, I noticed that a lot of them still depend on a _monkeypatch method defined inside the connector classes themselves to mock external services when MOCK_CONNECTIONS is enabled.
It does the job, but it feels a bit off having test-related logic sitting inside production code. Also, since this approach sometimes overrides global/module-level state, it can lead to subtle side effects, especially when the test suite grows or runs in parallel.
Another thing I noticed is that this pattern is different from what analyzers are already doing. They use a cleaner and more consistent approach with get_mocked_response(), which keeps things easier to follow.
On top of that, most of the mocked responses are currently hardcoded as Python dictionaries in the code, which makes them a bit harder to read, maintain, or reuse.
What feels problematic
- Test logic (
_monkeypatch) living inside production connector classes
- Global state being modified during tests
- Different mocking styles between connectors and analyzers
- Mock data being hardcoded instead of reusable
Suggested direction
- Start phasing out
_monkeypatch from connectors
- Align connectors with the same mocking pattern used by analyzers (e.g.,
get_mocked_response())
- Move mock responses into JSON files under the
tests/ directory
- Refactor existing tests accordingly
Affected connectors (from what I saw)
opencti.py
yeti.py
slack.py
email_sender.py
misp.py
Expected outcome
This should clean up the connector code by removing test-specific logic, make tests more predictable, and bring consistency across the project. It would also make it easier for someone new to understand how mocking is handled.
Additional context
Happy to help with this refactor if this direction makes sense. It seems like a good step toward improving overall test structure and maintainability.
Description
While working with the connector tests, I noticed that a lot of them still depend on a
_monkeypatchmethod defined inside the connector classes themselves to mock external services whenMOCK_CONNECTIONSis enabled.It does the job, but it feels a bit off having test-related logic sitting inside production code. Also, since this approach sometimes overrides global/module-level state, it can lead to subtle side effects, especially when the test suite grows or runs in parallel.
Another thing I noticed is that this pattern is different from what analyzers are already doing. They use a cleaner and more consistent approach with
get_mocked_response(), which keeps things easier to follow.On top of that, most of the mocked responses are currently hardcoded as Python dictionaries in the code, which makes them a bit harder to read, maintain, or reuse.
What feels problematic
_monkeypatch) living inside production connector classesSuggested direction
_monkeypatchfrom connectorsget_mocked_response())tests/directoryAffected connectors (from what I saw)
opencti.pyyeti.pyslack.pyemail_sender.pymisp.pyExpected outcome
This should clean up the connector code by removing test-specific logic, make tests more predictable, and bring consistency across the project. It would also make it easier for someone new to understand how mocking is handled.
Additional context
Happy to help with this refactor if this direction makes sense. It seems like a good step toward improving overall test structure and maintainability.