Commit Graph

3913 Commits

Author SHA1 Message Date
Alexey Bezhan
db1c647873 Remove NotificationHistory.template relationship
Relationship attribute is not used by the application code, so we
can remove it without replacing it with a TemplateHistory one.

This also updates the foreign key constraint to refer to the composite
primary key for the TemplateHistory records.
2017-11-10 15:13:55 +00:00
Alexey Bezhan
bdcc89b403 Add TemplateHistory.redact_personalisation attribute
TemplateHistory objects need to be connected to the template's
TemplateRedacted record. This requires setting up a new SQLAlchemy
relationship to allow accessing redact_personalisation from
TemplateHistory instances.

We can avoid creating a foreign key in this case by setting up an
explicit `primaryjoin` expression. Since TemplateHistory.id is
created from Template.id and TemplateRedacted.template_id is
already a foreign key to Template.id the foreign key should always
be valid even without a DB constraint.
2017-11-10 15:13:55 +00:00
Alexey Bezhan
79d417f13b Add TemplateHistory.get_link method
Notifications.serialize calls `Notifications.template.get_link`, so
we need TemplateHistory objects to generate their API URLs.

This generates a link to the `/v2/` version of the endpoint.
2017-11-10 15:13:55 +00:00
Alexey Bezhan
c62f2a3f7c Update create_custom_template test helper to create history object
`create_custom_template` is not using `dao_create_template` since
it explicitly sets a template id. Notifications.template relationship
now refers to a TemplateHistory objects, so we need to make sure that
`create_custom_template` creates a matching TemplateHistory record
when it creates a Template.
2017-11-10 15:13:55 +00:00
Alexey Bezhan
94dae42902 Avoid assigning notification.template when creating test objects
`Notification.template` changed from being a Template relationship
to TemplateHistory. When a relationship attribute is being assigned,
SQLAlchemy checks that the assigned value type matches the relationship
type. Since most tests at the moment create a notification using a
Template instance this check fails.

Rewriting the tests to use TemplateHistory objects would require
changes to the majority of tests. Instead, when creating a notification
objects we assign the foreign key attributes directly. This skips the
SQLAlchemy type check, but we still get the constraint check on the
foreign keys, so a matching TemplateHistory object needs to exist in
the database.
2017-11-10 15:12:07 +00:00
Alexey Bezhan
e73513f519 Update Notification schema to preserve template_history attribute
`Notification.template_history` relationship has been removed but
we want to keep the `template_history` key in existing notification
serializations, so we serialize it from `Notifications.template`.

This keeps the data format the same, but both `template` and
`template_history` keys will now contain data from the `TemplateHistory`
instance.
2017-11-10 15:12:07 +00:00
Alexey Bezhan
a5ca66a01b Update notification queries with new TemplateHistory join
Changes the queries to use `Notification.template` relationship instead
of `.template_history` and TemplateHistory model insteaad of Template.
2017-11-10 15:12:07 +00:00
Alexey Bezhan
52e0367a13 Replace Notifications.template_history with Notifications.template
Each notification instance is connected to a specific version of the
template that was used to generate notification content. Template
versions are stored in TemplateHistory, so that's the table we need
to use for Notifications.template relationship.

Currently, using Notifications.template returns the latest template
version, which leads to incorrect content being returned by the API
if the template has been updated.

This change removes Notifications.template_history attribute and
replaces template relationship to refer to TemplateHistory instance
based on the notification's template ID and version.
2017-11-10 15:12:07 +00:00
Richard Chapman
1ace1b59a1 Merge pull request #1386 from alphagov/rc-monthly-template-usage
Handling Null template_id for monthly stats
2017-11-10 15:08:34 +00:00
Richard Chapman
28287abbd8 Added test to handle Null template_id
There are some Null template_ids in the production database which was
causing a failure as the stats_template_usage_by_month has a constraint
that it should not be null. This update only adds populated template_ids

- Updated Celery task
- Add a new test to test for null template_ids and not try to add them
to the stats
2017-11-10 13:49:58 +00:00
Rebecca Law
24b7acf671 Merge pull request #1385 from alphagov/fix-ip-restrictions
Change to fix ip restrictions
2017-11-10 12:16:15 +00:00
Rebecca Law
9e9642f6e9 Change to fix ip restrictions 2017-11-10 12:05:06 +00:00
kentsanggds
34aa8dfd01 Merge pull request #1373 from alphagov/ken-get-inbound_sms-api
Get inbound SMS API
2017-11-10 10:25:02 +00:00
Richard Chapman
e14275c19a Merge pull request #1377 from alphagov/rc-monthly-template-usage
Added scheduled task to get stats for template usage
2017-11-09 16:28:30 +00:00
Richard Chapman
485c4a7c10 Merge pull request #1374 from alphagov/pyup-update-sqlalchemy-1.1.14-to-1.1.15
Update sqlalchemy to 1.1.15
2017-11-09 16:28:04 +00:00
Venus Bailey
43ecb4e574 Merge pull request #1379 from alphagov/vb-annual-billing-schema
remove check to financial_year_start in schema as it can be None
2017-11-09 15:49:48 +00:00
Ken Tsang
99c2ccb496 Refactor schema 2017-11-09 15:24:30 +00:00
Ken Tsang
aac7551fc8 Removed provider_date and provider_reference 2017-11-09 15:24:08 +00:00
Ken Tsang
362072129f Add tests for dao_get_paginated_inbound_sms_for_service 2017-11-09 15:23:45 +00:00
Ken Tsang
36fb2fb28c Remove user_number from inbound sms dao 2017-11-09 15:23:16 +00:00
Ken Tsang
61a9f6e950 Removed provider_date and provider_reference from schema 2017-11-09 15:22:53 +00:00
Ken Tsang
cd4637a1d7 Removed provider_reference and provider_date from serialize 2017-11-09 15:22:27 +00:00
Richard Chapman
15e86170d9 Changed timezone back
Reverted back the timezoe change that was made in error and was making
two tests fail.
2017-11-09 14:55:45 +00:00
Leo Hemsted
e5c9ba93ff Merge pull request #1381 from alphagov/add-activate-endpoint
add separate activate user endpoint
2017-11-09 14:37:44 +00:00
Leo Hemsted
048ddbb2a4 add separate activate user endpoint 2017-11-09 14:27:24 +00:00
Richard Chapman
b78d989d4e Updates after review
- Modified the services_dao to return an int instead of a datetime to
make usage easier and removed the BST function on year as it is not
relevant for year
- Improved tests do there is less logic by ordering the result so there
is less reliance on the template id
- Renamed variable in stats_template_usage_by_month_dao.py to make it
consistent with the method
2017-11-09 14:13:42 +00:00
Richard Chapman
59df6bdbb6 Fixed imports which were producing broken tests
- Some tests for failing because of an import which was overriding the
one above it in test_scheduled_tasks.py
- Import error fixed in test_services_dao.py after a rename of a method
2017-11-09 11:33:56 +00:00
venusbb
23d8d54e0b remove check to financial_year_start in schema as it can be None 2017-11-09 11:07:11 +00:00
Chris Hill-Scott
670dabf091 Merge pull request #1378 from alphagov/fix-escaping-mmg-inbound
Fix escaping in inbound text messages from MMG
2017-11-09 10:49:38 +00:00
Richard Chapman
98b762869d Renamed database migration file after merge
Updated the database migration file name after a rebase as there were
two 0134 files and this would break the upgrade / downgrade process.
2017-11-09 10:41:43 +00:00
Richard Chapman
ff911c30d6 Added Scheduled task to get stats for template usage
Currently some pages are timing out due to the time it takes to perform
database queries. This is an attempt to improve the performance by
performing the query against the notification history table once a day
and use the notification table for a delta between midnight and the when
the page is run and combine the results.

- Added Celery task for doing the work
- Added a dao to handle the insert and update of the stats table
- Updated tests to test the new functionality
2017-11-09 10:34:49 +00:00
Richard Chapman
ab43803453 Removed unwanted migration commands
There were some unwanted db migration commands which crept into the
migration file. removed these as they are being handled in other PRs.
2017-11-09 10:34:49 +00:00
Richard Chapman
26617a921e Added database migration file
Currently the 'See templates used by month' report is timing out for
some services due to the query time of the data from notification_history
This is a stats table which will hold the data and will be updated by a
scheduled celery task once a day. This data will be combined with the
'live' data from notifications tables (which will be considerably less)
to form the data of the new report.
2017-11-09 10:34:49 +00:00
Richard Chapman
8973e42b86 Added a new table ‘stats_template_usage_by_month’
Currently the 'See templates used by month' report is timing out for
some services due to the query time of the data from notification_history
This is a stats table which will hold the data and will be updated by a
scheduled celery task once a day. This data will be combined with the
'live' data from notifications tables (which will be considerably less)
to form the data of the new report.
2017-11-09 10:34:49 +00:00
Richard Chapman
a77c316d2f Added stats annotation to a few more methods to track which methods need to be improved to increase billing performance 2017-11-09 10:34:49 +00:00
Chris Hill-Scott
4761d852e9 Fix escaping in inbound text messages from MMG
One of our providers gives us messages with special characters escaped,
ie a newline comes through as `\n`, not a literal newline. We shouldn’t
be showing these backslashes to any of our users. We also have examples
of real inbound messages containing `👍` and `’`, so we should continue
to display these properly.

It’s a bit tricky, because the strings we get from this provider are a
mixture of escape sequences (eg `\n`) and unicode characters (eg `😨`).
So we have to first convert the unicode character `😨` into an escape
sequence, `\U0001f628` in this example. We do this by encoding with
the `raw_unicode_escape` codec:

> Latin-1 encoding with \uXXXX and \UXXXXXXXX for other code points.
> Existing backslashes are not escaped in any way. It is used in the
> Python pickle protocol.

– https://docs.python.org/3/library/codecs.html#text-encodings

Then we turn this back into a string using the `unicode_escape` codec,
which transforms all escape sequences into their literal representations
(eg `\U0001f628` becomes `😨` and `\n` becomes a newline).
2017-11-08 13:32:30 +00:00
Leo Hemsted
4b99d6bb7a Merge pull request #1371 from alphagov/email-2fa
Email 2fa api endpoints
2017-11-08 11:44:48 +00:00
Ken Tsang
874ca5a8af Refactored code to remove user number handling 2017-11-08 11:02:02 +00:00
Leo Hemsted
0146e2d935 attempt to reduce code duplication 2017-11-07 16:47:40 +00:00
Leo Hemsted
67ae1fdb7b PR changes - some comment clarification and code simplifying 2017-11-07 16:47:40 +00:00
Leo Hemsted
a8ebb10c8e add tests for code used 2017-11-07 16:47:40 +00:00
Leo Hemsted
a231918317 put secret code in email url token 2017-11-07 16:47:26 +00:00
Leo Hemsted
6af616eb83 add name to personalisation and urlencode next param
also add tests
2017-11-07 16:47:26 +00:00
Leo Hemsted
dd326ec1d3 add new template to config
and rename existing new user email verification template for clarity
2017-11-07 16:47:26 +00:00
Leo Hemsted
57b7a392b2 add email 2fa template 2017-11-07 16:47:20 +00:00
Leo Hemsted
b2756ac99d add email code verification
by hitting POST /<user_id>/email-code, we create an email two factor
code to send to the user. That email contains a link with a token that
will sign the user in when opened.

Also some other things:

"email verification" (aka when you first create an account) doesn't
hit the API anymore

refactor 2fa code verification and sending to use jsonschema, and share code between sms and email

Die marshmallow die!
2017-11-07 16:45:49 +00:00
Ken Tsang
dca331e3ea Refactor to use query string rather than path params 2017-11-07 16:39:11 +00:00
Ken Tsang
ac15e7fd8b Renamed inbound_sms_list key name to received_text_messages 2017-11-07 13:29:40 +00:00
Ken Tsang
425d66bf29 Added more inbound sms tests 2017-11-07 13:29:40 +00:00
Ken Tsang
2e66fe5e38 Removed page from get paginated args as not used 2017-11-07 13:29:40 +00:00