Commit Graph

115 Commits

Author SHA1 Message Date
Katie Smith
a87be9b74a Use new value of SMS_CHAR_COUNT_LIMIT from utils
Admin, API and utils were all defining a value for SMS_CHAR_COUNT_LIMIT.
This value has been updated in notifications-utils to allow text
messages to be 4 fragments long and notifications-api now gets the value of
SMS_CHAR_COUNT_LIMIT from notifications-utils instead of defining it in
config.

Also updated some tests to check for the higher limit.
2018-08-16 16:34:34 +01:00
Chris Hill-Scott
8ae800b117 Pass date of letter to template preview
Otherwise all letters will show the current date.

Also beefed up the tests around this part of the code a bit.
2018-04-30 15:47:49 +01:00
Ken Tsang
7524402b56 Only hide the notify tag on precompiled on first page 2018-03-29 14:38:38 +01:00
Richard Chapman
79a6ce8782 * Updated imports to comply with pep8 for better maintainability
* Removed extra log messages so there are not two log messages being
generated per exception, as InvalidRequest also logs, updated the
InvalidRequest log message to include the exception type and exception
information
* Added extra asserts to ensure the exception messages are printed
2018-03-12 11:05:05 +00:00
Richard Chapman
04048aa220 Updated the notification template endpoint to extract the pdf page
before sending it to template preview. This stops the whole pdf file
being sent to template preview for each page which is really inefficient
on network traffic and memory usage.

* Added logic to the endpoint to extract the specific page requested
* Updated tests to add a mock for the new call to utils
* Added a new test case for exceptions in the PDF extraction process
2018-03-09 15:50:43 +00:00
kentsanggds
b0b0062b35 Merge pull request #1732 from alphagov/ken-hidden-in-json-response
Return `is_precompiled_letter` field as part of json for notification by id
2018-03-08 15:06:10 +00:00
Ken Tsang
7011b90bd4 Refactor is_precompiled_letter to model 2018-03-07 23:03:03 +00:00
Richard Chapman
d60e802f35 Removed the superfluous variable and pass through as it pulls it
automatically out of sys.exc_info.
2018-03-07 09:51:58 +00:00
Richard Chapman
77a3397ce5 Added option flag to the _get_png_preview method to determine if the
post method content is data or json format.
2018-03-06 15:35:00 +00:00
Richard Chapman
ed9936bba0 Fixed bug where the content header was not being passed onto the post
request. Changed data => json.

Added extra logging to display the error with more detail
2018-03-06 14:42:53 +00:00
Richard Chapman
d0df85a602 Fixed bug where the content header was not being passed onto the post
request. Changed data => json.

Added extra logging to display the error with more detail
2018-03-06 14:24:30 +00:00
Richard Chapman
e91a0efc43 Refactored code to make it more maintainable and changed an error type
* Rather than an abort 404 returned a 500 and InvalidRequest so that the
error is more easily handled on the admin console. If the file is
missing but expected to be there is actually an internal error for admin
* Refactored the code to remove duplicate code in calls to template
preview by creating a new private method which is called with specific
parameters
2018-03-05 14:54:18 +00:00
Richard Chapman
a4feaba309 Added tests to tests for precompiled flow and refactored a little
* Added is_precompiled_letter method to letter/utils.py
* Added tests for letter/utils.py
* Added tests for the rest endpoint
* Moved the Precompiled name to a central location
* Added hidden field to the test method to create a template
2018-03-05 14:11:37 +00:00
Richard Chapman
a9a67ce542 Updated API to handle pre-compiled pdfs
* added a method to letter/utils.py to get the PDF document from the S3
bucket
* added the logic to return the pdf or to produce a png of the pdf
2018-03-02 14:54:28 +00:00
Richard Chapman
42c1040604 Updated API to get it working with Admin.
* Added missing items from template which are required
* Returned the file as a JSON string with the file as a base64 encoded
string
* Updated tests to match teh desired format
2018-03-01 15:01:46 +00:00
Richard Chapman
4c8bc9f430 Added endpoint to get the letter preview from the template preview app
Is will allow the admin application to call into the api instead of
making the call itself. This will allow the api to make decision for
precompiled pdf without having to update the admin app.

- Added new endpoint
- Added tests for the endpoint
2018-03-01 15:01:46 +00:00
Rebecca Law
e59d6d470e Fix the problem with updating the reply_to or service_letter_contact_id for templates.
The history was not being updated properly, we think this is because the declaritive attribute is not being set propery by the property.
When reply_to: None it will update the service_letter_contact_id, but not the service_letter_contact, we think when the history_meta is build the history class and checking if the value is updated it depends which attribute it is checking first.

In order to fix this issue, there is a new dao method to update the reply_to on the Template and insert a new Template history.
2018-01-10 12:40:14 +00:00
Rebecca Law
cc839562da Fix the template history when updating the reply_to for a template.
It seems selecting the service_letter_contact in the validation method was causing SQLAlchemy to persist the object. When the dao was called to save the object nothing was different so we didn't persist the history object.

It may be time to take another look at how we version. :(
2018-01-09 16:41:58 +00:00
Rebecca Law
6a3831eace Added a boolean column to templates called is_letter_contact_blank.
If is_letter_contact_blank then the user has set the letter contact block to be blank on purpose
ELSE IF is_letter_contact_blank is false THEN use the template default
IF template default is blank THEN the service_letter_contact is blank use the service default
2018-01-04 11:33:10 +00:00
Rebecca Law
8732506138 Add reply_to in the template schema 2018-01-03 15:10:16 +00:00
Alexey Bezhan
da247680a4 Validate that template reply_to belongs to template's service
Checks that email/sms/letter reply to object has the same service_id
as the template it's being attached to, to make sure it's not possible
to retrieve data about return addresses for other services.
2017-12-19 10:23:24 +00:00
Leo Hemsted
28d5f9b87f flake8 - remove unused imports and ensure they're always at the top of the file 2017-11-28 14:28:01 +00:00
Alexey Bezhan
999afa7e0d Add reply_to to the list of template fields that can change 2017-11-22 14:29:37 +00:00
Ken Tsang
23618a186c Further refactoring 2017-07-06 12:27:57 +01:00
Ken Tsang
0b3277b8a4 Refactored to make code clearer 2017-07-06 12:27:57 +01:00
Ken Tsang
e0fbcb0dc6 Add permission check in for v2 post notification 2017-07-06 12:27:57 +01:00
Ken Tsang
e927723726 Update sms/email permission tests error msg 2017-07-06 12:27:57 +01:00
Ken Tsang
50066c6753 Prevent template creation or update w/o permission 2017-07-06 12:27:56 +01:00
Leo Hemsted
2f973b8af0 use created_by instead of updated_by
to behave in same way as other endpoints
2017-06-29 12:39:02 +01:00
Leo Hemsted
3f663daafe redacting a template now 400s if no updated_by_id supplied 2017-06-28 17:05:32 +01:00
Leo Hemsted
8ad10261ec add tests for redact_template rest 2017-06-28 16:53:39 +01:00
Leo Hemsted
29fc81090e add template personalisation redaction
If passing in `redact_personalisation` to the template update endpoint,
we should mark that template permanently as redacted - this means that
we won't ever return the personalisation for any notifications for it.

This is to be used with templates containing one time passwords, 2FA
codes or other sensitive information that you may not want service
workers to be able to see.

This is implemented via a separate table, `template_redacted`, which
just contains when the template was redacted.
2017-06-28 15:53:08 +01:00
Rebecca Law
78242812ef Register a before_request event for all blueprints, that defines the authentication requirement.
There are three authentication methods:
 - requires_no_auth - public endpoint that does not require an Authorisation header
 - requires_auth - public endpoints that need an API key in the Authorisation header
 - requires_admin_auth - private endpoint that requires an Authorisation header which contains the API key for the defined as the client admin user
2017-03-16 18:15:49 +00:00
Chris Hill-Scott
e507fed152 Quietly ignore extra personalisation
> If a user makes an API request with additional personalisation fields,
> we should simply discard any fields that the template doesn't have.
>
> This gives a couple of related advantages:
>
> - modifying template parameters no longer requires downtime for
>   clients - as they can pass in extra new parameters before a template
>   change, or continue passing in old unused parameters after removing
>   them from a template
>
> - services can pass in large user objects, for example, and then play
>   around with templates adding and removing fields at will
>
> we should make sure we still return an error if a user doesn't pass in
> a required parameter.

– https://www.pivotaltracker.com/story/show/140774195
2017-03-07 16:09:17 +00:00
Chris Hill-Scott
6e6d471cda Don’t strip HTML when saving templates
Right now we strip HTML from templates at the point of saving them. This
also converts stuff like ampersands to their entity form (eg &) and
this is what we save in the database.

This is a bad idea when you’re sending a text message or a letter, in
which an HTML entity makes no sense. But we still need to encode HTML in
the body of HTML emails.

The right place to do this is when rendering the templates. The code to
do this is now in utils. So this commit:
- pull in this new utils code
- removes the old
- adds some integration tests to make sure that everything is working
  as expected (more thorough unit tests are happening in utils)
2017-01-19 12:05:28 +00:00
Rebecca Law
41b49eb8e0 Make the update template endpoint work when process_type is present. 2017-01-17 15:48:51 +00:00
Chris Hill-Scott
59af44d7ab Update utils to 12.0.0
Includes:

- [x] https://github.com/alphagov/notifications-utils/pull/94 (breaking
      changes which are responsible for all the changes to the API in
      this PR)

The test for `get_sms_fragment_count` has been removed because this
method is already tested in utils here:

ac20f7e99e/tests/test_base_template.py (L140-L159)
2016-12-13 10:57:01 +00:00
Chris Hill-Scott
d449475dd5 Incorporate breaking utils changes
The `.replaced…` methods on instances of `Template` were removed in
https://github.com/alphagov/notifications-utils/pull/84
2016-12-02 10:42:11 +00:00
Chris Hill-Scott
68fd4947cb Update utils for Markdown parsing + subbranding
Markdown parsing (will be active but isn’t documented):
- [x] https://github.com/alphagov/notifications-utils/pull/54

Email sub branding (not used yet):
- [x] https://github.com/alphagov/notifications-utils/pull/55

Required modification to the email preview endpoint to make sure that
it only included the message and not any HTML.
2016-07-22 11:09:23 +01:00
Rebecca Law
3f11447bc8 A small refactor to use the SMS_TYPE and EMAIL_TYPE in code rather that 'sms' or 'email' 2016-06-30 15:41:51 +01:00
Chris Hill-Scott
0d9519c656 Remove wrapper around response object
Before:
```json
{'data': {'template': '…'}}
```

There’s no need to wrap the response in key because there will only
ever be one valid key for the template preview endpoint.

Flatter is better:
```json
{
  'content': '…',
  'subject': '…',
  'template_type': '…',
  …
}
```

The response will be different if there’s an error, but you should be
checking the status code first anyway.

This commit:
- changes the template preview endpoint to return the above format
- adds a test to make sure that the original `/service/…/template/…`
  endpoint still returns JSON in the same format (with a `data` key)
2016-06-17 12:57:43 +01:00
Chris Hill-Scott
cf91ce57fc Add a ‘preview template’ endpoint
There’s a need for users of the API to be able to take advantage of
Notify’s template rendering.

For example, there’s a service that’s building a case management system.
Their users are sending emails on a case-by-case basis. Before they
send an email, it’s ressuring to double check that the right data is
being inserted, that the right template is being used, etc.

This commit:
- adds a separate endpoint for previewing a template, with
  personalisation taken from the get parameters of the request
- beefs up the tests around getting a template

Not part of this pull request:
- making this enpoint publicly accessible
2016-06-17 09:19:22 +01:00
Adam Shimali
c268401554 Updated for pr comments 2016-06-15 16:19:28 +01:00
Adam Shimali
b33312b855 Change endpoint responses where there are marshalling, unmarshalling
or param errors to raise invalid data exception. That will cause
those responses to be handled in by errors.py, which will log
the errors.

Set most of schemas to strict mode so that marshmallow will raise
exception rather than checking for errors in return tuple from load.

Added handler to errors.py for marshmallow validation errors.
2016-06-15 14:37:51 +01:00
Rebecca Law
ec7d9b1c6e Use a more pythonic way to compare the dicts. 2016-06-01 13:55:04 +01:00
Rebecca Law
047a7d5488 Only test the items of the template that can change on an update request.
Return 200 if no change is made.
2016-06-01 12:19:59 +01:00
Rebecca Law
05e72b07ae Return status code 304 when template is not updated.
Moved import
2016-06-01 11:50:45 +01:00
Rebecca Law
0a9cdbd75a Do not create a new version of the template if there is no change to the tempalte. 2016-06-01 10:53:03 +01:00
Nicholas Staples
6e7383de33 Removed template subject uniqueness 2016-05-18 10:00:09 +01:00
Rebecca Law
917110870d Use the template version at the time the notification is created or at the time the job is created.
Update notifications/sms|email endpoint to send the template version to the queue.
Update the process_job celery talk to send the template version to the queue.
When the send_sms|send_email task runs it will get the template by id and version.

Created a data migration script to add the template_vesion column for jobs and notifications.
The existing jobs and notifications are given the template_version of the current template.
There is a chance this is the wrong template version, but deemed okay since the application is not live.

Create unit test for the dao_get_template_versions method.
Rename /template/<id>/version to /template/<id>/versions which returns all versions for that template id and service id.
2016-05-13 16:25:05 +01:00