2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-30 20:46:47 +00:00

refactor test setup

This commit is contained in:
Matthias Mair 2022-05-20 12:12:32 +02:00
parent 8385080e4c
commit 0d5dea3e6d
18 changed files with 90 additions and 395 deletions

View File

@ -12,10 +12,7 @@ from django.contrib.auth.models import Group
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
class InvenTreeAPITestCase(APITestCase): class UserMixin:
"""
Base class for running InvenTree API tests
"""
# User information # User information
username = 'testuser' username = 'testuser'
@ -53,12 +50,12 @@ class InvenTreeAPITestCase(APITestCase):
self.user.save() self.user.save()
for role in self.roles: for role in self.roles:
self.assignRole(role) self.assignRole(role, self.roles == ['all'])
if self.auto_login: if self.auto_login:
self.client.login(username=self.username, password=self.password) self.client.login(username=self.username, password=self.password)
def assignRole(self, role): def assignRole(self, role, assign_all: bool = False):
""" """
Set the user roles for the registered user Set the user roles for the registered user
""" """
@ -69,20 +66,26 @@ class InvenTreeAPITestCase(APITestCase):
for ruleset in self.group.rule_sets.all(): for ruleset in self.group.rule_sets.all():
if ruleset.name == rule: if ruleset.name == rule or assign_all:
if perm == 'view': if perm == 'view' or assign_all:
ruleset.can_view = True ruleset.can_view = True
elif perm == 'change': elif perm == 'change' or assign_all:
ruleset.can_change = True ruleset.can_change = True
elif perm == 'delete': elif perm == 'delete' or assign_all:
ruleset.can_delete = True ruleset.can_delete = True
elif perm == 'add': elif perm == 'add' or assign_all:
ruleset.can_add = True ruleset.can_add = True
ruleset.save() ruleset.save()
break break
class InvenTreeAPITestCase(UserMixin, APITestCase):
"""
Base class for running InvenTree API tests
"""
def getActions(self, url): def getActions(self, url):
""" """
Return a dict of the 'actions' available at a given endpoint. Return a dict of the 'actions' available at a given endpoint.

View File

@ -3,14 +3,13 @@ Pull rendered copies of the templated
only used for testing the js files! - This file is omited from coverage only used for testing the js files! - This file is omited from coverage
""" """
from django.test import TestCase # pragma: no cover
from django.contrib.auth import get_user_model # pragma: no cover
import os # pragma: no cover import os # pragma: no cover
import pathlib # pragma: no cover import pathlib
from InvenTree.InvenTree.helpers import InvenTreeTestCate # pragma: no cover
class RenderJavascriptFiles(TestCase): # pragma: no cover class RenderJavascriptFiles(InvenTreeTestCate): # pragma: no cover
""" """
A unit test to "render" javascript files. A unit test to "render" javascript files.
@ -18,18 +17,6 @@ class RenderJavascriptFiles(TestCase): # pragma: no cover
we need the fully-rendered files for linting and static tests. we need the fully-rendered files for linting and static tests.
""" """
def setUp(self):
user = get_user_model()
self.user = user.objects.create_user(
username='testuser',
password='testpassword',
email='user@gmail.com',
)
self.client.login(username='testuser', password='testpassword')
def download_file(self, filename, prefix): def download_file(self, filename, prefix):
url = os.path.join(prefix, filename) url = os.path.join(prefix, filename)

View File

@ -14,8 +14,10 @@ from wsgiref.util import FileWrapper
from django.http import StreamingHttpResponse from django.http import StreamingHttpResponse
from django.core.exceptions import ValidationError, FieldError from django.core.exceptions import ValidationError, FieldError
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.test import TestCase
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
from InvenTree.InvenTree.api_tester import UserMixin
import InvenTree.version import InvenTree.version
@ -781,3 +783,7 @@ def inheritors(cls):
subcls.add(child) subcls.add(child)
work.append(child) work.append(child)
return subcls return subcls
class InvenTreeTestCate(UserMixin, TestCase):
pass

View File

@ -2,12 +2,8 @@
from rest_framework import status from rest_framework import status
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.urls import reverse from django.urls import reverse
from InvenTree.InvenTree.helpers import InvenTreeTestCate
from InvenTree.api_tester import InvenTreeAPITestCase from InvenTree.api_tester import InvenTreeAPITestCase
@ -16,7 +12,7 @@ from users.models import RuleSet
from base64 import b64encode from base64 import b64encode
class HTMLAPITests(TestCase): class HTMLAPITests(InvenTreeTestCate):
""" """
Test that we can access the REST API endpoints via the HTML interface. Test that we can access the REST API endpoints via the HTML interface.
@ -24,33 +20,7 @@ class HTMLAPITests(TestCase):
which raised an AssertionError when using the HTML API interface, which raised an AssertionError when using the HTML API interface,
while the regular JSON interface continued to work as expected. while the regular JSON interface continued to work as expected.
""" """
roles = ['all']
def setUp(self):
super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
def test_part_api(self): def test_part_api(self):
url = reverse('api-part-list') url = reverse('api-part-list')

View File

@ -1,12 +1,11 @@
"""Tests for middleware functions""" """Tests for middleware functions"""
from django.test import TestCase
from django.contrib.auth import get_user_model
from django.urls import reverse from django.urls import reverse
from InvenTree.InvenTree.helpers import InvenTreeTestCate
class MiddlewareTests(TestCase):
class MiddlewareTests(InvenTreeTestCate):
"""Test for middleware functions""" """Test for middleware functions"""
def check_path(self, url, code=200, **kwargs): def check_path(self, url, code=200, **kwargs):
@ -14,15 +13,6 @@ class MiddlewareTests(TestCase):
self.assertEqual(response.status_code, code) self.assertEqual(response.status_code, code)
return response return response
def setUp(self):
super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user(username='username', email='user@email.com', password='password')
self.client.login(username='username', password='password')
def test_AuthRequiredMiddleware(self): def test_AuthRequiredMiddleware(self):
"""Test the auth middleware""" """Test the auth middleware"""

View File

@ -5,28 +5,17 @@ Unit tests for the main web views
import re import re
import os import os
from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from InvenTree.InvenTree.helpers import InvenTreeTestCate
class ViewTests(TestCase): class ViewTests(InvenTreeTestCate):
""" Tests for various top-level views """ """ Tests for various top-level views """
username = 'test_user' username = 'test_user'
password = 'test_pass' password = 'test_pass'
def setUp(self):
# Create a user
self.user = get_user_model().objects.create_user(self.username, 'user@email.com', self.password)
self.user.set_password(self.password)
self.user.save()
result = self.client.login(username=self.username, password=self.password)
self.assertEqual(result, True)
def test_api_doc(self): def test_api_doc(self):
""" Test that the api-doc view works """ """ Test that the api-doc view works """

View File

@ -7,7 +7,6 @@ from unittest import mock
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
import django.core.exceptions as django_exceptions import django.core.exceptions as django_exceptions
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.conf import settings from django.conf import settings
from djmoney.money import Money from djmoney.money import Money
@ -457,18 +456,12 @@ class TestStatus(TestCase):
self.assertEqual(ready.isImportingData(), False) self.assertEqual(ready.isImportingData(), False)
class TestSettings(TestCase): class TestSettings(helpers.InvenTreeTestCate):
""" """
Unit tests for settings Unit tests for settings
""" """
def setUp(self) -> None: superuser = True
self.user_mdl = get_user_model()
# Create a user for auth
user = get_user_model()
self.user = user.objects.create_superuser('testuser1', 'test1@testing.com', 'password1')
self.client.login(username='testuser1', password='password1')
def in_env_context(self, envs={}): def in_env_context(self, envs={}):
"""Patch the env to include the given dict""" """Patch the env to include the given dict"""
@ -574,18 +567,11 @@ class TestSettings(TestCase):
self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321') self.assertEqual(config.get_setting(TEST_ENV_NAME, None), '321')
class TestInstanceName(TestCase): class TestInstanceName(helpers.InvenTreeTestCate):
""" """
Unit tests for instance name Unit tests for instance name
""" """
def setUp(self):
# Create a user for auth
user = get_user_model()
self.user = user.objects.create_superuser('testuser', 'test@testing.com', 'password')
self.client.login(username='testuser', password='password')
def test_instance_name(self): def test_instance_name(self):
# default setting # default setting

View File

@ -2,9 +2,6 @@ from datetime import datetime, timedelta
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from rest_framework.test import APITestCase from rest_framework.test import APITestCase
from rest_framework import status from rest_framework import status
@ -30,25 +27,11 @@ class TestBuildAPI(APITestCase):
'build', 'build',
] ]
def setUp(self): roles = [
# Create a user for auth 'build.change',
user = get_user_model() 'build.add',
self.user = user.objects.create_user('testuser', 'test@testing.com', 'password') 'build.delete',
]
g = Group.objects.create(name='builders')
self.user.groups.add(g)
for rule in g.rule_sets.all():
if rule.name == 'build':
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
g.save()
self.client.login(username='testuser', password='password')
def test_get_build_list(self): def test_get_build_list(self):
""" """

View File

@ -1,18 +1,16 @@
from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from datetime import datetime, timedelta from datetime import datetime, timedelta
from InvenTree.InvenTree.helpers import InvenTreeTestCate
from .models import Build from .models import Build
from stock.models import StockItem from stock.models import StockItem
from InvenTree.status_codes import BuildStatus from InvenTree.status_codes import BuildStatus
class BuildTestSimple(TestCase): class BuildTestSimple(InvenTreeTestCate):
fixtures = [ fixtures = [
'category', 'category',
@ -21,27 +19,11 @@ class BuildTestSimple(TestCase):
'build', 'build',
] ]
def setUp(self): rules = [
# Create a user for auth 'build.change',
user = get_user_model() 'build.add',
user.objects.create_user('testuser', 'test@testing.com', 'password') 'build.delete',
]
self.user = user.objects.get(username='testuser')
g = Group.objects.create(name='builders')
self.user.groups.add(g)
for rule in g.rule_sets.all():
if rule.name == 'build':
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
g.save()
self.client.login(username='testuser', password='password')
def test_build_objects(self): def test_build_objects(self):
# Ensure the Build objects were correctly created # Ensure the Build objects were correctly created
@ -106,7 +88,7 @@ class BuildTestSimple(TestCase):
self.assertEqual(build.status, BuildStatus.CANCELLED) self.assertEqual(build.status, BuildStatus.CANCELLED)
class TestBuildViews(TestCase): class TestBuildViews(InvenTreeTestCate):
""" Tests for Build app views """ """ Tests for Build app views """
fixtures = [ fixtures = [
@ -116,28 +98,15 @@ class TestBuildViews(TestCase):
'build', 'build',
] ]
rules = [
'build.change',
'build.add',
'build.delete',
]
def setUp(self): def setUp(self):
super().setUp() super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user('username', 'user@email.com', 'password')
g = Group.objects.create(name='builders')
self.user.groups.add(g)
for rule in g.rule_sets.all():
if rule.name == 'build':
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
g.save()
self.client.login(username='username', password='password')
# Create a build output for build # 1 # Create a build output for build # 1
self.build = Build.objects.get(pk=1) self.build = Build.objects.get(pk=1)

View File

@ -4,11 +4,10 @@ import json
from datetime import timedelta from datetime import timedelta
from django.test import TestCase, Client from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from django.urls import reverse from django.urls import reverse
from InvenTree.api_tester import InvenTreeAPITestCase from InvenTree.api_tester import InvenTreeAPITestCase
from InvenTree.helpers import str2bool from InvenTree.helpers import InvenTreeTestCate, str2bool
from plugin.models import NotificationUserSetting, PluginConfig from plugin.models import NotificationUserSetting, PluginConfig
from plugin import registry from plugin import registry
@ -18,7 +17,7 @@ from .api import WebhookView
CONTENT_TYPE_JSON = 'application/json' CONTENT_TYPE_JSON = 'application/json'
class SettingsTest(TestCase): class SettingsTest(InvenTreeTestCate):
""" """
Tests for the 'settings' model Tests for the 'settings' model
""" """
@ -27,16 +26,6 @@ class SettingsTest(TestCase):
'settings', 'settings',
] ]
def setUp(self):
user = get_user_model()
self.user = user.objects.create_user('username', 'user@email.com', 'password')
self.user.is_staff = True
self.user.save()
self.client.login(username='username', password='password')
def test_settings_objects(self): def test_settings_objects(self):
# There should be two settings objects in the database # There should be two settings objects in the database

View File

@ -1,12 +1,11 @@
""" Unit tests for Company views (see views.py) """ """ Unit tests for Company views (see views.py) """
from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group from InvenTree.InvenTree.helpers import InvenTreeTestCate
class CompanyViewTestBase(TestCase): class CompanyViewTestBase(InvenTreeTestCate):
fixtures = [ fixtures = [
'category', 'category',
@ -17,33 +16,9 @@ class CompanyViewTestBase(TestCase):
'supplier_part', 'supplier_part',
] ]
def setUp(self): rules = [
super().setUp() 'all',
]
# Create a user
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
class CompanyViewTest(CompanyViewTestBase): class CompanyViewTest(CompanyViewTestBase):
""" """

View File

@ -4,14 +4,12 @@ Unit testing for BOM export functionality
import csv import csv
from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group from InvenTree.InvenTree.helpers import InvenTreeTestCate
class BomExportTest(TestCase): class BomExportTest(InvenTreeTestCate):
fixtures = [ fixtures = [
'category', 'category',
@ -20,33 +18,11 @@ class BomExportTest(TestCase):
'bom', 'bom',
] ]
roles = ['all']
def setUp(self): def setUp(self):
super().setUp() super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
self.url = reverse('bom-download', kwargs={'pk': 100}) self.url = reverse('bom-download', kwargs={'pk': 100})
def test_bom_template(self): def test_bom_template(self):

View File

@ -3,13 +3,14 @@
from allauth.account.models import EmailAddress from allauth.account.models import EmailAddress
from django.conf import settings from django.conf import settings
from django.contrib.auth import get_user_model
from django.test import TestCase from django.test import TestCase
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
import os import os
from InvenTree.InvenTree.helpers import InvenTreeTestCate
from .models import Part, PartCategory, PartCategoryStar, PartStar, PartTestTemplate from .models import Part, PartCategory, PartCategoryStar, PartStar, PartTestTemplate
from .models import rename_part_image from .models import rename_part_image
from .templatetags import inventree_extras from .templatetags import inventree_extras
@ -21,15 +22,9 @@ from common.models import InvenTreeSetting, InvenTreeUserSetting, NotificationEn
from common.notifications import storage, UIMessageNotification from common.notifications import storage, UIMessageNotification
class TemplateTagTest(TestCase): class TemplateTagTest(InvenTreeTestCate):
""" Tests for the custom template tag code """ """ Tests for the custom template tag code """
def setUp(self):
# Create a user for auth
user = get_user_model()
self.user = user.objects.create_user('testuser', 'test@testing.com', 'password')
self.client.login(username='testuser', password='password')
def test_define(self): def test_define(self):
self.assertEqual(int(inventree_extras.define(3)), 3) self.assertEqual(int(inventree_extras.define(3)), 3)
@ -330,24 +325,13 @@ class TestTemplateTest(TestCase):
self.assertEqual(variant.getTestTemplates().count(), n + 1) self.assertEqual(variant.getTestTemplates().count(), n + 1)
class PartSettingsTest(TestCase): class PartSettingsTest(InvenTreeTestCate):
""" """
Tests to ensure that the user-configurable default values work as expected. Tests to ensure that the user-configurable default values work as expected.
Some fields for the Part model can have default values specified by the user. Some fields for the Part model can have default values specified by the user.
""" """
def setUp(self):
# Create a user for auth
user = get_user_model()
self.user = user.objects.create_user(
username='testuser',
email='test@testing.com',
password='password',
is_staff=True
)
def make_part(self): def make_part(self):
""" """
Helper function to create a simple part Helper function to create a simple part
@ -461,7 +445,7 @@ class PartSettingsTest(TestCase):
Part.objects.create(name='abc', revision='6', description='A part', IPN=' ') Part.objects.create(name='abc', revision='6', description='A part', IPN=' ')
class PartSubscriptionTests(TestCase): class PartSubscriptionTests(InvenTreeTestCate):
fixtures = [ fixtures = [
'location', 'location',
@ -470,15 +454,7 @@ class PartSubscriptionTests(TestCase):
] ]
def setUp(self): def setUp(self):
# Create a user for auth super().setUp()
user = get_user_model()
self.user = user.objects.create_user(
username='testuser',
email='test@testing.com',
password='password',
is_staff=True
)
# electronics / IC / MCU # electronics / IC / MCU
self.category = PartCategory.objects.get(pk=4) self.category = PartCategory.objects.get(pk=4)
@ -578,7 +554,7 @@ class PartSubscriptionTests(TestCase):
self.assertTrue(self.part.is_starred_by(self.user)) self.assertTrue(self.part.is_starred_by(self.user))
class BaseNotificationIntegrationTest(TestCase): class BaseNotificationIntegrationTest(InvenTreeTestCate):
""" Integration test for notifications """ """ Integration test for notifications """
fixtures = [ fixtures = [
@ -589,15 +565,7 @@ class BaseNotificationIntegrationTest(TestCase):
] ]
def setUp(self): def setUp(self):
# Create a user for auth super().setUp()
user = get_user_model()
self.user = user.objects.create_user(
username='testuser',
email='test@testing.com',
password='password',
is_staff=True
)
# Add Mailadress # Add Mailadress
EmailAddress.objects.create(user=self.user, email='test@testing.com') EmailAddress.objects.create(user=self.user, email='test@testing.com')

View File

@ -2,8 +2,6 @@
from django.test import TestCase from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from .models import Part from .models import Part
@ -19,34 +17,11 @@ class PartViewTestCase(TestCase):
'supplier_part', 'supplier_part',
] ]
roles = ['all']
def setUp(self): def setUp(self):
super().setUp() super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
class PartListTest(PartViewTestCase): class PartListTest(PartViewTestCase):
def test_part_index(self): def test_part_index(self):

View File

@ -4,7 +4,6 @@ from django.test import TestCase
from django.conf import settings from django.conf import settings
from django.urls import include, re_path, reverse from django.urls import include, re_path, reverse
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from error_report.models import Error from error_report.models import Error
@ -261,32 +260,8 @@ class PanelMixinTests(TestCase):
'stock', 'stock',
] ]
def setUp(self): roles = ['all']
super().setUp()
# Create a user which has all the privelages
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
def test_installed(self): def test_installed(self):
"""Test that the sample panel plugin is installed""" """Test that the sample panel plugin is installed"""

View File

@ -1,14 +1,13 @@
""" Unit tests for Stock views (see views.py) """ """ Unit tests for Stock views (see views.py) """
from django.test import TestCase
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group from InvenTree.InvenTree.helpers import InvenTreeTestCate
# from common.models import InvenTreeSetting # from common.models import InvenTreeSetting
class StockViewTestCase(TestCase): class StockViewTestCase(InvenTreeTestCate):
fixtures = [ fixtures = [
'category', 'category',
@ -19,36 +18,7 @@ class StockViewTestCase(TestCase):
'stock', 'stock',
] ]
def setUp(self): roles = ['all']
super().setUp()
# Create a user
user = get_user_model()
self.user = user.objects.create_user(
username='username',
email='user@email.com',
password='password'
)
self.user.is_staff = True
self.user.save()
# Put the user into a group with the correct permissions
group = Group.objects.create(name='mygroup')
self.user.groups.add(group)
# Give the group *all* the permissions!
for rule in group.rule_sets.all():
rule.can_view = True
rule.can_change = True
rule.can_add = True
rule.can_delete = True
rule.save()
self.client.login(username='username', password='password')
class StockListTest(StockViewTestCase): class StockListTest(StockViewTestCase):
""" Tests for Stock list views """ """ Tests for Stock list views """

View File

@ -1,9 +1,8 @@
from django.test import TestCase
from django.db.models import Sum from django.db.models import Sum
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
import datetime import datetime
from InvenTree.InvenTree.helpers import InvenTreeTestCate
from InvenTree.status_codes import StockHistoryCode from InvenTree.status_codes import StockHistoryCode
@ -14,7 +13,7 @@ from part.models import Part
from build.models import Build from build.models import Build
class StockTest(TestCase): class StockTest(InvenTreeTestCate):
""" """
Tests to ensure that the stock location tree functions correcly Tests to ensure that the stock location tree functions correcly
""" """
@ -29,6 +28,8 @@ class StockTest(TestCase):
] ]
def setUp(self): def setUp(self):
super().setUp()
# Extract some shortcuts from the fixtures # Extract some shortcuts from the fixtures
self.home = StockLocation.objects.get(name='Home') self.home = StockLocation.objects.get(name='Home')
self.bathroom = StockLocation.objects.get(name='Bathroom') self.bathroom = StockLocation.objects.get(name='Bathroom')
@ -39,14 +40,6 @@ class StockTest(TestCase):
self.drawer2 = StockLocation.objects.get(name='Drawer_2') self.drawer2 = StockLocation.objects.get(name='Drawer_2')
self.drawer3 = StockLocation.objects.get(name='Drawer_3') self.drawer3 = StockLocation.objects.get(name='Drawer_3')
# Create a user
user = get_user_model()
user.objects.create_user('username', 'user@email.com', 'password')
self.client.login(username='username', password='password')
self.user = user.objects.get(username='username')
# Ensure the MPTT objects are correctly rebuild # Ensure the MPTT objects are correctly rebuild
Part.objects.rebuild() Part.objects.rebuild()
StockItem.objects.rebuild() StockItem.objects.rebuild()

View File

@ -1,10 +1,10 @@
from django.test import TestCase from django.test import TestCase
from django.apps import apps from django.apps import apps
from django.urls import reverse from django.urls import reverse
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from InvenTree.InvenTree.helpers import InvenTreeTestCate
from users.models import RuleSet, Owner from users.models import RuleSet, Owner
@ -160,20 +160,11 @@ class RuleSetModelTest(TestCase):
self.assertEqual(group.permissions.count(), 0) self.assertEqual(group.permissions.count(), 0)
class OwnerModelTest(TestCase): class OwnerModelTest(InvenTreeTestCate):
""" """
Some simplistic tests to ensure the Owner model is setup correctly. Some simplistic tests to ensure the Owner model is setup correctly.
""" """
def setUp(self):
""" Add users and groups """
# Create a new user
self.user = get_user_model().objects.create_user('username', 'user@email.com', 'password')
# Put the user into a new group
self.group = Group.objects.create(name='new_group')
self.user.groups.add(self.group)
def do_request(self, endpoint, filters, status_code=200): def do_request(self, endpoint, filters, status_code=200):
response = self.client.get(endpoint, filters, format='json') response = self.client.get(endpoint, filters, format='json')
self.assertEqual(response.status_code, status_code) self.assertEqual(response.status_code, status_code)