Skip to content

Commit

Permalink
Update tests to use proxy model
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheps committed Jan 22, 2024
1 parent df85cc9 commit 1d784cf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 51 deletions.
32 changes: 14 additions & 18 deletions netbox/users/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse

from users.models import ObjectPermission, Token
from users.models import ObjectPermission, Token, NetBoxUser
from utilities.testing import APIViewTestCases, APITestCase, create_test_user
from utilities.utils import deepmerge


User = get_user_model()


class AppTest(APITestCase):

def test_root(self):
Expand All @@ -22,7 +18,7 @@ def test_root(self):


class UserTest(APIViewTestCases.APIViewTestCase):
model = User
model = NetBoxUser
view_namespace = 'users'
brief_fields = ['display', 'id', 'url', 'username']
validation_excluded_fields = ['password']
Expand All @@ -48,11 +44,11 @@ class UserTest(APIViewTestCases.APIViewTestCase):
def setUpTestData(cls):

users = (
User(username='User_1', password='password1'),
User(username='User_2', password='password2'),
User(username='User_3', password='password3'),
NetBoxUser(username='User_1', password='password1'),
NetBoxUser(username='User_2', password='password2'),
NetBoxUser(username='User_3', password='password3'),
)
User.objects.bulk_create(users)
NetBoxUser.objects.bulk_create(users)

def test_that_password_is_changed(self):
"""
Expand All @@ -71,7 +67,7 @@ def test_that_password_is_changed(self):
'username': 'user1',
'password': 'abc123',
}
user = User.objects.create_user(**user_credentials)
user = NetBoxUser.objects.create_user(**user_credentials)

data = {
'password': 'newpassword'
Expand All @@ -82,7 +78,7 @@ def test_that_password_is_changed(self):

self.assertEqual(response.status_code, 200)

updated_user = User.objects.get(id=user.id)
updated_user = NetBoxUser.objects.get(id=user.id)

self.assertTrue(updated_user.check_password(data['password']))

Expand Down Expand Up @@ -177,7 +173,7 @@ def test_provision_token_valid(self):
'username': 'user1',
'password': 'abc123',
}
user = User.objects.create_user(**user_credentials)
user = NetBoxUser.objects.create_user(**user_credentials)

data = {
**user_credentials,
Expand Down Expand Up @@ -216,7 +212,7 @@ def test_provision_token_other_user(self):
ObjectPermission.objects.filter(users=self.user).delete()

self.add_permissions('users.add_token')
user2 = User.objects.create_user(username='testuser2')
user2 = NetBoxUser.objects.create_user(username='testuser2')
data = {
'user': user2.id,
}
Expand Down Expand Up @@ -254,11 +250,11 @@ def setUpTestData(cls):
Group.objects.bulk_create(groups)

users = (
User(username='User 1', is_active=True),
User(username='User 2', is_active=True),
User(username='User 3', is_active=True),
NetBoxUser(username='User 1', is_active=True),
NetBoxUser(username='User 2', is_active=True),
NetBoxUser(username='User 3', is_active=True),
)
User.objects.bulk_create(users)
NetBoxUser.objects.bulk_create(users)

object_type = ContentType.objects.get(app_label='dcim', model='device')

Expand Down
39 changes: 18 additions & 21 deletions netbox/users/tests/test_filtersets.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import datetime

from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
from django.utils.timezone import make_aware

from users import filtersets
from users.models import ObjectPermission, Token
from users.models import ObjectPermission, Token, NetBoxUser
from utilities.testing import BaseFilterSetTests

User = get_user_model()


class UserTestCase(TestCase, BaseFilterSetTests):
queryset = User.objects.all()
queryset = NetBoxUser.objects.all()
filterset = filtersets.UserFilterSet

@classmethod
Expand All @@ -28,40 +25,40 @@ def setUpTestData(cls):
Group.objects.bulk_create(groups)

users = (
User(
NetBoxUser(
username='User1',
first_name='Hank',
last_name='Hill',
email='[email protected]',
is_staff=True,
is_superuser=True
),
User(
NetBoxUser(
username='User2',
first_name='Dale',
last_name='Gribble',
email='[email protected]'
),
User(
NetBoxUser(
username='User3',
first_name='Bill',
last_name='Dauterive',
email='[email protected]'
),
User(
NetBoxUser(
username='User4',
first_name='Jeff',
last_name='Boomhauer',
email='[email protected]'
),
User(
NetBoxUser(
username='User5',
first_name='Debbie',
last_name='Grund',
is_active=False
)
)
User.objects.bulk_create(users)
NetBoxUser.objects.bulk_create(users)

users[0].groups.set([groups[0]])
users[1].groups.set([groups[1]])
Expand Down Expand Up @@ -145,11 +142,11 @@ def setUpTestData(cls):
Group.objects.bulk_create(groups)

users = (
User(username='User1'),
User(username='User2'),
User(username='User3'),
NetBoxUser(username='User1'),
NetBoxUser(username='User2'),
NetBoxUser(username='User3'),
)
User.objects.bulk_create(users)
NetBoxUser.objects.bulk_create(users)

object_types = (
ContentType.objects.get(app_label='dcim', model='site'),
Expand Down Expand Up @@ -192,7 +189,7 @@ def test_group(self):
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_user(self):
users = User.objects.filter(username__in=['User1', 'User2'])
users = NetBoxUser.objects.filter(username__in=['User1', 'User2'])
params = {'user_id': [users[0].pk, users[1].pk]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
params = {'user': [users[0].username, users[1].username]}
Expand Down Expand Up @@ -232,11 +229,11 @@ class TokenTestCase(TestCase, BaseFilterSetTests):
def setUpTestData(cls):

users = (
User(username='User1'),
User(username='User2'),
User(username='User3'),
NetBoxUser(username='User1'),
NetBoxUser(username='User2'),
NetBoxUser(username='User3'),
)
User.objects.bulk_create(users)
NetBoxUser.objects.bulk_create(users)

future_date = make_aware(datetime.datetime(3000, 1, 1))
past_date = make_aware(datetime.datetime(2000, 1, 1))
Expand All @@ -252,7 +249,7 @@ def test_q(self):
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_user(self):
users = User.objects.order_by('id')[:2]
users = NetBoxUser.objects.order_by('id')[:2]
params = {'user_id': [users[0].pk, users[1].pk]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
params = {'user': [users[0].username, users[1].username]}
Expand Down
14 changes: 6 additions & 8 deletions netbox/users/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from django.contrib.auth import get_user_model
from django.test import TestCase


User = get_user_model()
from users.models import NetBoxUser


class UserConfigTest(TestCase):

@classmethod
def setUpTestData(cls):

user = User.objects.create_user(username='testuser')
user = NetBoxUser.objects.create_user(username='testuser')
user.config.data = {
'a': True,
'b': {
Expand All @@ -32,7 +30,7 @@ def setUpTestData(cls):
user.config.save()

def test_get(self):
userconfig = User.objects.get(username='testuser').config
userconfig = NetBoxUser.objects.get(username='testuser').config

# Retrieve root and nested values
self.assertEqual(userconfig.get('a'), True)
Expand All @@ -52,7 +50,7 @@ def test_get(self):
self.assertEqual(userconfig.get('b.foo.x.invalid', 'DEFAULT'), 'DEFAULT')

def test_all(self):
userconfig = User.objects.get(username='testuser').config
userconfig = NetBoxUser.objects.get(username='testuser').config
flattened_data = {
'a': True,
'b.foo': 101,
Expand All @@ -66,7 +64,7 @@ def test_all(self):
self.assertEqual(userconfig.all(), flattened_data)

def test_set(self):
userconfig = User.objects.get(username='testuser').config
userconfig = NetBoxUser.objects.get(username='testuser').config

# Overwrite existing values
userconfig.set('a', 'abc')
Expand Down Expand Up @@ -95,7 +93,7 @@ def test_set(self):
userconfig.set('a.x', 1)

def test_clear(self):
userconfig = User.objects.get(username='testuser').config
userconfig = NetBoxUser.objects.get(username='testuser').config

# Clear existing values
userconfig.clear('a')
Expand Down
6 changes: 2 additions & 4 deletions netbox/users/tests/test_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from dcim.models import Site
from dcim.tables import SiteTable
from users.models import NetBoxUser
from users.preferences import UserPreference
from utilities.testing import TestCase

Expand All @@ -16,9 +17,6 @@
}


User = get_user_model()


class UserPreferencesTest(TestCase):
user_permissions = ['dcim.view_site']

Expand All @@ -42,7 +40,7 @@ def test_userpreference(self):

@override_settings(DEFAULT_USER_PREFERENCES=DEFAULT_USER_PREFERENCES)
def test_default_preferences(self):
user = User.objects.create(username='User 1')
user = NetBoxUser.objects.create(username='User 1')
userconfig = user.config

self.assertEqual(userconfig.data, DEFAULT_USER_PREFERENCES)
Expand Down

0 comments on commit 1d784cf

Please sign in to comment.