2022-06-01 15:37:39 +00:00
|
|
|
"""Unit tests for Stock views (see views.py)."""
|
2019-08-09 10:13:23 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
from django.contrib.auth.models import Group
|
2019-08-09 10:13:23 +00:00
|
|
|
from django.urls import reverse
|
2022-05-20 10:12:32 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
from common.models import InvenTreeSetting
|
|
|
|
from InvenTree.status_codes import StockStatus
|
2023-05-16 21:35:26 +00:00
|
|
|
from InvenTree.unit_test import InvenTreeTestCase
|
2023-01-24 22:28:36 +00:00
|
|
|
from stock.models import StockItem, StockLocation
|
|
|
|
from users.models import Owner
|
2021-01-06 10:00:45 +00:00
|
|
|
|
2019-08-09 10:13:23 +00:00
|
|
|
|
2022-05-20 10:33:51 +00:00
|
|
|
class StockViewTestCase(InvenTreeTestCase):
|
2022-06-01 15:37:39 +00:00
|
|
|
"""Mixin for Stockview tests."""
|
2019-08-09 10:13:23 +00:00
|
|
|
|
|
|
|
fixtures = [
|
|
|
|
'category',
|
|
|
|
'part',
|
|
|
|
'company',
|
|
|
|
'location',
|
|
|
|
'supplier_part',
|
|
|
|
'stock',
|
|
|
|
]
|
|
|
|
|
2022-05-20 15:43:51 +00:00
|
|
|
roles = 'all'
|
2019-08-09 10:13:23 +00:00
|
|
|
|
2022-05-20 10:32:25 +00:00
|
|
|
|
2019-08-09 10:13:23 +00:00
|
|
|
class StockListTest(StockViewTestCase):
|
2022-06-01 15:37:39 +00:00
|
|
|
"""Tests for Stock list views."""
|
2019-08-09 10:13:23 +00:00
|
|
|
|
|
|
|
def test_stock_index(self):
|
2022-06-01 15:37:39 +00:00
|
|
|
"""Test stock index page."""
|
2019-08-09 10:13:23 +00:00
|
|
|
response = self.client.get(reverse('stock-index'))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
|
2022-08-25 10:07:01 +00:00
|
|
|
class StockDetailTest(StockViewTestCase):
|
|
|
|
"""Unit test for the 'stock detail' page"""
|
|
|
|
|
|
|
|
def test_basic_info(self):
|
|
|
|
"""Test that basic stock item info is rendered"""
|
|
|
|
|
|
|
|
url = reverse('stock-item-detail', kwargs={'pk': 1})
|
|
|
|
|
|
|
|
response = self.client.get(url)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
html = str(response.content)
|
|
|
|
|
|
|
|
# Part name
|
|
|
|
self.assertIn('Stock Item: M2x4 LPHS', html)
|
|
|
|
|
|
|
|
# Quantity
|
|
|
|
self.assertIn('<h5>Available Quantity</h5>', html)
|
2022-09-08 05:18:43 +00:00
|
|
|
self.assertIn('<h5>4000', html)
|
2022-08-25 10:07:01 +00:00
|
|
|
|
|
|
|
# Batch code
|
|
|
|
self.assertIn('Batch', html)
|
|
|
|
self.assertIn('<td>B123</td>', html)
|
|
|
|
|
|
|
|
# Actions to check
|
|
|
|
actions = [
|
|
|
|
"id=\\\'stock-count\\\' title=\\\'Count stock\\\'",
|
|
|
|
"id=\\\'stock-add\\\' title=\\\'Add stock\\\'",
|
|
|
|
"id=\\\'stock-remove\\\' title=\\\'Remove stock\\\'",
|
|
|
|
"id=\\\'stock-move\\\' title=\\\'Transfer stock\\\'",
|
|
|
|
"id=\\\'stock-duplicate\\\'",
|
|
|
|
"id=\\\'stock-edit\\\'",
|
|
|
|
"id=\\\'stock-delete\\\'",
|
|
|
|
]
|
|
|
|
|
|
|
|
# Initially we should not have any of the required permissions
|
|
|
|
for act in actions:
|
|
|
|
self.assertNotIn(act, html)
|
|
|
|
|
|
|
|
# Give the user all the permissions
|
|
|
|
self.assignRole('stock.add')
|
|
|
|
self.assignRole('stock.change')
|
|
|
|
self.assignRole('stock.delete')
|
|
|
|
|
|
|
|
response = self.client.get(url)
|
|
|
|
html = str(response.content)
|
|
|
|
|
|
|
|
for act in actions:
|
|
|
|
self.assertIn(act, html)
|
|
|
|
|
|
|
|
|
2020-12-03 12:32:01 +00:00
|
|
|
class StockOwnershipTest(StockViewTestCase):
|
2022-06-01 15:37:39 +00:00
|
|
|
"""Tests for stock ownership views."""
|
2023-01-24 22:28:36 +00:00
|
|
|
test_item_id = 11
|
|
|
|
test_location_id = 1
|
2020-12-03 12:32:01 +00:00
|
|
|
|
|
|
|
def enable_ownership(self):
|
2023-01-24 22:28:36 +00:00
|
|
|
"""Helper function to turn on ownership control."""
|
2020-12-03 12:32:01 +00:00
|
|
|
# Enable stock location ownership
|
|
|
|
|
|
|
|
InvenTreeSetting.set_setting('STOCK_OWNERSHIP_CONTROL', True, self.user)
|
|
|
|
self.assertEqual(True, InvenTreeSetting.get_setting('STOCK_OWNERSHIP_CONTROL'))
|
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
def assert_ownership(self, assertio: bool = True, user=None):
|
|
|
|
"""Helper function to check ownership control."""
|
|
|
|
if user is None:
|
|
|
|
user = self.user
|
|
|
|
|
|
|
|
item = StockItem.objects.get(pk=self.test_item_id)
|
|
|
|
self.assertEqual(assertio, item.check_ownership(user))
|
|
|
|
|
|
|
|
location = StockLocation.objects.get(pk=self.test_location_id)
|
|
|
|
self.assertEqual(assertio, location.check_ownership(user))
|
2020-12-03 16:56:45 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
def assert_api_change(self):
|
|
|
|
"""Helper function to get response to API change."""
|
|
|
|
return self.client.patch(
|
|
|
|
reverse('api-stock-detail', args=(self.test_item_id,)),
|
|
|
|
{'status': StockStatus.DAMAGED},
|
|
|
|
content_type='application/json',
|
|
|
|
)
|
2021-01-12 20:02:44 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
def test_owner_no_ownership(self):
|
|
|
|
"""Check without ownership control enabled. Should always return True."""
|
|
|
|
self.assert_ownership(True)
|
2020-12-03 16:56:45 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
def test_ownership_as_superuser(self):
|
|
|
|
"""Test that superuser are always allowed to access items."""
|
2020-12-03 12:32:01 +00:00
|
|
|
self.enable_ownership()
|
2020-12-03 16:56:45 +00:00
|
|
|
|
2023-01-24 22:28:36 +00:00
|
|
|
# Check with superuser
|
|
|
|
self.user.is_superuser = True
|
|
|
|
self.user.save()
|
|
|
|
self.assert_ownership(True)
|
|
|
|
|
|
|
|
def test_ownership_functions(self):
|
|
|
|
"""Test that ownership is working correctly for StockItem/StockLocation."""
|
|
|
|
self.enable_ownership()
|
|
|
|
item = StockItem.objects.get(pk=self.test_item_id)
|
|
|
|
location = StockLocation.objects.get(pk=self.test_location_id)
|
|
|
|
|
|
|
|
# Check that user is not allowed to change item
|
|
|
|
self.assertTrue(item.check_ownership(self.user)) # No owner -> True
|
|
|
|
self.assertTrue(location.check_ownership(self.user)) # No owner -> True
|
|
|
|
self.assertContains(self.assert_api_change(), 'You do not have permission to perform this action.', status_code=403)
|
|
|
|
|
|
|
|
# Adjust group rules
|
|
|
|
group = Group.objects.get(name='my_test_group')
|
|
|
|
rule = group.rule_sets.get(name='stock')
|
|
|
|
rule.can_change = True
|
|
|
|
rule.save()
|
|
|
|
|
|
|
|
# Set owner to group of user
|
|
|
|
group_owner = Owner.get_owner(group)
|
|
|
|
item.owner = group_owner
|
|
|
|
item.save()
|
|
|
|
location.owner = group_owner
|
|
|
|
location.save()
|
|
|
|
|
|
|
|
# Check that user is allowed to change item
|
|
|
|
self.assertTrue(item.check_ownership(self.user)) # Owner is group -> True
|
|
|
|
self.assertTrue(location.check_ownership(self.user)) # Owner is group -> True
|
|
|
|
self.assertContains(self.assert_api_change(), f'"status":{StockStatus.DAMAGED}', status_code=200)
|
|
|
|
|
|
|
|
# Change group
|
|
|
|
new_group = Group.objects.create(name='new_group')
|
|
|
|
new_group_owner = Owner.get_owner(new_group)
|
|
|
|
item.owner = new_group_owner
|
|
|
|
item.save()
|
|
|
|
location.owner = new_group_owner
|
|
|
|
location.save()
|
|
|
|
|
|
|
|
# Check that user is not allowed to change item
|
|
|
|
self.assertFalse(item.check_ownership(self.user)) # Owner is not in group -> False
|
|
|
|
self.assertFalse(location.check_ownership(self.user)) # Owner is not in group -> False
|