Skip to content

Commit

Permalink
removed redirect in case of a single search result
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke authored and maringuu committed Sep 12, 2024
1 parent 4ece27a commit 32dbd1c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
34 changes: 15 additions & 19 deletions src/test/acceptance/test_advanced_search.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from __future__ import annotations

import json
from urllib.parse import quote

import pytest

from test.common_helper import (
assert_search_result,
create_test_file_object,
create_test_firmware,
generate_analysis_entry,
Expand Down Expand Up @@ -32,36 +35,32 @@ def test_advanced_search_get(self, test_client):
assert b'<h3 class="mb-3">Advanced Search</h3>' in rv.data

def test_advanced_search(self, test_client):
rv = test_client.post(
response = test_client.post(
'/database/advanced_search',
content_type='multipart/form-data',
data={'advanced_search': '{}'},
follow_redirects=True,
)
assert b'Please enter a valid search request' not in rv.data
assert parent_fw.uid.encode() in rv.data
assert child_fo.uid.encode() not in rv.data
assert_search_result(response, included=[parent_fw], excluded=[child_fo])

@pytest.mark.usefixtures('intercom_backend_binding')
def test_advanced_search_file_object(self, test_client):
rv = test_client.post(
response = test_client.post(
'/database/advanced_search',
content_type='multipart/form-data',
data={'advanced_search': json.dumps({'uid': child_fo.uid})},
follow_redirects=True,
)
assert b'Please enter a valid search request' not in rv.data
assert b'<strong>UID:</strong> ' + parent_fw.uid.encode() not in rv.data
assert b'<strong>UID:</strong> ' + child_fo.uid.encode() in rv.data
assert_search_result(response, included=[child_fo], excluded=[parent_fw])

def test_advanced_search_only_firmwares(self, test_client):
query = {'advanced_search': json.dumps({'uid': child_fo.uid}), 'only_firmwares': 'True'}
response = test_client.post(
'/database/advanced_search', content_type='multipart/form-data', data=query, follow_redirects=True
).data.decode()
assert 'Please enter a valid search request' not in response
assert child_fo.uid not in response
assert parent_fw.uid in response
'/database/advanced_search',
content_type='multipart/form-data',
data={'advanced_search': json.dumps({'uid': child_fo.uid}), 'only_firmwares': 'True'},
follow_redirects=True,
)
assert_search_result(response, included=[parent_fw], excluded=[child_fo])

def test_advanced_search_inverse_only_firmware(self, test_client):
query = {
Expand All @@ -71,11 +70,8 @@ def test_advanced_search_inverse_only_firmware(self, test_client):
}
response = test_client.post(
'/database/advanced_search', content_type='multipart/form-data', follow_redirects=True, data=query
).data.decode()
assert 'Please enter a valid search request' not in response
assert child_fo.uid not in response
assert f'<strong>UID:</strong> {parent_fw.uid}' not in response
assert f'<strong>UID:</strong> {other_fw.uid}' in response
)
assert_search_result(response, included=[other_fw], excluded=[child_fo, parent_fw])

def test_rest_recursive_firmware_search(self, test_client):
query = quote(json.dumps({'file_name': child_fo.file_name}))
Expand Down
16 changes: 16 additions & 0 deletions src/test/common_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
from base64 import standard_b64encode
from contextlib import contextmanager
from copy import deepcopy
from http import HTTPStatus
from pathlib import Path
from typing import TYPE_CHECKING

from helperFunctions.fileSystem import get_src_dir
from helperFunctions.tag import TagColor
from objects.file import FileObject
from objects.firmware import Firmware

if TYPE_CHECKING:
from werkzeug.test import TestResponse


def get_test_data_dir():
"""
Expand Down Expand Up @@ -315,3 +320,14 @@ def generate_analysis_entry(
'tags': tags or {},
'result': analysis_result or {},
}


def assert_search_result(response: TestResponse, included: list[FileObject], excluded: list[FileObject]):
assert response.status_code == HTTPStatus.OK
html = response.data.decode()
assert 'Browse Firmware Database' in html, 'wrong page'
assert 'Please enter a valid search request' not in html, 'error in query'
for fo in included:
assert f"href='/analysis/{fo.uid}'" in html, f'file {fo.uid} should be included in the result'
for fo in excluded:
assert f"href='/analysis/{fo.uid}'" not in html, f'file {fo.uid} should not be included in the result'
30 changes: 18 additions & 12 deletions src/test/unit/web_interface/test_app_advanced_search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

from storage.db_interface_frontend import MetaEntry
from test.common_helper import TEST_FW_2, TEST_TEXT_FILE, CommonDatabaseMock
from test.common_helper import TEST_FW_2, TEST_TEXT_FILE, CommonDatabaseMock, assert_search_result

if TYPE_CHECKING:
from werkzeug.test import TestResponse


class DbMock(CommonDatabaseMock):
Expand Down Expand Up @@ -36,29 +43,28 @@ def generic_search(
class TestAppAdvancedSearch:
def test_advanced_search(self, test_client):
response = _do_advanced_search(test_client, {'advanced_search': '{}'})
assert TEST_FW_2.uid in response
assert TEST_TEXT_FILE.uid not in response
assert_search_result(response, included=[TEST_FW_2], excluded=[TEST_TEXT_FILE])

def test_advanced_search_firmware(self, test_client):
response = _do_advanced_search(test_client, {'advanced_search': f'{{"_id": "{TEST_FW_2.uid}"}}'})
assert TEST_FW_2.uid in response
assert TEST_TEXT_FILE.uid not in response
assert_search_result(response, included=[TEST_FW_2], excluded=[TEST_TEXT_FILE])

def test_advanced_search_file_object(self, test_client):
response = _do_advanced_search(test_client, {'advanced_search': f'{{"_id": "{TEST_TEXT_FILE.uid}"}}'})
assert TEST_FW_2.uid not in response
assert TEST_TEXT_FILE.uid in response
assert_search_result(response, included=[TEST_TEXT_FILE], excluded=[TEST_FW_2])

def test_advanced_search_only_firmwares(self, test_client):
response = _do_advanced_search(
test_client,
{'advanced_search': f'{{"_id": "{TEST_TEXT_FILE.uid}"}}', 'only_firmwares': 'True'},
)
assert TEST_FW_2.uid in response
assert TEST_TEXT_FILE.uid not in response
assert_search_result(response, included=[TEST_FW_2], excluded=[TEST_TEXT_FILE])


def _do_advanced_search(test_client, query: dict) -> str:
def _do_advanced_search(test_client, query: dict) -> TestResponse:
return test_client.post(
'/database/advanced_search', data=query, content_type='multipart/form-data', follow_redirects=True
).data.decode()
'/database/advanced_search',
data=query,
content_type='multipart/form-data',
follow_redirects=True,
)
6 changes: 0 additions & 6 deletions src/web_interface/components/database_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def browse_database(self, query: str = '{}', only_firmwares=False, inverted=Fals
only_firmwares=search_parameters['only_firmware'],
inverted=search_parameters['inverted'],
)
if self._query_has_only_one_result(firmware_list, search_parameters['query']):
return redirect(url_for('show_analysis', uid=firmware_list[0][0]))
except QueryConversionException as exception:
error_message = exception.get_message()
return render_template('error.html', message=error_message)
Expand Down Expand Up @@ -126,10 +124,6 @@ def _get_search_parameters(self, query, only_firmware, inverted):
search_parameters['query'] = self._add_date_to_query(search_parameters['query'], request.args.get('date'))
return search_parameters

@staticmethod
def _query_has_only_one_result(result_list, query):
return len(result_list) == 1 and query != '{}'

def _search_database(self, query, skip=0, limit=0, only_firmwares=False, inverted=False):
meta_list = self.db.frontend.generic_search(
query, skip, limit, only_fo_parent_firmware=only_firmwares, inverted=inverted, as_meta=True
Expand Down

0 comments on commit 32dbd1c

Please sign in to comment.