Commit Graph

8300 Commits

Author SHA1 Message Date
Pea Tyczynska
e82b8bc33c Bump utils to revert changes to placeholders that introduced
a bug.
2021-07-09 14:45:41 +01:00
Pea Tyczynska
96f34bbd45 Merge pull request #3286 from alphagov/bump-utils-fix-placeholder-bug
Bump utils to bring in fix for optional placeholder bug
2021-07-09 11:24:42 +01:00
Katie Smith
18bb1c2ac4 Merge pull request #3277 from alphagov/bcast-service-permissions
Remove user permissions if service becomes a broadcast service
2021-07-09 09:12:45 +01:00
Pea Tyczynska
c28e9451d4 Bump moto version to try solve dependencies version conflict
Also update mock import statements in some test files as they
stopped working with this dependency update.
2021-07-08 15:37:19 +01:00
Pea Tyczynska
9e8682ac29 Bump utils to bring in fix for optional placeholder bug
See https://github.com/alphagov/notifications-utils/pull/878 for
details.

Changes we had to make for our app and tests to work correctly
after the dependency updates:

1. Update emergency alerts polygons test because we changed
how exact we are with locations of the points on the map.

2. Use Flask's g object to set additional request attributes

So far we have been storing them in _request_ctx_stack which is
an innard for Flask's request context.

Because of major update to Werkzeug dependency, which Flask relies
on, the way we were using it stopped working, so we had a new
way to set those values.
The way we set those values now, by using g object, seems to also
be favoured in Flask documentation:
https://flask.palletsprojects.com/en/1.1.x/reqcontext/#how-the-context-works
2021-07-08 12:18:09 +01:00
Katie Smith
fc0b9736eb Remove user permissions if service becomes a broadcast service
The "normal" service permissions and broadcast service permissions are
going to be different with no overlap. This means that if you were
viewing the team members page, there might be permissions in the
database that are not visible on the frontend if a service has changed
type. For example, someone could have the 'manage_api_keys' permission,
which would not show up on the team members page of a broadcast service.
To avoid people having permissions which aren't visible in admin, we now
remove all permissions from users when their service is converted to a
broadcast service.

Permisions for invited users are also removed.

It's not possible to convert a broadcast service to a normal service, so
we don't need to cover for this scenario.
2021-07-07 16:13:35 +01:00
Katie Smith
29a13a8fae Merge pull request #3274 from alphagov/new-permissions
Add new broadcast related permissions
2021-07-07 15:31:21 +01:00
Katie Smith
e5fdd8ee1f Add new broadcast related permissions
We want to have new permissions which will be used specifically for
broadcasts:
- `create_broadcasts`
- `approve_broadcasts`
- `reject_broadcasts`
- `cancel_broadcasts`

Cancel and reject will always go together, but having separate database
permissions makes things easier to change in the future.

The permission column of the permissions table is an enum. We can add values
in the alembic upgrade script, but removing individual values from an
enum is not supported by Postgres. To remove values, we have to recreate
the enum with the old values.
2021-07-07 14:54:13 +01:00
Ben Thorner
273d14fbe4 Merge pull request #3280 from alphagov/webauthn-user-flag
Add flag to say if user is eligible for WebAuthn
2021-07-01 11:10:06 +01:00
Ben Thorner
6cf24899dd Let existing WebAuthn users continue using it
It's not a big deal if a user is no longer eligible to register a
security key, so we may as well let them continue using it. This
avoids putting them in a limbo state if we don't immediately change
their auth type when they're no longer eligible to use the feature.
2021-06-30 15:41:43 +01:00
Rebecca Law
f4c0f1f1df Merge pull request #3282 from alphagov/fix-bug-check_service_over_daily_limit
Convert value from redis to an int.
2021-06-29 14:11:28 +01:00
Rebecca Law
c501c92bf3 Convert value from redis to an int.
This has been properly tested locally with redis enabled.
2021-06-29 14:00:25 +01:00
Rebecca Law
864554f125 Merge pull request #3281 from alphagov/fix-bug-check_service_over_daily_limit
Fix bug in check_service_over_daily_limit
2021-06-29 13:20:14 +01:00
Rebecca Law
ed788cb0bd Fix bug in check_service_over_daily_limit
I forgot to return service_stats if the cache exists. And fixed the tests to check service_stats value.
2021-06-29 12:59:20 +01:00
Rebecca Law
310e1cb4e2 Merge pull request #3276 from alphagov/daily-limit-redis-cache
Correct the daily limits cache.
2021-06-29 12:06:35 +01:00
Ben Thorner
2fa6327efb Add flag to say if user is eligible for WebAuthn
Currently we have some data-driven roles to say who can use this
feature. Adding a flag in the API means we can avoid API calls in
the Admin app to determine the same.

Allowing members of the GOV.UK Notify service to use the feature
is a workaround, so we can avoid making someone a Platform Admin
before they've protected their account with it.
2021-06-28 13:35:24 +01:00
Rebecca Law
18dd9050a4 - make sure when processing a job that we check the total_sent + job.notification_count against the service.message_limit. 2021-06-28 13:07:48 +01:00
Ben Thorner
2b292ebd16 Refactor "notify_service" function into a fixture
This means we can use it in the next commit. Also, it was surprising
for the function to be returning a tuple of values, instead of just
the service object. Since the consumers of the function only needed
the user as auditing data, it's fine to use the first team member.
2021-06-25 17:46:07 +01:00
Rebecca Law
fd7486d751 - Merge daily limit functions into one, refactor call for daily limit check from process_job
- refactor tests to standardise test names
- refactor some tests to be more clear
- remove unnecessary tests
- include missing test
2021-06-24 11:05:22 +01:00
Katie Smith
5c7b0d1dfb Merge pull request #3279 from alphagov/report-fix
Fix the endpoint for the monthly status report
2021-06-24 08:57:26 +01:00
Katie Smith
0f42b4dbec Fix the endpoint for the monthly status report
This wasn't working - the error given when trying to access it was
`TypeError: Object of type 'Row' is not JSON serializable` when we tried
to serialize a SQLAlchemy Row.

I haven't looked too far into what has changed to stop this from
working, but have just changed the endpoint to return a nested list instead.
2021-06-23 16:03:58 +01:00
Rebecca Law
57fb9da414 - change the condition so that we don't reset the cache if it's zero
- set the cache if it doesn't exist so there is an expiry of 24 hours.
2021-06-23 15:09:09 +01:00
Chris Hill-Scott
1c1023a877 Merge pull request #3257 from alphagov/refactor-high-volume-check
Refactor high volume into serialised service model
2021-06-23 09:01:27 +01:00
Rebecca Law
35b20ba363 Correct the daily limits cache.
Last year we had an issue with the daily limit cache and the query that was populating it. As a result we have not been checking the daily limit properly. This PR should correct all that.

The daily limit cache is not being incremented in app.notifications.process_notifications.persist_notification, this method is and should always be the only method used to create a notification.
We increment the daily limit cache is redis is enabled (and it is always enabled for production) and the key type for the notification is team or normal.

We check if the daily limit is exceed in many places:
 - app.celery.tasks.process_job
 -  app.v2.notifications.post_notifications.post_notification
 - app.v2.notifications.post_notifications.post_precompiled_letter_notification
 - app.service.send_notification.send_one_off_notification
 - app.service.send_notification.send_pdf_letter_notification

If the daily limits cache is not found, set the cache to 0 with an expiry of 24 hours. The daily limit cache key is service_id-yyy-mm-dd-count, so each day a new cache is created.

The best thing about this PR is that the app.service_dao.fetch_todays_total_message_count query has been removed. This query was not performant and had been wrong for ages.
2021-06-22 16:15:36 +01:00
Rebecca Law
c44ec57c17 Merge pull request #3266 from alphagov/update-notifications-model-with-indexes
Tidy up models
2021-06-21 12:43:08 +01:00
Rebecca Law
ff79c65cab Move the indexes to be inline with the table. 2021-06-21 12:06:38 +01:00
Ben Thorner
916a1b55c5 Merge pull request #3272 from alphagov/remove-test-script
Run tests directly from the Makefile
2021-06-16 17:32:06 +01:00
David McDonald
69212827eb Merge pull request #3270 from alphagov/broadcast-status-transition-tests
Broadcast status transition tests
2021-06-16 16:04:15 +01:00
Ben Thorner
149976bfab Run tests directly from the Makefile
Contributes to: https://github.com/alphagov/notifications-manuals/issues/9

Precedent: https://github.com/alphagov/notifications-admin/pull/3897
2021-06-16 13:05:55 +01:00
Ben Thorner
47b1e280bb Merge pull request #3271 from alphagov/dont-upgrade-ci
Don't try to upgrade DB on CI
2021-06-16 11:49:57 +01:00
Chris Hill-Scott
57249b43c8 Refactor high volume into serialised service model
Just looks a bit tidier and less repetitive.

I’ve only done this for the serialised service because:
- we’re only checking this in places where we’re already using the
  serialised service
- if we want to check this elsewhere there’s a good chance that new code
  should be using the serialised service, since it’ll itself be doing
  some kind of performance optimisation
2021-06-16 10:46:18 +01:00
Rebecca Law
467794c212 Merge pull request #3269 from alphagov/permanent-failure-for-letters
Add permanent-failure for letters.
2021-06-16 10:42:08 +01:00
Rebecca Law
d4a42471cb Merge pull request #3267 from alphagov/fix-daily-totals-query
Improve the query to get today's totals for a service.
2021-06-16 07:34:01 +01:00
Ben Thorner
d98ea6a8bc Don't try to upgrade DB on CI
This was added by mistake - the Concourse pipeline never did this
previously, and errors if we try (the necessary environment vars
aren't present, even if we wanted to).
2021-06-15 17:33:19 +01:00
David McDonald
5b409bd3c3 Add test coverage for broadcast status transition
This was mentioned in an old pen test report that you could send a
request twice to set a broadcast message as broadcasting which would
trigger us to send two alerts.

It looks like this is now fixed and this test coverage backs that up.

Note, it's unlikely that it would have been an issue anyway as the CBC
would likely have rejected the message as it would notice it is a
duplicate.

Note, this test coverage is not supposed to be exhaustive of all the
potential transitions but covers the vast majority of ones that we care
about.

See `BroadcastStatusType.ALLOWED_STATUS_TRANSITIONS` for allowed
transitions.
2021-06-15 17:27:21 +01:00
David McDonald
54fe8ee68d Remove old todo for support of draft to broadcasting transition
It looks like we were allowing broadcasts to transition from draft to
broadcasting in one go. This isn't valid now. It should go draft,
pending approval and then broadcasting.

It looks like this was a leftover bit of support in our code for when we
were building stuff out and is no longer needed.
2021-06-15 17:18:54 +01:00
Rebecca Law
6d09313b18 Merge pull request #3268 from alphagov/update-job-sooner
Update the job_status to in-progress sooner.
2021-06-15 15:45:52 +01:00
Rebecca Law
2c36898684 Add permanent-failure for letters.
It's possible a letter can pass our validation but our print provider can not print the letter. The letter will be marked as permanent failure in this case. Typically happens with precompiled letters.
2021-06-15 15:12:46 +01:00
Ben Thorner
92d52de68f Merge pull request #3254 from alphagov/fix-ci-bootstrap
Fix 'flask db upgrade' not working on CI
2021-06-15 10:23:33 +01:00
Ben Thorner
c89200a833 Fix 'flask db upgrade' not working on CI
Related to: https://github.com/alphagov/notifications-aws/pull/905

Previously this would fail because the Docker image we use for CI
builds doesn't have an 'environment.sh' file; it uses preset env
vars instead. This makes the command to upgrade the DB optional -
if the env file is missing, the error should be self evident.
2021-06-15 09:58:19 +01:00
Rebecca Law
8af10eb1f0 Update the job_status to in-progress sooner.
We had a situation where the delivery-worker app instance was terminated before the job was marked as `in-progress`, presumably because the query to check the daily limits was taking too long to complete.
If the job was in progress the `check_job_status` task would have restarted the job.
Updating the status to in-progress sooner will help.
2021-06-15 07:58:17 +01:00
Rebecca Law
08bb5c657f Fix the query to get todays totals for a service.
The query had a group by on notification_type and notification_status, this not only slows the query down but is wrong. The query only looked at the first result, but this query would return as many rows as different notification types and status, meaning the results do not include the correct number.

Are we concerned that all status types are included. For example letters can be cancelled or have validation-failures which shouldn't be included in the daily limit check.
2021-06-14 15:29:21 +01:00
Rebecca Law
0688a16cb2 Tidy up models
- Update the Notification and NotificationHistory model to reflect the database.
- Updates to datatypes, removal of indexes and addition of indexes.

Why?
After running the `flask db migrate` command there are many deltas because we did some work to update the notification and notification_history tables, however, the SQLAlchemy models were not updated to reflect those changes. This PR cleans up all those deltas.
However, there are still some differences that can be done but we can look at that in another PR.
2021-06-14 14:43:34 +01:00
Katie Smith
ec646c3071 Merge pull request #3264 from alphagov/billing-report
Add new total_letters field to the billing report data
2021-06-11 14:45:03 +01:00
Katie Smith
0148b3dba6 Add new total_letters field to the billing report data
This adds total_letters to the data that is returned by the
`/platform-stats/data-for-billing-report` endpoint so that we can add
total letters as a column in the CSV file that can be downloaded.
2021-06-11 11:31:22 +01:00
David McDonald
6a99a1fbc2 Merge pull request #3262 from alphagov/operator-channel
Operator channel
2021-06-10 09:57:33 +01:00
David McDonald
be035664c4 Add operator channel to broadcast settings route
Looks identical to the government channel in terms of the interface
2021-06-09 13:49:06 +01:00
David McDonald
d18bf2c48a Add operator channel migration
Looks identical to the government channel migration 354
2021-06-09 13:48:02 +01:00
Leo Hemsted
8e1a144f87 Merge pull request #3229 from alphagov/data-error
make sure all non-uuid service ids 403 in api keys
2021-06-07 14:09:22 +01:00
Leo Hemsted
c53ed4107b Merge pull request #3260 from alphagov/verify-to-complete
rename verify webauth endpoint to complete
2021-06-04 13:25:10 +01:00