Skip to content
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

Fallback to empty list when block type is not present #22846

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/olympia/blocklist/mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ def __init__(self, storage: SafeStorage):

@cached_property
def blocked_items(self) -> List[str]:
return self._data.get(self.data_type_key(MLBFDataType.BLOCKED))
return self._data.get(self.data_type_key(MLBFDataType.BLOCKED), [])

@cached_property
def soft_blocked_items(self) -> List[str]:
return self._data.get(self.data_type_key(MLBFDataType.SOFT_BLOCKED))
return self._data.get(self.data_type_key(MLBFDataType.SOFT_BLOCKED), [])

@cached_property
def not_blocked_items(self) -> List[str]:
return self._data.get(self.data_type_key(MLBFDataType.NOT_BLOCKED))
return self._data.get(self.data_type_key(MLBFDataType.NOT_BLOCKED), [])


class MLBFDataBaseLoader(BaseMLBFLoader):
Expand Down
33 changes: 33 additions & 0 deletions src/olympia/blocklist/tests/test_mlbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ def test_loads_data_from_file(self):
loader = MLBFStorageLoader(self.storage)
assert loader._raw == self._data

def test_fallback_to_empty_list_for_missing_key(self):
for key in self._data.keys():
new_data = self._data.copy()
new_data.pop(key)
KevinMind marked this conversation as resolved.
Show resolved Hide resolved
# Generate a corrupted `cache.json` file
# (we do this for each key).
with self.storage.open('cache.json', 'w') as f:
json.dump(new_data, f)
loader = MLBFStorageLoader(self.storage)
assert loader._raw == {**new_data, key: []}
KevinMind marked this conversation as resolved.
Show resolved Hide resolved


class TestMLBFDataBaseLoader(_MLBFBase):
def test_load_returns_expected_data(self):
Expand Down Expand Up @@ -452,6 +463,28 @@ def test_diff_block_soft_to_hard(self):
),
}

def test_diff_invalid_cache(self):
KevinMind marked this conversation as resolved.
Show resolved Hide resolved
addon, _ = self._blocked_addon(file_kw={'is_signed': True})
base = MLBF.generate_from_db()
# Overwrite the cache file with an empty object
with base.storage.open(base.data._cache_path, 'w') as f:
json.dump({}, f)

previous_mlbf = MLBF.load_from_storage(base.created_at)

mlbf = MLBF.generate_from_db()

# The diff should include the blocked version because the
# corrupted cache file is replaced with empty lists
assert mlbf.generate_diffs(previous_mlbf=previous_mlbf) == {
BlockType.BLOCKED: (
MLBF.hash_filter_inputs([(addon.block.guid, addon.current_version.version)]),
[],
1,
),
BlockType.SOFT_BLOCKED: ([], [], 0),
}

def test_generate_stash_returns_expected_stash(self):
addon, block = self._blocked_addon()
block_versions = [
Expand Down
Loading