Coverage for accounts/tests.py: 100%
57 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-16 10:58 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-03-16 10:58 +0000
1from django.contrib.auth.models import User
2from django.test import TestCase, Client
3from django.urls import reverse
6# Create your tests here.
8class AccountCreateViewTests(TestCase):
9 def setUp(self):
10 """
11 Set up the test environment by creating a new client instance.
13 Args:
14 self (AccountCreateViewTests): The current instance of the test class.
15 """
16 self.client = Client()
18 def test_create_account_get_request(self):
19 """
20 Test that GET request to create_account renders the correct template.
22 This method tests whether a GET request to the 'create_account' view
23 returns a successful response with the expected status code (200) and
24 uses the 'create_account.html' template.
26 Args:
27 self (AccountCreateViewTests): The current instance of the test class.
28 """
29 response = self.client.get(reverse('create_account'))
30 self.assertEqual(response.status_code, 200)
31 self.assertTemplateUsed(response, 'create_account.html')
33 def test_create_account_post_valid_data(self):
34 """
35 Test that POST request with valid data creates a new user and renders confirm_create.html.
37 This method tests whether a POST request to the 'create_account' view
38 with valid email and password data creates a new user in the database
39 and renders the 'confirm_create.html' template.
41 Args:
42 self (AccountCreateViewTests): The current instance of the test class.
43 """
44 email = "newuser@example.com"
45 password = "anothersecurepassword123"
47 response = self.client.post(reverse('create_account'), {
48 'email': email,
49 'password': password
50 })
52 # Check if the response status code is 200 (render)
53 self.assertEqual(response.status_code, 200)
55 # Check if the correct template is rendered
56 self.assertTemplateUsed(response, 'confirm_create.html')
58 # Ensure that the new user exists in the database
59 user_exists = User.objects.filter(email=email).exists()
60 self.assertTrue(user_exists)
62 def test_create_account_post_invalid_data(self):
63 """
64 Test that POST request with invalid data (e.g., missing email or password) shows error message.
66 This method tests whether a POST request to the 'create_account' view
67 with invalid data (such as missing email or password) returns a successful
68 response with the expected status code (200), renders the 'confirm_create.html'
69 template, and includes an error message in the context.
71 Args:
72 self (AccountCreateViewTests): The current instance of the test class.
73 """
74 response = self.client.post(reverse('create_account'), {
75 'email': '',
76 'password': ''
77 })
79 self.assertEqual(response.status_code, 200)
80 self.assertTemplateUsed(response, 'confirm_create.html')
81 self.assertIn('save_error', response.context)
82 self.assertTrue(response.context['save_error'])
84 def test_create_account_post_duplicate_email(self):
85 """
86 Test that POST request with a duplicate email shows error message.
88 This method tests whether a POST request to the 'create_account' view
89 with a duplicate email address returns a successful response with the
90 expected status code (200), renders the 'confirm_create.html' template,
91 and includes an error message in the context.
93 Args:
94 self (AccountCreateViewTests): The current instance of the test class.
95 """
96 existing_user = User.objects.create_user(
97 username="existinguser@example.com",
98 email="existinguser@example.com",
99 password="securepassword123"
100 )
101 response = self.client.post(reverse('create_account'), {
102 'email': "existinguser@example.com",
103 'password': "anotherpassword123"
104 })
106 self.assertEqual(response.status_code, 200)
107 self.assertTemplateUsed(response, 'confirm_create.html')
108 self.assertIn('save_error', response.context)
109 self.assertTrue(response.context['save_error'])
113class ProfileViewTests(TestCase):
114 def setUp(self):
115 """
116 Set up a client and an authenticated user before running tests.
118 This includes creating a test user with username, email, and password.
119 """
120 self.client = Client()
121 self.user = User.objects.create_user(
122 username="testuser",
123 email="testuser@example.com",
124 password="securepassword123"
125 )
127 def test_profile_get_request_anonymous_user(self):
128 """
129 Test that a GET request to the profile view redirects an anonymous user
130 to the login page with next parameter set to the profile URL.
132 :return: None
133 """
134 response = self.client.get(reverse('profile'))
135 self.assertRedirects(response, '/accounts/login/?next=/accounts/profile/')
137 def test_profile_get_request_authenticated_user(self):
138 """
139 Test that a GET request to the profile view renders the correct template
140 for an authenticated user and returns a 200 OK status code.
142 :return: None
143 """
144 self.client.login(username="testuser", password="securepassword123")
145 response = self.client.get(reverse('profile'))
146 self.assertEqual(response.status_code, 200)
147 self.assertTemplateUsed(response, 'profile.html')
149 def test_profile_post_request(self):
150 """
151 Test that a POST request to the profile view updates user data and
152 redirects the authenticated user back to the profile page after successful update.
153 Also checks if the updated user data is reflected in the database.
155 :return: None
156 """
157 self.client.login(username="testuser", password="securepassword123")
159 # Make the POST request with new user data
160 response = self.client.post(reverse('profile'), {
161 'username': "newtestuser",
162 'email': "newtestuser@example.com",
163 'last_name': "test_last_name"
164 })
166 # Check that we are redirected after successful update
167 self.assertEqual(response.status_code, 302)
169 # Follow the redirect using a GET request with follow=True
170 follow_response = self.client.get(response.url, follow=True)
172 # Assert that the final response has a status code of 200 and that the correct template is used
173 self.assertEqual(follow_response.status_code, 200)
174 self.assertTemplateUsed(follow_response, 'profile.html')
176 # Check that the user data has been updated correctly in the database
177 updated_user = User.objects.get(username="newtestuser")
178 self.assertEqual(updated_user.last_name, "test_last_name")
180 def test_profile_post_request_invalid_data(self):
181 """
182 Test that a POST request to the profile view with invalid or incomplete data
183 returns a 200 OK status code and displays an error message in the context.
185 :return: None
186 """
187 self.client.login(username="testuser", password="securepassword123")
188 response = self.client.post(reverse('profile'), {
189 'username': "",
190 'email': "invalidemail"
191 })
192 self.assertEqual(response.status_code, 200) # Expect a redirect after invalid data submission
194 # Check if the error message is present in the context
195 self.assertIn('save_error', response.context)