-
Notifications
You must be signed in to change notification settings - Fork 468
fix: library objects synchronization on library update #2268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nas-tabchiche
wants to merge
5
commits into
main
Choose a base branch
from
CA-1034-when-updating-a-library-with-a-framework-containing-reference-controls-these-are-not-updated-correctly
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7302e7d
detach no longer used reference controls from library on lib update
nas-tabchiche bb18df9
use UPSERT strategy
nas-tabchiche 78cfd92
factorize sync methods and synchronize threats on library update
nas-tabchiche 5113309
add reasonable test coverage for LibraryUpdater's sync methods
nas-tabchiche 7a1f264
Merge branch 'main' into CA-1034-when-updating-a-library-with-a-frame…
nas-tabchiche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
import pytest | ||
|
||
from core.models import ( | ||
LibraryUpdater, | ||
LoadedLibrary, | ||
StoredLibrary, | ||
ReferenceControl, | ||
Threat, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def old_library(): | ||
"""Fixture to create a real LoadedLibrary instance in the test DB.""" | ||
return LoadedLibrary.objects.create( | ||
urn="urn:lib:old:1", | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
version=1, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def new_library_content(): | ||
"""Fixture for the content of the new StoredLibrary.""" | ||
return { | ||
"provider": "NewProvider", | ||
"dependencies": ["urn:lib:dep:1"], | ||
"reference_controls": [ | ||
{"urn": "urn:rc:1", "name": "Control One New"}, | ||
{"urn": "urn:rc:2", "name": "Control Two Updated"}, | ||
], | ||
"threats": [ | ||
{"urn": "urn:threat:100", "name": "New Threat"}, | ||
{"urn": "urn:threat:200", "name": "Threat Two Updated"}, | ||
], | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def new_library(new_library_content): | ||
"""Fixture to create a real StoredLibrary instance in the test DB.""" | ||
return StoredLibrary.objects.create( | ||
urn="urn:lib:new:1", | ||
content=new_library_content, | ||
dependencies=new_library_content["dependencies"], | ||
provider=new_library_content["provider"], | ||
version=1, | ||
) | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestLibraryUpdater: | ||
"""Test suite for the object synchronization logic in LibraryUpdater.""" | ||
|
||
def test_synchronize_full_mixed_mode_for_controls(self, old_library, new_library): | ||
"""Tests create, update, and unlink operations for ReferenceControls in one go.""" | ||
ReferenceControl.objects.create( | ||
urn="urn:rc:2", | ||
name="Control Two Original", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
ReferenceControl.objects.create( | ||
urn="urn:rc:3", | ||
name="Control Three To Unlink", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_reference_controls() | ||
|
||
assert ReferenceControl.objects.count() == 3 | ||
assert ReferenceControl.objects.get(urn="urn:rc:1").name == "Control One New" | ||
assert ( | ||
ReferenceControl.objects.get(urn="urn:rc:2").name == "Control Two Updated" | ||
) | ||
assert ReferenceControl.objects.get(urn="urn:rc:3").library is None | ||
assert len(created_objects) == 1 and created_objects[0].urn == "urn:rc:1" | ||
|
||
def test_synchronize_threats(self, old_library, new_library): | ||
"""Tests create, update, and unlink operations for Threats in one go.""" | ||
# Arrange | ||
Threat.objects.create( | ||
urn="urn:threat:200", | ||
name="Threat Two Original", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
Threat.objects.create( | ||
urn="urn:threat:300", | ||
name="Threat Three To Unlink", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_threats() | ||
|
||
assert Threat.objects.count() == 3 | ||
assert Threat.objects.get(urn="urn:threat:100").name == "New Threat" | ||
assert Threat.objects.get(urn="urn:threat:200").name == "Threat Two Updated" | ||
assert Threat.objects.get(urn="urn:threat:300").library is None | ||
assert len(created_objects) == 1 and created_objects[0].urn == "urn:threat:100" | ||
|
||
def test_synchronize_only_creates_new_objects(self, old_library, new_library): | ||
"""Tests that all incoming objects are created when the DB is empty.""" | ||
assert ReferenceControl.objects.count() == 0 | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_reference_controls() | ||
|
||
assert ReferenceControl.objects.count() == 2 | ||
assert len(created_objects) == 2 | ||
assert {obj.urn for obj in created_objects} == {"urn:rc:1", "urn:rc:2"} | ||
|
||
def test_synchronize_only_updates_existing_objects(self, old_library, new_library): | ||
"""Tests that existing objects are updated and no new ones are created.""" | ||
ReferenceControl.objects.create( | ||
urn="urn:rc:1", | ||
name="Original Name 1", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
ReferenceControl.objects.create( | ||
urn="urn:rc:2", | ||
name="Original Name 2", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
assert ReferenceControl.objects.count() == 2 | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_reference_controls() | ||
|
||
assert ReferenceControl.objects.count() == 2 | ||
assert ReferenceControl.objects.get(urn="urn:rc:1").name == "Control One New" | ||
assert ( | ||
ReferenceControl.objects.get(urn="urn:rc:2").name == "Control Two Updated" | ||
) | ||
assert len(created_objects) == 0 | ||
|
||
def test_synchronize_only_unlinks_objects(self, old_library, new_library): | ||
"""Tests that all existing objects are unlinked when incoming data is empty.""" | ||
new_library.content["reference_controls"] = [] | ||
new_library.save() | ||
ReferenceControl.objects.create( | ||
urn="urn:rc:to_unlink", | ||
name="Old Control", | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="TestProvider", | ||
) | ||
assert ReferenceControl.objects.count() == 1 | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_reference_controls() | ||
|
||
assert ReferenceControl.objects.count() == 1 | ||
assert ReferenceControl.objects.get(urn="urn:rc:to_unlink").library is None | ||
assert created_objects == [] | ||
|
||
def test_synchronize_no_changes_needed(self, old_library, new_library): | ||
"""Tests that nothing changes when DB state matches incoming data.""" | ||
rc1_data = new_library.content["reference_controls"][0] | ||
rc2_data = new_library.content["reference_controls"][1] | ||
ReferenceControl.objects.create( | ||
urn=rc1_data["urn"], | ||
name=rc1_data["name"], | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="NewProvider", | ||
is_published=True, | ||
) | ||
ReferenceControl.objects.create( | ||
urn=rc2_data["urn"], | ||
name=rc2_data["name"], | ||
library=old_library, | ||
locale="en", | ||
default_locale=True, | ||
provider="NewProvider", | ||
is_published=True, | ||
) | ||
assert ReferenceControl.objects.count() == 2 | ||
|
||
updater = LibraryUpdater(old_library, new_library) | ||
created_objects = updater.update_reference_controls() | ||
|
||
assert ReferenceControl.objects.count() == 2 | ||
assert len(created_objects) == 0 | ||
# Verify data is unchanged | ||
rc1_db = ReferenceControl.objects.get(urn=rc1_data["urn"]) | ||
assert rc1_db.name == rc1_data["name"] | ||
assert rc1_db.library == old_library | ||
nas-tabchiche marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generic sync helper can silently hijack rows & violate uniqueness.
Key issues spotted:
existing_obj_map
is built withNo
library
filter ⇒ objects that belong to another library are pulled in and laterre-assigned.
Result: cross-library objects get “stolen”, breaking referential integrity.
update_fields
contains every key pluslibrary
plusunique_field
(e.g.urn
).Updating primary identifiers is useless and risks hitting unique constraints; keep them immutable.
Union of arbitrary
item_data.keys()
goes straight intobulk_update
.A rogue field in YAML ⇒ runtime
FieldError
.Suggested minimal fix:
(You may also want to wrap the whole sync in a transaction to keep old ↔ new state consistent.)
🤖 Prompt for AI Agents