Commit Graph

116 Commits

Author SHA1 Message Date
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
Richard Chapman
7f32b06615 Replaced the JSONDecodeError with a ValueError as it was failing on
Jenkins.
2018-03-07 09:32:56 +00:00
Richard Chapman
cf2a506b3b Removed the import and directly used json.decoder.JSONDecodeError
as it was failing on Jenkins.
2018-03-06 18:20:45 +00:00
Richard Chapman
3f9a37d8a3 Updated the import for JSONDecodeError to import from from json.decoder
as it was failing on Jenkins.
2018-03-06 17:31:08 +00:00
Richard Chapman
e1e7f13f23 Added tests to ensure the parameter to the mock post request are the
correct type for the call as there are some that require json and some
binary. The additional checks ensure that that json decode either fails
or succeeds in the correct case.
2018-03-06 16:16:29 +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
bfdd385ba3 Tidied up imports. Removed local imports and made a global import so its
easier to maintain in future.
2018-03-05 17:08:09 +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
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
a052020f84 - simplify if statement
- use the template.get_reply_to_text() in send_notification, it's already using the right thing.
2018-01-08 16:54:19 +00:00
Rebecca Law
d6683d9630 If template has a default address use that address ELSE the address is blank. 2018-01-08 15:54:58 +00:00
Rebecca Law
5be5e17e46 Because life is difficult today...
Need to alter the column to nullable and remove reference to the column in any code. Next PR will drop column
2018-01-05 15:55:02 +00:00
Rebecca Law
0c5625fb8b Fix the schema for reply_to and added reply_to_text. 2018-01-04 15:56: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
33e8e55dc6 Update the tests and revert adding the service_letter_contact_id 2018-01-03 15:30:26 +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
Alexey Bezhan
54ef53cefe Add a test for creating a template with reply_to through the API 2017-11-30 17:31:39 +00:00
Leo Hemsted
82acec87b6 flake8 the entire repo now that the tests are good 👌 2017-11-28 17:29:43 +00:00
Leo Hemsted
b1cccdcc6b First batch of flake8 changes.
Many unused variables, and replacing some old fixtures with
admin_request before I realised just how many there where 😩
2017-11-28 17:00:01 +00:00
Chris Hill-Scott
b2ba2afbcd List templates in alphabetical order
Currently templates are ordered by the newest created first. This made
sense when, after creating a new template, you were landed on the page
that listed all the templates. In other words, you needed to see
confirmation of the thing that you’ve just done.

Now (since https://github.com/alphagov/notifications-admin/pull/1195)
you get landed on the page for just that template. So you always see
the template you’ve just created, no matter how the list of templates is
ordered. So we are free to change the order of the templates.

Ordering by time created is not great, because it gives users no control
over which templates appear first. For example, our research reported
this from one team:

> One frustration they have is that when you add a new template it
> automatically goes to the top of the list. To get round this, whenever
> they add a new template they delete all of the existing ones and start
> again because they want to keep their templates in numerical order.
> They'd like to be able to control the order of templates in the list.

We _could_ give people some sort of drag-and-drop template ordering
thing. But this feels like overkill. I think that alphabetical order is
better because:

- it’s easily discoverable – anyone who wants to know how a list is
  ordered can quickly tell just by looking at it
- it’s universal – everyone knows how alphabetical ordering works
- it’s familiar – this is how people documents on their computer are
  ordered; there’s no new UI to learn
- it’s what users are doing already – from the same service as above:
  > They number their templates 1,2a, 2b, 3a etc

So this commit changes the ordering from newest created first to
alphabetical.

Previous changes to template order and navigation:
- https://github.com/alphagov/notifications-admin/pull/1163
- https://github.com/alphagov/notifications-admin/pull/1195
- https://github.com/alphagov/notifications-admin/pull/1330

Implementation notes
---

I refactored some of the tests here. I still don’t think they’re great
tests, but they’re a little more Pythonic now at least.

I also added a sort by template type, so that the order is deterministic
when you have, for example, an email template and a text message
template with the same name. If you have two text message templates with
the same name you’re on your own.
2017-11-23 15:11:07 +00:00
Alexey Bezhan
7b1f07dd31 Add tests for reading and updating template reply_to through the API 2017-11-22 14:30:44 +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
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
Rebecca Law
49917ccafd Add redact_personalisation to TemplateSchema 2017-06-29 18:16:03 +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
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
28d5da638b Remove old style fixture
Use new `client` one instead
2016-11-11 17:05:41 +00:00
Chris Hill-Scott
6127ee422a Test can get letter template
It works already, just making sure it stays working.
2016-11-11 16:47:07 +00:00
Chris Hill-Scott
73fe331dc8 Test can create letters template
It works already, just making sure it stays working.
2016-11-11 16:47:06 +00:00
Chris Hill-Scott
f34b58e7cd Delete redundant test
Not needed now that the test above this one is parametrized.
2016-11-11 16:47:06 +00:00
Chris Hill-Scott
aef62dcffb Parametrize template create
Cleaner than having two almost identical tests.
2016-11-11 16:47:06 +00: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
Chris Hill-Scott
c2ae4773da Order templates by newest created first
When you add a new template, it’s probably the one that you want to do
subsequent stuff with. But it’s also helpful to see the template in
context (with its siblings) to understand that there are multiple
templates. So we don’t want to do what we do in
https://github.com/alphagov/notifications-admin/pull/648
for adding a new template.

But we _can_ make your brand-new template appear first by always
ordering by when the template was created.

This also removes the confusion caused by having `updated_at` affecting
order, and causing the templates to move around all the time.
2016-06-06 10:44:40 +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
1797d9360b Update template json to return created_at and updated_at. Also fixed a bug where updated_at was not being added to the templates_history model. 2016-05-19 17:02:55 +01:00
Nicholas Staples
6e7383de33 Removed template subject uniqueness 2016-05-18 10:00:09 +01:00