Coverage for author\test_views.py: 100%
67 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-29 10:02 +0100
« prev ^ index » next coverage.py v7.4.4, created at 2024-06-29 10:02 +0100
1from django.contrib.auth.models import User
2from django.test import TestCase
3from django.urls import reverse
5from author.models import Author
8class TestAuthorView(TestCase):
10 def setUp(self):
11 """
12 Set up the test environment by creating a superuser,
13 an author, and saving the author.
14 """
15 self.user = User.objects.create_superuser(
16 username="myUsername",
17 password="myPassword",
18 email="test@test.com"
19 )
20 self.author = Author(name="Test Author")
21 self.author.save()
23 def test_render_authors_page_authorised_user_admin(self):
24 """
25 Test case to verify that the authors page is rendered correctly for
26 an authorised user with admin privileges.
27 """
28 self.client.login(username='myUsername', password='myPassword')
29 response = self.client.get(reverse('authors'))
30 self.assertEqual(response.status_code, 200)
31 self.assertIn(b"Authors", response.content)
32 self.assertIn(b"Add Author", response.content)
34 def test_render_authors_page_authorised_user_not_admin(self):
35 """
36 Test case to verify that an authorized user who is not an admin
37 receives a 403 Forbidden response when accessing the authors page.
38 """
39 self.user = User.objects.create_user(
40 username='reader',
41 password='readerPassword',
42 email='test@test.com'
43 )
44 self.client.login(username='reader', password='readerPassword')
45 response = self.client.get(reverse('authors'))
46 self.assertIn('403', response.url)
48 def test_render_authors_page_unauthorised_user(self):
49 """
50 Test case to verify that an unauthorised user is redirected to
51 the login page when trying to access the authors page.
52 """
53 response = self.client.get(reverse('authors'))
54 self.assertEqual(response.status_code, 302)
56 def test_successful_author_creation(self):
57 """Test case to verify successful creation of an author."""
58 self.client.login(username='myUsername', password='myPassword')
59 post_data = {'name': 'Test Author2'}
60 self.assertEqual(Author.objects.count(), 1)
61 self.client.post(reverse('add_author'), post_data)
62 self.assertEqual(Author.objects.count(), 2)
64 def test_add_author_non_admin(self):
65 """
66 Test case to verify that a non-admin user cannot add an author.
67 """
68 self.user = User.objects.create_user(
69 username='reader',
70 password='readerPassword',
71 email='test@test.com'
72 )
73 self.client.login(username='reader', password='readerPassword')
74 post_data = {'name': 'Test Author2'}
75 original_count = Author.objects.count()
76 self.client.post(reverse('add_author'), post_data)
77 self.assertEqual(Author.objects.count(), original_count)
78 response = self.client.get(reverse('add_author'))
79 self.assertEqual(response.status_code, 302)
81 def test_successful_author_update(self):
82 """Test case to verify successful author update."""
83 self.client.login(username='myUsername', password='myPassword')
84 new_name = 'William Shakespeare'
85 response = self.client.get(reverse('update_author', kwargs={
86 'author_id': self.author.id}))
88 self.assertEqual(response.context['form_input'].fields['name'].initial,
89 self.author.name)
91 self.client.post(reverse('update_author', kwargs={
92 'author_id': self.author.id}), {'name': new_name})
93 updated_name = Author.objects.get(id=self.author.id).name
94 self.assertEqual(updated_name, new_name)
96 def test_update_author_non_admin(self):
97 """
98 Test case to verify that a non-admin user cannot update an author.
99 """
100 self.user = User.objects.create_user(
101 username='reader',
102 password='readerPassword',
103 email='test@test.com'
104 )
105 self.client.login(username='reader', password='readerPassword')
106 post_data = {'name': 'Updated Author'}
108 original_name = self.author.name
109 self.client.post(
110 reverse('update_author', kwargs={'author_id': self.author.id}),
111 post_data)
113 self.author.refresh_from_db()
115 self.assertEqual(self.author.name, original_name)
116 response = self.client.get(
117 reverse('update_author', kwargs={'author_id': self.author.id}))
118 self.assertEqual(response.status_code, 302)
120 def test_successful_author_delete(self):
121 """Test case to verify successful deletion of an author."""
122 self.client.login(username='myUsername', password='myPassword')
123 self.assertEqual(Author.objects.count(), 1)
124 self.client.post(
125 reverse('delete_author', kwargs={'author_id': self.author.id}))
126 self.assertEqual(Author.objects.count(), 0)
128 def test_delete_author_non_admin(self):
129 """
130 Test case to verify that a non-admin user cannot delete an author.
131 """
132 self.user = User.objects.create_user(
133 username='reader',
134 password='readerPassword',
135 email='test@test.com'
136 )
137 self.client.login(username='reader', password='readerPassword')
138 original_count = Author.objects.count()
139 self.client.post(
140 reverse('delete_author', kwargs={'author_id': self.author.id}))
141 self.assertEqual(Author.objects.count(), original_count)