Skip to content

Commit

Permalink
feat: added rest delete FW endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke authored and maringuu committed Nov 27, 2024
1 parent 9d4832a commit 9c51a1d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/test/integration/web_interface/rest/test_rest_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,11 @@ def test_rest_download_with_summary(self, backend_db):
f'/rest/firmware/{test_firmware.uid}?summary=true', follow_redirects=True
)
assert test_firmware.processed_analysis['dummy']['summary'][0].encode() in request_with_summary.data

def test_rest_delete(self, backend_db):
test_firmware = create_test_firmware(device_class='test class', device_name='test device', vendor='test vendor')
backend_db.add_object(test_firmware)
assert backend_db.is_firmware(test_firmware.uid)

self.test_client.delete(f'/rest/firmware/{test_firmware.uid}')
assert backend_db.is_firmware(test_firmware.uid) is False
18 changes: 18 additions & 0 deletions src/test/unit/web_interface/rest/test_rest_firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'tags': 'tag1,tag2',
'requested_analysis_systems': ['file_type'],
}
DELETED_FILES = 1337


class DbMock(CommonDatabaseMock):
Expand All @@ -39,6 +40,12 @@ def get_complete_object_including_all_summaries(uid):
fw.processed_analysis['dummy']['summary'] = {'included_files': 'summary'}
return fw if uid == fw.uid else None

def is_firmware(self, uid):
return uid == TEST_FW.uid

def delete_firmware(self, uid):
return DELETED_FILES, DELETED_FILES


@pytest.mark.WebInterfaceUnitTestConfig(database_mock_class=DbMock)
class TestRestFirmware:
Expand Down Expand Up @@ -171,3 +178,14 @@ def test_request_with_summary(self, test_client):
result = test_client.get(f'/rest/firmware/{TEST_FW.uid}?summary=true').json
assert 'firmware' in result
assert 'summary' in result['firmware']['analysis']['dummy'], 'included file summaries should be included'

def test_delete_success(self, test_client):
result = test_client.delete(f'/rest/firmware/{TEST_FW.uid}').json
assert 'error_message' not in result
assert 'deleted_files' in result
assert result['deleted_files'] == DELETED_FILES

def test_delete_unknown_uid(self, test_client):
result = test_client.delete('/rest/firmware/foobar').json
assert 'error_message' in result
assert result['error_message'] == 'No firmware with UID foobar found'
13 changes: 13 additions & 0 deletions src/web_interface/rest/rest_firmware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import binascii
import logging
from base64 import standard_b64decode
Expand Down Expand Up @@ -214,3 +216,14 @@ def _update_analysis(self, uid, update):
if unpack:
update.append('unpacker')
return success_message({}, self.URL, {'uid': uid, 'update': update})

@roles_accepted(*PRIVILEGES['delete'])
def delete(self, uid: str) -> tuple[dict, int]:
"""
Delete existing firmware including all unique unpacked files (i.e. all files not occurring in other firmware)
and all analyses.
"""
if not self.db.frontend.is_firmware(uid):
return error_message(f'No firmware with UID {uid} found', self.URL, {'uid': uid})
_, deleted_files = self.db.admin.delete_firmware(uid)
return success_message({'deleted_files': deleted_files}, self.URL, {'uid': uid})

0 comments on commit 9c51a1d

Please sign in to comment.