Commit Graph

79 Commits

Author SHA1 Message Date
Pea Tyczynska
41d1cf453d Update limit to 1MB and update tests
SES rejects email messages bigger than 10485760 bytes (just over 10 MB per message (after base64 encoding)):
https://docs.aws.amazon.com/ses/latest/DeveloperGuide/quotas.html#limits-message

Base64 is apparently wasteful because we use just 64 different values per byte, whereas a byte can represent
256 different characters. That is, we use bytes (which are 8-bit words) as 6-bit words. There is
a waste of 2 bits for each 8 bits of transmission data. To send three bytes of information
(3 times 8 is 24 bits), you need to use four bytes (4 times 6 is again 24 bits). Thus the base64 version
of a file is 4/3 larger than it might be. So we use 33% more storage than we could.
https://lemire.me/blog/2019/01/30/what-is-the-space-overhead-of-base64-encoding/

That brings down our max safe size to 7.5 MB == 7500000 bytes before base64 encoding

But this is not the end! The message we send to SES is structured as follows:
"Message": {
    'Subject': {
        'Data': subject,
    },
    'Body': {'Text': {'Data': body}, 'Html': {'Data': html_body}}
},
Which means that we are sending the contents of email message twice in one request: once in plain text
and once with html tags. That means our plain text content needs to be much shorter to make sure we
fit within the limit, especially since HTML body can be much byte-heavier than plain text body.

Hence, we decided to put the limit at 1MB, which is equivalent of between 250 and 500 pages of text.
That's still an extremely long email, and should be sufficient for all normal use, while at the same
time giving us safe margin while sending the emails through Amazon SES.
2020-10-29 14:07:49 +00:00
Pea Tyczynska
9708b09ba3 Tailor message-too-long error message
depending on the notification type.

Up until now, only sms messages could get message-too-long error,
but now we also need to validate the size of email messages, so
the message content needs to be tailored to the notification type.
2020-10-29 14:07:48 +00:00
Rebecca Law
89a8d8912a Set postage and international for letters uploaded with a CSV
If the letter is outside of the United Kingdom, set the postage and international flag.
2020-08-10 09:58:49 +01:00
Rebecca Law
725ce38a06 Merge pull request #2943 from alphagov/international-postage
Set international postage and international flag for international letters
2020-08-10 08:22:52 +01:00
Rebecca Law
10fe7d9fe8 Add postage for send-one-off letters.
The postage is set to europe or rest-of-world for international letters, otherwise the template postage is used.

Also set international for letters.
2020-08-03 14:01:59 +01:00
Chris Hill-Scott
4d896aa642 Rename function in service utils
To reflect the new name of the feature.

squash! Rename function in service utils

Rename function, variable and argument names in service utils
2020-07-28 12:55:55 +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
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
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
b8c2c6b291 Merge pull request #2887 from alphagov/cache-the-serialised-things
Serialise and cache services and API keys
2020-06-26 09:18:45 +01:00
Rebecca Law
ce32e577b7 Remove the use of schedule_for in post_notifications.
Years ago we started to implement a way to schedule a notification. We hit a problem but we never came up with a good solution and the feature never made it back to the top of the priority list.

This PR removes the code for scheduled_for. There will be another PR to drop the scheduled_notifications table and remove the schedule_notifications service permission

Unfortunately, I don't think we can remove the `scheduled_for` attribute from the notification.serialized method because out clients might fail if something is missing. For now I have left it in but defaulted the value to None.
2020-06-24 14:54:40 +01:00
Chris Hill-Scott
320bca70f7 Serialise service, API keys and permissions
By serialising these straight away we can:
- not go back to the database later, potentially closing the connection
  sooner
- potentially cache the serialised data, meaning we don’t touch the
  database at all
2020-06-23 16:00:41 +01:00
Pea M. Tyczynska
75a7e9b7fd Merge pull request #2892 from alphagov/refactor-crown-dependency-check
Refactor crown dependency check
2020-06-23 11:19:17 +01:00
Pea Tyczynska
7a4f5a4bff Refactor crown dependency check 2020-06-22 18:14:22 +01:00
Chris Hill-Scott
5a2f2a9ec2 Rename JSONModel to SerialisedModel 2/2
This class doesn’t actually wrap JSON, it wraps serialised data.

So this name feels better.
2020-06-22 10:20:53 +01:00
Chris Hill-Scott
e6b7e0e16c Rename JSONModel to SerialisedModel 1/2
This class doesn’t actually wrap JSON, it wraps serialised data.

So this name feels better.

This commit only renames the file for an easier diff.
2020-06-22 10:20:53 +01:00
Chris Hill-Scott
ad2328fc05 Serialise template immediately after fetching
This commit changes the code in post notification endpoint to handle a
serialised template (ie a `dict`) rather than a database object.

This is the first step towards being able to cache the template and not
hit the database on every request.

There should be no functional changes here, it’s just refactoring.

There are some changes to the tests where the signature of functions
has changed.

Importing of the template schema has to be done at a function level,
otherwise Marshmallow gets weird.

This commit also copies the `JSONModel` class from the admin app, which
turns serialised data (a dict made from JSON) into an object on which
certain predefined properties are allowed.

This means we can still do the caching of serialised data, without
having to change too much of the code in the app, or make it ugly by
sprinkling dict lookups everywhere.

We’re not copying all of JSONModel from the admin app, just the bits we
need. We don’t need to compare or hash these objects, they’re just used
for lookups. And redefining `__getattribute__` scares Leo.
2020-06-22 10:20:51 +01:00
Pea Tyczynska
ef9f3c1e5f Make sure we check if a service can send to number harmoniously
We were checking this separately in two places in the code. Now
we will have this logic in one place, in validators.

Also pull in utils version that recognises crown depenency numbers
as international.
2020-06-19 15:59:15 +01:00
Leo Hemsted
15ce9fe3f9 add metrics for redis timings 2020-06-12 14:52:22 +01:00
Rebecca Law
852cf478f8 Remove the check for daily limits 2020-03-17 15:51:21 +00:00
Rebecca Law
a994e8fb6e Update validators to use is_message_too_long()
- update check_sms_content_char_count to use the SMSTemplate.is_message_too_long function, and updated the error message to align with the message returned by the admin app.
- Update the the code used by version 1 of the api to use the validate_template method.
  - I did find a couple of services still using the old api, however, this change should not affect them as I checked the messages being sent and they are not too long.
  - We will be sending a message to them to see if they can upgrade.
- Update the log message for authenication to include the URL - makes it easier to track if a service is using version 1 of the api.
2020-03-10 09:38:16 +00:00
Rebecca Law
8d005f68dd Merge branch 'validate_send_file_by_email_contact_deets' of github.com:alphagov/notifications-api into validate_send_file_by_email_contact_deets 2020-02-26 16:10:46 +00:00
Rebecca Law
95d48d40a9 Update error message, now includes the url where the service can add contact details. 2020-02-26 16:04:15 +00:00
Pea M. Tyczynska
7ca35b44a7 Our new error message now has better grammar and form matching other errors more
Co-Authored-By: karlchillmaid <karl.chillmaid@digital.cabinet-office.gov.uk>
2020-02-26 13:38:48 +00:00
Pea M. Tyczynska
6d21515adf Apply suggestions from code review
Better grammar for our new error message.

Co-Authored-By: karlchillmaid <karl.chillmaid@digital.cabinet-office.gov.uk>
2020-02-26 10:45:39 +00:00
Pea Tyczynska
9a12d0e80e Update error message after discussion with Karl 2020-02-25 17:10:22 +00:00
Pea Tyczynska
ed1bc8d806 All services can send files by email if they have set contact_link 2020-02-25 16:11:53 +00:00
Pea Tyczynska
c17100af37 Bump utils version and improve error message content 2019-11-26 11:19:01 +00:00
Pea Tyczynska
f4ba82225b Use new Template method .is_message_empty()
This method has been now added to Template subclasses
used by sms, emails and letters, so we can use it to valdiate if
message is not empty.

Use new template method .is_message_empty()

Refactor function name and add a test
2019-11-26 11:18:00 +00:00
Pea Tyczynska
9c804f701b Validate against messages with no content 2019-11-26 11:17:59 +00:00
Alexey Bezhan
462c8db4c3 Lower service rate limit log level to INFO
Services reaching rate limits are triggering our alerts and make it
hard to find actual exceptions in the logs.

As far as the API is concerned this is not an exceptional state,
so we shouldn't log it as such.
2018-11-27 15:50:51 +00:00
Rebecca Law
d0cbdce6c4 Update the error message when a service does not have permission to send a notification type. 2018-11-20 11:01:48 +00:00
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
Richard Chapman
eee572276f Removed the imports for statsd as the decorators are no longer being
used.
2018-01-26 09:39:11 +00:00
Richard Chapman
083bfb9ea5 Removed the performance testing statsd calls as they are no longer
needed for monitoring the performance of the v2 endpoints. They were put
in as a temporary measure whilst sustained performance testing was
taking place.
2018-01-26 09:08:45 +00:00
Chris Hill-Scott
01cf175cb2 Don’t respect the whitelist for one off sending
The whitelist was built to help developers and designers making
prototypes to do realistic usability testing of them, without having to
go through the whole go live process.

These users are sending messages using the API. The whitelist wasn’t
made available to users uploading spreadsheets. The users sending one
off messages are similar to those uploading spreadsheets, not those
using the API. Therefore they shouldn’t be able to use the whitelist to
expand the range of recipients they can send to.

Passing the argument through three methods doesn’t feel that great, but
can’t think of a better way without major refactoring…
2018-01-22 11:42:37 +00:00
Katie Smith
7b4abd076b Add validation to check that sms recipient is not None
Previously, if the SMS recipient was None there would be a 500 error
with no message displayed to the user. We now check if the recipient is
None and raise a BadRequestError if this is the case.
2018-01-15 09:20:46 +00:00
Katie Smith
b07db16cd1 Get rate limit from service.rate_limit column (not config)
PR #1550 added the rate_limit column to the Service table.

This PR removes the rate limits from the config and uses rate_limit from
the Service model instead. Rate limits are still separated into 'team',
'normal' and 'test', but these values are the same for a service.

Pivotal story https://www.pivotaltracker.com/story/show/153992529
2018-01-11 10:28:11 +00:00
Alexey Bezhan
509441f1d9 Add validators for service_letter_contact_id and reply_to
Validators check that service_letter_contact_id belongs to the
same service as the notification/template.

Generic reply_to validator calls the correct function for the given
type (for either notification or template). It can be used by the
template API endpoints to verify that given reply_to ID has the same
service_id as the template itself.

The original approach was to create a DB foreign key constraint,
but this caused issues with the `version_class` decorator saving
related Service objects without creating a history record.
2017-12-19 10:23:24 +00:00
Leo Hemsted
88c878c83e don't hit the query to get daily msg stats if redis is disabled 2017-12-04 11:12:26 +00:00
Richard Chapman
5f25d3162a Disable Redis for Staging added statsd for post methods
- Disable Redis as there is a current connection limit of 256 which
 could slow down the request if they are all used
 - Added statd to methods in the post to help spot any bottlenecks
2017-12-01 15:41:53 +00:00
Rebecca Law
e0d0b2f4fd Merge branch 'add-reply-to-notifications' of github.com:alphagov/notifications-api into add-reply-to-notifications 2017-11-23 16:57:17 +00:00
Rebecca Law
75d99ea397 [WIP] 2017-11-23 16:01:05 +00:00
Rebecca Law
fe6bafcfb2 Update the from number in the response of post notification. 2017-11-10 14:17:29 +00:00
Rebecca Law
830619194e Renamed some tests.
Fix some imports.
Added test for a function
2017-11-01 11:01:20 +00:00
Rebecca Law
4eec11b633 Added an optional parameter in the form for POST /v2/notifications/sms and /service/<service_id>/send-notification to pass in the SMS sender id.
The send_sms_to_provider still needs to use the SMS sender being passed in to the POST.

As part of https://www.pivotaltracker.com/story/show/152106587
2017-10-30 13:36:49 +00:00
Rebecca Law
769a7a275b Insert a row for the mapping of notification to email_reply_to for one off notifications. 2017-10-11 16:23:31 +01:00
Richard Chapman
7974e59eb7 Changes after review. Updates to style and import order to be more consistent with other code.
Added clarification to an error message to give better debugging information.
Removed using dao_get_reply_to_by_service_id in tests to be more consistent with other code and use the test db functions or remove the need for a call altogether making the code less complex.
2017-10-05 16:50:18 +01:00
Richard Chapman
2fbe492d5c [2/10] Allow API calls to specify the reply address option (#1291)
* Added service_email_reply_to_id to the POST /v2/notifications/email and a test to test the validator

* Caught NoResultFound exception in check_service_email_reply_to_id as it was not being caught when there there was no valid service_id or reply_to_id. Fixed failing tests which were not passing due to the NoResultFound exception and added further tests to check for the good path through the code and an test to check for an invalid service_id

* Added service_email_reply_to_id to the POST /v2/notifications/email and a test to test the validator

* Caught NoResultFound exception in check_service_email_reply_to_id as it was not being caught when there there was no valid service_id or reply_to_id. Fixed failing tests which were not passing due to the NoResultFound exception and added further tests to check for the good path through the code and an test to check for an invalid service_id

* Fixed code style in validators.py to confirm with rules
Update the name of email_reply_to_id to conform better with other attributes in the schema and the resultant code in post_notifications.py
Fixed code style in test_validators.py to confirm with rules
Added tests to test_post_notifications.py to test the email_reply_to_id being present and being incorrect, it being optional is being tested by other tests.

* Added service_email_reply_to_id to the POST /v2/notifications/email and a test to test the validator

* Added service_email_reply_to_id to the POST /v2/notifications/email and a test to test the validator

* Caught NoResultFound exception in check_service_email_reply_to_id as it was not being caught when there there was no valid service_id or reply_to_id. Fixed failing tests which were not passing due to the NoResultFound exception and added further tests to check for the good path through the code and an test to check for an invalid service_id

* Caught NoResultFound exception in check_service_email_reply_to_id as it was not being caught when there there was no valid service_id or reply_to_id. Fixed failing tests which were not passing due to the NoResultFound exception and added further tests to check for the good path through the code and an test to check for an invalid service_id

* Fixed code style in validators.py to confirm with rules
Update the name of email_reply_to_id to conform better with other attributes in the schema and the resultant code in post_notifications.py
Fixed code style in test_validators.py to confirm with rules
Added tests to test_post_notifications.py to test the email_reply_to_id being present and being incorrect, it being optional is being tested by other tests.

* Minor update after manual merge to fix check style rule break in test_validators.py where a single space was introduced.

* Updates after code review. Moved the template from the exception message as it was not required and updated the error message to match the field name in the sschema for better debugging and error identification.

* Fixed test after update of exception message
2017-10-04 14:34:45 +01:00
Leo Hemsted
9caf45451e make persist_notification require kwargs
when functions get as big as that, it's confusing to try and work out what
things are what. By including a * as the first arg, we require that anyone
calling the function has to use kwargs to reference the parameters
2017-07-19 17:02:19 +01:00