Add exhaustive pytest test suite (363 tests)#185
Merged
Conversation
Cover all modules: c_flag, c_badge, c_certification, c_exam, c_fillinblank, c_multiplechoice, c_navigator, errors, base_flag, certificate_svc, training_api, hook, and all flag submodules (advanced, adversaries, agents, autonomous, operations, developers, plugins/compass/manx/response, attack, manual). Includes shared conftest with framework stubs for standalone testing.
There was a problem hiding this comment.
Pull request overview
Adds an extensive pytest-based test suite for the training plugin, aiming to validate core models, services, hooks, and all flag categories without requiring a Caldera server runtime.
Changes:
- Added many new unit tests (incl. TrainingApi, CertificateService, hook, models, and flag modules).
- Introduced
tests/conftest.pywith stubs/fixtures to run tests standalone (mocking Caldera/framework deps). - Added
pytest.inito configure pytest discovery and asyncio behavior.
Reviewed changes
Copilot reviewed 26 out of 39 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_training_api.py | Adds broad coverage for TrainingApi endpoints/helpers. |
| tests/test_hook.py | Tests plugin hook entrypoints (enable, expansion, flag loading helpers). |
| tests/test_certificate_svc.py | Tests CertificateService signing/indexing/issuance logic with temp dirs. |
| tests/conftest.py | Provides shared fixtures and module stubs to run tests without Caldera/reportlab/aiohttp runtime. |
| tests/app/test_navigator.py | Tests Navigator flag behavior and helpers. |
| tests/app/test_multiplechoice.py | Tests MultipleChoice correctness, display contract, and multi-select. |
| tests/app/test_flag.py | Tests Flag base behavior (activation, completion timestamps, display, storage). |
| tests/app/test_fillinblank.py | Tests FillInBlank verification and display contract. |
| tests/app/test_exam.py | Tests Exam model display/unique/store behaviors. |
| tests/app/test_errors.py | Tests exception hierarchy/behavior for training plugin errors. |
| tests/app/test_certification.py | Expands Certification tests for badge/flag lookup, display, storage, unique. |
| tests/app/test_base_flag.py | Tests BaseFlag static helpers and standard verification/reset flows. |
| tests/app/test_badge.py | Expands Badge tests for display, unique, and multi-flag behavior. |
| tests/app/flags/plugins/test_flags.py | Adds tests for plugin flags (compass/manx/response). |
| tests/app/flags/operations/test_flags.py | Adds tests for operations flags 0–3. |
| tests/app/flags/manual/test_flags.py | Adds tests for manual blue flags. |
| tests/app/flags/developers/test_flags.py | Adds tests for developer flags. |
| tests/app/flags/autonomous/test_flags.py | Adds tests for autonomous blue flags. |
| tests/app/flags/attack/test_flags.py | Adds tests for ATT&CK quiz blue flags. |
| tests/app/flags/agents/test_flags.py | Adds tests for agent-related red flags. |
| tests/app/flags/agents/test_blue_flags.py | Adds tests for agent-related blue flags. |
| tests/app/flags/adversaries/test_flags.py | Adds tests for adversary-related flags. |
| tests/app/flags/advanced/test_flag2.py | Adds tests for advanced flag 2. |
| tests/app/flags/advanced/test_flag1.py | Adds tests for advanced flag 1. |
| tests/app/flags/advanced/test_flag0.py | Expands tests for advanced flag 0 (contact validation + verify). |
| pytest.ini | Configures pytest to discover tests and use asyncio auto mode. |
Comments suppressed due to low confidence (4)
tests/test_training_api.py:1
AnswerFlagdefinesverifytwice; the second (sync) method overrides the first (async) one. If any code path doesawait flag.verify(services)this will raise at runtime (awaiting a non-coroutine / bool). Use two distinct method names (e.g., keepasync def verify(self, services)and add a separatedef verify_answer(self, answer)), or use the realFillInBlankclass in this test instead of overriding the contract ofFlag.verify.
tests/conftest.py:1conftest.pyunconditionally injects anappmodule intosys.modules, which can silently override a real Calderaapppackage if it exists in the environment (e.g., running these tests from a larger Caldera checkout / monorepo). Prefer only stubbing when the import truly fails (try/except import), or gate this behavior behind an env var so the suite can run against real framework modules when available.
tests/conftest.py:1- This stub replaces
aiohttpentirely, which can mask incompatibilities between the realaiohttp.webAPI and the plugin code (and can also break other test modules in the same run that rely on real aiohttp). If aiohttp is a manageable test dependency, consider using the real package; otherwise, keep the stub constrained (e.g., only patch the specific symbols used viamonkeypatchin the tests that need it, rather than globally overridingsys.modules).
tests/app/test_flag.py:1 - This assertion is a tautology and will always pass, so it doesn’t validate any behavior. If the intent is “doesn’t crash,” explicitly assert that the property returns a boolean; if the intent is to validate the missing-file behavior, patch the filesystem check (e.g.,
os.path.exists) toFalseand asserthas_solution_guide is False.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+50
to
+61
| async def test_correct_chain_length(self): | ||
| """Verifies the chain-length check independently.""" | ||
| op = MagicMock() | ||
| op.chain = [MagicMock()] | ||
| services = {'data_svc': AsyncMock()} | ||
| services['data_svc'].locate = AsyncMock(return_value=[op]) | ||
| # Patch the function to isolate the chain-length logic | ||
| with patch.object(BaseFlag, 'is_operation_successful', wraps=None) as mock_fn: | ||
| # Just test the underlying logic: chain length == num_links | ||
| assert len(op.chain) == 1 | ||
|
|
||
| @pytest.mark.asyncio |
- test_base_flag.py: rewrite test_correct_chain_length to actually call await BaseFlag.is_operation_successful() with mocked data_svc and a mock operation, asserting it returns True when chain length and traits match expectations
Contributor
Author
|
@github-copilot review |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
tests/conftest.pywith shared fixtures and framework stubs for standalone testing (no Caldera server needed)pytest.inifor test configurationModules Covered
c_flag,c_badge,c_certification,c_exam,c_fillinblank,c_multiplechoice,c_navigator,errorsbase_flag(all static helpers),certificate_svc(signing, issuance, index),training_api(all endpoints)enable,expansion,_load_flags,_apply_hidden_accessflags/advanced(flag_0, flag_1, flag_2)flags/adversaries(flag_0, flag_1, flag_2)flags/agents(flag_0-7, blue_0-3)flags/autonomous(blue_0, blue_1, blue_3)flags/operations(flag_0-3)flags/developers(flag_0, flag_2, flag_6, flag_7)flags/plugins(compass/flag_0, manx/flag_0-1, response/flag_0-1)flags/attack(blue_0, blue_1)flags/manual(blue_0, blue_1a)Test plan
pytest tests/ -v-- 0 failures)