Coverage for reader\tests.py: 100%
31 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
1import os
3from django.conf import settings
4from django.contrib.auth.models import User
5from django.core.files.uploadedfile import SimpleUploadedFile
6from django.test import TestCase
7from django.urls import reverse
9from reader.models import ProfileImage
12class TestProfileDetail(TestCase):
14 def setUp(self):
15 """
16 Set up the test environment by creating a user object.
18 This method is executed before each test case to ensure a clean and
19 consistent state for testing.
20 It creates a user object with a specified username, password and email.
22 Args:
23 None
25 Returns:
26 None
27 """
28 self.user = User.objects.create_user(
29 username="myUsername",
30 password="myPassword",
31 email="test@test.com"
32 )
34 def test_render_profile_page_authorised_user(self):
35 """
36 Test case to verify that the profile page is rendered correctly for an
37 authorised user.
38 """
39 self.client.login(username='myUsername', password='myPassword')
40 response = self.client.get(reverse('profile'))
41 self.assertEqual(response.status_code, 200)
42 self.assertIn(b"Information", response.content)
43 self.assertIn(b"Profile Image", response.content)
45 def test_render_profile_page_unauthorised_user(self):
46 """
47 Test case to verify that an unauthorised user is redirected to the
48 login page when accessing the profile page.
49 """
50 self.assertEqual(self.client.get(reverse('profile')).status_code, 302)
52 def test_successful_profile_update(self):
53 """
54 Test case to verify successful profile update.
56 This test case logs in a user, updates their profile information
57 including first name, last name, email, and profile image. It then
58 asserts that the updated user object has the correct values for first
59 name, last name, and email. It also checks that the profile image has
60 been successfully updated.
62 Returns:
63 None
64 """
65 self.client.login(username='myUsername', password='myPassword')
66 image_path = os.path.join(settings.BASE_DIR, 'static', 'img',
67 'profile-image.png')
68 with open(image_path, 'rb') as img_file:
69 img_data = img_file.read()
71 updated_data = {
72 'first-name': 'UpdatedFirstName',
73 'last-name': 'UpdatedLastName',
74 'email': 'updated@test.com',
75 'fileInput': SimpleUploadedFile("test_image.png", img_data,
76 content_type="image/png")
77 }
79 self.client.post(reverse('profile'), data=updated_data)
80 updated_user = User.objects.get(username='myUsername')
81 self.assertEqual(updated_user.first_name, 'UpdatedFirstName')
82 self.assertEqual(updated_user.last_name, 'UpdatedLastName')
83 self.assertEqual(updated_user.email, 'updated@test.com')
84 updated_image = ProfileImage.objects.get(user=updated_user)
85 self.assertIsNotNone(updated_image.image.public_id)