From e1ff4b6e873eeef71e9121d8b702c4d9b6458173 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Fri, 20 May 2022 23:01:20 +0200 Subject: [PATCH] more class use --- InvenTree/order/test_views.py | 36 ++++++------------- InvenTree/plugin/base/action/test_action.py | 10 ++---- .../plugin/base/barcodes/test_barcode.py | 11 ++---- .../plugin/base/integration/test_mixins.py | 9 ++--- .../builtin/action/test_simpleactionplugin.py | 13 +++---- .../barcodes/test_inventree_barcode.py | 13 ++----- .../plugin/samples/integration/test_sample.py | 12 ++----- 7 files changed, 28 insertions(+), 76 deletions(-) diff --git a/InvenTree/order/test_views.py b/InvenTree/order/test_views.py index e38ea7ecef..aad0fed25d 100644 --- a/InvenTree/order/test_views.py +++ b/InvenTree/order/test_views.py @@ -1,12 +1,11 @@ """ Unit tests for Order views (see views.py) """ -from django.test import TestCase from django.urls import reverse -from django.contrib.auth import get_user_model -from django.contrib.auth.models import Group + +from InvenTree.helpers import InvenTreeTestCase -class OrderViewTestCase(TestCase): +class OrderViewTestCase(InvenTreeTestCase): fixtures = [ 'category', @@ -19,27 +18,14 @@ class OrderViewTestCase(TestCase): 'order', ] - def setUp(self): - super().setUp() - - # Create a user - user = get_user_model().objects.create_user('username', 'user@email.com', 'password') - - # Ensure that the user has the correct permissions! - g = Group.objects.create(name='orders') - user.groups.add(g) - - for rule in g.rule_sets.all(): - if rule.name in ['purchase_order', 'sales_order']: - rule.can_change = True - rule.can_add = True - rule.can_delete = True - - rule.save() - - g.save() - - self.client.login(username='username', password='password') + roles = [ + 'purchase_order.change', + 'purchase_order.add', + 'purchase_order.delete', + 'sales_order.change', + 'sales_order.add', + 'sales_order.delete', + ] class OrderListTest(OrderViewTestCase): diff --git a/InvenTree/plugin/base/action/test_action.py b/InvenTree/plugin/base/action/test_action.py index 16790ccd62..84482da4f7 100644 --- a/InvenTree/plugin/base/action/test_action.py +++ b/InvenTree/plugin/base/action/test_action.py @@ -1,8 +1,8 @@ """ Unit tests for action plugins """ from django.test import TestCase -from django.contrib.auth import get_user_model +from InvenTree.InvenTree.helpers import InvenTreeTestCase from plugin import InvenTreePlugin from plugin.mixins import ActionMixin @@ -65,15 +65,9 @@ class ActionMixinTests(TestCase): }) -class APITests(TestCase): +class APITests(InvenTreeTestCase): """ Tests for action api """ - def setUp(self): - # Create a user for auth - user = get_user_model() - self.test_user = user.objects.create_user('testuser', 'test@testing.com', 'password') - self.client.login(username='testuser', password='password') - def test_post_errors(self): """Check the possible errors with post""" diff --git a/InvenTree/plugin/base/barcodes/test_barcode.py b/InvenTree/plugin/base/barcodes/test_barcode.py index 43a713a57b..15deb2665c 100644 --- a/InvenTree/plugin/base/barcodes/test_barcode.py +++ b/InvenTree/plugin/base/barcodes/test_barcode.py @@ -4,16 +4,15 @@ Unit tests for Barcode endpoints """ -from django.contrib.auth import get_user_model from django.urls import reverse -from rest_framework.test import APITestCase from rest_framework import status +from InvenTree.InvenTree.api_tester import InvenTreeAPITestCase from stock.models import StockItem -class BarcodeAPITest(APITestCase): +class BarcodeAPITest(InvenTreeAPITestCase): fixtures = [ 'category', @@ -23,11 +22,7 @@ class BarcodeAPITest(APITestCase): ] def setUp(self): - # Create a user for auth - user = get_user_model() - user.objects.create_user('testuser', 'test@testing.com', 'password') - - self.client.login(username='testuser', password='password') + super().setUp() self.scan_url = reverse('api-barcode-scan') self.assign_url = reverse('api-barcode-link') diff --git a/InvenTree/plugin/base/integration/test_mixins.py b/InvenTree/plugin/base/integration/test_mixins.py index f60e6a2802..9759e0f00d 100644 --- a/InvenTree/plugin/base/integration/test_mixins.py +++ b/InvenTree/plugin/base/integration/test_mixins.py @@ -1,7 +1,6 @@ """ Unit tests for base mixins for plugins """ from django.conf import settings -from django.contrib.auth import get_user_model from django.test import TestCase from django.urls import include, re_path, reverse @@ -24,7 +23,7 @@ class BaseMixinDefinition: self.assertIn(self.MIXIN_HUMAN_NAME, [item['human_name'] for item in self.mixin.registered_mixins]) -class SettingsMixinTest(BaseMixinDefinition, TestCase): +class SettingsMixinTest(BaseMixinDefinition, InvenTreeTestCase): MIXIN_HUMAN_NAME = 'Settings' MIXIN_NAME = 'settings' MIXIN_ENABLE_CHECK = 'has_settings' @@ -40,9 +39,7 @@ class SettingsMixinTest(BaseMixinDefinition, TestCase): pass self.mixin_nothing = NoSettingsCls() - user = get_user_model() - self.test_user = user.objects.create_user('testuser', 'test@testing.com', 'password') - self.test_user.is_staff = True + super().setUp() def test_function(self): # settings variable @@ -54,7 +51,7 @@ class SettingsMixinTest(BaseMixinDefinition, TestCase): self.assertEqual(self.mixin_nothing.get_setting('ABCD'), '') # right setting - self.mixin.set_setting('SETTING1', '12345', self.test_user) + self.mixin.set_setting('SETTING1', '12345', self.user) self.assertEqual(self.mixin.get_setting('SETTING1'), '12345') # no setting diff --git a/InvenTree/plugin/builtin/action/test_simpleactionplugin.py b/InvenTree/plugin/builtin/action/test_simpleactionplugin.py index 92e1affa67..0cadc27d9f 100644 --- a/InvenTree/plugin/builtin/action/test_simpleactionplugin.py +++ b/InvenTree/plugin/builtin/action/test_simpleactionplugin.py @@ -1,20 +1,15 @@ """ Unit tests for action plugins """ -from django.test import TestCase -from django.contrib.auth import get_user_model - +from InvenTree.InvenTree.helpers import InvenTreeTestCase from plugin.builtin.action.simpleactionplugin import SimpleActionPlugin -class SimpleActionPluginTests(TestCase): +class SimpleActionPluginTests(InvenTreeTestCase): """ Tests for SampleIntegrationPlugin """ def setUp(self): - # Create a user for auth - user = get_user_model() - self.test_user = user.objects.create_user('testuser', 'test@testing.com', 'password') + super().setUp() - self.client.login(username='testuser', password='password') self.plugin = SimpleActionPlugin() def test_name(self): @@ -33,7 +28,7 @@ class SimpleActionPluginTests(TestCase): "action": 'simple', "result": True, "info": { - "user": "testuser", + "user": self.username, "hello": "world", }, } diff --git a/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py b/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py index 1424d89ff2..0c1a91ed6a 100644 --- a/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py +++ b/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py @@ -1,14 +1,14 @@ # -*- coding: utf-8 -*- """Unit tests for InvenTreeBarcodePlugin""" -from django.contrib.auth import get_user_model from django.urls import reverse -from rest_framework.test import APITestCase from rest_framework import status +from InvenTree.InvenTree.api_tester import InvenTreeAPITestCase -class TestInvenTreeBarcode(APITestCase): + +class TestInvenTreeBarcode(InvenTreeAPITestCase): fixtures = [ 'category', @@ -17,13 +17,6 @@ class TestInvenTreeBarcode(APITestCase): 'stock' ] - def setUp(self): - # Create a user for auth - user = get_user_model() - user.objects.create_user('testuser', 'test@testing.com', 'password') - - self.client.login(username='testuser', password='password') - def test_errors(self): """ Test all possible error cases for assigment action diff --git a/InvenTree/plugin/samples/integration/test_sample.py b/InvenTree/plugin/samples/integration/test_sample.py index 733e443638..1d477144a3 100644 --- a/InvenTree/plugin/samples/integration/test_sample.py +++ b/InvenTree/plugin/samples/integration/test_sample.py @@ -1,19 +1,11 @@ """ Unit tests for action plugins """ -from django.test import TestCase -from django.contrib.auth import get_user_model +from InvenTree.InvenTree.helpers import InvenTreeTestCase -class SampleIntegrationPluginTests(TestCase): +class SampleIntegrationPluginTests(InvenTreeTestCase): """ Tests for SampleIntegrationPlugin """ - def setUp(self): - # Create a user for auth - user = get_user_model() - user.objects.create_user('testuser', 'test@testing.com', 'password') - - self.client.login(username='testuser', password='password') - def test_view(self): """check the function of the custom sample plugin """ response = self.client.get('/plugin/sample/ho/he/')