Commit Graph

969 Commits

Author SHA1 Message Date
Imdad Ahad
8b64aa7e79 Use POST endpoint for updating a user attr 2016-11-10 12:07:29 +00:00
Imdad Ahad
31dfc36094 Merge branch 'master' into update-user-profile-endpoints 2016-11-09 12:41:21 +00:00
Imdad Ahad
3f10e59db3 Add user dao method to update a single user attr 2016-11-07 17:42:39 +00:00
Imdad Ahad
461d8a9b2c Add separate endpoint to update a single user attr 2016-11-07 17:42:23 +00:00
Imdad Ahad
8a126c7387 Add a schema to validate a single user attr 'strictly' 2016-11-07 17:41:49 +00:00
Chris Hill-Scott
bec20d3854 Merge pull request #728 from alphagov/case-insensitive-auth-header
Make bearer prefix on auth header case insensitive
2016-11-07 14:59:09 +01:00
Martyn Inglis
15ba0a3eb1 Added job id to logger 2016-11-07 11:59:46 +00:00
Chris Hill-Scott
9248e72c50 Make bearer prefix on auth header case insensitive
From a support ticket:

> the "Bearer" prefix on the auth header is case sensitive. Can this be
> made case-insensitive?

Sure can 🙃
2016-11-07 10:49:05 +00:00
Rebecca Law
36ac00811d Create dict for the fields in the error response. 2016-11-02 09:13:48 +00:00
Rebecca Law
e32978b72a Merge branch 'master' into post-sms-v2 2016-11-02 09:08:26 +00:00
Rebecca Law
482d10545b Improvements to the tests.
Update AuthError with a to_dict_v2 method.
2016-11-01 10:33:34 +00:00
Rebecca Law
a358f3cb3a Error handler for schema validation errors 2016-10-31 15:43:11 +00:00
Rebecca Law
fc298367c5 Updated test_validators to test the contents of the error messages.
Added some tests to the test_post_notifications.
Added a errorhandler for AuthErrors.

This endpoint is not being used anywhere, however there is some common code being used in the v1 post endpoint. The only thing that may be affected is the error response, hopefully they are the same.
2016-10-31 12:22:26 +00:00
Rebecca Law
8cf2fc72a8 - Refactor version 1 of post notificaitons to use the common persist_notificaiton and send_notification_to_queue methods.
- It would be nice to refactor the send_sms and send_email tasks to use these common functions as well, that way I can get rid of the new Notifications.from_v2_api_request method.
- Still not happy with the format of the errors. Would like to find a happy place, where the message is descript enough that we do not need external documentation to explain the error. Perhaps we still only need documentation to explain the trial mode concept.
2016-10-28 17:10:00 +01:00
Rebecca Law
6e4bad135a - Implemented persist_notification and send_notification_to_queue in the process_notifications module
- Not sure I want to create a new classmethod on Notifications to create from v2 request. Will take another look at that.
2016-10-27 17:34:54 +01:00
Rebecca Law
c2eecdae36 - Add validation methods for post notification.
- Use these validation methods in post_sms_notification and the version 1 of post_notification.
- Create a v2 error handlers.
- InvalidRequest has a to_dict method for private and v1 error responses and a to_dict_v2 method to create the v2 of the error responses.
- Each validation method has extensive unit tests, so the unit test for the endpoint do not need to check every error case, but check that the error handle formats the message correctly.
- The format of the error messages is still a work on progress.
- This version of the api could be deployed without causing a problem to the application.
- The new endpoing is still a work in progress and is not being used yet.
2016-10-27 11:46:37 +01:00
Leo Hemsted
a45c62d41d add can_send_letters flag to services
defaults to false
2016-10-26 13:01:40 +01: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
Rebecca Law
a5e07d8aff V2 schemas for post sms notifications, post_sms_request and post_sms_response 2016-10-25 14:53:31 +01:00
Leo Hemsted
a1cc092d3b fix logger.exception syntax
when given any log function with multiple parameters, the python logging utils
assume the first param is a format string and the rest are arguments to pass
in - we were passing in the exception object to `logger.exception`, however,
the purpose of .exception is to add the exception object itself - so we didn't
need to
2016-10-17 17:44:17 +01:00
Leo Hemsted
65197a6c91 handle Exception and remove duplication in errors.py
ensure that if unexpected Exceptions are thrown, we handle them correctly
(log and then return JSON)

also remove some branches that will never trip, and combine a couple of
identical functions
2016-10-17 17:41:39 +01:00
Leo Hemsted
6e015b5a7a Merge pull request #713 from alphagov/email-exception
Don't retry invalid emails
2016-10-17 11:26:50 +01:00
Chris Hill-Scott
2a65bf3bb5 Merge pull request #711 from alphagov/extend-scheduling-to-96-hours
Let jobs be scheduled up to 96 hours in the future
2016-10-14 15:33:44 +01:00
Leo Hemsted
c69d2aa778 add tests for ses client 2016-10-13 16:07:32 +01:00
Leo Hemsted
a2c3d265de remove unused former send_sms_to_provider and send_sms_to_email functions
they were superceded by deliver_sms and deliver_email in the same file 3 wks ago
2016-10-13 15:53:01 +01:00
Leo Hemsted
a095aa41f3 don't retry task if InvalidEmailError
just record it as a technical error - retrying wont fix a bad email
2016-10-13 15:27:47 +01:00
Leo Hemsted
c0c62e02b7 move statsd call out of generic tryexcept
we shouldn't try and use statsd to log an error if they fail, for example

[we also shouldn't retry sending the message but that's a problem for another time]
2016-10-13 14:17:17 +01:00
Rebecca Law
d9ced9f912 Merge pull request #712 from alphagov/change-email-confirmation
Change email confirmation
2016-10-13 14:01:23 +01:00
Rebecca Law
9ffdf66c49 Rename the endpoints.
Increase test coverage to include the encrypted message sent to the task.
2016-10-13 11:59:47 +01:00
Chris Hill-Scott
9f36b363f2 Let jobs be scheduled up to 96 hours in the future
If you want to send a job on Monday morning, you should be able to
schedule it on Friday. You shouldn’t need to work on the weekend.

96 hours is a full 4 days, so you can schedule a job at any time on
Friday for any time on Monday.

We’ve checked with the information assurance people, and they’re OK with
us holding the data for this extra amount of time.
2016-10-12 13:38:12 +01:00
Rebecca Law
b37eef974c Change email confirmation
- New endpoint to send a user an email to verify the email address when they want to change it.
2016-10-12 13:06:39 +01:00
Leo Hemsted
d8467bfc3c filter out test jobs from the GET /service/{}/job endpoint
this is so that the filtering, which we do on the admin side, is applied
before pagination - so that the pages returned are all valid displayable
jobs. unfortunately this means that another config value has to be copied
to the server side but it's not the end of the world
2016-10-11 14:30:40 +01:00
Chris Hill-Scott
717e73a9f7 Loosen key restriction on get notification
Currently getting a single notification by ID is restricted to
notifications created with the same key type.

This makes things awkward for the functional tests now we’ve removed the
ability to create live keys in trial mode. So this commit removes the
restriction, so that any key can get any notification, no matter how it
was created.

And you’re never going to guess a UUID, so the chances of this giving
you privileged access to someone’s personal information is none.

This does not change the get all notifications endpoint, which
absolutely should be restricted by key type.
2016-10-10 17:29:38 +01:00
Chris Hill-Scott
88362cb075 Merge pull request #706 from alphagov/sort-jobs-by-processed-time-first
Sort jobs by processed time first
2016-10-10 14:03:27 +01:00
Chris Hill-Scott
b4291684b7 Sort jobs by processed time first
Say you have a dashboard with some jobs you sent. Normally looks like:

job | sent
--- | ---
file.csv | **5pm**
file.csv | 3pm
file.csv | 1pm
file.csv | 11am

However if your 5pm job was scheduled at lunchtime, then it will look
like this:

job | sent
--- | ---
file.csv | 3pm
file.csv | 1pm
file.csv | **5pm**
file.csv | 11am

This is because the jobs are sorted by when they were created, not when
they were sent. It looks wrong.

**For jobs that have already been sent**

This commit changes the sort order to be based on `processed_at`
instead.

**For upcoming jobs**

If a job doesn’t have a `processed_at` time then it’s scheduled, but
hasn’t started yet. Only in this case should we still be sorting by
`created_at`.
2016-10-10 09:36:53 +01:00
Chris Hill-Scott
74be99e7c7 Let team key send to whitelist
There is an overlap between team key/trial mode/whitelist. But it’s not
a complete overlap. So it’s hard to understand all the different
permutations of which key lets you send to which people when.

This commit tries to reduce the differences between these concepts. So
for a user of the API

**In trial mode**

- You can send to anyone in your team or whitelist, using the team key
- You can simulate sending to anyone, using the simulate key

**When you’re live**

- You can send to anyone in your team or whitelist, using the team key
- You can simulate sending to anyone, using the simulate key
- You can send to anyone with the live key

So doing a `git diff` on that list, the only difference between being in
trial mode and live mode is now:

`+` You can send to anyone with the live key

**(How trial mode used to work)**
- You can send to anyone in your team or whitelist, using the normal key
- You can simulate sending to anyone, using the simulate key
- You can send to _just_ people in your team using the team key
2016-10-07 15:38:36 +01:00
Leo Hemsted
bdb4da4976 tests n stuff 2016-10-07 13:08:41 +01:00
Leo Hemsted
d22d055e21 only process jobs if they're pending
help prevent issues where scheduled jobs are processed twice. note this is NOT
a watertight solution - it holds no locks, and there is no guarantee that the
status won't have updated between asserting that its status is 'pending' and
updating it to be 'in progress'
2016-10-07 12:54:11 +01:00
Leo Hemsted
16dd16c026 move updating into the dao fn
this helps manage the transaction by keeping it inside one function in the dao,
so after the function completes you know that the transaction has been released
and concurrent processing can resume
2016-10-07 12:35:08 +01:00
Martyn Inglis
897ad6a957 prevent race conditions in run_scheduled_jobs queuing jobs multiple times
we were running into issues where multiple beats queue up the
run_scheduled_jobs task at the same time, and concurrency issues with selecting
scheduled jobs causes both tasks to trigger processing of the job.

Use with_for_update, which calls through to the postgres SELECT  ... FOR UPDATE
which locks other SELECT FOR UPDATES (ie other threads running same code) until
the rows are set to pending and the transaction completes - so the second
thread will not find any rows
2016-10-07 12:35:02 +01:00
Rebecca Law
6065ed57cf Fix for the job status
- It seems that when we changed the name of the job.status column that we didn't update the code to use job.job_status.
- Therefore none of the jobs since then have had the job status updated.
- Now that this is fix we can show the job status when there is an error like "sending exceeds limits"
  - This could happen if a job is scheduled to run at the top of the hour, so at the time of the job creation the limit was not exceed, but at the time of processing the job the limit is exceed.
2016-10-05 14:56:32 +01:00
Chris Hill-Scott
7e2131d2b2 Merge pull request #699 from alphagov/monthly-billable-units
Add DAO and endpoint for getting billable units/financial year
2016-10-04 16:08:31 +01:00
Chris Hill-Scott
826eaaf5b3 Remove unecessary brackets when returning tuple 2016-10-04 13:05:15 +01:00
Chris Hill-Scott
219d5943c1 Return iterator from query for speed 2016-10-04 13:04:42 +01:00
Chris Hill-Scott
d352c0eed9 Really fix the timezones
Two main changes:

- uses `astimezone` instead of `replace` because `replace` doesn’t
  handle daylight savings time correctly [1]
- create the notifications one second before midnight in BST, because
  midnight is actually counted as being start of the _next_ day, month,
  etc

1. http://www.saltycrane.com/blog/2009/05/converting-time-zones-datetime-objects-python/#add-timezone-localize
2016-10-04 13:00:37 +01:00
Rebecca Law
6f83258004 Update Template.version and TemplateHistory.version nullable=False to match the db 2016-10-04 10:47:34 +01:00
Chris Hill-Scott
76d5f14952 Filter unbillable notifications before calculating
Notifications with a `billable_units` count of `0` wont have any effect
on the result, but including them in the query will slow down the
grouping and summing of the results because it’ll have to loop over more
rows.
2016-10-04 09:51:41 +01:00
Chris Hill-Scott
7abe40b506 Make billing year aware of British Summer Time
April 1st is in British summer time, ie 1hr ahead of UTC. The database
stores everything in UTC, so for accurate comparisions we need to make
sure that `get_financial_year()` returns a UTC, datetime-aware
timestamp that is 1hr ahead of midnight.

This also means that when we group notifications by month, the months
need to be in BST. So the line between one year and another is actually
01:00 on April 1st, _not_ 00:00 on April 1st.

There’s no way we’ve found to do this in SQLAlchemy or raw Postgres,
especially because we don’t store the timestamps with a timezone in the
database.

So the grouping and summing of the notifications has to be done in
Python.
2016-10-04 09:08:27 +01:00
Chris Hill-Scott
def1d253aa Add endpoint to get billable units/financial year
`/services/ef7a665d-11a4-425a-a180-a67ca00b69d7/billable-units?year=2016`

Pretty much just passes through to the DAO layer. Validates that year
is:

- present (there’s no need for unbounded queries on this endpoint)
- an integer
2016-10-03 15:50:55 +01:00
Chris Hill-Scott
6a5e947220 Add DAO for getting billable units/financial year
In order to invoice people we need to know how many text message
fragments they’ve sent per month.

This should be per (government) financial year, ie April 1st to April
1st because we’ll only ever show a page for one year (because the
250,000 allowance is topped up at the start of every financial year).

This commit only does the DAO bit, not the REST bit.
2016-10-03 15:50:54 +01:00