Commit Graph

7381 Commits

Author SHA1 Message Date
Leo Hemsted
efa2e75e56 add broadcast message tests
sorry for big unhelpful test commit
2020-07-09 14:19:58 +01:00
Leo Hemsted
61a5730596 add more friendly datetime validator to jsonschema
add `datetime` format (note, not the built-in `date-time`) to our json
schemas. this uses the iso8601 library to try and parse the string.

also, move `strict-rfc3339` and `rfc3987` (used by jsonschema to
validate `date-time` and `uri` formats respectively from test
requirements to regular requirements. if they're not installed,
validation silently succeeds, so validation wouldnt reject anything bad
on prod, only in unit tests.
2020-07-09 14:19:58 +01:00
Leo Hemsted
67f6dcae45 add broadcast message crud
new blueprint `/service/<id>/broadcast-message` with the following
endpoints:

* GET / - get all broadcast messages for a service
* GET /<id> - get a single broadcast message
* POST / - create a new broadcast message
* POST /<id> - update an existing broadcast message's data
* POST /<id>/status - move a broadcast message to a new status

I've kept the regular data update (eg personalisation, start and end
times) separate from the status update, just to keep separation of
concerns a bit more rigid, especially around who can update.

I've included schemas for the three POSTs, they're pretty
straightforward.
2020-07-09 14:19:56 +01:00
Rebecca Law
40f747d8ef Merge pull request #2911 from alphagov/clean-up
Clean up
2020-07-09 07:56:22 +01:00
Pea M. Tyczynska
5538c9697b Merge pull request #2905 from alphagov/dont-fail-check-migration
always run migrations if app is down
2020-07-08 11:49:32 +01:00
Pea M. Tyczynska
9186083ea7 Merge pull request #2796 from alphagov/split-letters-into-zips-based-on-postage
Split letters into zips based on postage
2020-07-08 11:49:21 +01:00
Leo Hemsted
d2007674a9 Merge pull request #2918 from alphagov/serialised-template-fix
remove broadcast_data from SerialisedTemplate.ALLOWED_PROPERTIES
2020-07-08 09:55:24 +01:00
Leo Hemsted
9c25b7dfd5 remove broadcast_data from SerialisedTemplate.ALLOWED_PROPERTIES
ALLOWED_PROPERTIES is a list containing fields that it will attempt to
deserialize into the returned object. If a field isn't present on the
underlying data source (whether that's a dump from a marshmallow schema,
a blob retrieved from redis, or anything else), then SerialisedModel
will raise an exception. However, SerialisedModel isn't involved when
setting the cache, so with that said the correct flow for adding a
column to a cached database model:

PR #1

* add new column to DB
* add new column to model, and to template_schema (because that schema
  is used to create the dict that goes in to redis). New redis keys
  start getting populated.

Deploy that through, and then clear redis

PR #2

* add new column to SerialisedTemplate.ALLOWED_PROPERTIES. this means
  that it'll start reading that value from redis
* now instances of the app will always have the new field in their
  template objects, whether they came from database directly or from
  the cache

This commit removes the field from ALLOWED_PROPERTIES. After it's
deployed we'll be able to clear redis, observe redis being populated
with the new field, and then we'll be able to re-add it to
ALLOWED_PROPERTIES, ready to use.
2020-07-07 17:00:44 +01:00
Leo Hemsted
c3dadde505 Merge pull request #2914 from alphagov/broadcast-models
Broadcast DB models
2020-07-07 15:25:00 +01:00
Leo Hemsted
3486355354 remove any broadcast templates in downgrade step 2020-07-06 17:43:44 +01:00
Leo Hemsted
0282a76bf7 rename template.serialize to serialize_for_v2
make it clear that this is for the public api, and we shouldn't add
fields to it without considering impacts

also add the broadcast_messages relationship on service and template to
the exclude from the marshmallow schemas, so it's not included elsewhere
2020-07-06 16:42:31 +01:00
Leo Hemsted
5f337f7914 add broadcast_message to database
new broadcast_message table. contains a whole bunch of timestamps, a
couple of user ids for who created and who approved, some
personalisation and a template id/version. areas is a jsonb field - it
is expected that this will be an array, for example `["england",
"wales"]`

intended valid state transitions are as follows, from an initial status
of draft

* draft -> pending-approval, rejected
* pending-approval -> broadcasting, rejected, (draft maybe)
* broadcasting -> completed, cancelled

rejected, completed, cancelled and technical-failure are terminating
states.

also copy across the enum switching code from migration 0060 (adding
letter type).
2020-07-06 16:37:35 +01:00
Leo Hemsted
7ecd7341b0 add broadcast to template_types and add broadcast_data
had to go through the code and change a few places where we filter on
template types. i specifically didn't worry about jobs or notifications.

Also, add braodcast_data - a json column that might contain arbitrary
broadcast data that we'll figure out as we go. We don't know what it'll
look like, but it should be returned by the API
2020-07-06 15:47:13 +01:00
Pea M. Tyczynska
3954cd0e4a Merge pull request #2916 from alphagov/prevent-race-condition-sms-status
Do not update notification to sending if the status is already final
2020-07-06 13:16:36 +01:00
Chris Hill-Scott
8d92a1ec6b Merge pull request #2915 from alphagov/bump-utils-40.2.1
Bump utils to 40.2.1
2020-07-06 10:47:35 +01:00
Pea Tyczynska
efdaadbdf4 Do not update notification to sending if the status is already final
This prevents a race condition when we get delivery receipt before
updating notification to sending, and so the sending status would
supersede the delivered status, and the notification would time out
as temporary-failure after three days.
2020-07-03 17:19:03 +01:00
Chris Hill-Scott
e9dfbeca37 Bump utils to 40.2.1
Changes: https://github.com/alphagov/notifications-utils/compare/40.0.0...40.2.1
2020-07-03 14:27:28 +01:00
Pea M. Tyczynska
106185f418 Merge pull request #2913 from alphagov/turn-stubs-off-staging
Turn off the sms stub and email stub in staging
2020-07-03 10:33:29 +01:00
Pea Tyczynska
e02508d6f7 Turn off the sms stub and email stub in staging 2020-07-02 17:23:50 +01:00
Pea Tyczynska
61de908c5d Simplify putting letters in right postage folders 2020-07-02 16:26:44 +01:00
David McDonald
7e4acda633 Merge pull request #2910 from alphagov/inbound-sms-metric
Add prometheus client metric for number of inbound text messages
2020-06-30 11:49:57 +01:00
Leo Hemsted
c04c79a0a7 Merge pull request #2907 from alphagov/broadcast-permission
broadcast service permission
2020-06-30 10:15:17 +01:00
David McDonald
1e253b2257 Add prometheus client metric for number of inbound text messages
As we gradually move from statsd to prometheus, we change the metric to
be a prometheus metric rather than statsd.

The change worth pointing out is that we have dropped the 'successful'
and 'failed' statuses from the metrics. I don't think it's useful to
have these statuses. It's very rare for an inbound message to fail when
we receive it and when it does, we raise an error and see it in our
logs. We aren't going to be looking at a graph of it as it's a rare
event, not typical behaviour that we want to monitor with a graph.
2020-06-30 10:13:55 +01:00
Chris Hill-Scott
ab3d25ea91 Merge pull request #2909 from alphagov/fix-international-one-off
Fix sending one-off international text messages
2020-06-30 09:29:13 +01:00
Rebecca Law
1be479a15a Merge branch 'master' into clean-up 2020-06-30 09:05:12 +01:00
Rebecca Law
6768ac0d35 Merge branch 'master' of github.com:alphagov/notifications-api 2020-06-30 09:04:58 +01:00
Rebecca Law
7fb3b3db18 Small changes to tidy up the code 2020-06-30 09:04:24 +01:00
Chris Hill-Scott
5ef9ad2953 Fix sending one-off international text messages
This was broken because sometimes `service.permissions` is a list of
strings (for when we’re caching the service object) and sometimes it’s a
list of permission objects (when we’re dealing with ORM objects).

Because the validator code is shared, the least-messy way to fix it is
to make sure it can handle both types.

It can’t just take a list of permissions as argument, because it uses
other fields on the service.

It would be messy to rewrite the endpoint to use a serialised service
because the tests all expect to be dealing with database objects, so it
would be a faff to change what they’re mocking.
2020-06-29 14:43:33 +01:00
Chris Hill-Scott
40f09097c8 Merge pull request #2908 from alphagov/fix-template-history-created-by
Fix template history created by
2020-06-29 13:12:44 +01:00
Chris Hill-Scott
bf6e468a7c Keep excluding created_by from TemplateSchemaNoDetail 2020-06-29 12:48:24 +01:00
David McDonald
7d68280445 Merge pull request #2901 from alphagov/turn-off-sync-statsd-for-posts
Turn off statsd wrapper for synchronous statsd calls during POSTs
2020-06-29 12:47:49 +01:00
David McDonald
12f460adc5 Turn off statsd wrapper for synchronous statsd calls during POSTs
This commit turns off StatsD metrics for the following
- the `dao_create_notification` function
- the `user-agent` metric
- the response times and response codes per flask endpoint

This has been done with the purpose of not having the creation of text
messages or emails call out to StatsD during the request process. These
are the three current metrics that are currently called during the
processing of one of those requests and so have been removed from the
API.

The reason for removing the calls out to StatsD when processing a
request to send a notification is that we have seen two incidents
recently affected by DNS resolution for StatsD (either by a slow down in
resolution time or a failure to resolve). These POST requests are our
most critical code path and we don't want them to be affected by any
potential unforeseen trouble with StatsD DNS resolution.

We are not going to miss the removal of these metrics.
- the `dao_create_notification` metric is rarely/never looked at
- the `user-agent` metric is rarely/never looked at and can be got from
  our logs if we want it
- the response times and response codes per flask endpoint are already
  exposed using the gds metrics python library

I did not remove the statsd metrics from any other parts of the API
because
- As the POST notification endpoints are the main source of web traffic,
  we should have already removed most calls to StatsD which should
  greatly reduce the chance of their being any further issues with
  DNS resolution
- Some of the other metrics still provide value so no point deleting
  them if we don't need to
- The metrics on celery tasks will not affect any HTTP requests from
  users as they are async and also we do not currently have the
  infrastructure set up to replace them with a prometheus HTTP endpoint that
  can be scraped so this would require more work
2020-06-29 12:40:22 +01:00
Chris Hill-Scott
f541ad42d6 Revert "Avoid extra query when serialising Template created_by"
This reverts commit 58a9862cd1.

That commit tried to optimize the fetch template query by

However it had the side effect of making Marshmallow ignore `created_by`
when loading the JSON in the post request. So the field on the model was
never being updated, just copied from the original template.

The quick way of getting things to work again is to revert this
optimisation.

There’s probably some Marshmallow magic we could use to avoid the extra
query and still have created_by not be ignored.

It does mean we’re introducing an extra query, but I’m not too fussed
about that since the caching seems to be working well.
2020-06-29 12:30:08 +01:00
Chris Hill-Scott
02e5ff61a4 Test that template history updates created by 2020-06-29 12:30:08 +01:00
Leo Hemsted
63f6d8df6b broadcast service permission
it'll eventually be a template type too, so add the _TYPE suffix to the var name to make refactoring easier
2020-06-29 11:53:57 +01:00
Chris Hill-Scott
a0a5cde15e Merge pull request #2906 from alphagov/fix-callback-serialisation
Fix serialisation of callbacks
2020-06-26 16:52:26 +01:00
Chris Hill-Scott
8def7d0d3b Fix serialisation of callbacks
Because the IDs of our callback and inbound SMS APIs were stored in
lists instead of directly on the serialised model they weren’t getting
cast to a string before trying to JSONify them. And JSON doesn’t know
what to do with a UUID object.

For some reason this was only affecting the endpoint for fetching
inbound SMS.
2020-06-26 16:31:49 +01:00
Chris Hill-Scott
8cef34d770 Merge pull request #2904 from alphagov/validate-from-serialised-model
Validate from serialised model
2020-06-26 15:35:18 +01:00
Leo Hemsted
b01ec05aaf run migrations if app is down
normally we check the app's status page to see if migrations need
running. However, if the _status endpoint doesn't respond with 200, we
don't necessarily want to abort the deploy - we may be trying to deploy
a code fix that fixes that status endpoint for example.

We don't know whether to run the migrations or not, so err on the side
of caution by re-running the migration. The migration itself might be
the fix that gets the app working after all.

had to do a little song and dance because sometimes the response won't
be populated before an exception is thrown
2020-06-26 15:28:28 +01:00
Chris Hill-Scott
aa0c019fea Merge pull request #2896 from alphagov/get-letter-data-from-filenames
Get letter data for provided filenames
2020-06-26 14:51:19 +01:00
Chris Hill-Scott
9f41e77bf7 Add rate_limit and message_limit to SerialisedService
The API needs these to check whether a service can send a notification.
This commit also updates all the tests in `test_validators.py` to take
a serialised service, not a database object.
2020-06-26 14:10:25 +01:00
Chris Hill-Scott
1f315b06e2 Revert "Revert "Merge pull request #2902 from alphagov/fix-imports""
This reverts commit b8fe7b8e61.
2020-06-26 14:10:21 +01:00
Chris Hill-Scott
3ffdb3093b Revert "Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things""
This reverts commit 7e85e37e1d.
2020-06-26 14:10:12 +01:00
Chris Hill-Scott
6d52d733a4 Merge pull request #2903 from alphagov/revert-serialisation-changes
Revert serialisation changes
2020-06-26 14:07:58 +01:00
Chris Hill-Scott
59aba018bd Ensure rate limit is in serialised service
Once we start using the serialised service to power the `POST`
notifications endpoint it needs to include rate limit to do the rate
limit checks.
2020-06-26 13:46:32 +01:00
Chris Hill-Scott
7e85e37e1d Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things"
This reverts commit b8c2c6b291, reversing
changes made to 351aca2c5a.
2020-06-26 13:42:44 +01:00
Chris Hill-Scott
b8fe7b8e61 Revert "Merge pull request #2902 from alphagov/fix-imports"
This reverts commit e00c0355b6, reversing
changes made to 832b589980.
2020-06-26 13:42:22 +01:00
Rebecca Law
41312f950d Merge branch 'master' of github.com:alphagov/notifications-api 2020-06-26 13:24:28 +01:00
Chris Hill-Scott
e00c0355b6 Merge pull request #2902 from alphagov/fix-imports
Fix circular import
2020-06-26 12:25:42 +01:00
Chris Hill-Scott
616523bf74 Fix circular import
We changed auth.py to import from app.serialised_models here:
https://github.com/alphagov/notifications-api/pull/2887/files#diff-77cbb1e03185c7319f0311371c438b0cR11

`serialised_models.py` imports from `templates_dao.py`

`templates_dao.py` imports from `users_dao.py`

`users_dao.py` imports from `errors.py`

`errors.py` imports from `auth.py` … and the circle is complete 💥

For some reason this caused the Celery workers to crash on startup, but
not the app. Which I guess is why the integration tests didn’t catch
this?
2020-06-26 10:20:32 +01:00