2021-07-22 14:05:11 +01:00
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache
|
|
|
|
|
from app.utils.user_permissions import (
|
2019-04-11 16:40:25 +01:00
|
|
|
roles,
|
2018-02-28 15:37:49 +00:00
|
|
|
translate_permissions_from_admin_roles_to_db,
|
|
|
|
|
)
|
2016-02-26 13:07:35 +00:00
|
|
|
|
|
|
|
|
|
2016-11-30 17:01:44 +00:00
|
|
|
class InviteApiClient(NotifyAdminAPIClient):
|
2016-02-26 13:07:35 +00:00
|
|
|
|
|
|
|
|
def init_app(self, app):
|
2018-02-09 15:03:32 +00:00
|
|
|
super().init_app(app)
|
|
|
|
|
|
2017-12-20 17:17:17 +00:00
|
|
|
self.admin_url = app.config['ADMIN_BASE_URL']
|
2016-02-26 13:07:35 +00:00
|
|
|
|
2019-03-15 14:57:39 +00:00
|
|
|
def create_invite(self,
|
|
|
|
|
invite_from_id,
|
|
|
|
|
service_id,
|
|
|
|
|
email_address,
|
|
|
|
|
permissions,
|
|
|
|
|
auth_type,
|
|
|
|
|
folder_permissions):
|
2016-02-26 13:07:35 +00:00
|
|
|
data = {
|
2019-11-04 12:20:09 +00:00
|
|
|
'service': service_id,
|
2016-02-26 13:07:35 +00:00
|
|
|
'email_address': email_address,
|
2016-02-29 11:03:35 +00:00
|
|
|
'from_user': invite_from_id,
|
2018-02-28 15:37:49 +00:00
|
|
|
'permissions': ','.join(sorted(translate_permissions_from_admin_roles_to_db(permissions))),
|
2017-12-20 17:17:17 +00:00
|
|
|
'auth_type': auth_type,
|
|
|
|
|
'invite_link_host': self.admin_url,
|
2019-03-15 14:57:39 +00:00
|
|
|
'folder_permissions': folder_permissions,
|
2016-02-26 13:07:35 +00:00
|
|
|
}
|
2016-08-11 14:20:43 +01:00
|
|
|
data = _attach_current_user(data)
|
2016-02-26 13:07:35 +00:00
|
|
|
resp = self.post(url='/service/{}/invite'.format(service_id), data=data)
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
return resp['data']
|
2016-02-26 15:33:17 +00:00
|
|
|
|
|
|
|
|
def get_invites_for_service(self, service_id):
|
2018-11-30 17:39:39 +00:00
|
|
|
return self.get(
|
|
|
|
|
'/service/{}/invite'.format(service_id)
|
|
|
|
|
)['data']
|
2016-02-29 17:35:21 +00:00
|
|
|
|
2021-03-08 12:51:35 +00:00
|
|
|
def get_invited_user(self, invited_user_id):
|
|
|
|
|
return self.get(
|
|
|
|
|
f'/invite/service/{invited_user_id}'
|
|
|
|
|
)['data']
|
|
|
|
|
|
2021-03-05 17:04:35 +00:00
|
|
|
def get_invited_user_for_service(self, service_id, invited_user_id):
|
2020-08-17 14:30:09 +01:00
|
|
|
return self.get(
|
|
|
|
|
f'/service/{service_id}/invite/{invited_user_id}'
|
|
|
|
|
)['data']
|
|
|
|
|
|
2019-04-11 16:40:25 +01:00
|
|
|
def get_count_of_invites_with_permission(self, service_id, permission):
|
|
|
|
|
if permission not in roles.keys():
|
|
|
|
|
raise TypeError('{} is not a valid permission'.format(permission))
|
|
|
|
|
return len([
|
|
|
|
|
invited_user for invited_user in self.get_invites_for_service(service_id)
|
|
|
|
|
if invited_user.has_permission_for_service(service_id, permission)
|
|
|
|
|
])
|
|
|
|
|
|
2016-03-03 17:53:16 +00:00
|
|
|
def check_token(self, token):
|
2021-03-12 15:57:46 +00:00
|
|
|
return self.get(url='/invite/service/check/{}'.format(token))['data']
|
2016-03-01 16:12:26 +00:00
|
|
|
|
|
|
|
|
def cancel_invited_user(self, service_id, invited_user_id):
|
|
|
|
|
data = {'status': 'cancelled'}
|
2016-08-11 14:20:43 +01:00
|
|
|
data = _attach_current_user(data)
|
2016-03-01 17:56:39 +00:00
|
|
|
self.post(url='/service/{0}/invite/{1}'.format(service_id, invited_user_id),
|
|
|
|
|
data=data)
|
2016-02-29 17:35:21 +00:00
|
|
|
|
2018-04-20 16:32:02 +01:00
|
|
|
@cache.delete('service-{service_id}')
|
|
|
|
|
@cache.delete('user-{invited_user_id}')
|
2016-03-03 17:53:16 +00:00
|
|
|
def accept_invite(self, service_id, invited_user_id):
|
|
|
|
|
data = {'status': 'accepted'}
|
|
|
|
|
self.post(url='/service/{0}/invite/{1}'.format(service_id, invited_user_id),
|
|
|
|
|
data=data)
|
|
|
|
|
|
2018-10-26 15:39:32 +01:00
|
|
|
|
|
|
|
|
invite_api_client = InviteApiClient()
|