Fix notifications not being auto acknowledged

Add tests to confirm this is the case, and fix and edge
case that when notifications engines are incorrectly configured
for a task, to skip them.

Change-Id: Ib715924b4e068e3d0c9a43b55183fe86eb27e38f
This commit is contained in:
Adrian Turjak 2018-02-08 16:07:35 +13:00
parent a0fb0b1339
commit feff951dd9
5 changed files with 106 additions and 10 deletions

View File

@ -450,6 +450,10 @@ class AdminAPITests(APITestCase):
self.assertEqual(response.json(),
{"errors": ["No notification with this id."]})
@modify_dict_settings(TASK_SETTINGS={
'key_list': ['create_project', 'notifications'],
'operation': 'delete',
})
def test_notification_acknowledge(self):
"""
Test that you can acknowledge a notification.
@ -519,6 +523,10 @@ class AdminAPITests(APITestCase):
{'errors':
['No notification with this id.']})
@modify_dict_settings(TASK_SETTINGS={
'key_list': ['create_project', 'notifications'],
'operation': 'delete',
})
def test_notification_re_acknowledge(self):
"""
Test that you cant reacknowledge a notification.
@ -552,6 +560,10 @@ class AdminAPITests(APITestCase):
self.assertEqual(response.json(),
{'notes': ['Notification already acknowledged.']})
@modify_dict_settings(TASK_SETTINGS={
'key_list': ['create_project', 'notifications'],
'operation': 'delete',
})
def test_notification_acknowledge_no_data(self):
"""
Test that you have to include 'acknowledged': True to the request.

View File

@ -170,15 +170,18 @@ def create_notification(task, notes, error=False, engines=True):
class_conf = settings.TASK_SETTINGS.get(
task.task_type, settings.DEFAULT_TASK_SETTINGS)
for note_engine, conf in (class_conf.get('notifications', {})).items():
if error:
conf = conf.get('error', {})
else:
conf = conf.get('standard', {})
if not conf:
continue
engine = settings.NOTIFICATION_ENGINES[note_engine](conf)
engine.notify(task, notification)
notification_conf = class_conf.get('notifications', {})
if notification_conf:
for note_engine, conf in notification_conf.items():
if error:
conf = conf.get('error', {})
else:
conf = conf.get('standard', {})
if not conf:
continue
engine = settings.NOTIFICATION_ENGINES[note_engine](conf)
engine.notify(task, notification)
return notification

View File

@ -115,7 +115,7 @@ class EmailNotification(NotificationEngine):
email.send(fail_silently=False)
if not notification.error:
notification.acknowledge = True
notification.acknowledged = True
notification.save()
except SMTPException as e:
notes = {

View File

View File

@ -0,0 +1,81 @@
# Copyright (C) 2015 Catalyst IT Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from django.core import mail
from rest_framework import status
from adjutant.api.models import Task, Notification
from adjutant.common.tests.fake_clients import (
FakeManager, setup_identity_cache)
from adjutant.common.tests.utils import (
AdjutantAPITestCase, modify_dict_settings)
@mock.patch('adjutant.common.user_store.IdentityManager',
FakeManager)
class NotificationTests(AdjutantAPITestCase):
@modify_dict_settings(TASK_SETTINGS={
'key_list': ['create_project', 'notifications'],
'operation': 'override',
'value': {
'EmailNotification': {
'standard': {
'emails': ['example@example.com'],
'reply': 'no-reply@example.com',
'template': 'notification.txt'
},
'error': {
'emails': ['example@example.com'],
'reply': 'no-reply@example.com',
'template': 'notification.txt'
}
}
}
})
def test_new_project_sends_notification(self):
"""
Confirm that the email notification engine correctly acknowledges
notifications it sends out.
"""
setup_identity_cache()
url = "/v1/actions/CreateProject"
data = {'project_name': "test_project", 'email': "test@example.com"}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
headers = {
'project_name': "test_project",
'project_id': "test_project_id",
'roles': "admin,_member_",
'username': "test@example.com",
'user_id': "test_user_id",
'authenticated': True
}
new_task = Task.objects.all()[0]
url = "/v1/tasks/" + new_task.uuid
response = self.client.post(url, {'approved': True}, format='json',
headers=headers)
self.assertEqual(Notification.objects.count(), 1)
self.assertEqual(len(mail.outbox), 3)
notif = Notification.objects.all()[0]
self.assertEqual(notif.task.uuid, new_task.uuid)
self.assertTrue(notif.acknowledged)