Skip to content

Improve webservice coverage#186

Draft
wuan wants to merge 19 commits intomainfrom
webservice_coverage
Draft

Improve webservice coverage#186
wuan wants to merge 19 commits intomainfrom
webservice_coverage

Conversation

@wuan
Copy link
Copy Markdown
Owner

@wuan wuan commented Apr 23, 2026

No description provided.

wuan and others added 14 commits April 23, 2026 09:45
- Add comprehensive tests for Blitzortung class (65 tests)
- Move application and connection pool setup to service/base.py
- Make services injectable via constructor for testing
- Keep BLITZORTUNG_TEST guard in service/base.py for test isolation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add try-except around log setup to handle permission errors gracefully.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Fix NameError by moving LogObserver definition before application setup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Move Blitzortung and LogObserver classes to service/base.py
- Keep webservice.py as minimal entry point for twistd
- Tests now import from service/base.py

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Matches the new location of the Blitzortung class in service/base.py.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The tests import from service/base.py which has no side effects.
The webservice.py is only loaded by twistd.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Not needed since tests use mocks and don't need this factory.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

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

Refactors the Blitzortung JSON-RPC webservice implementation into a dedicated blitzortung.service.base module and adds a new unit test suite to improve coverage.

Changes:

  • Added blitzortung/service/base.py containing Blitzortung and LogObserver.
  • Refactored blitzortung/cli/webservice.py to import the new service module and start the server after connection pool creation.
  • Added tests/service/test_base.py to cover key service behaviors and helpers; updated start_webservice entry point imports.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.

File Description
tests/service/test_base.py Adds new tests for Blitzortung and LogObserver.
blitzortung/service/base.py Introduces shared webservice implementation (moved from CLI module).
blitzortung/cli/webservice.py Simplifies twistd entry module to use the extracted service implementation and async pool init.
blitzortung/cli/start_webservice.py Adjusts twistd launcher imports for the refactored webservice module.

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

Comment thread tests/service/test_base.py Outdated
Comment thread tests/service/test_base.py Outdated
Comment thread tests/service/test_base.py Outdated
Comment thread blitzortung/cli/webservice.py Outdated
Comment thread blitzortung/cli/webservice.py Outdated
Comment thread blitzortung/cli/webservice.py
Comment thread blitzortung/cli/start_webservice.py Outdated
Comment thread blitzortung/service/base.py Outdated
Comment thread blitzortung/service/base.py Outdated
wuan and others added 3 commits April 23, 2026 11:29
- Add tests for jsonrpc_get_strikes_grid
- Add tests for jsonrpc_get_global_strikes_grid
- Add tests for jsonrpc_get_local_strikes_grid
- Add tests for __check_period and __restart_period

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix test name: test_removes_header_for_max_version
- Fix LogObserver test to not test buggy path
- Add valid user-agent to forbidden IP test
- Catch OSError and log instead of silent Exception handling
- Remove manual startService() call (relies on setServiceParent)
- Add back reactor installation for performance
- Remove redundant import from start_webservice.py
- Remove unused imports from service/base.py
- Fix get_global_strikes_grid to use global_strike_grid_query

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The reactor was already installed by twistd, so importing 'reactor'
gives the instance, not the module. Fixed by importing epollreactor
or kqreactor module explicitly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

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 4 out of 4 changed files in this pull request and generated 7 comments.


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

Comment thread blitzortung/cli/webservice.py Outdated
Comment on lines +433 to +443

mock_strike_grid_query = Mock()
mock_strike_grid_query.create = Mock(return_value=(Mock(), Mock()))
mock_strike_grid_query.combine_result = Mock(return_value=Mock())

forbidden_ips = {'192.168.1.100': True}

bt = Blitzortung(
mock_pool,
None,
strike_grid_query=mock_strike_grid_query,
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

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

These tests instantiate Blitzortung without providing strike_query/global_strike_grid_query/histogram_query. The Blitzortung constructor will then call blitzortung.service._query() which depends on blitzortung.INJECTOR; in unit tests this is likely uninitialized and can cause test failures. Pass mocks for all query dependencies (or patch blitzortung.service._query) to keep the test isolated.

Suggested change
mock_strike_grid_query = Mock()
mock_strike_grid_query.create = Mock(return_value=(Mock(), Mock()))
mock_strike_grid_query.combine_result = Mock(return_value=Mock())
forbidden_ips = {'192.168.1.100': True}
bt = Blitzortung(
mock_pool,
None,
strike_grid_query=mock_strike_grid_query,
mock_strike_query = Mock()
mock_strike_grid_query = Mock()
mock_strike_grid_query.create = Mock(return_value=(Mock(), Mock()))
mock_strike_grid_query.combine_result = Mock(return_value=Mock())
mock_global_strike_grid_query = Mock()
mock_histogram_query = Mock()
forbidden_ips = {'192.168.1.100': True}
bt = Blitzortung(
mock_pool,
None,
strike_query=mock_strike_query,
strike_grid_query=mock_strike_grid_query,
global_strike_grid_query=mock_global_strike_grid_query,
histogram_query=mock_histogram_query,

Copilot uses AI. Check for mistakes.
Comment thread tests/service/test_base.py
Comment thread blitzortung/service/base.py Outdated
Comment thread blitzortung/service/base.py Outdated
Comment thread blitzortung/service/base.py Outdated
Comment thread blitzortung/service/base.py Outdated
wuan and others added 2 commits April 23, 2026 12:12
- Fix reactor installation to catch ReactorAlreadyInstalledError in kqreactor branch
- Fix referer check to use 'not request.getHeader(\'referer\')'
- Fix X-Forwarded-For parsing to handle different separators
- Improve test isolation by adding missing mocks for query dependencies

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Validates region is within 1-7 range to prevent KeyError when
accessing grid[region]. Uses __force_range for consistency.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

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.

2 participants