Commit Graph

12755 Commits

Author SHA1 Message Date
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
Chris Hill-Scott
1787f9f42f Merge pull request #4261 from alphagov/refactor-permissions-tests
Refactor permissions tests
2022-06-06 16:05:04 +01:00
Chris Hill-Scott
30eebf3586 Reorder and label test cases
This commit takes the existing test cases, removes duplicates, and tries
to add a human-readable comment explaining what each one is testing.
2022-06-06 15:10:27 +01:00
Chris Hill-Scott
ea9c7e6102 Use existing user fixture 2022-06-06 15:10:27 +01:00
Chris Hill-Scott
f779a97b5c Use decorator as decorator
This syntax makes it clearer what is being tested here, because it’s
unusual to see a decorator being manually called with function as its
first argument.

It’s also consistent with how the later tests in this file are written.
2022-06-06 13:55:25 +01:00
Chris Hill-Scott
122a045142 Define user inside each test
This user is only re-used once, which isn’t a big saving. By putting it
inside the test it’s easy to see what special conditions are being set
up that result in the expected outcometest result.
2022-06-06 13:46:39 +01:00
Chris Hill-Scott
60870c69a7 Split platform admin test out
By making the one platform admin case a separate test we no longer
need to pass in a `user` or `kwargs` to the parametrize every time,
making it easier to read.
2022-06-06 13:45:29 +01:00
Chris Hill-Scott
f79c3f27e3 Use fixture to create user dict
This removes a bunch of dummy data which isn’t relevant to the tests
being run.
2022-06-06 13:44:36 +01:00
Chris Hill-Scott
e6e2770a04 Rewrite test_permissions with parametrize
It’s easier to see what the different test cases are when they are laid
out with `parametrize`, rather than separate functions with lots of
boilerplate.
2022-06-06 13:43:46 +01:00
Chris Hill-Scott
15131d003b Let client_request do user mocking
`client_request.login` already mocks calls to get the current user, so
we don’t need to do it manually.
2022-06-06 13:41:52 +01:00
Chris Hill-Scott
684fc5057a Move non-success handling out of helper
The helper function handles both tests that pass, and tests that are
expected to fail (either by raising an exception or returning a redirect
to the login page).

By moving the handling of cases which are expected to fail out of the
helper function we can make the helper function less complex, which will
make further refactoring easier.
2022-06-06 13:39:42 +01:00
Chris Hill-Scott
84d4c1e0b5 Remove catching of exception that is never raised
View functions won’t raise an `Unauthorized` exception, they will return
a redirect to the login page instead.
2022-06-06 13:38:59 +01:00
Chris Hill-Scott
3c25de3f85 Merge pull request #4256 from alphagov/bump-flask-jinja-werkzeug
Bump Flask, Jinja and Werkzeug to their latest versions
2022-06-06 12:21:23 +01:00
Chris Hill-Scott
61045fab89 Bump itsdangerous to the latest version
We were blocked on using the very latest version of itsdangerous because
it was only compatible with versions of Flask greater than 2.

Now that we’re using the latest version of Flask we can also keep this
dependency up to date.
2022-06-06 12:13:38 +01:00
Chris Hill-Scott
8b7f2fbf04 Stop using _external=True in tests
It looks like, by default, Flask no longer makes full URLs, for example
`https://example.com/path`. Instead it does `/path`. This will still
work fine, and if anything is better because it reduces the number of
bytes of HTML we are sending.

It won’t mean that requests go over `http` instead of `https` without
the protocol because we set the appropriate HSTS header here:
0c57da7781/ansible/roles/paas-proxy/templates/admin.conf.j2 (L11)

This commit changes all our tests to reflect that URLs no longer have
the protocol and domain in them. `_external=True` is Flask’s way of
saying whether a URL should be generated with the domain and protocol
(`True`) or without it (`False`).

Again, I can’t find the changelog or diff where this was introuduced,
but if you’d like to go spelunking then here’s a starting point:
50374e3cfe/src/flask/helpers.py (L192)
2022-06-06 12:12:52 +01:00
Chris Hill-Scott
5b5d4af681 Work around inconsistent request context in tests
The failing test here[1] does two things:
1. makes a request to /sign-out
2. calls the index route, without actually making a request

This means that when the `login_manager.unauthorized_handler`[2] looks
at Flask’s `request` object it gets the request context from 1., not 2.,
because 2. isn’t actually a request. The means it sets the value of the
`next` parameter to that of the request, not of the index route.

Basically at some point Flask has changed and decided that 2. isn’t a
proper request, so won’t set new request context.

This isn’t a realistic test because nothing would call the index
function directly, it would always be as part of a request to that page.

But to make the minimal change to fix the breaking tests this commit
makes the check a bit more general, i.e. that the redirect is to the
sign in page with any `next` parameter, not a specific `next` parameter.

1. 9111a7fc86/tests/app/utils/test_user.py (L130-L138)
2. 9111a7fc86/app/main/views/sign_in.py (L86)
2022-06-06 12:12:52 +01:00
Chris Hill-Scott
fc833c802e Update tests to reflect unencoded commas in URLs
I can’t find the changelog for this but it looks like somewhere someone
has decided that commas don’t need to be URL-encoded. This is true for
use in `href` attributes because it’s unambiguous that the comma is part
of the URL (unlike a closing quote for example, which could be
misinterpreted as HTML syntax).

This commit jut changes the test to reflect that the URLs generated by
Flask now have raw commas in them.
2022-06-06 12:12:52 +01:00
Chris Hill-Scott
2a62586799 Reflect change in argument name
`filename_or_fp` was changed to `path_or_file` here:
https://github.com/pallets/flask/pull/3828/files#diff-1f51c8ded4d4ff7e13badab599ef22436c529c2b5f9c25dc6250c1f9fd985440R479
2022-06-06 12:12:52 +01:00
Chris Hill-Scott
78a1a3099d Work around incompatibilty between govuk-frontend-jinja and Flask 2
This line:
ddbe208a97/govuk_frontend_jinja/flask_ext.py (L22)

Raises `KeyError: 'extensions'` when using Flask 2

I think this is because there are no default Jinja extensions in Flask
as of pallets/flask@81ba6c2 in accordance with
https://github.com/pallets/jinja/issues/1203

So we need to manually add an `extensions` field to the `jinja_options`
`dict` if one doesn’t exist already.

Issue raised here: https://www.github.com/Crown-Commercial-Service/govuk-frontend-jinja/issues/66
2022-06-06 12:12:52 +01:00
Chris Hill-Scott
8ea2f49ee6 Bump Flask, Jinja and Werkzeug to latest versions
Now that we’ve upgraded itsdangerous to the latest version we are:
- unblocked from upgrading to Flask 2, which requires a recent version
  of itsdangerous
- unblocked from upgrading Jinja and Werkzeug to the latest versions,
  which require Flask 2

This commit just does the version upgrades, breaking changes will be
addressed in subsequent commits.
2022-06-06 12:12:52 +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