Commit Graph

8156 Commits

Author SHA1 Message Date
Rebecca Law
dcd08a0e45 Add unit test for archiving a template with a template folder 2021-04-27 12:30:50 +01:00
Rebecca Law
f941768d8c Change the query to get the services to purge to use query on the db.Model rather than db.session.query.
`service_ids_to_purge` is a list of `row` object rather than a list of `UUID`.

NOTE: db.session.query(Service).filter(Service.id.notin_(services_with_data_retention)).all() would have also worked. It seems that only selecting attributes from the db.Model has caused the change.
2021-04-27 08:36:34 +01:00
Rebecca Law
1b070d69a1 The update of SQLAlchemy 1.4.10 has caused some conflicts in our code. This PR fixes most of those conflicts.
- sqlalchemy.sql.expression.case must include an else statement.
- clearly define list of columns for inbound_sms_history insert, getting the list from InboundSmsHistory.__table__.c was causing data type errors.
- remove relationships when not needed, the foreign key relationship is established in the creation of the column. This will get rid of the warnings referenced here: http://sqlalche.me/e/14/qzyx.
- update queries now that he user relationship in ServiceUser db model has been removed.
- move the check that a template is archived to the view instead of the dao method. The check was clearing the session before the version history could be done.

Deleting notifications in the night tasks still needs to be
investigated. The raw sql is causing an error.
2021-04-26 11:50:30 +01:00
Pea Tyczynska
e1a626855d Update error messages after SQLAlchemy version update 2021-04-23 16:48:36 +01:00
Pea Tyczynska
66127f3800 Apply requirements update 2021-04-22 16:59:15 +01:00
pyup-bot
c07e991c0a Update sqlalchemy from 1.3.23 to 1.4.10 2021-04-21 14:00:02 +01:00
Katie Smith
7ab0403ae7 Merge pull request #3216 from alphagov/bcast-logging
Add detail to broadcast logging messages
2021-04-21 09:03:38 +01:00
Rebecca Law
fbd231cefd Merge pull request #3179 from alphagov/add-international-firetext-api-key
Add international API key for firetext
2021-04-21 08:49:11 +01:00
Katie Smith
e6357c91c9 Add more details to messages in send_broadcast_provider_message task
This ensures that the log messages both contain broadcast_event id and
broadcast_provider_message id. It also removes the broadcast_event
reference since this isn't particularly useful in helping to find an
event.
2021-04-20 15:34:49 +01:00
Katie Smith
c9c4bd8b44 Clarify log line when sending a link test
It wasn't clear what the ID in the message was. It's not possible to add
more details to the message - we don't create a broadcast message or
event for a link test.
2021-04-20 14:54:53 +01:00
Pea Tyczynska
38af26cc78 Merge pull request #3199 from alphagov/admin-cancel-broadcast
Allow platform admins to cancel broadcasts.
2021-04-20 14:47:45 +01:00
Rebecca Law
f3fdd3b09b Add internation api key for firetext.
We want to start using Firetext for sending international SMS. They
require us to use a different API key for international SMS because it
requires a new code path to switch the sender ID to something that the
country will accept.
This PR does not include switching the sender of international SMS to
Firetext but sets us up to do so.
2021-04-20 13:58:55 +01:00
Pea Tyczynska
fce6a2d8dc Allow platform admins to cancel broadcasts.
Also update error message for when someone does not have permissions.

The message referenced approving broadcasts specifically, whereas
people would also see it if they try to cancel or reject
broadcast without permission.

Why we allow platform admins to cancel broadcasts:
we do this so they can react quickly if a broadcast was
approved by accident.
2021-04-20 12:27:38 +01:00
Rebecca Law
f2167aae2b Merge pull request #3213 from alphagov/set-annual_billing-to-default-if-missing
Set free allow to default if row is missing when calling GET
2021-04-20 08:02:04 +01:00
Katie Smith
3d71815956 Merge pull request #3209 from alphagov/pyup-scheduled-update-2021-04-14
Scheduled weekly dependency update for week 15
2021-04-19 14:09:31 +01:00
Katie Smith
1f301fd0f1 Pin marshmallow-sqlalchemy below v0.24.0
This was pinned to `<0.24.1` not `<0.24.0` by mistake, which meant pyup
was trying to upgrade from version `0.23.1`.
2021-04-19 13:43:59 +01:00
Rebecca Law
ae57521b39 Simplify the get_free_sms_fragment limit for the case when the row is
missing, by setting the free allowance to the default.
2021-04-19 13:29:04 +01:00
Katie Smith
6287b40732 Stop pyup upgrading Cryptography
We'd specified that the version should be less than 3.4 previously, but
needed to also tell pyup not to change that.
2021-04-19 11:52:58 +01:00
Katie Smith
1439f1b528 Freeze requirements 2021-04-19 11:38:55 +01:00
pyup-bot
4943498107 Update prometheus-client from 0.9.0 to 0.10.1 2021-04-19 11:38:55 +01:00
pyup-bot
29c89ee03a Update gunicorn from 20.0.4 to 20.1.0 2021-04-19 11:38:55 +01:00
pyup-bot
f58221b0c0 Update eventlet from 0.30.1 to 0.30.2 2021-04-19 11:38:55 +01:00
Rebecca Law
bcd1939179 Merge pull request #3212 from alphagov/update-zendesk-message
Update the Zendesk ticket content for `check_if_letters_still_in_created`
2021-04-19 11:36:28 +01:00
Rebecca Law
91542ad33e Merge pull request #3197 from alphagov/update-annual-billing-if-org-changes
Set default free allow when organisation for a service changes
2021-04-19 11:34:24 +01:00
Rebecca Law
d4009ffc52 Rename database management functions.
Rename @transactional to @autocommit.
Rename nested_transaction to tranaction.
2021-04-19 10:56:00 +01:00
Rebecca Law
34a378a60e Update the Zendesk ticket content for
`check_if_letters_still_in_created`

The message to Zendesk includes a list of notification ids, this isn't
really necessary and is included in the run book. Creation of the
Zendesk ticket can fail if the message is too long, removing the list of
ids can prevent that from happening.
2021-04-19 10:47:25 +01:00
David McDonald
346fff807f Merge pull request #3211 from alphagov/remove-api-callbacks-app
Remove api-sms-callbacks app
2021-04-16 12:41:13 +01:00
David McDonald
dbc947e900 Remove api-sms-callbacks app
We no longer need this as it has been replaced by the api-sms-receipts
app which is the new app with the correct name but no functionality
change.
2021-04-16 11:30:01 +01:00
Katie Smith
e737217c3f Merge pull request #3208 from alphagov/check-bcast-service-live
Check if service is in live mode before sending a broadcast
2021-04-16 09:11:53 +01:00
Ben Thorner
c026d16c05 Merge pull request #3210 from alphagov/fix-apply-async
Fix apply_async not working with positional kwargs
2021-04-15 17:31:16 +01:00
Ben Thorner
be02573147 Fix apply_async not working with positional kwargs
Celery's apply_async function accepts 'kwargs' as (get ready to be
confused) either a positional argument, or a keyword argument:

Positional: apply_async(['args'], {'kw': 'args'})

Keyword: apply_async(args=['args'], kwargs={'kw': 'args'})

We rely on the positional form in at least one place [1]. This fixes
the overload of apply_async to cope with both forms, and continue to
pass through any other (confusion time again) keyword args to super(),
such as queue="queue".

Note that we've also decided to stop accepting other positional args,
since this is unnecessarily confusing, and we don't currently rely on
it in our code. This stops it creeping in in future.

[1]: fde927e00e/app/job/rest.py (L186)
2021-04-15 17:21:21 +01:00
Katie Smith
59978fd99a Check if a service is live before sending a broadcast
We only want to send a broadcast if the broadcast message is not stubbed
and the service is live at the point at which the broadcast event should
be created. This is to prevent the situation where a broadcast service is
switched to live / trial mode in between the message being created and
approved (we log an error if this happens).

A stubbed broadcast message with a trial mode service at the point of
approval is not an issue - trial mode services can approve their own
broadcasts. In this situation, we don't create the broadcast event but
also don't need to log an error.
2021-04-15 15:01:32 +01:00
Katie Smith
df82b514d1 Don't create broadcast event unless necessary
If we're not going to send a broadcast, we don't need to create the
BroadcastEvent in the database. The BroadcastMessage contains all the
data we need - the BroadcastEvent is not used.

Not creating the event when we won't send the broadcast (e.g. when the
broadcast message was created when the service was in trial mode) adds
an extra layer of security.
2021-04-15 14:55:45 +01:00
Ben Thorner
fde927e00e Merge pull request #3205 from alphagov/celery-consistency-tweaks
Small refactor for new Celery / StatsD code
2021-04-15 11:40:42 +01:00
David McDonald
bbc2de10de Merge pull request #3206 from alphagov/sms-receipts
Add sms receipts app manifest config
2021-04-15 09:17:23 +01:00
Ben Thorner
f85dad5acf Merge pull request #3203 from alphagov/remove-statsd-decorators
Remove redundant @statsd timing decorators
2021-04-14 10:04:04 +01:00
Rebecca Law
93908bacda New strategy for transaction management.
Introduce a contextmanger function to handle exceptions and nested
transactions. Using the nested_transaction will start a
nested transaction with `db.session.begin_nested`, once the nested
transaction is complete the commit will happen.
`@transactional` has been updated to commit unless in a nested
transaction.
2021-04-14 07:04:17 +01:00
Rebecca Law
cf35135605 Adding @nested_transactional for transactions that require more than one
db update/insert.

Using a savepoint for the multiple transactions allows us to rollback if
there is an error when executing the second db transaction.
However, this does add a bit of complexity. Developers need to manage
the db session when calling multiple nested tranactions.

Unit tests have been added to test this functionality and some end to
end tests have been done to make sure all transactions are rollback if
there is an exception while executing the transaction.
2021-04-14 07:03:57 +01:00
Rebecca Law
9a03e579d6 When a service is created add the default annual billing for the service.
This will need to be merged before https://github.com/alphagov/notifications-admin/pull/3855, it will be that until the admin PR is merged the annual billing will be set twice, but that's not an issue.
2021-04-14 07:03:57 +01:00
Rebecca Law
69e5ddae4f When a service is associated with a organisation set the free allowance to
the default free allowance for the organisation type.

The update/insert for the default free allowance is done in a separate
transaction. Updates to services need to happen in a transaction to
trigger the insert into the ServicesHistory table. For that reason the
call to set_default_free_allowance_for_service is done after the service
is updated.
I've added a try/except around the set_default_free_allowance_for_service call to ensure we still get the update to the service but get an exception log if the update to annual_billing fails. I believe it's important to preserve the update to the service in the unlikely event that the annual_billing upsert fails.
2021-04-14 07:03:57 +01:00
David McDonald
39d7c347b0 Add sms receipts app manifest config
This app will replace the `notify-api-sms-callbacks` app as it is an app
that handles receipts, not callbacks.

After this and the corresponding concourse PR is merged and deployed (at which
point we will have two apps sharing the traffic) we can then put in PRs
to remove the `notify-api-sms-callbacks` app.

There is a chance it could all be done as a single PR (or at least one
for the API and one for the concourse pipelines) but I'm playing it safe
and doing it as a very clear two step process just in case.
2021-04-13 18:03:05 +01:00
Ben Thorner
5eb265138b Remove unnecessary statsd_client parameter
It turns out this is available from the app object [1], and we were
already assuming this in the tests.

[1]: 48c6c822e8/notifications_utils/clients/statsd/statsd_client.py (L52)
2021-04-13 15:12:55 +01:00
Ben Thorner
ec6d87cd0f Simplify argument passing in apply_async
This avoids the need to keep in-sync with any future changes to the
signature, and reduces the amount of irrelevant code to read.
2021-04-13 15:12:45 +01:00
David McDonald
2e6d761691 Merge pull request #3204 from alphagov/broadcast-envars
Broadcast envars
2021-04-12 17:25:15 +01:00
David McDonald
4437d60dd7 Only give broadcasts worker IAM creds for CBC proxy
There is no need to give it to any of the other workers and so the fewer
instances that have these creds the better.

You can verify this works by running
```
CF_APP=notify-api CF_SPACE=preview make generate-manifest
```

vs

```
CF_APP=notify-delivery-worker-broadcasts CF_SPACE=preview make generate-manifest
```
2021-04-12 17:05:42 +01:00
David McDonald
295162c81d Move CBC proxy enable check
This change will make our development environments closer to production
even if they aren't hooked up to the CBC proxy lambda functions.

Now in development, we will create the broadcast event and create tasks
for each broadcast provider event. We will still not create actual
broadcast provider message rows in the DB and talk to the CBC proxies.

This should be helpful in development to catch any issues we introduce
to do with sending broadcast messaging. In time we may wish to have some
fake CBC proxies in the AWS tools account that we can interact with to
make it even more realistic.
2021-04-12 17:05:41 +01:00
Ben Thorner
e3e067c795 Remove redundant @statsd timing decorators
These are superseded by timing task execution generically in the
NotifyTask superclass [1]. Note that we need to wait until we've
gathered enough data under the new metrics before removing these.

[1]: https://github.com/alphagov/notifications-api/pull/3201#pullrequestreview-633549376
2021-04-12 15:19:18 +01:00
Ben Thorner
3e507eea55 Merge pull request #3201 from alphagov/revamp-celery-stats
Migrate towards new metrics for Celery tasks
2021-04-12 15:04:37 +01:00
Ben Thorner
37f91e0214 Add tests for apply_async injecting request_id 2021-04-12 14:50:55 +01:00
Ben Thorner
df6e27d8fd Add test for extracting request_id in __call__
Tasks will fail if we leave the kwarg in, so I think it's quite
important that we test this works. We don't cover this in any other
test because we call the task functions directly, so the request_id
kwarg doesn't get injected beforehand.
2021-04-12 14:50:53 +01:00