|
8 | 8 | from unittest import mock |
9 | 9 |
|
10 | 10 | from django.contrib.auth import get_user_model |
| 11 | +from django.contrib.auth.models import Group |
11 | 12 | from django.contrib.contenttypes.models import ContentType |
12 | 13 | from django.core.cache import cache |
13 | 14 | from django.core.exceptions import ValidationError |
|
21 | 22 | import PIL |
22 | 23 |
|
23 | 24 | import common.validators |
| 25 | +from common.notifications import trigger_notification |
24 | 26 | from common.settings import get_global_setting, set_global_setting |
25 | 27 | from InvenTree.helpers import str2bool |
26 | 28 | from InvenTree.unit_test import ( |
@@ -1073,6 +1075,7 @@ class NotificationTest(InvenTreeAPITestCase): |
1073 | 1075 | """Tests for NotificationEntry.""" |
1074 | 1076 |
|
1075 | 1077 | fixtures = ['users'] |
| 1078 | + roles = ['admin.view'] |
1076 | 1079 |
|
1077 | 1080 | def test_check_notification_entries(self): |
1078 | 1081 | """Test that notification entries can be created.""" |
@@ -1162,6 +1165,72 @@ def test_bulk_delete(self): |
1162 | 1165 | self.assertEqual(NotificationMessage.objects.count(), 13) |
1163 | 1166 | self.assertEqual(NotificationMessage.objects.filter(user=self.user).count(), 3) |
1164 | 1167 |
|
| 1168 | + def test_simple(self): |
| 1169 | + """Test that a simple notification can be created.""" |
| 1170 | + trigger_notification( |
| 1171 | + Group.objects.get(name='Sales'), |
| 1172 | + user=self.user, |
| 1173 | + data={'message': 'This is a test notification'}, |
| 1174 | + ) |
| 1175 | + |
| 1176 | + def test_with_group(self): |
| 1177 | + """Test that a notification can be created with a group.""" |
| 1178 | + grp = Group.objects.get(name='Sales') |
| 1179 | + trigger_notification( |
| 1180 | + grp, |
| 1181 | + user=self.user, |
| 1182 | + data={'message': 'This is a test notification with group'}, |
| 1183 | + targets=[grp], |
| 1184 | + ) |
| 1185 | + |
| 1186 | + def test_wrong_target(self): |
| 1187 | + """Test that a notification with an invalid target raises an error.""" |
| 1188 | + with self.assertLogs() as cm: |
| 1189 | + trigger_notification( |
| 1190 | + Group.objects.get(name='Sales'), |
| 1191 | + user=self.user, |
| 1192 | + data={'message': 'This is a test notification'}, |
| 1193 | + targets=['invalid_target'], |
| 1194 | + ) |
| 1195 | + self.assertIn('Unknown target passed to t', str(cm[1])) |
| 1196 | + |
| 1197 | + def test_wrong_obj(self): |
| 1198 | + """Test that a object without a reference is raising an issue.""" |
| 1199 | + |
| 1200 | + class SampleObj: |
| 1201 | + pass |
| 1202 | + |
| 1203 | + with self.assertRaises(KeyError) as cm: |
| 1204 | + trigger_notification( |
| 1205 | + SampleObj(), |
| 1206 | + user=self.user, |
| 1207 | + data={'message': 'This is a test notification'}, |
| 1208 | + ) |
| 1209 | + self.assertIn('Could not resolve an object reference for', str(cm.exception)) |
| 1210 | + |
| 1211 | + # Without reference, it should not raise an error |
| 1212 | + trigger_notification( |
| 1213 | + Group.objects.get(name='Sales'), |
| 1214 | + user=self.user, |
| 1215 | + data={'message': 'This is a test notification'}, |
| 1216 | + ) |
| 1217 | + |
| 1218 | + def test_recent(self): |
| 1219 | + """Test that a notification is not created if it was already sent recently.""" |
| 1220 | + grp = Group.objects.get(name='Sales') |
| 1221 | + trigger_notification( # |
| 1222 | + grp, category='core', context={'name': 'test'}, targets=[self.user] |
| 1223 | + ) |
| 1224 | + self.assertEqual(NotificationMessage.objects.count(), 1) |
| 1225 | + |
| 1226 | + # Should not create a new notification |
| 1227 | + with self.assertLogs(logger='inventree') as cm: |
| 1228 | + trigger_notification( |
| 1229 | + grp, category='core', context={'name': 'test'}, targets=[self.user] |
| 1230 | + ) |
| 1231 | + self.assertEqual(NotificationMessage.objects.count(), 1) |
| 1232 | + self.assertIn('as recently been sent for', str(cm[1])) |
| 1233 | + |
1165 | 1234 |
|
1166 | 1235 | class CommonTest(InvenTreeAPITestCase): |
1167 | 1236 | """Tests for the common config.""" |
|
0 commit comments