Commit Graph

12639 Commits

Author SHA1 Message Date
Ben Thorner
1c4a2b4790 Cache templates and folders in TemplateList
This gives us the performance gains identified in [^1] for the test
service described in the spike:

- user_template_folders - from 10s to a little above 3s on its own

- get_templates_and_folders - from 10s to below 6s on its own

In combination, these two uses of caching reduce the test page load
time from 10s to a little above 3s. This is slightly higher than in
the spike PR due to all the extra work we're doing to generate the
"move to" list of folders, as described in a previous commit.

The render time is unchanged for services with few folders. We start
to see the benefit of this change at around 200 templates + folders,
with no evidence that any service will experience worse render times,
despite the extra work we're doing from previous commits.

[^1]: https://github.com/alphagov/notifications-admin/pull/4251
2022-06-07 11:05:36 +01:00
Ben Thorner
2771ae1274 Remove redundant user param from template methods
This will finally make it possible to cache some of the methods.
2022-06-07 11:05:35 +01:00
Ben Thorner
9126c8ca5e Move _template_ methods to TemplateList class
This is a straight move with a few minor tweaks:

- Some references to self.X in the code from Service now need to
become self.service.X.

- Some reference to self.service.Y in TemplateList can now become
self.Y for the methods that were migrated.

The remaining _template_ methods in Service are still called from
multiple places and there's no performance gain to motivate moving
them out of the Service class, which is now a more manageable size.
2022-06-07 11:05:34 +01:00
Ben Thorner
bcfc6ce707 Decouple deletion tests from "get_templates" fn
Using create_template here is easier than template_json as it has
various parameters preset [^1].

[^1]: https://github.com/alphagov/notifications-admin/blob/master/tests/conftest.py#L3986
2022-06-07 11:05:33 +01:00
Ben Thorner
487dc1b488 Test _template_folders functions via TemplateList
This is part of the overall migration of the "_template_folders"
methods to the TemplateList class. Moving the existing tests now
will make the actual migration easier to follow.

To emulate the second and third tests, we need to grab a specific
folder from the TemplateList and then look at its folders - these
are set based on "get_template_folders" as before.
2022-06-07 11:05:30 +01:00
Ben Thorner
057b4ee7a5 Simplify tests for get_(user)_template_folders
This will make it easier to understand the diff when we move these
tests to operate via TemplateList. Despite the verbosity, the only
attribute we were actually checking here was the name, as a way of
identifying which folders had been returned.

Looking at the three tests:

1. The first is checking we can correctly filter all folders that a
user can access, which involves appending the names of any parent
folders the user doesn't have direct access to.

2. The second is checking the same thing but also that we filter the
set of folders to just the children of a parent.

3. The third is just checking the filtering of child folders, without
any user filtering or name aggregation applied.

I've adapted tests (2) and (3) to make it clearer what is tested,
focussing the tests on a specific folder and its contents.
2022-06-07 10:44:35 +01:00
Ben Thorner
b97bf19b45 Make creating TemplateListServices consistent
TemplateListServices are used when we want to show the service
as an additional layer of hierarchy when a user copies a template,
potentially across services [^1].

Normally a TemplateFolder is given "folders" and "templates" by
TemplateList [^2]; TemplateListService was doing it the other
way round and getting its own instead.

Using a subclass of TemplateList means we can make the approach
consistent, which will support the caching approach later on, as
well as simplifying how we work with templates and folders.

[^1]: 2e637f801f/app/main/views/templates.py (L356)
[^2]: bef0382cca/app/models/template_list.py (L31-L36)
2022-06-07 10:43:30 +01:00
Ben Thorner
a87138b9f9 Refactor loop over TemplateList items
This is a technique we'll use in later commits.
2022-06-06 10:36:49 +01:00
Ben Thorner
ad4ef12251 Reuse TemplateList to display move-to options
This is slightly less efficient than getting the folder dicts from
"get_user_template_folders" directly, since:

- TemplateList returns both templates and folders.
- TemplateList encapsulates the dicts in model classes.

We'll compensate for this later on:

- We'll introduce a new caching approach to make the call fast.
- We'll expose a property to avoid the "if" in the comprehension.
2022-06-06 10:36:48 +01:00
Ben Thorner
f500db44f1 Reuse TemplateList class when deleting a folder
Part of moving "get_template_folders" et al. into TemplateList so we
can cache it more effectively. This is slightly less efficient as
iterating a TemplateList will instantiate an object for each item
in the folder; but the difference is minimal.

Note that:

- The default template_type for TemplateList is "all".
- We need to pass realistic template "JSON" in the test now.
2022-06-06 10:36:47 +01:00
Ben Thorner
fe13bb8dbc Merge pull request #4254 from alphagov/fix-go-live-bug-179736794
Fix go-live checks ignoring nested templates
2022-06-06 10:36:28 +01:00
Ben Thorner
524fa12cde Merge pull request #4260 from alphagov/bump-utils-56
Bump utils to version 56.0.0
2022-06-06 10:28:20 +01:00
Ben Thorner
efd862ba9f Merge pull request #4259 from alphagov/no-support-882
Document +882 numbers are not supported
2022-06-06 10:27:35 +01:00
Ben Thorner
ee3d2d1804 Bump utils to version 56.0.0
The only impactful change is the major version itself, where I've
fixed the breaking changes due to the upgrade of PyPDF2 [^1] and
checked there are no deprecation warnings when I run the tests.

[^1]: https://github.com/alphagov/notifications-utils/pull/973
2022-06-01 13:29:54 +01:00
Ben Thorner
e9f2522abd Document +882 numbers are not supported
These are "countryless" phone numbers used by e.g. satellite phone
providers [^1]. We decided not to support them for now because:

- The use case is low: only one service is asking for this prefix.
- MMG will charge at 4x for them but apparently they cost more.

We can't put this "negative" data in the usual place [^2] because
this would _enable_ sending via this number. Until we have more
cases like this it's easiest to just tack on this fake info.

[^1]: https://en.wikipedia.org/wiki/International_Networks_(country_code)
[^2]: ca2506c6a6/notifications_utils/recipients.py (L569)
2022-05-31 17:32:41 +01:00
Katie Smith
9111a7fc86 Merge pull request #4255 from alphagov/bump-itsdangerous
Upgrade itsdangerous from 1.1.0 to 2.0.1
2022-05-27 14:19:40 +01:00
Ben Thorner
32efb9a03d Fix go-live checks ignoring nested templates
This is a very low impact bug since a user can always create such
templates after their service is live and not be subject to checks
we do before that point. Still, we may as well fix it.

The main benefit of this change is actually to contribute towards
moving methods like "get_templates" out of the Service class so
we can simplify and cache their results more effectively.

Note: I wanted to simplify the Service class further as the two
"has_" properties are only used once in the app code. Unfortunately
they are tightly coupled in one of the tests as well [^1].

[^1]: bef0382cca/tests/app/main/views/service_settings/test_service_settings.py (L1961-L1962)
2022-05-27 12:46:37 +01:00
Ben Thorner
1b96a13565 Add xfail tests for go-live reply-to/sender check
This represents a bug where a user can request to go live without
setting a reply-to email address or SMS sender despite the service
having one or more email or SMS templates, respectively.

We will make these tests pass in the next commit.
2022-05-27 12:46:36 +01:00
Ben Thorner
c6977b13a0 Simplify stubs in go-live tests
This replaces multiple stubs with a single stub on the lower level
API client method to return the desired set of templates. You can
see this most clearly in the diff for the "_sms_sender_" test:

- Add a stub for "get_service_templates"
- Remove stubs for "all_templates" and "get_templates"

In order to make the change, I had to separate the reply-to set of
tests from the "_things_right" tests because "count_of_templates"
was actually in conflict with "count_of_email_templates". To make
the new test I copied the original and removed unnecessary stubs
from both of them depending on what's being tested.

I'm not sure what the "_things_right" test name means; the name of
the new test is at least consistent with others in the file.

Note: I also removed the "assert mock_templates.called is True"
lines as they wasn't adding any value that I can see.
2022-05-27 12:46:32 +01:00
Katie Smith
600a9afad7 Upgrade itsdangerous from 1.1.0 to 2.0.1
This upgrades itsdangerous by a major version.

When testing most routes we:
* use the `client_request` fixture
  * under the hood this logs in the user with `TestClient.login`
  * logging in the user signs their session with a secret and the current time

For some tests we also:
* wrap the test method with a `freeze_time()` decorator to simulate a past date and time

When Pytest calls the wrapped test method:
* any application code which tries to get the current time will get the frozen time
* any application code getting the current user means decoding the session
* the code which decodes the session will see that the session was created in the future, in other words it has a negative age
  * as of ItsDangerous 2.0.0 signatures with a negative age raise an exception

To avoid all the tests which freeze time failing, this adds itsdangerous
to the list of packages that freezegun ignores.

We can't yet upgrade to a version of itsdangerous that is >= 2.1.0
because there are compatibility issues with Flask 1.x.
2022-05-27 11:45:00 +01:00
Chris Hill-Scott
89ae3b9f3c Merge pull request #4253 from alphagov/prometheus-client-0.14.1
Upgrade Prometheus client and all sub-dependencies
2022-05-27 11:33:34 +01:00
Chris Hill-Scott
bdeda88bda Run pip-compile --upgrade 2022-05-26 15:00:49 +01:00
Chris Hill-Scott
f1bf991a3c Update prometheus-client from 0.14.0 to 0.14.1 2022-05-26 14:58:27 +01:00
karlchillmaid
bef0382cca Merge pull request #4252 from alphagov/update-review-date-for-roadmap
Update review date
2022-05-26 13:02:59 +01:00
karlchillmaid
f2a87ebf43 Update review date
Update review date to fit IP’s timings for our new roadmap.
2022-05-26 12:13:36 +01:00
Katie Smith
e60eb91631 Merge pull request #4249 from alphagov/add-link-types
Add the filetype to download links
2022-05-25 08:19:26 +01:00
Katie Smith
0d167984f1 Add filetype to platform admin report download buttons 2022-05-24 10:27:27 +01:00
Katie Smith
cdfe852d25 Add filetype to all links to download user reports / examples 2022-05-24 10:27:27 +01:00
Katie Smith
4338953d5f Add filetype to MOU links 2022-05-24 09:54:33 +01:00
Katie Smith
073636d74f Fix links on the '/features/security' page
- Changed the 'contact us' link to point to our support form, not the
  Notify homepage
- Updated the link to the details about CHECK based testing, since the
  site we were linking to no longer exists.
2022-05-24 09:54:33 +01:00
Chris Hill-Scott
d1c6e06bf1 Merge pull request #4248 from alphagov/pyup-pin-core
Stop pyup trying to upgrade blocked dependencies
2022-05-20 11:26:43 +01:00
Chris Hill-Scott
83bb7fa8b1 Stop pyup trying to upgrade blocked dependencies
We can’t upgrade Jinja or Werkzeug until we’re on Flask 2.x.x. We can’t
upgrade Flask to 1.1.3 because it pins older versions of Jinja and
Werkzeug than the ones we’re using. We can’t upgrade Flask to 2.x.x
until we upgrade itsdangerous to 2.x.x, which is blocked by
https://github.com/alphagov/notifications-admin/pull/4044/files
2022-05-19 11:13:57 +01:00
Katie Smith
2e637f801f Merge pull request #4247 from alphagov/page-count-fun
Fix bug where "Bad Request: Letter does not have a page x" was seen in logs
2022-05-17 09:08:22 +01:00
Katie Smith
d00c438802 Include placeholders in letter length check
When filling in the letter address having clicked 'back'
(https://www.pivotaltracker.com/story/show/181513431).
2022-05-16 15:44:52 +01:00
David McDonald
19e6e38426 Calculate page count based on template values when previewing
Similar to the bug shown here
https://www.pivotaltracker.com/story/show/181513431, but to fix the case
when previewing a letter send using a CSV upload it wasn't using
template values to calculate the page length.
2022-05-16 15:44:05 +01:00
David McDonald
732bfffb93 Remove duplicative call to get_page_count_for_letter
For some reason we were calling it twice. We can just reuse the
value already calculated
2022-05-16 11:26:05 +01:00
Chris Hill-Scott
e467167c62 Merge pull request #4245 from alphagov/pip-compile-upgrade
Run `pip-compile --upgrade`
2022-05-16 11:10:14 +01:00
Chris Hill-Scott
7e57bfd6a1 Run pip-compile --upgrade
This upgrades all the sub-dependencies which we don’t pin in
requirements.txt
2022-05-13 15:41:00 +01:00
Chris Hill-Scott
5de6171db3 Merge pull request #4244 from alphagov/xlrd-2
Upgrade xlrd to version 2 and force `.xlsm` files to open with pyexcel-xlsx instead
2022-05-13 15:31:52 +01:00
Chris Hill-Scott
42de68e6a0 Update other Excel-related dependencies
Make sure the latest versions of all Excel-related dependencies all work
nicely together.
2022-05-13 13:24:36 +01:00
Chris Hill-Scott
80854ab2cc Force xlsm files to open with pyexcel-xlsx
`.xlsm` files are like `.xlxs` files but with macros enabled. They store
data in the same XML-based format as `.xlsx` files.

Pyexcel will try to use the xlrd package to parse `.xlsm` files. This
used to work because xlrd used to support reading `.xlsx` files. xlrd
has dropped support for `.xlsx` files in version 2 because of security
concerns. This means that when pyexcel asks xlrd to parse a `.xlsm` file
it causes an error.

This commit adds some branching to force `.xlsm` files to be opened
with pyexcel-xlsx instead, which does support `.xlsx` files.
2022-05-13 13:17:55 +01:00
Chris Hill-Scott
ded87706c7 Upgrade xlrd to version 2
xlrd is a library for reading data and formatting information from
Excel files in the historical .xls format.

Version 2 of xlrd no longer supports anything other than .xls files.

We were using it to also support reading .xlsm files (old Excel files
with macro support).

We could keep using the old version of this dependency, but hopefully
this niche version of an ancient file format is obscure enough that
no-one is using it, and we can drop support, keeping our dependencies
up to date.
2022-05-12 15:48:12 +01:00
Ben Thorner
5641ce2c15 Merge pull request #4243 from alphagov/fix-usage-api-181935935
Fix keys used to render monthly usage
2022-05-11 13:56:16 +01:00
Ben Thorner
4abb6110c8 Fix keys used to render monthly usage
These were out-of-sync with the API.
2022-05-11 13:28:33 +01:00
Ben Thorner
84dde0f824 Merge pull request #4229 from alphagov/monthly-usage-multirate-181935935
Support multiple rates in monthly SMS usage
2022-05-11 11:26:44 +01:00
Chris Hill-Scott
5c4b361345 Merge pull request #4240 from alphagov/humanize-4.1.0
Update humanize from 4.0.0 to 4.1.0
2022-05-11 10:06:48 +01:00
Ben Thorner
bb0fb73bc8 Minor tweaks in response to PR comments
In response to [^1] and [^2].

[^1]: https://github.com/alphagov/notifications-admin/pull/4229#discussion_r869181152
[^2]: https://github.com/alphagov/notifications-admin/pull/4229#discussion_r869186063
2022-05-10 17:00:51 +01:00
Ben Thorner
9ac33f7baf Merge pull request #4242 from alphagov/fix-missing-audit-task-182116071
Fix missing audit task in package.json
2022-05-10 13:25:25 +01:00
Ben Thorner
51f9b0cef0 Fix missing audit task in package.json
This was intended to go in [^1] but I think it got lost in a rebase.

[^1]: https://github.com/alphagov/notifications-admin/pull/4237
2022-05-10 11:33:24 +01:00
Chris Hill-Scott
478f222419 Merge pull request #4238 from alphagov/whitespace-api-key-js
Remove whitespace around API key in JS templates
2022-05-10 11:08:47 +01:00