Commit Graph

8382 Commits

Author SHA1 Message Date
Ben Thorner
43ddfe0560 Remove old "simple_polygons" fields in schemas
These were missed in [1].

[1]: https://github.com/alphagov/notifications-api/pull/3314
2021-09-01 17:23:29 +01:00
Ben Thorner
be7272b44f Merge pull request #3314 from alphagov/next-stage-areas-migration-178986763
Switch "areas" field to "areas_2" format
2021-09-01 16:16:30 +01:00
Chris Hill-Scott
6a2cd7e9bf Merge pull request #3307 from alphagov/remove-email-address-validate-password-reset
Don’t update `email_access_validated_at` on password reset
2021-09-01 10:06:48 +01:00
Chris Hill-Scott
2c7e4657ce Don’t update email_access_validated_at on password reset
As of https://github.com/alphagov/notifications-admin/pull/4000/files
the admin app is doing this, so we don’t need to do it here as well.
2021-09-01 09:54:54 +01:00
Chris Hill-Scott
43f010ea1b Merge pull request #3315 from alphagov/no-0-weight
Don’t call random.choices with zero weighting
2021-08-31 12:06:03 +01:00
Chris Hill-Scott
6900505b05 Don’t call random.choices with zero weighting
As of 041d8b48a2
it’s not valid to call `random.choices` without giving at least one of
the options a positive weighting.

This makes sense, because giving a zero weighting is effectively saying
‘theres’s only one choice, but don’t choose it’.

In our codebase this is applicable where there’s only one international
provider, which we want to use even when it’s been de-prioritised for
domestic SMS.

This doesn’t cause a problem now, but will if we upgrade to Python
versions greater than 3.9.0.
2021-08-31 11:08:27 +01:00
Ben Thorner
bf0bf4e31c Favour new "areas" format for PagerDuty alerts
Broadcasts created via the API [1] and the Admin app [2] should
both now have this field set. It's also more informative to show
this, and broadcasts created via the API don't have IDs anyway.

There's a small risk that an old broadcast that gets approved won't
have this data, but it's for information only and we intend to
backfill all old broadcasts in the near future.

[1]: 023a06d5fb
[2]: 7dbe3afa19
2021-08-27 14:22:12 +01:00
Ben Thorner
ec1171f85c Switch "areas" field to "areas_2" format
The Admin app is now temporarily using the "areas_2" field while
we migrate "areas" to the new format [1].

[1]: https://github.com/alphagov/notifications-admin/pull/4004
2021-08-27 14:22:11 +01:00
Ben Thorner
a7d92b9058 Replace / remove redundant uses of "areas"
In one case ("areas=['manchester']") the format was even invalid,
but in general the original value of the column is pretty much
irrelevant for tests that involve updating it (it's highly unlikely
the column would default to the same value as the test data).
2021-08-27 13:31:49 +01:00
Ben Thorner
194f54c38f Add missing tests for old format areas
This was (sort of) missed in [1], but it hasn't caused a problem
because the code to create/update broadcasts will populate areas
in the old ("areas") and the new ("ids") formats anyway [2].

However, we're about to remove create/update support code, so we
need to have something in place to cope with old and new format
data until we backfill old broadcasts with the new format [3].

[1]: https://github.com/alphagov/notifications-api/pull/3312
[2]: https://github.com/alphagov/notifications-api/pull/3312/files#diff-6be38b59e387630a0c6b8fc60312b7ba53ba9f36c54594fa5690646f286dd2b9R141
[3]: 9667433b7e
2021-08-27 13:18:12 +01:00
Ben Thorner
55f71fa12b Merge pull request #3312 from alphagov/prepare-broadcast-refactor-178986763
Prepare to enhance area data for broadcasts
2021-08-26 15:41:21 +01:00
Ben Thorner
023a06d5fb Start dual running with "areas" and "names"
For the public API we actually receive a "name" instead of an ID,
which we also want to start sending from the Admin app.

Unlike IDs, which aren't really used anywhere, we want the names
to display the alerts on gov.uk/alerts.
2021-08-26 15:34:25 +01:00
Ben Thorner
8f39d476bd Start dual running with "areas" and (area) "ids"
This is necessary until:

- The Admin app is using the new "areas(_2)" format to store and
retrieve data.

- We've migrated all existing broadcast messages to use the new
format.

Note that "areas" / "ids" isn't actually used for anything except
printing out the PagerDuty message - it's not sent to the proxy [1].

[1]: 6edc6c70aa/app/celery/broadcast_message_tasks.py (L190-L193)
2021-08-26 15:34:24 +01:00
Ben Thorner
fd7ebbebb0 Introduce "areas_2" so we can repurpose "areas"
Currently we have:

- An "areas" column in the DB that stores a JSON blob.
- An "areas" field inside the "areas" JSON that stores area IDs.
- Each field has to be manually copied into the JSON column.

We want to move to:

- An "areas" column in the DB (unchanged).
- An "ids" field inside the "areas" JSON (to replace "areas").
- The Admin app sending other data inside an "areas" JSON field.

The API design for areas is confusing and difficult to extend.
Here we duplicate the current API functionality using an "areas_2"
field. Once the Admin app is using this field, we'll be able to
rename it to just "areas", which is where we want to get to.

In the next commits we'll build on this to support the migration
from "areas"."areas" to "areas"."ids".
2021-08-26 15:34:23 +01:00
Ben Thorner
277db4e6c7 Merge pull request #3313 from alphagov/force-override-broadcast-178986763
Allow old broadcasts to be overridden
2021-08-26 14:25:41 +01:00
Ben Thorner
44b388dc69 Allow old broadcasts to be overridden
This is a temporary feature to make it easy to migrate the format
of the "areas" column and backfill extra data for it.

It's not possible to use this feature to update the status of an
old broadcast message, so the risk from this override is minimal.
2021-08-26 12:40:43 +01:00
Chris Hill-Scott
6edc6c70aa Merge pull request #3310 from alphagov/revert-3309-update-utils-coordinate-transformation
Revert "Update utils to bring in coordinate transformation"
2021-08-20 16:31:47 +01:00
Chris Hill-Scott
f3e6d92046 Revert "Update utils to bring in coordinate transformation" 2021-08-20 16:05:39 +01:00
Chris Hill-Scott
c50ef90d53 Merge pull request #3309 from alphagov/update-utils-coordinate-transformation
Update utils to bring in coordinate transformation
2021-08-20 15:43:51 +01:00
Chris Hill-Scott
e92be8b034 Pass polygons through if they’re small already
If a polygon is smaller than the largest polygon in our dataset of
simplified polygons then we’re only throwing away useful detail by
simplifying it.

We should still simplify larger polygons as a fallback, to avoid sending
anything to the CBC that we’re not sure it will like.

The thresholds here are low: we can raise them as we test and experiment
more.

Here’s some data about the Flood Warning Service polygons

Percentile | 80% | 90%   | 95%    | 98%     | 99%     | 99.9%
-----------|-----|-------|--------|---------|---------|---------
Point count| 226 | 401.9 | 640.45 | 1015.38 | 1389.07 | 3008.609

Percentile    | 80% | 90%   | 95%    | 98%     | 99%     | 99.9%
--------------|-----|-------|--------|---------|---------|---------
Polygon count |2----|3------|5-------|8--------|10-------|40.469
2021-08-19 11:08:53 +01:00
Chris Hill-Scott
a6135fb8ab Bump utils
This new version of utils implements the transformation of our polygons
to a Cartesian plane. In other words, it converts them from being
defined in spherical degrees to metres.

For the API this means our simplification will be slightly more
accurate.
2021-08-19 11:08:18 +01:00
Leo Hemsted
5cb97253ac Merge pull request #3244 from alphagov/allowlist
Allowlist
2021-08-17 14:43:33 +01:00
Leo Hemsted
543bff1c30 bump utils to fix sending to international numbers via allowlist 2021-08-16 10:10:24 +01:00
Pea Tyczynska
9334204cd2 Merge pull request #3305 from alphagov/fetch-alerts-list-for-govuk-alerts
get_broadcasts returns a list of alerts for gov.uk/alerts
2021-08-12 14:33:46 +01:00
Pea Tyczynska
9d2f8347b2 get_broadcasts returns a list of broadcasts for gov.uk/alerts 2021-08-12 14:03:33 +01:00
Pea Tyczynska
0f7f219a55 dao_get_all_broadcast_messages returns just fields govuk alerts need 2021-08-11 14:43:27 +01:00
Pea Tyczynska
74c9ca2bf6 Fetch all broadcast messages that are or were transmitted
Regardless of channel.
Do not include:
- broadcasts older than 25.05.2021
- stubbed broadcasts
- broadcasts that were not transmitted. So only broadcasting,
cancelled and completed make the list;
2021-08-11 14:43:27 +01:00
Ben Thorner
cae429dadc Merge pull request #3303 from alphagov/refactor-test-auth-headers-179039225
Follow-up refactorings to authorization code
2021-08-11 13:54:27 +01:00
Ben Thorner
e8ad12f016 Merge pull request #3304 from alphagov/switch-to-per-app-api-keys-179039225
Switch to per-app secrets from internal APIs
2021-08-11 11:22:10 +01:00
Ben Thorner
e1dec3f9b8 Switch to per-app secrets from internal APIs
Relates to: [1]

[1]: https://github.com/alphagov/notifications-credentials/pull/231
2021-08-05 17:24:56 +01:00
Ben Thorner
96c527038c Only log authorization for public requests
This is the original behaviour [1]. Since all internal requests will
have corresponding logs from public-facing apps that are making them,
there's little value in logging them.

Logging internal requests doesn't lead to a significant increase in
our overall log ingestion: a rough estimate is its an extra 5000 logs
per minute, out of about 900K per minute.

[1]: e08d726f05/app/authentication/auth.py (L153)
2021-08-04 16:16:31 +01:00
Ben Thorner
09e3ba6836 DRY-up creating auth headers for requests
The rest of the tests need to construct the header directly so they
can pass custom tokens. But for the three tests that actually make
a request to prove the auth functions work as wrappers, we can use
the same factory functions we use everywhere else in the tests.
2021-08-04 16:06:23 +01:00
Ben Thorner
0312e2a528 Split generating authorization headers by type
In response to [1].

[1]: https://github.com/alphagov/notifications-api/pull/3300#discussion_r681653248
2021-08-04 15:13:52 +01:00
Ben Thorner
5a1636e41f Merge pull request #3300 from alphagov/multi-internal-auth-179039225
Split up authentication for internal API endpoint
2021-08-04 14:35:54 +01:00
Ben Thorner
a3cbea218f Rename "key" to "secret" for consistency
While "key" is the term used by the JWT library, all the rest of
our code - the ApiKey model, the Python client - all use the term
"secret" instead. Although "secret" is less precise than "key", it
does help avoid confusion with (api) key (as a model object).
2021-08-03 15:58:29 +01:00
Ben Thorner
4b7ad89f6a Add pretend authenticated API for govuk-alerts
We can define the API properly in future work. I've used a separate
blueprint from "broadcasts" since this API is purely internal, and
it's helpful to make it clear it's specific to govuk-alerts.
2021-08-03 15:58:28 +01:00
Ben Thorner
3e32fc99b8 Rename ADMIN_CLIENT_USER_NAME to say CLIENT_ID
"user name" implies we're doing basic auth, which we're not. We
should use the standard terminology for bearer tokens.
2021-08-03 15:58:27 +01:00
Ben Thorner
323feedb1f Backfill missing tests for requires_auth function 2021-08-03 15:58:26 +01:00
Ben Thorner
cd0a950571 Colocate requires_auth test with the others
This is to help minimise the diff for the next commit.
2021-08-03 15:58:25 +01:00
Ben Thorner
f5d2edaa0a Rewrite requires_admin_auth tests to be generic
This adds previously missing tests and changes the existing ones
to test the "requires_internal_auth" function directly. In order
to make the tests generic, we have a fake auth function and an
associated fixture.

Having generic tests for internal auth will make it easier to add
other "requires..." functions in future.
2021-08-03 15:58:24 +01:00
Ben Thorner
193e7e0004 Rewrite other requires_auth tests to call function
This makes the tests easier to read by avoiding request boilerplate
and making a clearer link between the name of the test and that we
are actually testing that specific function.
2021-08-03 15:58:23 +01:00
Ben Thorner
4fc820c0cc Remove redundant test about no auth proxy checks
This is a lot of code to check we haven't written a single line,
which we can just visually see isn't there. We should avoid having
tests that check code _isn't_ there, as such testing is infinite.
2021-08-03 15:58:22 +01:00
Ben Thorner
65d0dc43f0 Move happy path request-based tests to top of file
We can simplify the rest of the tests to avoid the boilerplate of
making an actual request. But it's worth keeping these two to prove
the wrapper work correctly for an arbitrary route.
2021-08-03 15:58:21 +01:00
Ben Thorner
9da937ab1d DRY-up tests for decoding JWT tokens
Previously we had a lot of duplicate tests inconsistently checking
each of the "requires_" functions. Since both of them now use the
same "_decode_jwt_token" helper, we can consolidate all the tests
onto that. In future commits we'll look at testing the top-level
functions in terms of what they do specifically.
2021-08-03 15:58:20 +01:00
Ben Thorner
2c568698d1 Simplify tests for get auth token / issuer
This switches to testing the two functions directly as trying to
test them through the top-level "requires_..." functions or calls
to endpoints doesn't scale as we add more of them.

While this has a slight risk that a "requires_..." function might
not be using these helpers, it seems unlikely and we can always
add a mock to check this if we're concerned in future.
2021-08-03 15:42:40 +01:00
Ben Thorner
1d806d65eb Standardise auth checks for both kinds of API auth
Previously "requires_auth" and "requires_admin_auth" had similar
but different ways of checking their keys. This switches them to
use the same checks, with the admin / internal auth passing in a
fake / stub set of "api keys" to check.

Pulling out the logic this way will make it easier to unpick the
tests, so we can focus on testing what's unique to each kind of
API auth and avoid future duplication when we start calling the
"requires_internal_auth" method with other client_ids.

Note that a couple of error messages / response codes have changed
for admin / internal auth. None of these occur in practice, so we
can make them consistent with the behaviour for the public API.
2021-08-03 15:42:39 +01:00
Ben Thorner
e08d726f05 DRY-up and simplify creating JWT tokens in tests
Previously this was heavily duplicated but with the odd test using
a __create_token method. This adds some fixtures to remove all the
boilerplate and standardise how tokens are created in each test.
2021-08-03 15:42:37 +01:00
Ben Thorner
36afd4210f Rename tests to match the function under test
This makes it easier to see what we're missing. I also tweaked one
test where a parameter wasn't necessary.
2021-08-03 15:41:40 +01:00
Pea Tyczynska
82e7724c56 Merge pull request #3296 from alphagov/separate_error_for_cancelling_cancelled_letters
Show separate error for when user tries to cancel letter
2021-08-03 10:29:47 +01:00
Chris Hill-Scott
17a046683f Merge pull request #3299 from alphagov/revoke-api-keys-when-service-becomes-broadcast
Revoke API keys when changing broadcast settings
2021-08-02 10:48:16 +01:00