|
1 | | -from allauth.account.models import EmailAddress |
2 | | -from django.contrib.auth import get_user_model |
3 | | -from django.test import TestCase |
4 | | -from django.urls import reverse |
5 | | -from django.utils import timezone |
6 | | - |
7 | | -from .models import UserProfile |
8 | | - |
9 | | - |
10 | | -class DashboardViewTest(TestCase): |
11 | | - def setUp(self): |
12 | | - """Set up user data for testing""" |
13 | | - |
14 | | - self.username = "testuser" |
15 | | - self.password = "securepassword123" |
16 | | - self.user = get_user_model().objects.create_user( |
17 | | - email=self.email, password=self.password, username=self.username |
18 | | - ) |
19 | | - EmailAddress.objects.create( |
20 | | - user=self.user, email=self.email, verified=True, primary=True |
21 | | - ) |
22 | | - self.dashboard_url = reverse("dashboard") |
23 | | - |
24 | | - def test_dashboard_view_redirects_for_anonymous_user(self): |
25 | | - # Ensure anonymous users are redirected to the login page |
26 | | - response = self.client.get(self.dashboard_url) |
27 | | - self.assertEqual(response.status_code, 302) # Should redirect to login |
28 | | - self.assertRedirects( |
29 | | - response, f"{reverse('account_login')}?next={self.dashboard_url}" |
30 | | - ) |
31 | | - |
32 | | - def test_dashboard_view_accessible_to_authenticated_user(self): |
33 | | - # Log in the test user using Allauth-compatible email login |
34 | | - self.client.login(email=self.email, password=self.password) |
35 | | - response = self.client.get(self.dashboard_url) |
36 | | - self.assertEqual(response.status_code, 200) # Should be accessible |
37 | | - self.assertTemplateUsed( |
38 | | - response, "home/dashboard.html" |
39 | | - ) # Ensure the correct template is used |
40 | | - |
41 | | - def test_dashboard_view_displays_username(self): |
42 | | - # Log in the test user |
43 | | - self.client.login(email=self.email, password=self.password) |
44 | | - response = self.client.get(self.dashboard_url) |
45 | | - self.assertContains( |
46 | | - response, f"Logged in as {self.username}" |
47 | | - ) # Ensure the email is displayed correctly |
48 | | - |
49 | | - def test_dashboard_view_has_logout_link(self): |
50 | | - # Log in the test user |
51 | | - self.client.login(email=self.email, password=self.password) |
52 | | - response = self.client.get(self.dashboard_url) |
53 | | - self.assertContains( |
54 | | - response, reverse("account_logout") |
55 | | - ) # Ensure the logout link is present |
56 | | - |
57 | | - def test_dashboard_view_displays_level_xp(self): |
58 | | - self.client.login(email=self.email, password=self.password) |
59 | | - user_profile = UserProfile.objects.create(user=self.user, dob=timezone.now()) |
60 | | - |
61 | | - response = self.client.get(self.dashboard_url) |
62 | | - self.assertContains(response, f"Level: {user_profile.level}") |
63 | | - self.assertContains(response, f"XP: {user_profile.xp}") |
64 | | - self.assertNotContains(response, reverse("onboarding")) |
65 | | - |
66 | | - def test_dashboard_view_not_displays_level_xp_on_incomplete_profile(self): |
67 | | - self.client.login(email=self.email, password=self.password) |
68 | | - response = self.client.get(self.dashboard_url) |
69 | | - self.assertNotContains(response, "Level:") |
70 | | - self.assertNotContains(response, "XP:") |
71 | | - self.assertContains(response, reverse("onboarding")) |
72 | | - |
73 | | - |
74 | | -# Home Page Test |
75 | | -class HomeViewsTest(TestCase): |
76 | | - def setUp(self): |
77 | | - """Set up user data for testing""" |
78 | | - |
79 | | - self.username = "testuser" |
80 | | - self.password = "securepassword123" |
81 | | - self.user = get_user_model().objects.create_user( |
82 | | - email=self.email, password=self.password, username=self.username |
83 | | - ) |
84 | | - EmailAddress.objects.create( |
85 | | - user=self.user, email=self.email, verified=True, primary=True |
86 | | - ) |
87 | | - |
88 | | - self.home_url = reverse("home") |
89 | | - self.login_url = reverse("account_login") |
90 | | - self.logout_url = reverse("account_logout") |
91 | | - self.dashboard_url = reverse("dashboard") |
92 | | - |
93 | | - def test_home_view_status_code(self): |
94 | | - """Test if the home view returns a 200 status code""" |
95 | | - response = self.client.get(self.home_url) |
96 | | - self.assertEqual(response.status_code, 200) |
97 | | - |
98 | | - def test_home_view_template(self): |
99 | | - """Test if the home view renders the correct template""" |
100 | | - response = self.client.get(self.home_url) |
101 | | - self.assertTemplateUsed(response, "home/home.html") |
102 | | - |
103 | | - |
104 | | -class OnboardingViewTests(TestCase): |
105 | | - def setUp(self): |
106 | | - """Set up user data for testing""" |
107 | | - |
108 | | - self.username = "testuser" |
109 | | - self.password = "securepassword123" |
110 | | - self.user = get_user_model().objects.create_user( |
111 | | - email=self.email, password=self.password, username=self.username |
112 | | - ) |
113 | | - EmailAddress.objects.create( |
114 | | - user=self.user, email=self.email, verified=True, primary=True |
115 | | - ) |
116 | | - |
117 | | - self.url = reverse("onboarding") # URL for onboarding view |
118 | | - self.login_url = reverse("account_login") |
119 | | - self.logout_url = reverse("account_logout") |
120 | | - self.dashboard_url = reverse("dashboard") |
121 | | - |
122 | | - def test_redirect_to_dashboard_if_user_profile_exists(self): |
123 | | - # Create a UserProfile for the user |
124 | | - UserProfile.objects.create(user=self.user, dob=timezone.now()) |
125 | | - |
126 | | - # Login with the test user |
127 | | - self.client.login(email=self.email, password=self.password) |
128 | | - |
129 | | - # Send a GET request to the onboarding page |
130 | | - response = self.client.get(self.url) |
131 | | - |
132 | | - # Check if the response is a redirect to the dashboard |
133 | | - self.assertRedirects(response, reverse("dashboard")) |
134 | | - |
135 | | - def test_onboarding_form_is_displayed_if_user_profile_does_not_exist(self): |
136 | | - # Ensure the user doesn't have a profile |
137 | | - self.assertFalse(UserProfile.objects.filter(user=self.user).exists()) |
138 | | - |
139 | | - # Login with the test user |
140 | | - self.client.login(email=self.email, password=self.password) |
141 | | - |
142 | | - # Send a GET request to the onboarding page |
143 | | - response = self.client.get(self.url) |
144 | | - |
145 | | - # Check if the response contains the onboarding form |
146 | | - self.assertContains(response, '<form method="post"') |
147 | | - |
148 | | - def test_no_profile_creation_on_invalid_post(self): |
149 | | - # Login with the test user |
150 | | - self.client.login(email=self.email, password=self.password) |
151 | | - |
152 | | - # Send a POST request with invalid data (e.g., empty dob) |
153 | | - data = {"dob": ""} # Invalid date of birth (empty string) |
154 | | - self.client.post(self.url, data) |
155 | | - |
156 | | - # Check if the UserProfile is not created |
157 | | - self.assertFalse(UserProfile.objects.filter(user=self.user).exists()) |
| 1 | +# |
0 commit comments