1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Introduced a custom TestCase

This commit is contained in:
Jeremy Stretch
2020-01-30 16:03:52 -05:00
parent 1a25f5a7f2
commit 43b2c36066

View File

@ -2,11 +2,44 @@ import logging
from contextlib import contextmanager from contextlib import contextmanager
from django.contrib.auth.models import Permission, User from django.contrib.auth.models import Permission, User
from django.test import Client, TestCase as _TestCase
from rest_framework.test import APITestCase as _APITestCase from rest_framework.test import APITestCase as _APITestCase
from users.models import Token from users.models import Token
class TestCase(_TestCase):
user_permissions = ()
def setUp(self):
# Create the test user and assign permissions
self.user = User.objects.create_user(username='testuser')
self.add_permissions(*self.user_permissions)
# Initialize the test client
self.client = Client()
self.client.force_login(self.user)
def add_permissions(self, *names):
"""
Assign a set of permissions to the test user. Accepts permission names in the form <app>.<action>_<model>.
"""
for name in names:
app, codename = name.split('.')
perm = Permission.objects.get(content_type__app_label=app, codename=codename)
self.user.user_permissions.add(perm)
def remove_permissions(self, *names):
"""
Remove a set of permissions from the test user, if assigned.
"""
for name in names:
app, codename = name.split('.')
perm = Permission.objects.get(content_type__app_label=app, codename=codename)
self.user.user_permissions.remove(perm)
class APITestCase(_APITestCase): class APITestCase(_APITestCase):
def setUp(self): def setUp(self):