|
|
@@ -1,4 +1,3 @@
|
|
|
-from django.contrib.auth import get_user_model
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
from django.urls import reverse
|
|
|
|
|
|
@@ -6,10 +5,9 @@ from core.models import ObjectType
|
|
|
from dcim.models import DeviceType, Manufacturer, Site
|
|
|
from extras.choices import *
|
|
|
from extras.models import *
|
|
|
+from users.models import Group, User
|
|
|
from utilities.testing import ViewTestCases, TestCase
|
|
|
|
|
|
-User = get_user_model()
|
|
|
-
|
|
|
|
|
|
class CustomFieldTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
|
model = CustomField
|
|
|
@@ -620,3 +618,166 @@ class CustomLinkTest(TestCase):
|
|
|
response = self.client.get(site.get_absolute_url(), follow=True)
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
self.assertIn(f'FOO {site.name} BAR', str(response.content))
|
|
|
+
|
|
|
+
|
|
|
+class SubscriptionTestCase(
|
|
|
+ ViewTestCases.CreateObjectViewTestCase,
|
|
|
+ ViewTestCases.DeleteObjectViewTestCase,
|
|
|
+ ViewTestCases.ListObjectsViewTestCase,
|
|
|
+ ViewTestCases.BulkDeleteObjectsViewTestCase
|
|
|
+):
|
|
|
+ model = Subscription
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ site_ct = ContentType.objects.get_for_model(Site)
|
|
|
+ sites = (
|
|
|
+ Site(name='Site 1', slug='site-1'),
|
|
|
+ Site(name='Site 2', slug='site-2'),
|
|
|
+ Site(name='Site 3', slug='site-3'),
|
|
|
+ Site(name='Site 4', slug='site-4'),
|
|
|
+ )
|
|
|
+ Site.objects.bulk_create(sites)
|
|
|
+
|
|
|
+ cls.form_data = {
|
|
|
+ 'object_type': site_ct.pk,
|
|
|
+ 'object_id': sites[3].pk,
|
|
|
+ }
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ super().setUp()
|
|
|
+
|
|
|
+ sites = Site.objects.all()
|
|
|
+ user = self.user
|
|
|
+
|
|
|
+ subscriptions = (
|
|
|
+ Subscription(object=sites[0], user=user),
|
|
|
+ Subscription(object=sites[1], user=user),
|
|
|
+ Subscription(object=sites[2], user=user),
|
|
|
+ )
|
|
|
+ Subscription.objects.bulk_create(subscriptions)
|
|
|
+
|
|
|
+ def _get_url(self, action, instance=None):
|
|
|
+ if action == 'list':
|
|
|
+ return reverse('account:subscriptions')
|
|
|
+ return super()._get_url(action, instance)
|
|
|
+
|
|
|
+ def test_list_objects_anonymous(self):
|
|
|
+ self.client.logout()
|
|
|
+ url = reverse('account:subscriptions')
|
|
|
+ login_url = reverse('login')
|
|
|
+ self.assertRedirects(self.client.get(url), f'{login_url}?next={url}')
|
|
|
+
|
|
|
+ def test_list_objects_with_permission(self):
|
|
|
+ return
|
|
|
+
|
|
|
+ def test_list_objects_with_constrained_permission(self):
|
|
|
+ return
|
|
|
+
|
|
|
+
|
|
|
+class NotificationGroupTestCase(ViewTestCases.PrimaryObjectViewTestCase):
|
|
|
+ model = NotificationGroup
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ users = (
|
|
|
+ User(username='User 1'),
|
|
|
+ User(username='User 2'),
|
|
|
+ User(username='User 3'),
|
|
|
+ )
|
|
|
+ User.objects.bulk_create(users)
|
|
|
+ groups = (
|
|
|
+ Group(name='Group 1'),
|
|
|
+ Group(name='Group 2'),
|
|
|
+ Group(name='Group 3'),
|
|
|
+ )
|
|
|
+ Group.objects.bulk_create(groups)
|
|
|
+
|
|
|
+ notification_groups = (
|
|
|
+ NotificationGroup(name='Notification Group 1'),
|
|
|
+ NotificationGroup(name='Notification Group 2'),
|
|
|
+ NotificationGroup(name='Notification Group 3'),
|
|
|
+ )
|
|
|
+ NotificationGroup.objects.bulk_create(notification_groups)
|
|
|
+ for i, notification_group in enumerate(notification_groups):
|
|
|
+ notification_group.users.add(users[i])
|
|
|
+ notification_group.groups.add(groups[i])
|
|
|
+
|
|
|
+ cls.form_data = {
|
|
|
+ 'name': 'Notification Group X',
|
|
|
+ 'description': 'Blah',
|
|
|
+ 'users': [users[0].pk, users[1].pk],
|
|
|
+ 'groups': [groups[0].pk, groups[1].pk],
|
|
|
+ }
|
|
|
+
|
|
|
+ cls.csv_data = (
|
|
|
+ 'name,description,users,groups',
|
|
|
+ 'Notification Group 4,Foo,"User 1,User 2","Group 1,Group 2"',
|
|
|
+ 'Notification Group 5,Bar,"User 1,User 2","Group 1,Group 2"',
|
|
|
+ 'Notification Group 6,Baz,"User 1,User 2","Group 1,Group 2"',
|
|
|
+ )
|
|
|
+
|
|
|
+ cls.csv_update_data = (
|
|
|
+ "id,name",
|
|
|
+ f"{notification_groups[0].pk},Notification Group 7",
|
|
|
+ f"{notification_groups[1].pk},Notification Group 8",
|
|
|
+ f"{notification_groups[2].pk},Notification Group 9",
|
|
|
+ )
|
|
|
+
|
|
|
+ cls.bulk_edit_data = {
|
|
|
+ 'description': 'New description',
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+class NotificationTestCase(
|
|
|
+ ViewTestCases.DeleteObjectViewTestCase,
|
|
|
+ ViewTestCases.ListObjectsViewTestCase,
|
|
|
+ ViewTestCases.BulkDeleteObjectsViewTestCase
|
|
|
+):
|
|
|
+ model = Notification
|
|
|
+
|
|
|
+ @classmethod
|
|
|
+ def setUpTestData(cls):
|
|
|
+ site_ct = ContentType.objects.get_for_model(Site)
|
|
|
+ sites = (
|
|
|
+ Site(name='Site 1', slug='site-1'),
|
|
|
+ Site(name='Site 2', slug='site-2'),
|
|
|
+ Site(name='Site 3', slug='site-3'),
|
|
|
+ Site(name='Site 4', slug='site-4'),
|
|
|
+ )
|
|
|
+ Site.objects.bulk_create(sites)
|
|
|
+
|
|
|
+ cls.form_data = {
|
|
|
+ 'object_type': site_ct.pk,
|
|
|
+ 'object_id': sites[3].pk,
|
|
|
+ }
|
|
|
+
|
|
|
+ def setUp(self):
|
|
|
+ super().setUp()
|
|
|
+
|
|
|
+ sites = Site.objects.all()
|
|
|
+ user = self.user
|
|
|
+
|
|
|
+ notifications = (
|
|
|
+ Notification(object=sites[0], user=user),
|
|
|
+ Notification(object=sites[1], user=user),
|
|
|
+ Notification(object=sites[2], user=user),
|
|
|
+ )
|
|
|
+ Notification.objects.bulk_create(notifications)
|
|
|
+
|
|
|
+ def _get_url(self, action, instance=None):
|
|
|
+ if action == 'list':
|
|
|
+ return reverse('account:notifications')
|
|
|
+ return super()._get_url(action, instance)
|
|
|
+
|
|
|
+ def test_list_objects_anonymous(self):
|
|
|
+ self.client.logout()
|
|
|
+ url = reverse('account:notifications')
|
|
|
+ login_url = reverse('login')
|
|
|
+ self.assertRedirects(self.client.get(url), f'{login_url}?next={url}')
|
|
|
+
|
|
|
+ def test_list_objects_with_permission(self):
|
|
|
+ return
|
|
|
+
|
|
|
+ def test_list_objects_with_constrained_permission(self):
|
|
|
+ return
|