From 549858cba3316f338250618dcf58ec3207172e31 Mon Sep 17 00:00:00 2001 From: Colin B Date: Wed, 25 Oct 2023 17:22:56 +0100 Subject: [PATCH 1/5] feat: record metadata view --- app/__init__.py | 6 +- app/main/routes.py | 36 ++++++++++ app/templates/main/poc-search.html | 2 +- app/templates/main/record.html | 102 ++++++++++++++--------------- app/tests/test_record_metadata.py | 66 +++++++++++++++++++ 5 files changed, 159 insertions(+), 53 deletions(-) create mode 100644 app/tests/test_record_metadata.py diff --git a/app/__init__.py b/app/__init__.py index 781a2123..65acc0b3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -21,7 +21,11 @@ def null_to_dash(value): - return "—" if value == "null" else value + if value == "null": + return "-" + if value is None: + return "-" + return value def create_app(config_class=Config): diff --git a/app/main/routes.py b/app/main/routes.py index 29b9a1f1..6174c57b 100644 --- a/app/main/routes.py +++ b/app/main/routes.py @@ -99,6 +99,7 @@ def poc_search(): search_logic.generate_open_search_client_and_make_poc_search(query) ) results = open_search_response["hits"]["hits"] + session["search_results"] = results num_records_found = len(results) @@ -110,6 +111,41 @@ def poc_search(): ) +@bp.route("/record", methods=["GET"]) +def record(): + """ + Render the record details page. + + This function retrieves search results from the session, looks for a specific + record based on the 'record_id' provided in the query parameters, and renders + the record details on the 'record.html' template. + + Returns: + A rendered HTML page with record details. + """ + # Retrieve the search results from the session + results = session.get("search_results", []) + + # Get the record_id from the query parameters + record_id = request.args.get("record_id") + + if not record_id: + return render_template("404.html") + + # Find the specific record in the search results + record_details = None + + for result in results: + if result["_source"]["id"] == record_id: + record_details = result["_source"] + break + + if not record_details: + return render_template("404.html") + + return render_template("record.html", consignment_files=record_details) + + @bp.route("/browse", methods=["GET"]) def browse(): return render_template("browse.html") diff --git a/app/templates/main/poc-search.html b/app/templates/main/poc-search.html index 1ff920c8..5716763c 100644 --- a/app/templates/main/poc-search.html +++ b/app/templates/main/poc-search.html @@ -46,7 +46,7 @@

{{ num_records_found }} records found

{% for record in results %} - {{ record._source.file_name }} + {{ record._source.file_name }} {{ record._source.description }} {{ record._source.date_last_modified }} {{ record._source.legal_status }} diff --git a/app/templates/main/record.html b/app/templates/main/record.html index ec6d6a26..ba3231b7 100644 --- a/app/templates/main/record.html +++ b/app/templates/main/record.html @@ -6,11 +6,11 @@ {% block pageTitle %}Record – {{config['SERVICE_NAME']}} – GOV.UK{% endblock %} {% block beforeContent %} - {{ super() }} - {{ govukBackLink({ - 'text': "Back", - 'href': url_for('main.index') - }) }} +{{ super() }} +{{ govukBackLink({ +'text': "Back", +'href': url_for('main.poc_search') +}) }} {% endblock %} {% block content %} @@ -20,54 +20,54 @@

Record metadata

-
-
-
ID
-
{{ consignment_files._source.id | null_to_dash }}
-
-
-
Source organisation
-
{{ consignment._source.Source_Organization | null_to_dash }}
-
-
-
Consignment ID
-
{{ consignment_files._source.join_field.parent | null_to_dash }}
-
-
-
File name
-
{{ consignment_files._source.file_name | null_to_dash }}
-
-
-
Description
-
{{ consignment_files._source.description | null_to_dash }}
-
-
-
Rights copyright
-
{{ consignment_files._source.rights_copyright | null_to_dash }}
+
+
+
ID
+
{{ consignment_files.id | null_to_dash }}
-
-
Legal status
-
{{ consignment_files._source.legal_status | null_to_dash }}
-
-
-
Held by
-
{{ consignment_files._source.held_by | null_to_dash }}
-
-
-
Last modified
-
{{ consignment_files._source.date_last_modified | null_to_dash }}
-
-
-
Closure type
-
{{ consignment_files._source.closure_type | null_to_dash }}
-
-
-
Language
-
{{ consignment_files._source.language | null_to_dash }}
-
-
+
+
Source organisation
+
{{ consignment_files.Source_Organization | null_to_dash }}
+
+
+
Consignment ID
+
{{ consignment_files.join_field.parent | null_to_dash }}
+
+
+
File name
+
{{ consignment_files.file_name | null_to_dash }}
+
+
+
Description
+
{{ consignment_files.description | null_to_dash }}
+
+
+
Rights copyright
+
{{ consignment_files.rights_copyright | null_to_dash }}
+
+
+
Legal status
+
{{ consignment_files.legal_status | null_to_dash }}
+
+
+
Held by
+
{{ consignment_files.held_by | null_to_dash }}
+
+
+
Last modified
+
{{ consignment_files.date_last_modified | null_to_dash }}
+
+
+
Closure type
+
{{ consignment_files.closure_type | null_to_dash }}
+
+
+
Language
+
{{ consignment_files.language | null_to_dash }}
+
+
-
+ {% endblock %} diff --git a/app/tests/test_record_metadata.py b/app/tests/test_record_metadata.py new file mode 100644 index 00000000..2371dac7 --- /dev/null +++ b/app/tests/test_record_metadata.py @@ -0,0 +1,66 @@ +import re +from playwright.sync_api import Page, expect +import time + + +def test_page_title_and_header(page: Page): + """ + Given the user accesses AYR + When the user loads the record page + Then the AYR record title should be displayed + """ + page.goto("http://localhost:5000/record") + expect(page).to_have_title( + re.compile("Page not found – AYR - Access Your Records – GOV.UK") + ) + expect(page.get_by_text("Page not found")).to_be_visible() + + +def test_invalid_record(page: Page): + """ + Given the user accesses AYR + When the user loads an invalid record page + Then the AYR 404 page should be displayed + """ + page.goto("http://localhost:5000/record") + expect(page.get_by_text("Page not found")).to_be_visible() + + +def test_back_link(page: Page): + """ + Given a user is on the record page + When the user selects the back button / breadcrumb + Then the user should be navigated back to the the results page + """ + page.goto("http://localhost:5000/poc-search-view") + page.locator("#searchInput").click() + page.locator("#searchInput").fill("public record") + page.get_by_role("button", name="Search").click() + expect(page.get_by_text("records found")).to_be_visible() + page.get_by_role("link", name="file-b2.txt").click() + page.get_by_role("link", name="Back", exact=True).click() + page.wait_for_url("/poc-search-view") + page.close() + + +def test_searched_record_metadata(page: Page): + """ + Given the user has clicked on a result displayed on the search page with results displayed. + When the user is on the record page + Then the table should display the relevant metadata for the record such as + "Source organisation" and "Consignment ID," + """ + time.sleep(10) + page.goto("http://localhost:5000/poc-search-view") + page.locator("#searchInput").click() + page.locator("#searchInput").fill("public record") + page.get_by_role("button", name="Search").click() + expect(page.get_by_text("records found")).to_be_visible() + page.get_by_role("link", name="file-b2.txt").click() + + # Verify if the expected metadata is visible on the record page + assert page.locator("dt:has-text('Source organisation') + dd").is_visible() + assert page.locator("dt:has-text('Consignment ID') + dd").is_visible() + + # Close the page + page.close() From 443ba8f92ad560dcc1f74f3160294603bf1bf4e7 Mon Sep 17 00:00:00 2001 From: Colin B Date: Fri, 27 Oct 2023 15:25:49 +0100 Subject: [PATCH 2/5] fix: pre-commit --- app/tests/test_record_metadata.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/tests/test_record_metadata.py b/app/tests/test_record_metadata.py index 2371dac7..ace4f8f4 100644 --- a/app/tests/test_record_metadata.py +++ b/app/tests/test_record_metadata.py @@ -1,7 +1,8 @@ import re -from playwright.sync_api import Page, expect import time +from playwright.sync_api import Page, expect + def test_page_title_and_header(page: Page): """ From 61e99a903d616bc338514fd77e2afff174051a70 Mon Sep 17 00:00:00 2001 From: Anthony Hashemi Date: Mon, 30 Oct 2023 11:20:58 +0000 Subject: [PATCH 3/5] Delete old uncommented record metadata test file --- e2e_tests/test_record_metadata.py | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 e2e_tests/test_record_metadata.py diff --git a/e2e_tests/test_record_metadata.py b/e2e_tests/test_record_metadata.py deleted file mode 100644 index a12cb68e..00000000 --- a/e2e_tests/test_record_metadata.py +++ /dev/null @@ -1,17 +0,0 @@ -# import re -# from playwright.sync_api import Page, expect - - -# def test_page_title_and_header(page: Page): -# page.goto("/record") -# expect(page).to_have_title( -# re.compile("Record – AYR - Access Your Records – GOV.UK") -# ) -# expect(page.get_by_text("Record metadata")).to_be_visible() - - -# def test_back_link(page: Page): -# page.goto("/record") -# page.get_by_role("link", name="Back", exact=True).click() -# page.wait_for_url("/") -# page.close() From 7f47c4193193fbe61f6e67598d768d35243b013a Mon Sep 17 00:00:00 2001 From: Anthony Hashemi Date: Mon, 30 Oct 2023 11:22:11 +0000 Subject: [PATCH 4/5] Move proper test_record_metadata.py playwright test to e2e_tests folder --- {app/tests => e2e_tests}/test_record_metadata.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {app/tests => e2e_tests}/test_record_metadata.py (100%) diff --git a/app/tests/test_record_metadata.py b/e2e_tests/test_record_metadata.py similarity index 100% rename from app/tests/test_record_metadata.py rename to e2e_tests/test_record_metadata.py From 529137ff2fa93c1cb82495a87f48b7a5e159d275 Mon Sep 17 00:00:00 2001 From: Anthony Hashemi Date: Mon, 30 Oct 2023 12:35:54 +0000 Subject: [PATCH 5/5] Add secret key to TestingConfig so that tests run fine in CI --- pyproject.toml | 1 + testing_config.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 41dfe10f..313cc241 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,6 +7,7 @@ readme = "README.md" [tool.bandit] exclude_dirs = ['*_test.py', '*/test_*.py', 'venv'] +skips = ["B105"] [tool.poetry.dependencies] python = "^3.11" diff --git a/testing_config.py b/testing_config.py index be97d999..0703881b 100644 --- a/testing_config.py +++ b/testing_config.py @@ -3,4 +3,5 @@ class TestingConfig(Config): TESTING = True + SECRET_KEY = "TEST_SECRET_KEY" # pragma: allowlist secret WTF_CSRF_ENABLED = False