2018-02-20 11:22:17 +00:00
|
|
|
from app.notify_client import NotifyAdminAPIClient, _attach_current_user
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class OrgInviteApiClient(NotifyAdminAPIClient):
|
|
|
|
|
|
|
|
|
|
def init_app(self, app):
|
2018-03-07 18:09:33 +00:00
|
|
|
super().init_app(app)
|
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
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)
|
2023-07-12 12:09:44 -04:00
|
|
|
resp = self.post(url='/organization/{}/invite'.format(org_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']
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2023-07-12 12:09:44 -04:00
|
|
|
def get_invites_for_organization(self, org_id):
|
|
|
|
|
endpoint = '/organization/{}/invite'.format(org_id)
|
2018-02-19 16:53:29 +00:00
|
|
|
resp = self.get(endpoint)
|
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']
|
2018-02-19 16:53:29 +00:00
|
|
|
|
2021-03-05 17:04:35 +00:00
|
|
|
def get_invited_user_for_org(self, org_id, invited_org_user_id):
|
2020-08-17 14:30:09 +01:00
|
|
|
return self.get(
|
2023-07-12 12:09:44 -04:00
|
|
|
f'/organization/{org_id}/invite/{invited_org_user_id}'
|
2020-08-17 14:30:09 +01:00
|
|
|
)['data']
|
|
|
|
|
|
store invited org user ids in session
first of a two step process to remove invited user objects from the
session. we're removing them because they're of variable size, and with
a lot of folder permissions they can cause the session to exceed the 4kb
cookie size limit and not save properly.
this commit looks at invited org users only.
in this step, start saving the invited org user's id to the
session alongside the session object. Then, if the invited_org_user_id
is present in the next step of the invite flow, fetch the user object
from the API instead of from the session. If it's not present (due to a
session set by an older instance of the admin app), then just use the
old code to get the entire object out of the session.
For invites where the user is small enough to persist to the cookie,
this will still save both the old and the new way, but will always make
an extra check to the API, I think this minor performance hit is totally
fine. For invites where the user is too big to persist, they'll still
fail for now, and will need to wait until the next PR comes along and
stops saving the large invited user object to the session entirely.
2021-03-08 10:01:12 +00:00
|
|
|
def get_invited_user(self, invited_user_id):
|
|
|
|
|
return self.get(
|
2023-07-12 12:09:44 -04:00
|
|
|
f'/invite/organization/{invited_user_id}'
|
store invited org user ids in session
first of a two step process to remove invited user objects from the
session. we're removing them because they're of variable size, and with
a lot of folder permissions they can cause the session to exceed the 4kb
cookie size limit and not save properly.
this commit looks at invited org users only.
in this step, start saving the invited org user's id to the
session alongside the session object. Then, if the invited_org_user_id
is present in the next step of the invite flow, fetch the user object
from the API instead of from the session. If it's not present (due to a
session set by an older instance of the admin app), then just use the
old code to get the entire object out of the session.
For invites where the user is small enough to persist to the cookie,
this will still save both the old and the new way, but will always make
an extra check to the API, I think this minor performance hit is totally
fine. For invites where the user is too big to persist, they'll still
fail for now, and will need to wait until the next PR comes along and
stops saving the large invited user object to the session entirely.
2021-03-08 10:01:12 +00:00
|
|
|
)['data']
|
|
|
|
|
|
2018-02-19 16:53:29 +00:00
|
|
|
def check_token(self, token):
|
2023-07-12 12:09:44 -04:00
|
|
|
resp = self.get(url='/invite/organization/check/{}'.format(token))
|
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']
|
2018-02-19 16:53:29 +00:00
|
|
|
|
|
|
|
|
def cancel_invited_user(self, org_id, invited_user_id):
|
|
|
|
|
data = {'status': 'cancelled'}
|
|
|
|
|
data = _attach_current_user(data)
|
2023-07-12 12:09:44 -04:00
|
|
|
self.post(url='/organization/{0}/invite/{1}'.format(org_id, invited_user_id),
|
2018-02-19 16:53:29 +00:00
|
|
|
data=data)
|
|
|
|
|
|
|
|
|
|
def accept_invite(self, org_id, invited_user_id):
|
|
|
|
|
data = {'status': 'accepted'}
|
2023-07-12 12:09:44 -04:00
|
|
|
self.post(url='/organization/{0}/invite/{1}'.format(org_id, invited_user_id),
|
2018-02-19 16:53:29 +00:00
|
|
|
data=data)
|
|
|
|
|
|
2018-10-26 15:39:32 +01:00
|
|
|
|
|
|
|
|
org_invite_api_client = OrgInviteApiClient()
|