This repository was archived by the owner on Jun 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtests.py
executable file
·248 lines (209 loc) · 8.2 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from django.test import TestCase
from django.test import Client
from django.urls import reverse
from .models import Notes
from django.contrib.auth.models import User
# Create your tests here.
def create_note(title, user):
note = Notes(title=title, user=user)
note.save()
return note
def create_user(username, password):
user = User(username=username)
user.set_password(password)
user.save()
return user
class StaticViewsTests(TestCase):
def setup(self):
self.client = Client()
def test_home_renders_correctly(self):
"""
Home page should load
"""
response = self.client.get(reverse("notes:home"))
self.assertEqual(response.status_code, 200)
def test_about_renders_correctly(self):
"""
About page should load
"""
response = self.client.get(reverse("notes:about"))
self.assertEqual(response.status_code, 200)
def test_music_renders_correctly(self):
"""
Music page should load
"""
response = self.client.get(reverse("notes:music"))
self.assertEqual(response.status_code, 200)
def test_sponsor_renders_correctly(self):
"""
Sponsor page should load
"""
response = self.client.get(reverse("notes:sponsor"))
self.assertEqual(response.status_code, 200)
def test_privacy_policy_renders_correctly(self):
"""
Privacy Policy page should load
"""
response = self.client.get(reverse("notes:privacy"))
self.assertEqual(response.status_code, 200)
def test_tos_renders_correctly(self):
"""
Terms of Service page should load
"""
response = self.client.get(reverse("notes:tos"))
self.assertEqual(response.status_code, 200)
class NotesListViewTests(TestCase):
def setup(self):
self.client = Client()
def test_list_view_works_correctly(self):
"""
The view must contain the created notes
"""
user = create_user(username="TestUser", password="test000")
self.client.login(username="TestUser", password="test000")
create_note(title="TestNote1", user=user)
create_note(title="TestNote2", user=user)
response = self.client.get(reverse("notes:list"))
self.assertContains(response, "TestNote1")
self.assertContains(response, "TestNote2")
def test_list_view_with_search_works_correctly(self):
"""
The view must return only the queried notes
"""
user = create_user(username="TestUser", password="test000")
self.client.post(reverse("login"), {
"username": "TestUser",
"password": "test000"
})
create_note(title="TestNote1", user=user)
create_note(title="TestNote2", user=user)
response = self.client.get(
"/notes/?q=TestNote1"
) # Did not use reverse. This test will fail if url changes
self.assertContains(response, "TestNote1")
self.assertNotContains(response, "TestNote2")
class NotesCreateViewTests(TestCase):
def setup(self):
self.client = Client()
def test_unauthenticated_user_cannot_make_a_note(self):
"""
Unauthenticated user shouldn't be able to make a note
"""
self.client.post(
reverse("notes:create"),
{
"title": "Test Note",
"content": "Test Content",
"category": "Test cat"
},
)
self.assertEquals(Notes.objects.all().count(), 0)
def test_authenticated_user_can_make_a_note(self):
"""
Authenticateded user should be able to succesfully make a note
"""
create_user(username="TestUser", password="test000")
self.client.login(username="TestUser", password="test000")
self.client.post(
reverse("notes:create"),
{
"title": "Test Note",
"content": "Test Content",
"category": "Test cat"
},
)
self.assertEquals(Notes.objects.all().count(), 1)
class NotesDeleteViewTests(TestCase):
def setup(self):
self.client = Client()
def test_correct_user_can_delete(self):
"""
Author of the note should be able to delete the note
"""
user1 = create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1", user=user1
) # User1 created the note and they will try to delete it
self.client.post(reverse("notes:delete", kwargs={"slug": note.slug}))
self.assertEquals(Notes.objects.all().count(), 0)
def test_incorrect_user_cannot_delete(self):
"""
If user is not author of the note they shouldn't be able to delete the note
"""
create_user(username="TestUser1", password="test000")
user2 = create_user(username="TestUser2", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1", user=user2
) # User2 created the note and user1 will try to delete it
self.client.post(reverse("notes:delete", kwargs={"slug": note.slug}))
self.assertEquals(Notes.objects.all().count(), 1)
class NotesDetailsViewTests(TestCase):
def setup(self):
self.client = Client()
def test_correct_user_can_view(self):
"""
Author of the note should be able to see the note
"""
user1 = create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1",
user=user1) # User1 created the note and they will try to see it
response = self.client.get(
reverse("notes:detail", kwargs={"slug": note.slug}))
self.assertContains(response, "TestNote1")
def test_incorrect_user_cannot_view(self):
"""
If user is not author of the note they shouldn't be able to view the note
"""
create_user(username="TestUser1", password="test000")
user2 = create_user(username="TestUser2", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1",
user=user2) # User2 created the note and user1 will try to view it
response = self.client.get(
reverse("notes:detail", kwargs={"slug": note.slug}))
self.assertEquals(response.status_code, 403)
class NotesUpdateViewTests(TestCase):
def setup(self):
self.client = Client()
def test_correct_user_can_update(self):
"""
Author of the note should be able to succesfully update the note
"""
user1 = create_user(username="TestUser1", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1", user=user1
) # User1 created the note and they will try to update it
self.client.post(
reverse("notes:update", kwargs={"slug": note.slug}),
{
"title": "Updated_test_note",
"content": "test_content",
"category": "CS"
},
)
self.assertEqual(Notes.objects.all()[0].title, "Updated_test_note")
def test_incorrect_user_cannot_update(self):
"""
If user is not author of the note they shouldn't be able to view the note
"""
create_user(username="TestUser1", password="test000")
user2 = create_user(username="TestUser2", password="test000")
self.client.login(username="TestUser1", password="test000")
note = create_note(
title="TestNote1", user=user2
) # User2 created the note and user1 will try to update it
self.client.post(
reverse("notes:update", kwargs={"slug": note.slug}),
{
"title": "Updated_test_note",
"content": "test_content",
"category": "CS"
},
)
self.assertNotEqual(Notes.objects.all()[0].title, "Updated_test_note")