Login added, tests completed
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 16s
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 3s
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 16s
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 3s
This commit is contained in:
256
boxes/tests.py
256
boxes/tests.py
@@ -1,4 +1,5 @@
|
||||
from django.contrib.admin.sites import AdminSite
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||
from django.db import IntegrityError
|
||||
from django.test import Client, TestCase
|
||||
@@ -8,6 +9,19 @@ from .admin import BoxAdmin, BoxTypeAdmin, ThingAdmin, ThingTypeAdmin
|
||||
from .models import Box, BoxType, Thing, ThingType
|
||||
|
||||
|
||||
class AuthTestCase(TestCase):
|
||||
"""Base test case that provides authenticated client."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test user and authenticated client."""
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpass123'
|
||||
)
|
||||
self.client = Client()
|
||||
self.client.login(username='testuser', password='testpass123')
|
||||
|
||||
|
||||
class BoxTypeModelTests(TestCase):
|
||||
"""Tests for the BoxType model."""
|
||||
|
||||
@@ -340,13 +354,9 @@ class ThingAdminTests(TestCase):
|
||||
self.assertEqual(self.admin.search_fields, ('name', 'description'))
|
||||
|
||||
|
||||
class IndexViewTests(TestCase):
|
||||
class IndexViewTests(AuthTestCase):
|
||||
"""Tests for the index view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test client."""
|
||||
self.client = Client()
|
||||
|
||||
def test_index_returns_200(self):
|
||||
"""Index page should return 200 status."""
|
||||
response = self.client.get('/')
|
||||
@@ -362,13 +372,19 @@ class IndexViewTests(TestCase):
|
||||
response = self.client.get('/')
|
||||
self.assertContains(response, '/admin/')
|
||||
|
||||
def test_index_requires_login(self):
|
||||
"""Index page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get('/')
|
||||
self.assertRedirects(response, '/login/?next=/')
|
||||
|
||||
class BoxDetailViewTests(TestCase):
|
||||
|
||||
class BoxDetailViewTests(AuthTestCase):
|
||||
"""Tests for the box detail view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.client = Client()
|
||||
super().setUp()
|
||||
self.box_type = BoxType.objects.create(
|
||||
name='Standard Box',
|
||||
width=200,
|
||||
@@ -488,13 +504,19 @@ class BoxDetailViewTests(TestCase):
|
||||
url = reverse('box_detail', kwargs={'box_id': 'BOX001'})
|
||||
self.assertEqual(url, '/box/BOX001/')
|
||||
|
||||
def test_box_detail_requires_login(self):
|
||||
"""Box detail page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get(f'/box/{self.box.id}/')
|
||||
self.assertRedirects(response, f'/login/?next=/box/{self.box.id}/')
|
||||
|
||||
class ThingDetailViewTests(TestCase):
|
||||
|
||||
class ThingDetailViewTests(AuthTestCase):
|
||||
"""Tests for thing detail view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.client = Client()
|
||||
super().setUp()
|
||||
self.box_type = BoxType.objects.create(
|
||||
name='Standard Box',
|
||||
width=200,
|
||||
@@ -554,14 +576,36 @@ class ThingDetailViewTests(TestCase):
|
||||
url = reverse('thing_detail', kwargs={'thing_id': 1})
|
||||
self.assertEqual(url, '/thing/1/')
|
||||
|
||||
def test_thing_detail_requires_login(self):
|
||||
"""Thing detail page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get(f'/thing/{self.thing.id}/')
|
||||
self.assertRedirects(response, f'/login/?next=/thing/{self.thing.id}/')
|
||||
|
||||
class SearchViewTests(TestCase):
|
||||
def test_thing_detail_move_to_box(self):
|
||||
"""Thing can be moved to another box via POST."""
|
||||
new_box = Box.objects.create(id='BOX002', box_type=self.box_type)
|
||||
response = self.client.post(
|
||||
f'/thing/{self.thing.id}/',
|
||||
{'new_box': 'BOX002'}
|
||||
)
|
||||
self.assertRedirects(response, f'/thing/{self.thing.id}/')
|
||||
self.thing.refresh_from_db()
|
||||
self.assertEqual(self.thing.box, new_box)
|
||||
|
||||
def test_thing_detail_move_shows_all_boxes(self):
|
||||
"""Thing detail page should show all available boxes in dropdown."""
|
||||
Box.objects.create(id='BOX002', box_type=self.box_type)
|
||||
Box.objects.create(id='BOX003', box_type=self.box_type)
|
||||
response = self.client.get(f'/thing/{self.thing.id}/')
|
||||
self.assertContains(response, 'BOX001')
|
||||
self.assertContains(response, 'BOX002')
|
||||
self.assertContains(response, 'BOX003')
|
||||
|
||||
|
||||
class SearchViewTests(AuthTestCase):
|
||||
"""Tests for search view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test client."""
|
||||
self.client = Client()
|
||||
|
||||
def test_search_returns_200(self):
|
||||
"""Search page should return 200 status."""
|
||||
response = self.client.get('/search/')
|
||||
@@ -577,13 +621,19 @@ class SearchViewTests(TestCase):
|
||||
response = self.client.get('/search/')
|
||||
self.assertContains(response, 'id="results-container"')
|
||||
|
||||
def test_search_requires_login(self):
|
||||
"""Search page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get('/search/')
|
||||
self.assertRedirects(response, '/login/?next=/search/')
|
||||
|
||||
class SearchApiTests(TestCase):
|
||||
|
||||
class SearchApiTests(AuthTestCase):
|
||||
"""Tests for search API."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.client = Client()
|
||||
super().setUp()
|
||||
self.box_type = BoxType.objects.create(
|
||||
name='Standard Box',
|
||||
width=200,
|
||||
@@ -664,13 +714,19 @@ class SearchApiTests(TestCase):
|
||||
self.assertEqual(results[0]['type'], 'Electronics')
|
||||
self.assertEqual(results[0]['box'], 'BOX001')
|
||||
|
||||
def test_search_api_requires_login(self):
|
||||
"""Search API should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get('/search/api/?q=test')
|
||||
self.assertRedirects(response, '/login/?next=/search/api/%3Fq%3Dtest')
|
||||
|
||||
class AddThingsViewTests(TestCase):
|
||||
|
||||
class AddThingsViewTests(AuthTestCase):
|
||||
"""Tests for add things view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.client = Client()
|
||||
super().setUp()
|
||||
self.box_type = BoxType.objects.create(
|
||||
name='Standard Box',
|
||||
width=200,
|
||||
@@ -704,17 +760,22 @@ class AddThingsViewTests(TestCase):
|
||||
self.assertContains(response, 'Picture')
|
||||
|
||||
def test_add_things_post_valid(self):
|
||||
"""Adding valid things should redirect to box detail."""
|
||||
"""Adding valid things should show success message and create things."""
|
||||
response = self.client.post(f'/box/{self.box.id}/add/', {
|
||||
'form-TOTAL_FORMS': '3',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
'form-0-name': 'Arduino Uno',
|
||||
'form-0-thing_type': self.thing_type.id,
|
||||
'form-0-description': 'A microcontroller',
|
||||
'form-1-name': 'LED Strip',
|
||||
'form-1-thing_type': self.thing_type.id,
|
||||
'form-1-description': 'Lighting component',
|
||||
'form-2-name': '',
|
||||
'form-2-thing_type': '',
|
||||
'form-2-description': '',
|
||||
})
|
||||
self.assertRedirects(response, f'/box/{self.box.id}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'Added 2 things successfully')
|
||||
self.assertEqual(Thing.objects.count(), 2)
|
||||
|
||||
def test_add_things_post_required_name(self):
|
||||
@@ -728,28 +789,33 @@ class AddThingsViewTests(TestCase):
|
||||
self.assertContains(response, 'This field is required')
|
||||
|
||||
def test_add_things_post_partial_valid_invalid(self):
|
||||
"""Partial submission: one valid, one missing name."""
|
||||
"""Partial submission: one valid, one missing name - nothing saved due to formset validation."""
|
||||
response = self.client.post(f'/box/{self.box.id}/add/', {
|
||||
'form-TOTAL_FORMS': '2',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
'form-0-name': 'Arduino Uno',
|
||||
'form-0-thing_type': self.thing_type.id,
|
||||
'form-1-name': '',
|
||||
'form-1-thing_type': self.thing_type.id,
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'This field is required')
|
||||
self.assertEqual(Thing.objects.count(), 1)
|
||||
# Formset validation fails, so nothing is saved
|
||||
self.assertEqual(Thing.objects.count(), 0)
|
||||
|
||||
def test_add_things_creates_thing_types(self):
|
||||
"""Can create new thing types while adding things."""
|
||||
"""Can add things with different thing types."""
|
||||
new_type = ThingType.objects.create(name='Components')
|
||||
response = self.client.post(f'/box/{self.box.id}/add/', {
|
||||
'form-TOTAL_FORMS': '2',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
'form-0-name': 'Resistor',
|
||||
'form-0-thing_type': new_type.id,
|
||||
'form-1-name': 'Capacitor',
|
||||
'form-1-thing_type': new_type.id,
|
||||
})
|
||||
self.assertRedirects(response, f'/box/{self.box.id}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'Added 2 things successfully')
|
||||
self.assertEqual(Thing.objects.count(), 2)
|
||||
self.assertEqual(Thing.objects.filter(thing_type=new_type).count(), 2)
|
||||
|
||||
@@ -757,12 +823,13 @@ class AddThingsViewTests(TestCase):
|
||||
"""Submitting empty forms should not create anything."""
|
||||
response = self.client.post(f'/box/{self.box.id}/add/', {
|
||||
'form-TOTAL_FORMS': '2',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
'form-0-name': '',
|
||||
'form-0-thing_type': self.thing_type.id,
|
||||
'form-0-thing_type': '',
|
||||
'form-1-name': '',
|
||||
'form-1-thing_type': self.thing_type.id,
|
||||
'form-1-thing_type': '',
|
||||
})
|
||||
self.assertRedirects(response, f'/box/{self.box.id}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(Thing.objects.count(), 0)
|
||||
|
||||
def test_add_things_box_not_exists(self):
|
||||
@@ -775,6 +842,7 @@ class AddThingsViewTests(TestCase):
|
||||
self.thing_type_2 = ThingType.objects.create(name='Mechanical')
|
||||
response = self.client.post(f'/box/{self.box.id}/add/', {
|
||||
'form-TOTAL_FORMS': '3',
|
||||
'form-INITIAL_FORMS': '0',
|
||||
'form-0-name': 'Bolt',
|
||||
'form-0-thing_type': self.thing_type_2.id,
|
||||
'form-1-name': 'Nut',
|
||||
@@ -782,7 +850,139 @@ class AddThingsViewTests(TestCase):
|
||||
'form-2-name': 'Washer',
|
||||
'form-2-thing_type': self.thing_type_2.id,
|
||||
})
|
||||
self.assertRedirects(response, f'/box/{self.box.id}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'Added 3 things successfully')
|
||||
things = Thing.objects.all()
|
||||
self.assertEqual(things.count(), 3)
|
||||
for thing in things:
|
||||
self.assertEqual(thing.box, self.box)
|
||||
|
||||
def test_add_things_requires_login(self):
|
||||
"""Add things page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get(f'/box/{self.box.id}/add/')
|
||||
self.assertRedirects(response, f'/login/?next=/box/{self.box.id}/add/')
|
||||
|
||||
|
||||
class ThingTypeDetailViewTests(AuthTestCase):
|
||||
"""Tests for thing type detail 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
|
||||
)
|
||||
self.parent_type = ThingType.objects.create(name='Electronics')
|
||||
self.child_type = ThingType.objects.create(
|
||||
name='Microcontrollers',
|
||||
parent=self.parent_type
|
||||
)
|
||||
self.thing = Thing.objects.create(
|
||||
name='Arduino Uno',
|
||||
thing_type=self.child_type,
|
||||
box=self.box
|
||||
)
|
||||
|
||||
def test_thing_type_detail_returns_200(self):
|
||||
"""Thing type detail page should return 200 status."""
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_thing_type_detail_returns_404_for_invalid(self):
|
||||
"""Thing type detail page should return 404 for non-existent type."""
|
||||
response = self.client.get('/thing-type/99999/')
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
def test_thing_type_detail_shows_type_name(self):
|
||||
"""Thing type detail page should show type name."""
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertContains(response, 'Electronics')
|
||||
|
||||
def test_thing_type_detail_shows_descendants(self):
|
||||
"""Thing type detail page should show descendant types."""
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertContains(response, 'Microcontrollers')
|
||||
|
||||
def test_thing_type_detail_shows_things(self):
|
||||
"""Thing type detail page should show things of this type."""
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertContains(response, 'Arduino Uno')
|
||||
|
||||
def test_thing_type_detail_uses_correct_template(self):
|
||||
"""Thing type detail page should use correct template."""
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertTemplateUsed(response, 'boxes/thing_type_detail.html')
|
||||
|
||||
def test_thing_type_detail_url_name(self):
|
||||
"""Thing type detail URL should be reversible by name."""
|
||||
url = reverse('thing_type_detail', kwargs={'type_id': 1})
|
||||
self.assertEqual(url, '/thing-type/1/')
|
||||
|
||||
def test_thing_type_detail_requires_login(self):
|
||||
"""Thing type detail page should redirect to login if not authenticated."""
|
||||
self.client.logout()
|
||||
response = self.client.get(f'/thing-type/{self.parent_type.id}/')
|
||||
self.assertRedirects(response, f'/login/?next=/thing-type/{self.parent_type.id}/')
|
||||
|
||||
|
||||
class LoginViewTests(TestCase):
|
||||
"""Tests for login view."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.client = Client()
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser',
|
||||
password='testpass123'
|
||||
)
|
||||
|
||||
def test_login_page_returns_200(self):
|
||||
"""Login page should return 200 status."""
|
||||
response = self.client.get('/login/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_login_page_contains_form(self):
|
||||
"""Login page should contain login form."""
|
||||
response = self.client.get('/login/')
|
||||
self.assertContains(response, 'Username')
|
||||
self.assertContains(response, 'Password')
|
||||
self.assertContains(response, 'Login')
|
||||
|
||||
def test_login_with_valid_credentials(self):
|
||||
"""Login with valid credentials should redirect to home."""
|
||||
response = self.client.post('/login/', {
|
||||
'username': 'testuser',
|
||||
'password': 'testpass123'
|
||||
})
|
||||
self.assertRedirects(response, '/')
|
||||
|
||||
def test_login_with_invalid_credentials(self):
|
||||
"""Login with invalid credentials should show error."""
|
||||
response = self.client.post('/login/', {
|
||||
'username': 'testuser',
|
||||
'password': 'wrongpassword'
|
||||
})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, 'username and password')
|
||||
|
||||
def test_login_redirects_to_next(self):
|
||||
"""Login should redirect to 'next' parameter after success."""
|
||||
response = self.client.post('/login/?next=/search/', {
|
||||
'username': 'testuser',
|
||||
'password': 'testpass123'
|
||||
})
|
||||
self.assertRedirects(response, '/search/')
|
||||
|
||||
def test_logout_redirects_to_login(self):
|
||||
"""Logout should redirect to login page."""
|
||||
self.client.login(username='testuser', password='testpass123')
|
||||
response = self.client.post('/logout/')
|
||||
self.assertRedirects(response, '/login/')
|
||||
|
||||
Reference in New Issue
Block a user