Add created_by to all appropriate requests.

This commit is contained in:
Nicholas Staples
2016-04-15 11:08:19 +01:00
parent 99d5a3afc7
commit 2085792742
6 changed files with 33 additions and 9 deletions

View File

@@ -0,0 +1,6 @@
from flask.ext.login import current_user
def _attach_current_user(data):
data['created_by'] = current_user.id
return data

View File

@@ -2,6 +2,7 @@ from __future__ import unicode_literals
from flask import url_for
from notifications_python_client.notifications import NotificationsAPIClient
from app.utils import BrowsableItem
from app.notify_client import _attach_current_user
class ServiceAPIClient(NotificationsAPIClient):
@@ -30,6 +31,7 @@ class ServiceAPIClient(NotificationsAPIClient):
"restricted": restricted,
"email_from": email_from
}
_attach_current_user(data)
return self.post("/service", data)['data']['id']
def delete_service(self, service_id):
@@ -37,7 +39,8 @@ class ServiceAPIClient(NotificationsAPIClient):
Delete a service.
"""
endpoint = "/service/{0}".format(service_id)
return self.delete(endpoint)
data = _attach_current_user({})
return self.delete(endpoint, data)
def get_service(self, service_id, *params):
"""
@@ -72,6 +75,7 @@ class ServiceAPIClient(NotificationsAPIClient):
"users": users,
"email_from": email_from
}
_attach_current_user(data)
endpoint = "/service/{0}".format(service_id)
return self.post(endpoint, data)
@@ -82,7 +86,8 @@ class ServiceAPIClient(NotificationsAPIClient):
endpoint = '/service/{service_id}/users/{user_id}'.format(
service_id=service_id,
user_id=user_id)
return self.delete(endpoint)
data = _attach_current_user({})
return self.delete(endpoint, data)
def create_service_template(self, name, type_, content, service_id, subject=None):
"""
@@ -98,6 +103,7 @@ class ServiceAPIClient(NotificationsAPIClient):
data.update({
'subject': subject
})
_attach_current_user(data)
endpoint = "/service/{0}/template".format(service_id)
return self.post(endpoint, data)
@@ -116,6 +122,7 @@ class ServiceAPIClient(NotificationsAPIClient):
data.update({
'subject': subject
})
_attach_current_user(data)
endpoint = "/service/{0}/template/{1}".format(service_id, id_)
return self.post(endpoint, data)
@@ -141,6 +148,7 @@ class ServiceAPIClient(NotificationsAPIClient):
Delete a service template.
"""
endpoint = "/service/{0}/template/{1}".format(service_id, template_id)
data = _attach_current_user({})
return self.delete(endpoint)
def find_all_service_email_from(self, user_id=None):

View File

@@ -1,4 +1,5 @@
from notifications_python_client.base import BaseAPIClient
from app.notify_client import _attach_current_user
class ApiKeyApiClient(BaseAPIClient):
@@ -20,8 +21,12 @@ class ApiKeyApiClient(BaseAPIClient):
def create_api_key(self, service_id, key_name, *params):
data = {"name": key_name}
_attach_current_user(data)
key = self.post(url='/service/{}/api-key'.format(service_id), data=data)
return key['data']
def revoke_api_key(self, service_id, key_id, *params):
return self.post(url='/service/{0}/api-key/revoke/{1}'.format(service_id, key_id), data=None)
data = _attach_current_user({})
return self.post(
url='/service/{0}/api-key/revoke/{1}'.format(service_id, key_id),
data=data)

View File

@@ -21,6 +21,7 @@ class InviteApiClient(BaseAPIClient):
'from_user': invite_from_id,
'permissions': permissions
}
_attach_current_user(data)
resp = self.post(url='/service/{}/invite'.format(service_id), data=data)
return InvitedUser(**resp['data'])
@@ -37,6 +38,7 @@ class InviteApiClient(BaseAPIClient):
def cancel_invited_user(self, service_id, invited_user_id):
data = {'status': 'cancelled'}
_attach_current_user(data)
self.post(url='/service/{0}/invite/{1}'.format(service_id, invited_user_id),
data=data)

View File

@@ -1,5 +1,6 @@
from notifications_python_client.base import BaseAPIClient
from app.notify_client import _attach_current_user
class JobApiClient(BaseAPIClient):
@@ -26,6 +27,6 @@ class JobApiClient(BaseAPIClient):
"original_file_name": original_file_name,
"notification_count": notification_count
}
_attach_current_user(data)
resp = self.post(url='/service/{}/job'.format(service_id), data=data)
return resp['data']

View File

@@ -1,11 +1,10 @@
from app.notify_client.job_api_client import JobApiClient
def test_client_creates_job_data_correctly(mocker):
import uuid
job_id = str(uuid.uuid4())
service_id = str(uuid.uuid4())
template_id = 1
def test_client_creates_job_data_correctly(mocker, fake_uuid):
job_id = fake_uuid
service_id = fake_uuid
template_id = fake_uuid
original_file_name = 'test.csv'
notification_count = 1
@@ -19,6 +18,9 @@ def test_client_creates_job_data_correctly(mocker):
expected_url = '/service/{}/job'.format(service_id)
client = JobApiClient()
mock_attach_user = mocker.patch(
'app.notify_client.job_api_client._attach_current_user',
return_value={'created_by': fake_uuid})
mock_post = mocker.patch('app.notify_client.job_api_client.JobApiClient.post')
client.create_job(job_id, service_id, template_id, original_file_name, notification_count)