Skip to content

Commit

Permalink
Add message_project_team endpoint (#479)
Browse files Browse the repository at this point in the history
* Add message_project_team api endpoint
* Add message_project_team to orchestra_api
* Reuse message_experts_slack_group
  • Loading branch information
jumasheff committed Sep 19, 2018
1 parent e1693a3 commit 95a6c2c
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions orchestra/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from orchestra.project_api.views import project_details_url
from orchestra.project_api.views import project_information
from orchestra.project_api.views import workflow_types
from orchestra.project_api.views import message_project_team
from orchestra.views import TimeEntryDetail
from orchestra.views import TimeEntryList
from orchestra.views import dashboard_tasks
Expand Down Expand Up @@ -79,4 +80,7 @@
url(r'^status/$',
status,
name='status'),
url(r'^project/message_project_team',
message_project_team,
name='message_project_team'),
]
10 changes: 10 additions & 0 deletions orchestra/orchestra_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ def assign_worker_to_task(worker_id, task_id):
response = _make_api_request('post', 'assign_worker_to_task',
data=json.dumps(data))
return json.loads(response.text)


def message_project_team(project_id, message):
data = {
'project_id': project_id,
'message': message
}
response = _make_api_request('post', 'message_project_team',
data=json.dumps(data))
return json.loads(response.text)
35 changes: 35 additions & 0 deletions orchestra/project_api/tests/test_project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from orchestra.project_api.auth import SignedUser
from orchestra.tests.helpers import OrchestraTestCase
from orchestra.tests.helpers.fixtures import setup_models
from orchestra.tests.helpers.fixtures import ProjectFactory
from orchestra.tests.helpers.google_apps import mock_create_drive_service
from orchestra.utils.load_json import load_encoded_json
from orchestra.utils.task_lifecycle import get_new_task_assignment
Expand Down Expand Up @@ -378,6 +379,40 @@ def test_assign_worker_to_task(self):
self.assertTrue('task_assignment_error' in data['errors'])
self.assertFalse(query.exists())

@patch('orchestra.project_api.views.message_experts_slack_group')
def test_message_project_team(self, mock_message_slack_group):
project = ProjectFactory(slack_group_id='test-project-1')
url = '/orchestra/api/project/message_project_team/'
test_message = 'this is a test message'
response = self.api_client.post(
url,
{'message': test_message, 'project_id': project.id},
format='json')
self.assertEqual(response.status_code, 200)
self.assertTrue(mock_message_slack_group.called)
# No project id provided
response = self.api_client.post(
url, {'message': test_message}, format='json')
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.json()['message'],
('An object with `message` and `project_id` attributes'
' should be supplied'))
# No message provided
response = self.api_client.post(
url, {'project_id': project.id}, format='json')
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.json()['message'],
('An object with `message` and `project_id` attributes'
' should be supplied'))
# Non-existent project_id provided
response = self.api_client.post(
url, {'message': 'text', 'project_id': 123}, format='json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json()['message'],
'No project for given id')

def test_permissions(self):
self.api_client.force_authenticate(user=AnonymousUser())

Expand Down
32 changes: 32 additions & 0 deletions orchestra/project_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from orchestra.utils.decorators import api_endpoint
from orchestra.utils.load_json import load_encoded_json
from orchestra.utils.task_lifecycle import assign_task
from orchestra.utils.notifications import message_experts_slack_group
from orchestra.project_api.auth import OrchestraProjectAPIAuthentication
from orchestra.project_api.auth import IsSignedUser

Expand Down Expand Up @@ -124,3 +125,34 @@ def assign_worker_to_task(request):
'success': success,
'errors': errors,
}


@api_endpoint(methods=['POST'],
permissions=(IsSignedUser,),
logger=logger,
auths=(OrchestraProjectAPIAuthentication,))
def message_project_team(request):
"""
Endpoint for sending arbitrary message to a project team.
Payload example:
{'message': 'Chat message', 'project_id': 'some-id-123'}
"""
data = load_encoded_json(request.body)
try:
message = data['message']
project_id = data['project_id']
project = Project.objects.get(id=project_id)
except KeyError:
text = ('An object with `message` and `project_id` attributes'
' should be supplied')
raise BadRequest(text)
except Project.DoesNotExist:
raise BadRequest('No project for given id')
if project.slack_group_id:
message_experts_slack_group(project.slack_group_id, message)
else:
error_message = (
"The following project doesn't have slack_group_id: {}"
).format(project)
raise BadRequest(error_message)
return {'success': True}

0 comments on commit 95a6c2c

Please sign in to comment.