Commit Graph

51 Commits

Author SHA1 Message Date
Kenneth Kehl
905df17f65 remove datetime.utcnow() 2024-05-23 13:59:51 -07:00
Carlo Costino
99edc88197 Localize notification_utils to the API
This changeset pulls in all of the notification_utils code directly into the API and removes it as an external dependency.  We are doing this to cut down on operational maintenance of the project and will begin removing parts of it no longer needed for the API.

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2024-05-16 10:17:45 -04:00
Kenneth Kehl
1ecb747c6d reformat 2023-08-29 14:54:30 -07:00
Steven Reilly
ff4190a8eb Remove letters-related code (#175)
This deletes a big ol' chunk of code related to letters. It's not everything—there are still a few things that might be tied to sms/email—but it's the the heart of letters function. SMS and email function should be untouched by this.

Areas affected:

- Things obviously about letters
- PDF tasks, used for precompiling letters
- Virus scanning, used for those PDFs
- FTP, used to send letters to the printer
- Postage stuff
2023-03-02 20:20:31 -05:00
Ryan Ahearn
3c035531aa Clean up and validate low static-scan findings 2022-08-19 14:32:11 +00:00
Ben Thorner
a91fde2fda Run auto-correct on app/ and tests/ 2021-03-12 11:45:45 +00:00
Chris Hill-Scott
3b0b96834d Do extra code style checks with flake8-bugbear
Flake8 Bugbear checks for some extra things that aren’t code style
errors, but are likely to introduce bugs or unexpected behaviour. A
good example is having mutable default function arguments, which get
shared between every call to the function and therefore mutating a value
in one place can unexpectedly cause it to change in another.

This commit enables all the extra warnings provided by Flake8 Bugbear,
except for:
- the line length one (because we already lint for that separately)
- B903 Data class should either be immutable or use `__slots__` because
  this seems to false-positive on some of our custom exceptions
- B902 Invalid first argument 'cls' used for instance method because
  some SQLAlchemy decorators (eg `declared_attr`) make things that
  aren’t formally class methods take a class not an instance as their
  first argument

It disables:
- _B306: BaseException.message is removed in Python 3_ because I think
  our exceptions have a custom structure that means the `.message`
  attribute is still present

Matches the work done in other repos:
- https://github.com/alphagov/notifications-admin/pull/3172/files
2020-12-22 16:26:45 +00: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
Katie Smith
ab956c9eb3 Update JSON schema postage validation for new values 2020-06-19 15:36:10 +01:00
Katie Smith
0b28766442 Reverts the new postage constraints
Reverts https://github.com/alphagov/notifications-api/pull/2843 and https://github.com/alphagov/notifications-api/pull/2848
2020-05-20 18:31:25 +01:00
Katie Smith
6d89b01f1e Update JSON schema postage validation for new values 2020-05-19 16:04:36 +01:00
Chris Hill-Scott
7032bac178 Start allowing any three lines in addresses
This is part of moving away from `postcode` and towards `address line 7`

We think it will be easier for people to map their existing data to our
API if we let them fill in any 3 lines, instead of requiring at least
1, 2, and postcode specifically.
2020-04-09 18:19:53 +01:00
Katie Smith
4fd74af3bd Revert "Update postage db constraints for international letters" 2020-04-08 10:53:51 +01:00
Katie Smith
e2effb6ee1 Update JSON schema postage validation for new values 2020-04-07 08:01:11 +01:00
Rebecca Law
5d6886242b Check that the request payload data is valid json.
By adding `force=True` to request.get_json() the mime type is ignore. If the data is not valid json the method will return a `BadRequestError` we catch that and throw our own error with a clear error message "Invalid JSON supplied in POST data".
If the json is valid return the json data or an empty dict if None is passed in.

This PR improves the error messages if the json is invalid, previously, the error message was "None object type" message which is not very helpful.
2019-11-21 15:23:11 +00:00
Leo Hemsted
c78a5d8536 Merge pull request #2662 from alphagov/utils-bump
Utils bump
2019-11-21 15:23:11 +00:00
Pea Tyczynska
685bff40d1 Stop validate function from being too complex by moving subfunctions out of it 2019-01-10 17:31:32 +00:00
Pea Tyczynska
5a1094b6fd Throw error if postage parameter for precompiled POST request incorrect 2019-01-10 16:04:06 +00:00
Rebecca Law
39198ed67e Using jsonschema for create_template.
Updated jsonschema to Draft7, this allowed a conditional validation on subject, if template_type == 'email' or 'letter' then subject is required.
This version is backward compatible with Draft4.
When creating TempalteRedacted, I've built the dict depending on if the created_by or created_by_id exists.
2018-11-07 16:17:08 +00:00
Leo Hemsted
fbe34041d6 add template folder CRUD
* create template folder
* rename template folder
* get list of template folders for service (not nested/presented in any
  particular way)
* delete template folder

Also removed `lazy=dynamic` from the `template_folder.templates`
relationship. lazy=dynamic returns a query object (which you can then
filter further). We just want to return the entire fetched list, at
least for now.
2018-10-31 14:28:16 +00:00
Katie Smith
8c22a6afda Change schema format name of datetime format
Renamed the 'datetime' format to 'datetime_within_next_day'. This format
is used to validate the date and time of scheduled notifications, not to
check the format of a datetime.
2018-06-27 09:11:38 +01:00
Rebecca Law
52bf6dabcd The regex to validate uuids was not rejecting uuids with a space at the end.
Switched to using a isinstance check on the string.
Added an order by clause to dao_get_template_usage_stats_by_service, it was causing an itermitten failure in the tests.
2018-02-15 13:34:06 +00:00
Ken Tsang
41f427d6dd Update definitions to check non empty strings 2017-09-11 15:37:40 +01:00
Leo Hemsted
5491adcabd address_line_2 is a required field 2017-08-07 14:23:44 +01:00
Leo Hemsted
11f8603319 Remove custom error message from personalisation validation
There's no longer a single err msg that fits all problems with
personalisation - since letters expect specific fields there
2017-07-27 16:49:37 +01:00
Leo Hemsted
7f4eec79e4 add POST letter schema
similar to sms/email, however, for consistency with response values
and internal storage, rather than supplying an "email_address" field
or a "phone_number" field, supply an "address_line_1" and "postcode"
field within the personalisation object.
2017-07-19 17:02:19 +01:00
Rebecca Law
828d5cd493 New table to store the inbound api information for a service. The table is versioned.
There is a new endpoint to create the inbound api and one to update it.
2017-06-15 11:32:51 +01:00
Rebecca Law
9f6c037530 Use iso8601 to validate scheduled_for datetime.
Added a validation method that always fails for scheduled notifications.
Comment out config for scheduled task.
The schedule notifications will be turned on once we can invite services to use it.
Waiting for the service permission story, must commit this in order to keep things from going stale.
2017-05-24 16:27:15 +01:00
Rebecca Law
a57dc18895 Add validation for scheduled_for where the date can not be in the past or more than 24 hours in the future. 2017-05-22 14:39:30 +01:00
Rebecca Law
751abb4b99 Update dateformat for scheduled_for to include minutes. 2017-05-22 14:15:35 +01:00
Rebecca Law
973cc2c4c9 Changed the scheduled_for datetime to only send and hour of a day to send.
Also expect the date being passed in is BST. The date is converted to UTC before saving. And converted to BST when returning a notification.
2017-05-17 15:06:15 +01:00
Rebecca Law
38e5b31e9a Update notification schemas with optional schedule_for element 2017-05-15 15:02:38 +01:00
Leo Hemsted
5ec4829d00 fix v2 schema phone/email validation when non-str passed in
jsonschema states:

> A format attribute can generally only validate a given set of
> instance types. If the type of the instance to validate is not in
> this set, validation for this format attribute and instance SHOULD
> succeed.

We were not checking for the type of the input, and our validators were
behaving in unexpected manners (throwing TypeErrors etc etc). Despite
us declaring that the phone_number field is of type `str`, we still
need to make sure the validator passes gracefully, so that the inbuilt
type check can be the bit that catches if someone passes in a non-str
value. We've seen this with people passing in integers instead of strs
for phone numbers. This'll make them receive a nice 400 error
(e.g. "phone_number 12345 is not of type string"), rather than us
having a 500 internal server error
2017-05-10 11:04:12 +01:00
Martyn Inglis
2a0f8c8808 Validate International phone numbers
- uses new utils methods to validate phone numbers
- defaults to International=True on validation. This ensures the validator works on all numbers
- Then check if the user can send this message to the number internationally if needed.
2017-04-26 15:56:45 +01:00
Leo Hemsted
501187a9f4 bump utils to 13.0.1
brings in a fix to InvalidEmail/Phone/AddressExceptions not being
instantiated correctly. `exception.message` is not a python standard,
so we shouldn't be relying on it to transmit exception reasons -
rather we should be using `str(exception)` instead. This involved a
handful of small changes to the schema validation
2017-01-09 16:37:58 +00:00
imdadahad
27952e96e0 Add comment for e.path schema validation fix
Add comment for e.path in schema validation and use popleft()
2016-12-16 12:41:19 +00:00
Imdad Ahad
7634c2e772 Add robustness to our v2 schema error messages:
* Ensure we dont raise exception if e.cause does not contain a message
* Ensure we handle case where e.path may be empty
* Refactor existing tests to conform to new format
2016-12-15 16:39:15 +00:00
Paul Craig
ab990679b3 Replace marshmallow with jsonschema
Created a new schema that accepts request parameters for the
get_notifications v2 route.
Using that to validate now instead of the marshmallow validation.

Also changed the way formatted error messages are returned because
the previous way was cutting off our failing `enum` messages.
2016-11-28 14:22:51 +00:00
Paul Craig
82ba2cd226 Create new notification schema
This is the schema that individual notifications will conform to
when they are returned from this API.
JSON logic enforces that the right keys are set depending on the
`"type"`. (eg a schema with `"type": "sms"` must have a
`"phone_number"` value and it cannot have an `"email_address"`)
2016-11-21 15:59:10 +00:00
Rebecca Law
f5e3c6f63b Remove print stmt and added assert error content 2016-11-17 14:02:44 +00:00
Rebecca Law
df62be421f Make email_from and subject required attributes of the email_content schema.
Update the format_checkers to raise the specific exception that why the validator can handle multiple messages.
Which led to a refactor of build_error_message.
2016-11-16 17:25:00 +00:00
Rebecca Law
b0ee09a9f6 Implemented the post email notifications endpoint for v2 2016-11-14 13:56:09 +00:00
Rebecca Law
488ba7a1ef Remove import 2016-11-11 10:56:21 +00:00
Rebecca Law
b0d88e0888 Removed OrderedDict
Added missing assert in test
2016-11-11 10:50:38 +00:00
Rebecca Law
c758694b98 Change validation error message to a string from a dict. 2016-11-10 16:30:51 +00:00
Rebecca Law
346d90e319 update V2 error response to
{status_code: 403,
 errors: [error: AuthError, message: token has expired}]
}
2016-11-09 14:56:54 +00:00
Rebecca Law
4cb38e2d12 Use status_code in error response.
Remove code.
2016-11-02 14:58:39 +00:00
Rebecca Law
db91a87fb2 Remove link from v2 error response 2016-11-02 14:48:15 +00:00
Rebecca Law
36ac00811d Create dict for the fields in the error response. 2016-11-02 09:13:48 +00:00
Rebecca Law
23a4f00e56 New package structure for the version 2 of the public api.
Start building up the validators required for post notificaiton.
The app/v2/errors.py is a rough sketch, will be passed a code, the error can look up the message and link for the error message.
2016-10-25 18:04:03 +01:00