mirror of
https://github.com/inventree/InvenTree.git
synced 2026-11-24 16:57:30 +00:00
Code structure refactor (#5582)
* moved docker files to /contrib/container * changed code owners to make more precise * updated CI to use new subdirs * added manual trigger for testing * moved ci files * moved assets into subdir * moved deploy template file to contrib * moved django files to src/backend * updated paths in scripts etc * updated reqs path * fixed version file path * fixed flake8 path * fixed path to node ressources * fixed task paths * added dep path for node * removed unused yarn lockfile * removed unused ci script * updated internal backend paths for tasks * updated translation stats path * fixed source path for coverage * fixed main commit repo path * fit in changes from testing * gather packager improvements (#149) * Matmair/issue5578 (#143) * moved docker files to /contrib/container * changed code owners to make more precise * updated CI to use new subdirs * added manual trigger for testing * moved ci files * moved assets into subdir * moved deploy template file to contrib * moved django files to src/backend * updated paths in scripts etc * updated reqs path * fixed version file path * fixed flake8 path * fixed path to node ressources * fixed task paths * added dep path for node * removed unused yarn lockfile * removed unused ci script * updated internal backend paths for tasks * updated translation stats path * fixed source path for coverage * fixed main commit repo path * fix docker path * use project dir * move project dir command * fixed docker paths * another fix? * seperate tasks out * remove tasks * some debugging * ci: add .deepsource.toml * Update .deepsource.toml * also ignore migrations * more debugging * fix path issues * remove debug script * fix style * change locale path * Fixed paths for requirements * Added dummy requirements to fool packager * fixed exec path * remove deepsource --------- Co-authored-by: deepsource-io[bot] <42547082+deepsource-io[bot]@users.noreply.github.com> * Added docs for file structure * Fixed style errors * updated deepsource paths * fix deepsource paths * fixed reqs * merge fixes * move newly added dirs too * fix reqs files * another dep fix * merge upstream/master * revert removal of tags * merge upstream * enabled detection of old config files * adapt coverage src * also detect and support old location for plugins.txt * style fix * fix ~/init.sh location * fix requirements path * fix config to current master * move new folders * fix import order * fix paths for qc_check * fix docs build * fix fix path * set docker project dir * just use a cd * set image path? * set file correct * fix copy path * fix tasks dir * fix init path * fix copy path * set prject dir * fix paths * remove old prod files * fix dev env path * set docker file * Fix devcontainer docker compose file * fix login attempt values * fix init.sh path * Fix pathing for Docker * Docker build fix - Set INVENTREE_BACKEND_DIR separately * Update init.sh * Fix path * Update requirements.txt * merge * fix rq merge * fix docker compose usage --------- Co-authored-by: deepsource-io[bot] <42547082+deepsource-io[bot]@users.noreply.github.com> Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
3317 files changed
+532
-1194
No files matched your search
@@ -0,0 +1,158 @@
|
||||
"""API tests for various user / auth API endpoints."""
|
||||
|
||||
import datetime
|
||||
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.urls import reverse
|
||||
|
||||
from InvenTree.unit_test import InvenTreeAPITestCase
|
||||
from users.models import ApiToken
|
||||
|
||||
|
||||
class UserAPITests(InvenTreeAPITestCase):
|
||||
"""Tests for user API endpoints."""
|
||||
|
||||
def test_user_api(self):
|
||||
"""Tests for User API endpoints."""
|
||||
response = self.get(reverse('api-user-list'), expected_code=200)
|
||||
|
||||
# Check the correct number of results was returned
|
||||
self.assertEqual(len(response.data), User.objects.count())
|
||||
|
||||
for key in ['username', 'pk', 'email']:
|
||||
self.assertIn(key, response.data[0])
|
||||
|
||||
# Check detail URL
|
||||
pk = response.data[0]['pk']
|
||||
|
||||
response = self.get(
|
||||
reverse('api-user-detail', kwargs={'pk': pk}), expected_code=200
|
||||
)
|
||||
|
||||
self.assertIn('pk', response.data)
|
||||
self.assertIn('username', response.data)
|
||||
|
||||
def test_group_api(self):
|
||||
"""Tests for the Group API endpoints."""
|
||||
response = self.get(reverse('api-group-list'), expected_code=200)
|
||||
|
||||
self.assertIn('name', response.data[0])
|
||||
|
||||
self.assertEqual(len(response.data), Group.objects.count())
|
||||
|
||||
# Check detail URL
|
||||
response = self.get(
|
||||
reverse('api-group-detail', kwargs={'pk': response.data[0]['pk']}),
|
||||
expected_code=200,
|
||||
)
|
||||
|
||||
self.assertIn('name', response.data)
|
||||
|
||||
|
||||
class UserTokenTests(InvenTreeAPITestCase):
|
||||
"""Tests for user token functionality."""
|
||||
|
||||
def test_token_generation(self):
|
||||
"""Test user token generation."""
|
||||
url = reverse('api-token')
|
||||
|
||||
self.assertEqual(ApiToken.objects.count(), 0)
|
||||
|
||||
# Generate multiple tokens with different names
|
||||
for name in ['cat', 'dog', 'biscuit']:
|
||||
data = self.get(url, data={'name': name}, expected_code=200).data
|
||||
|
||||
self.assertTrue(data['token'].startswith('inv-'))
|
||||
self.assertEqual(data['name'], name)
|
||||
|
||||
# Check that the tokens were created
|
||||
self.assertEqual(ApiToken.objects.count(), 3)
|
||||
|
||||
# If we re-generate a token, the value changes
|
||||
token = ApiToken.objects.filter(name='cat').first()
|
||||
|
||||
# Request the token with the same name
|
||||
data = self.get(url, data={'name': 'cat'}, expected_code=200).data
|
||||
|
||||
self.assertEqual(data['token'], token.key)
|
||||
|
||||
self.assertEqual(ApiToken.objects.count(), 3)
|
||||
|
||||
# Revoke the token, and then request again
|
||||
token.revoked = True
|
||||
token.save()
|
||||
|
||||
data = self.get(url, data={'name': 'cat'}, expected_code=200).data
|
||||
|
||||
self.assertNotEqual(data['token'], token.key)
|
||||
|
||||
# A new token has been generated
|
||||
self.assertEqual(ApiToken.objects.count(), 4)
|
||||
|
||||
# Test with a really long name
|
||||
data = self.get(url, data={'name': 'cat' * 100}, expected_code=200).data
|
||||
|
||||
# Name should be truncated
|
||||
self.assertEqual(len(data['name']), 100)
|
||||
|
||||
token.refresh_from_db()
|
||||
|
||||
# Check that the metadata has been updated
|
||||
keys = [
|
||||
'user_agent',
|
||||
'remote_addr',
|
||||
'remote_host',
|
||||
'remote_user',
|
||||
'server_name',
|
||||
'server_port',
|
||||
]
|
||||
|
||||
for k in keys:
|
||||
self.assertIn(k, token.metadata)
|
||||
|
||||
def test_token_auth(self):
|
||||
"""Test user token authentication."""
|
||||
# Create a new token
|
||||
token_key = self.get(
|
||||
url=reverse('api-token'), data={'name': 'test'}, expected_code=200
|
||||
).data['token']
|
||||
|
||||
# Check that we can use the token to authenticate
|
||||
self.client.logout()
|
||||
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token_key)
|
||||
|
||||
me = reverse('api-user-me')
|
||||
|
||||
response = self.client.get(me, expected_code=200)
|
||||
|
||||
# Grab the token, and update
|
||||
token = ApiToken.objects.first()
|
||||
self.assertEqual(token.key, token_key)
|
||||
self.assertIsNotNone(token.last_seen)
|
||||
|
||||
# Revoke the token
|
||||
token.revoked = True
|
||||
token.save()
|
||||
|
||||
self.assertFalse(token.active)
|
||||
|
||||
response = self.client.get(me, expected_code=401)
|
||||
self.assertIn('Token has been revoked', str(response.data))
|
||||
|
||||
# Expire the token
|
||||
token.revoked = False
|
||||
token.expiry = datetime.datetime.now().date() - datetime.timedelta(days=10)
|
||||
token.save()
|
||||
|
||||
self.assertTrue(token.expired)
|
||||
self.assertFalse(token.active)
|
||||
|
||||
response = self.client.get(me, expected_code=401)
|
||||
self.assertIn('Token has expired', str(response.data))
|
||||
|
||||
# Re-enable the token
|
||||
token.revoked = False
|
||||
token.expiry = datetime.datetime.now().date() + datetime.timedelta(days=10)
|
||||
token.save()
|
||||
|
||||
self.client.get(me, expected_code=200)
|
||||
Reference in new issue
Block a user