Box management page added
All checks were successful
Build containers when image tags change / build-if-image-changed (., web, containers, main container, git.baumann.gr/adebaumann/labhelper) (push) Successful in 20s
Build containers when image tags change / build-if-image-changed (data-loader, loader, initContainers, init-container, git.baumann.gr/adebaumann/labhelper-data-loader) (push) Successful in 5s

This commit is contained in:
2026-01-01 14:11:33 +01:00
parent 10cc24ff03
commit acde0cb2f8
7 changed files with 683 additions and 9 deletions

View File

@@ -587,7 +587,7 @@ class ThingDetailViewTests(AuthTestCase):
new_box = Box.objects.create(id='BOX002', box_type=self.box_type)
response = self.client.post(
f'/thing/{self.thing.id}/',
{'new_box': 'BOX002'}
{'action': 'move', 'new_box': 'BOX002'}
)
self.assertRedirects(response, f'/thing/{self.thing.id}/')
self.thing.refresh_from_db()
@@ -986,3 +986,327 @@ class LoginViewTests(TestCase):
self.client.login(username='testuser', password='testpass123')
response = self.client.post('/logout/')
self.assertRedirects(response, '/login/')
class BoxManagementViewTests(AuthTestCase):
"""Tests for box management view."""
def setUp(self):
"""Set up test fixtures."""
super().setUp()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
self.box = Box.objects.create(
id='BOX001',
box_type=self.box_type
)
def test_box_management_returns_200(self):
"""Box management page should return 200 status."""
response = self.client.get('/box-management/')
self.assertEqual(response.status_code, 200)
def test_box_management_shows_box_types(self):
"""Box management page should show box types."""
response = self.client.get('/box-management/')
self.assertContains(response, 'Standard Box')
def test_box_management_shows_boxes(self):
"""Box management page should show boxes."""
response = self.client.get('/box-management/')
self.assertContains(response, 'BOX001')
def test_box_management_shows_add_box_type_form(self):
"""Box management page should show add box type form."""
response = self.client.get('/box-management/')
self.assertContains(response, 'Add New Box Type')
self.assertContains(response, 'name="name"')
self.assertContains(response, 'name="width"')
self.assertContains(response, 'name="height"')
self.assertContains(response, 'name="length"')
def test_box_management_shows_add_box_form(self):
"""Box management page should show add box form."""
response = self.client.get('/box-management/')
self.assertContains(response, 'Add New Box')
self.assertContains(response, 'name="id"')
self.assertContains(response, 'name="box_type"')
def test_box_management_requires_login(self):
"""Box management page should redirect to login if not authenticated."""
self.client.logout()
response = self.client.get('/box-management/')
self.assertRedirects(response, '/login/?next=/box-management/')
class BoxTypeCRUDTests(AuthTestCase):
"""Tests for box type CRUD operations."""
def setUp(self):
"""Set up test fixtures."""
super().setUp()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
def test_add_box_type_post_creates_box_type(self):
"""Adding a box type should create it in the database."""
response = self.client.post('/box-type/add/', {
'name': 'Large Box',
'width': '400',
'height': '200',
'length': '600'
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertTrue(BoxType.objects.filter(name='Large Box').exists())
def test_add_box_type_invalid_data(self):
"""Adding a box type with invalid data should not create it."""
response = self.client.post('/box-type/add/', {
'name': 'Invalid Box',
'width': 'invalid',
'height': '100',
'length': '200'
})
self.assertEqual(response.status_code, 302)
self.assertFalse(BoxType.objects.filter(name='Invalid Box').exists())
def test_edit_box_type_updates_box_type(self):
"""Editing a box type should update it in the database."""
response = self.client.post(f'/box-type/{self.box_type.id}/edit/', {
'name': 'Updated Box',
'width': '300',
'height': '150',
'length': '450'
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.box_type.refresh_from_db()
self.assertEqual(self.box_type.name, 'Updated Box')
self.assertEqual(self.box_type.width, 300)
def test_edit_box_type_invalid_data(self):
"""Editing a box type with invalid data should not update it."""
old_name = self.box_type.name
response = self.client.post(f'/box-type/{self.box_type.id}/edit/', {
'name': 'Updated Box',
'width': 'invalid',
'height': '150',
'length': '450'
})
self.assertEqual(response.status_code, 302)
self.box_type.refresh_from_db()
self.assertEqual(self.box_type.name, old_name)
def test_delete_box_type_deletes_box_type(self):
"""Deleting a box type should remove it from the database."""
type_id = self.box_type.id
response = self.client.post(f'/box-type/{type_id}/delete/')
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertFalse(BoxType.objects.filter(id=type_id).exists())
def test_delete_box_type_with_boxes_redirects(self):
"""Deleting a box type with boxes should redirect without deleting."""
Box.objects.create(id='BOX001', box_type=self.box_type)
type_id = self.box_type.id
response = self.client.post(f'/box-type/{type_id}/delete/')
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertTrue(BoxType.objects.filter(id=type_id).exists())
class BoxCRUDTests(AuthTestCase):
"""Tests for box CRUD operations."""
def setUp(self):
"""Set up test fixtures."""
super().setUp()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
self.box = Box.objects.create(
id='BOX001',
box_type=self.box_type
)
def test_add_box_post_creates_box(self):
"""Adding a box should create it in the database."""
response = self.client.post('/box/add/', {
'id': 'BOX002',
'box_type': str(self.box_type.id)
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertTrue(Box.objects.filter(id='BOX002').exists())
def test_add_box_invalid_data(self):
"""Adding a box with invalid data should not create it."""
response = self.client.post('/box/add/', {
'id': '',
'box_type': str(self.box_type.id)
})
self.assertEqual(response.status_code, 302)
self.assertEqual(Box.objects.count(), 1)
def test_edit_box_updates_box_type(self):
"""Editing a box should update its box type in the database."""
new_type = BoxType.objects.create(
name='Large Box',
width=400,
height=200,
length=600
)
response = self.client.post(f'/box/{self.box.id}/edit/', {
'id': 'BOX001',
'box_type': str(new_type.id)
})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.box.refresh_from_db()
self.assertEqual(self.box.box_type, new_type)
def test_edit_box_invalid_data(self):
"""Editing a box with invalid data should not update it."""
old_id = self.box.id
response = self.client.post(f'/box/{self.box.id}/edit/', {
'id': '',
'box_type': str(self.box_type.id)
})
self.assertEqual(response.status_code, 302)
self.box.refresh_from_db()
self.assertEqual(self.box.id, old_id)
def test_delete_box_deletes_box(self):
"""Deleting a box should remove it from the database."""
box_id = self.box.id
response = self.client.post(f'/box/{box_id}/delete/')
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertFalse(Box.objects.filter(id=box_id).exists())
def test_delete_box_with_things_redirects(self):
"""Deleting a box with things should redirect without deleting."""
thing_type = ThingType.objects.create(name='Test')
Thing.objects.create(
name='Test Item',
thing_type=thing_type,
box=self.box
)
box_id = self.box.id
response = self.client.post(f'/box/{box_id}/delete/')
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, '/box-management/')
self.assertTrue(Box.objects.filter(id=box_id).exists())
class ThingPictureUploadTests(AuthTestCase):
"""Tests for thing picture deletion."""
def setUp(self):
"""Set up test fixtures."""
super().setUp()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
self.box = Box.objects.create(
id='BOX001',
box_type=self.box_type
)
self.thing_type = ThingType.objects.create(name='Electronics')
self.thing = Thing.objects.create(
name='Arduino Uno',
thing_type=self.thing_type,
box=self.box
)
self.image_data = (
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01'
b'\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00'
b'\x00\x00\x0cIDATx\x9cc\xf8\x0f\x00\x00\x01\x01\x00'
b'\x05\x18\xd8N\x00\x00\x00\x00IEND\xaeB`\x82'
)
def test_thing_detail_shows_add_picture_button(self):
"""Thing detail page should show 'Add picture' button when thing has no picture."""
response = self.client.get(f'/thing/{self.thing.id}/')
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Add picture')
def test_thing_detail_shows_change_picture_button(self):
"""Thing detail page should show 'Change picture' button when thing has a picture."""
image = SimpleUploadedFile(
name='test.png',
content=self.image_data,
content_type='image/png'
)
self.thing.picture = image
self.thing.save()
response = self.client.get(f'/thing/{self.thing.id}/')
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Change picture')
# Clean up
self.thing.picture.delete(save=False)
def test_thing_detail_shows_remove_button(self):
"""Thing detail page should show 'Remove' button when thing has a picture."""
image = SimpleUploadedFile(
name='test.png',
content=self.image_data,
content_type='image/png'
)
self.thing.picture = image
self.thing.save()
response = self.client.get(f'/thing/{self.thing.id}/')
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Remove')
# Clean up
self.thing.picture.delete(save=False)
def test_delete_picture_removes_picture(self):
"""Deleting a picture should remove it from the thing."""
image = SimpleUploadedFile(
name='test.png',
content=self.image_data,
content_type='image/png'
)
self.thing.picture = image
self.thing.save()
response = self.client.post(f'/thing/{self.thing.id}/', {
'action': 'delete_picture'
})
self.assertRedirects(response, f'/thing/{self.thing.id}/')
self.thing.refresh_from_db()
self.assertFalse(self.thing.picture.name)
def test_delete_picture_on_thing_without_picture(self):
"""Deleting a picture from a thing without a picture should succeed."""
response = self.client.post(f'/thing/{self.thing.id}/', {
'action': 'delete_picture'
})
self.assertRedirects(response, f'/thing/{self.thing.id}/')
self.thing.refresh_from_db()
self.assertFalse(self.thing.picture.name)
def test_delete_picture_on_thing_without_picture(self):
"""Deleting a picture from thing without picture should succeed."""
response = self.client.post(f'/thing/{self.thing.id}/', {
'action': 'delete_picture'
})
self.assertRedirects(response, f'/thing/{self.thing.id}/')
self.thing.refresh_from_db()
self.assertFalse(self.thing.picture.name)