Commit Graph

1716 Commits

Author SHA1 Message Date
Chris Hill-Scott
bde696cf56 Remove keyword args from call to create service
The cache decorator doesn’t work with functions that use keyword
arguments (at the moment).
2018-04-19 13:54:14 +01:00
Chris Hill-Scott
1c91e10d5d Clear user cache when deleting a service
The user JSON has a list of service IDs
2018-04-19 13:25:04 +01:00
Chris Hill-Scott
9a3f9b7273 Delete caches when user accepts invite
Accepting an invite changes:
- the `user_to_service` list of users returned by `GET /service/<id>`
- the `services` list return by `GET /user/<id>`

The latter change is causing the functional tests to fail.
2018-04-19 13:15:52 +01:00
Chris Hill-Scott
eb9aed6d01 Cache GET /user response in Redis
In the same way, and for the same reasons that we’re caching the service
object.

Here’s a sample of the data returned by the API – so we should make sure
that any changes to this data invalidate the cache.

If we ever change a user’s phone number (for example) directly in the
database, then we will need to invalidate this cache manually.

```python
{  
   'data':{  
      'organisations':[  
         '4c707b81-4c6d-4d33-9376-17f0de6e0405'
      ],
      'logged_in_at':'2018-04-10T11:41:03.781990Z',
      'id':'2c45486e-177e-40b8-997d-5f4f81a461ca',
      'email_address':'test@example.gov.uk',
      'platform_admin':False,
      'password_changed_at':'2018-01-01 10:10:10.100000',
      'permissions':{  
         '42a9d4f2-1444-4e22-9133-52d9e406213f':[  
            'manage_api_keys',
            'send_letters',
            'manage_users',
            'manage_templates',
            'view_activity',
            'send_texts',
            'send_emails',
            'manage_settings'
         ],
         'a928eef8-0f25-41ca-b480-0447f29b2c20':[  
            'manage_users',
            'manage_templates',
            'manage_settings',
            'send_texts',
            'send_emails',
            'send_letters',
            'manage_api_keys',
            'view_activity'
         ],
      },
      'state':'active',
      'mobile_number':'07700900123',
      'failed_login_count':0,
      'name':'Example',
      'services':[  
         '6078a8c0-52f5-4c4f-b724-d7d1ff2d3884',
         '6afe3c1c-7fda-4d8d-aa8d-769c4bdf7803',
      ],
      'current_session_id':'fea2ade1-db0a-4c90-93e7-c64a877ce83e',
      'auth_type':'sms_auth'
   }
}
```
2018-04-18 13:27:11 +01:00
Chris Hill-Scott
777bfa2244 Merge pull request #2014 from alphagov/redis-spike-CHS
Use Redis to cache API calls in admin app
2018-04-18 13:26:12 +01:00
Chris Hill-Scott
e3998e7db3 Reword request to go live message
We want to stop people writing support tickets that say something like
“I’ve just submitted a request to go live, how long does the process
take?”
2018-04-12 13:17:30 +01:00
Chris Hill-Scott
24dbe7b7b1 Add Redis cache between admin and API
Most of the time spent by the admin app to generate a page is spent
waiting for the API. This is slow for three reasons:

1. Talking to the API means going out to the internet, then through
   nginx, the Flask app, SQLAlchemy, down to the database, and then
   serialising the result to JSON and making it into a HTTP response
2. Each call to the API is synchronous, therefore if a page needs 3 API
   calls to render then the second API call won’t be made until the
   first has finished, and the third won’t start until the second has
   finished
3. Every request for a service page in the admin app makes a minimum
   of two requests to the API (`GET /service/…` and `GET /user/…`)

Hitting the database will always be the slowest part of an app like
Notify. But this slowness is exacerbated by 2. and 3. Conversely every
speedup made to 1. is multiplied by 2. and 3.

So this pull request aims to make 1. a _lot_ faster by taking nginx,
Flask, SQLAlchemy and the database out of the equation. It replaces them
with Redis, which as an in-memory key/value store is a lot faster than
Postgres. There is still the overhead of going across the network to
talk to Redis, but the net improvement is vast.

This commit only caches the `GET /service` response, but is written in
such a way that we can easily expand to caching other responses down the
line.

The tradeoff here is that our code is more complex, and we risk
introducing edge cases where a cache becomes stale. The mitigations
against this are:
- invalidating all caches after 24h so a stale cache doesn’t remain
  around indefinitely
- being careful when we add new stuff to the service response

---

Some indicative numbers, based on:
- `GET http://localhost:6012/services/<service_id>/template/<template_id>`
- with the admin app running locally
- talking to Redis running locally
- also talking to the API running locally, itself talking to a local
  Postgres instance
- times measured with Chrome web inspector, average of 10 requests

╲ | No cache | Cache service | Cache service and user | Cache service, user and template
-- | -- | -- | -- | --
**Request time** | 136ms | 97ms | 73ms | 37ms
**Improvement** | 0% | 41% | 88% | 265%

---

Estimates of how much storage this requires:

- Services: 1,942 on production × 2kb = 4Mb
- Users: 4,534 on production × 2kb = 9Mb
- Templates: 7,079 on production × 4kb = 28Mb
2018-04-10 12:58:35 +01:00
Chris Hill-Scott
44f42c8916 Be clearer about the templates needed to go live
Adding a ‘testing’ template it not enough. It needs to have some real
looking content, so that we can:
- work out what a service is doing
- assess whether that’s a reasonable (ie meeting the terms of use) thing
  to be doing with Notify

At the moment we’re having to go back to services quite a lot when they
request to go live and ask them for this stuff.
2018-04-09 13:40:03 +01:00
Chris Waszczuk
4f208ef849 Merge pull request #2010 from alphagov/one-off-fixxx
Add pagination to inbox page
2018-04-09 10:30:24 +01:00
chrisw
78d16709d6 reading messages for inbox from new most_recent endpoint
avoids us having to work out and display most recent messages
only on the front-end - it's now all done in api
2018-04-05 13:54:37 +01:00
Chris Hill-Scott
0b905249a7 Make send test letter preview use template ID
The check page expects template ID to be passed through in the URL not
the session now. The send test letter page wasn’t changed.

This commit changes it, and adds a test to make sure this path is
covered.
2018-04-04 17:10:22 +01:00
chrisw
1d32c766e8 remove X messages from Y users msg 2018-04-04 15:43:07 +01:00
chrisw
f5c467e4ff add pagination to inbox page 2018-04-04 15:41:17 +01:00
Chris Hill-Scott
b2722a0cd7 Put template_id back in the session
The start job endpoint needs the template ID in order to make the API
call.

It doesn’t make sense to add it to the start job URL, because users
could potentially start a job with the wrong template by hacking the URL
(which would blow up at some point, if the template didn’t match the
columns in the file).
2018-04-04 15:28:30 +01:00
Rebecca Law
fc846cf4a2 Merge pull request #2001 from alphagov/store-less-in-session
Stop storing `template_id` and `original_file_name` in session
2018-04-04 14:42:38 +01:00
Chris Hill-Scott
ba9935c49e Remove ‘check’ page’s reliance on session
A of this commit’s parent we are storing `template_id` and
`original_file_name` in the URL. Getting them from the URL is better,
so the check page no longer needs to look for them in the session. This
commit removes the code that looks for these values in the session.
2018-04-04 11:37:43 +01:00
Chris Hill-Scott
3dab34cd38 Store template ID and original file name in URL
At the moment you can’t press refresh on the check page if there’s
errors. This is because the session gets cleared when there’s errors.
This is a bad user experience.

The data that this page is relying on (from the session) is:
- template ID
- original file name

Neither of these things need to be in the session because:
- they are not secret
- the user can modify them already (by choosing a different template or
  renaming their file locally)

So this commit additionally stores them in the URL.
2018-04-04 11:30:27 +01:00
Chris Hill-Scott
d75f89daf5 Limit number of navigable financial years to 3
Because more than 3 looks ugly.
2018-04-03 18:13:38 +01:00
Chris Hill-Scott
e74d261ec3 Don’t store info about bad uploads in session
Because we now[1] store info about each file upload separately in the
session the session isn’t overridden every time you upload a file. This
is good because you can do multiple file uploads idempotently.

Generally we are cleaning up after ourselves because we pop anything to
do with that upload from the session. However there is an edge case: if
you never send the file then the info about the file stays in the
session in perpetuity[2]. This is generally happening when people are
uploading files that are impossible to send, ie ones that have errors.

So this commit makes two changes:

1. remove info about a file upload from the session as soon as we know
   that it contains errors
2. `POST` reuploads to the same endpoint as initial uploads because
   otherwise we need to keep info about bad uploads in the session,
   which would prevent us from doing 1.

1. https://github.com/alphagov/notifications-admin/pull/1968
2. or at least until the session is cleared by the user logging out
2018-03-29 11:56:53 +01:00
Chris Hill-Scott
dca5546cbd Only offer agreement download to non-crown for now
We don’t have the crown agreement in a nice downloadable format at the
moment.
2018-03-28 12:43:03 +01:00
Chris Hill-Scott
c7ab9f7f1a Link to the page to download the agreement
We prefer people downloading the agreement if they can. If we don’t know
which agreement they should be using (ie we don’t know their crown
status) then we fall back to having them contact us.
2018-03-27 11:35:17 +01:00
Chris Hill-Scott
68292d2299 Add endpoints to serve the agreement
Rather than making users contact us to get the agreement, we should just
let them download it, when we know which version to send them.

This commit adds two endpoints:
- one to serve a page which links to the agreement
- one to serve the agreement itself

These pages are not linked to anywhere because the underlying files
don’t exist yet. So I haven’t bothered putting real content on the page
yet either. I imagine the deploy sequence will be:

1. Upload the files to the buckets in each environment
2. Deploy this code through each enviroment, checking the links work
3. Make another PR to start linking to the endpoints added by this
   commit
2018-03-27 11:35:17 +01:00
Leo Hemsted
0c88556d17 Merge pull request #1977 from alphagov/human-readable-day
fix relative datetime function to handle dates correctly
2018-03-21 16:21:43 +00:00
Chris Waszczuk
59803348a4 Merge pull request #1974 from alphagov/org-links-dont-show-if-user-doesnt-have-permissions
Org links don't show if user doesn't have permissions
2018-03-21 16:15:42 +00:00
Leo Hemsted
df30562216 fix relative datetime function to handle dates correctly
Previously, we were looking at the day of the week - so messages sent
six days ago would show up as "tomorrow". We now look at the actual
date, so that won't happen again.

We were also subtracting an hour to make 00:00 this evening show up as
"midnight today", despite it technically being tomorrow. However, this
means that 00:59 tomorrow morning would show up as "00:59 today", a
full day out. So reduce that to just a minute, so it doesn't affect
other times of day.
2018-03-21 16:08:09 +00:00
chrisw
c47a4ab830 org links don't show if user doesn't have permissions 2018-03-21 15:23:01 +00:00
Chris Hill-Scott
0f8a6ca2fb Merge pull request #1969 from alphagov/no-free-text-from-url
Don’t populate support form with arbitrary text
2018-03-21 14:34:19 +00:00
Katie Smith
bc5f4c145a Delete duplicated tests
test_api_keys.py and test_api_integration.py were almost identical
files with only a few lines difference between them. By moving one
test we can now delete test_api_keys.py
2018-03-20 16:10:37 +00:00
Chris Hill-Scott
64b5f03dcd Don’t populate support form with arbitrary text
I don’t think it’s a massive risk (we’re certainly mitigating against
any XSS), but having a page on a GOV.UK domain where you can prefill
text on the page from a query string probably isn’t great.

So this commit restricts prefilling the support form to a set of
named questions.
2018-03-20 16:07:23 +00:00
Katie Smith
4db75f6a58 Display the two new virus states for letters
Precompiled letters can now have two additional states:
* pending-virus-check
* virus-scan-failed

Both new states should show in the notifications dashboard, and
virus-scan-failed should appear as an error state, with a descriptive
message. You should not be able to preview a letter in one of the two
new states, so the preview link has been removed for precompiled letters
in these states.
2018-03-20 14:54:29 +00:00
Chris Waszczuk
5f65bb5e56 Merge pull request #1968 from alphagov/fixed-session-overriding-while-uploading-csvs-2
Fixed sessions overriding while uploading csvs
2018-03-20 13:45:33 +00:00
chrisw
287230acac fixed sessions overriding while uploading csvs 2018-03-20 12:15:17 +00:00
kentsanggds
91adca10b5 Merge pull request #1961 from alphagov/ken-show-client-reference
Add client reference to API Integration / message log
2018-03-20 10:54:47 +00:00
Leo Hemsted
e000552e56 redirect to show_accounts_or_dashboard on login
show_accounts_or_dashboard has logic about where you should redirect
to. If we let it do this, then that's nicer than duplicating its
logic. We found that it wasn't accounting for orgs in redirects
properly.
2018-03-19 16:41:16 +00:00
Ken Tsang
c3238d8f68 Tests page when client_reference exists in noti 2018-03-19 16:12:14 +00:00
Leo Hemsted
90c40075c8 Merge pull request #1954 from alphagov/choose-accounts
Choose accounts
2018-03-19 15:26:06 +00:00
Chris Hill-Scott
cd3910556c Merge pull request #1960 from alphagov/dont-validate-across-channels
Don’t validate phone numbers when sending emails
2018-03-16 14:59:42 +00:00
Chris Hill-Scott
5a2fafb66b Don’t validate phone numbers when sending emails
If you have a placeholder called `((phone number))` in your email
template, and you try to send a one-off message then the form input will
attempt to validate your ‘phone number’.

This is not helpful if you’re trying to put a landline number in your
email, for example.

This only affects messages being sent through the one-off interface.

This commit makes the form be aware of template type, which fixes the
problem.
2018-03-16 14:17:43 +00:00
Chris Hill-Scott
c2bc7eca2c Make pricing page consistent in talking about MoU
We shouldn’t tell people on one page (the terms page) that we know about
their organisations agreement and then on the pricing page tell them to
contact us to find out what we know about the agreement.

So this commit adds the same logic from the terms page to the pricing
page, with wording that makes sense in the pricing context.
2018-03-16 13:10:40 +00:00
Leo Hemsted
666e77e699 redirect (301 MOVED PERMANENTLY) from old choose endpoints 2018-03-16 11:51:19 +00:00
Chris Hill-Scott
056c4ebb88 Rename GovernmentDomain to AgreementInfo
This better describes the data encapsulated by this class, and how we
are now using it.
2018-03-15 10:45:34 +00:00
Chris Hill-Scott
dd7fb71706 Merge pull request #1955 from alphagov/remove-mou-question
Clean up request to go live page
2018-03-15 10:25:24 +00:00
Chris Hill-Scott
59f9fe7df1 Finalise wording
Main changes are to better differentiate between the data sharing and
financial agreement and the terms of use.
2018-03-14 16:04:15 +00:00
Chris Hill-Scott
1ce4c874ad Allow message to be prefilled on feedback page
So that people don’t have to remember what they are supposed to be
asking for and go to the effort of typing out the message.
2018-03-14 16:02:09 +00:00
Chris Hill-Scott
6c47375d9f Make terms page smarter about the agreement
People are emailing us asking if their organisation has signed the
agreement. In some cases they have, so this is a waste of their and
our time.

This commit adds a bit of logic to the terms of use page to tell users
when their organisation has already signed the agreement.
2018-03-14 16:00:59 +00:00
Leo Hemsted
d5108d0418 fix my awful rebasing skills 2018-03-14 15:39:55 +00:00
Leo Hemsted
a82ac13d4e split tests out into two separate files 2018-03-14 15:39:55 +00:00
Leo Hemsted
a94fa5472d platform admins can go back to any service 2018-03-14 15:39:55 +00:00
Leo Hemsted
8ef36f13f2 finish tests for choose account page 2018-03-14 15:39:55 +00:00
Leo Hemsted
b7b9b8dd90 make sure mock login sets up session correctly 2018-03-14 15:39:55 +00:00