From a5f3d091f8a4bc22bd866a949a36d74513467bf2 Mon Sep 17 00:00:00 2001 From: Tony Locke Date: Thu, 7 Sep 2023 13:14:37 +0100 Subject: [PATCH] Sites HH data report: Can't access if viewer --- chellow/__init__.py | 1 + chellow/models.py | 15 +++++++++--- test/reports/test_report_183.py | 43 ++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/chellow/__init__.py b/chellow/__init__.py index 990aecc25..6d06557e5 100644 --- a/chellow/__init__.py +++ b/chellow/__init__.py @@ -203,6 +203,7 @@ def check_permissions(*args, **kwargs): or path in ( "/reports/169", + "/reports/183", "/reports/187", "/reports/247", "/reports/111", diff --git a/chellow/models.py b/chellow/models.py index 6beeeed79..52dbc8c1a 100644 --- a/chellow/models.py +++ b/chellow/models.py @@ -1108,6 +1108,15 @@ def insert_llfc( else: raise BadRequest("This party isn't a DNO.") + @classmethod + def insert( + cls, sess, participant, market_role, name, valid_from, valid_to, dno_code + ): + party = Party(participant, market_role, name, valid_from, valid_to, dno_code) + sess.add(party) + sess.flush() + return party + @staticmethod def find_by_participant_role(sess, participant, market_role, valid_from): return sess.execute( @@ -2260,9 +2269,9 @@ def update(self, name): self.name = name def insert_party(self, sess, market_role, name, valid_from, valid_to, dno_code): - party = Party(self, market_role, name, valid_from, valid_to, dno_code) - sess.add(party) - return party + return Party.insert( + sess, self, market_role, name, valid_from, valid_to, dno_code + ) def get_dno(self, sess): return sess.execute( diff --git a/test/reports/test_report_183.py b/test/reports/test_report_183.py index 62a6fa86a..cd0b1bbcc 100644 --- a/test/reports/test_report_183.py +++ b/test/reports/test_report_183.py @@ -1,9 +1,11 @@ from io import BytesIO from zipfile import ZipFile +from requests.auth import _basic_auth_str + from utils import match -from chellow.models import Site, User +from chellow.models import Contract, MarketRole, Participant, Site, User, UserRole from chellow.reports.report_183 import _process_site, none_content from chellow.utils import ct_datetime, to_utc @@ -68,6 +70,45 @@ def test_do_post_site_codes(mocker, client, sess): MockThread.assert_called_with(target=none_content, args=expected_args) +def test_do_post_viewer(mocker, raw_client, sess): + vf = to_utc(ct_datetime(2022, 7, 1)) + user_role = UserRole.insert(sess, "viewer") + user = User.insert(sess, "admin@example.com", "admin", user_role, None) + participant = Participant.insert(sess, "CALB", "Calb") + market_role = MarketRole.insert(sess, "Z", "Non-core") + participant.insert_party(sess, market_role, "neut", vf, None, "") + Contract.insert_non_core(sess, "configuration", "", {}, vf, None, {}) + sess.commit() + + MockThread = mocker.patch("chellow.reports.report_183.threading.Thread") + + data = { + "start_year": "2022", + "start_month": "07", + "start_day": "01", + "finish_year": "2022", + "finish_month": "07", + "finish_day": "31", + "type": "used", + "site_codes": "", + } + headers = {"Authorization": _basic_auth_str("admin@example.com", "admin")} + response = raw_client.post("/reports/183", data=data, headers=headers) + + match(response, 303) + + expected_args = ( + None, + "used", + to_utc(ct_datetime(2022, 7, 1)), + to_utc(ct_datetime(2022, 7, 31, 23, 30)), + user.id, + "sites_hh_data_202207312330_filter.zip", + ) + + MockThread.assert_called_with(target=none_content, args=expected_args) + + def test_process_site(sess): f = BytesIO() zf = ZipFile(f, mode="w")