Files
notifications-admin/app/notify_client/org_invite_api_client.py
Chris Hill-Scott 628e344b36 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-06-05 11:13:41 +01:00

43 lines
1.4 KiB
Python

from app.notify_client import NotifyAdminAPIClient, _attach_current_user
class OrgInviteApiClient(NotifyAdminAPIClient):
def init_app(self, app):
super().init_app(app)
self.admin_url = app.config['ADMIN_BASE_URL']
def create_invite(self, invite_from_id, org_id, email_address):
data = {
'email_address': email_address,
'invited_by': invite_from_id,
'invite_link_host': self.admin_url,
}
data = _attach_current_user(data)
resp = self.post(url='/organisation/{}/invite'.format(org_id), data=data)
return resp['data']
def get_invites_for_organisation(self, org_id):
endpoint = '/organisation/{}/invite'.format(org_id)
resp = self.get(endpoint)
return resp['data']
def check_token(self, token):
resp = self.get(url='/invite/organisation/{}'.format(token))
return resp['data']
def cancel_invited_user(self, org_id, invited_user_id):
data = {'status': 'cancelled'}
data = _attach_current_user(data)
self.post(url='/organisation/{0}/invite/{1}'.format(org_id, invited_user_id),
data=data)
def accept_invite(self, org_id, invited_user_id):
data = {'status': 'accepted'}
self.post(url='/organisation/{0}/invite/{1}'.format(org_id, invited_user_id),
data=data)
org_invite_api_client = OrgInviteApiClient()