Skip to content

Commit f3eb643

Browse files
committed
finish adding tests
1 parent 219401a commit f3eb643

File tree

5 files changed

+121
-45
lines changed

5 files changed

+121
-45
lines changed

irc/tests.py

Lines changed: 106 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@
2323
('O', 'Other'),
2424
)
2525

26-
def send_wrong_request(self, path):
27-
"""
28-
Tests that the method, the path of which is passed as a parameter to this function, responds with NO if:
29-
1) the http method is not POST
30-
2) the http method is POST, but TOKEN is not passed
31-
3) the http method is POST, but the passed TOKEN is wrong
32-
"""
33-
url = "/irc/%s" %path
34-
35-
response = self.client.get(url)
36-
self.assertContains(response, 'NO')
26+
class IrcViewTests(TestCase):
27+
def send_wrong_requests(self, path):
28+
"""
29+
Tests that the method, the path of which is passed as a parameter to this function, responds with NO if:
30+
1) the http method is not POST
31+
2) the http method is POST, but TOKEN is not passed
32+
3) the http method is POST, but the passed TOKEN is wrong
33+
"""
34+
url = "/irc/%s" %path
3735

38-
response = self.client.post(url)
39-
self.assertContains(response, 'NO')
36+
response = self.client.get(url)
37+
self.assertContains(response, 'NO')
4038

41-
response = self.client.post(url, {'token': 'wrongtoken'})
42-
self.assertContains(response, 'NO')
39+
response = self.client.post(url)
40+
self.assertContains(response, 'NO')
4341

44-
class IrcViewTests(TestCase):
42+
response = self.client.post(url, {'token': 'wrongtoken'})
43+
self.assertContains(response, 'NO')
44+
4545
def test_login_loads_correctly(self):
4646
"""
4747
Tests that the login page loads correctly
@@ -73,57 +73,112 @@ def test_irc(self):
7373
def test_irc_bot_add(self):
7474
"""
7575
Tests that the irc_bot_add method responds with OK if the correct TOKEN is passed, along with channel and raw
76-
Also tests that the response is NO when we send wrong requests through the send_wrong_request function
76+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
7777
"""
7878
response = self.client.post('/irc/add', {'token': 'enter', 'raw': ':nick!name@address PRIVMSG #test :Hello!', 'channel': '#psywerx'})
7979
self.assertContains(response, 'OK')
8080

81-
send_wrong_request(self, 'add')
81+
self.send_wrong_requests('add')
8282

8383
@patch('irc.views.TOKEN', 'enter')
8484
def test_karma_add(self):
8585
"""
8686
Tests that the karma_add method responds with OK if the correct TOKEN is passed, along with channel and nick
87-
Also tests that the response is NO when we send wrong requests through the send_wrong_request function
87+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
8888
"""
8989
response = self.client.post('/irc/karma', {'token': 'enter', 'nick': 'nickname', 'channel': '#psywerx'})
9090
self.assertContains(response, 'OK')
9191

92-
send_wrong_request(self, 'karma')
92+
self.send_wrong_requests('karma')
9393

9494
@patch('irc.views.TOKEN', 'enter')
9595
def test_karma_nick(self):
9696
"""
97-
Only runs through the method
97+
Tests that the karma_nick method returns the user's karma or all karma numbers if no nick is passed
98+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
9899
"""
99-
#has nick
100-
self.client.post('/irc/karma_nick', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
101-
#doesn't have nick
102-
self.client.post('/irc/karma_nick', {'token': 'enter', 'channel': '#psywerx'})
103-
104-
send_wrong_request(self, 'karma_nick')
100+
#user gets 2 karma with their first nick
101+
self.client.post('/irc/karma', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
102+
self.client.post('/irc/karma', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
103+
response = self.client.post('/irc/karma_nick', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
104+
self.assertEquals(response.content, '2')
105+
106+
#user additionally gets 1 karma with their other nick
107+
self.client.post('/irc/karma', {'token': 'enter', 'nick': 'nameOther', 'channel': '#psywerx'})
108+
response = self.client.post('/irc/karma_nick', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
109+
expected_message = '2 (or 3 with his other nicknames - nameOther)'
110+
self.assertEquals(response.content, expected_message)
111+
112+
#no nick is posted
113+
response = self.client.post('/irc/karma_nick', {'token': 'enter', 'channel': '#psywerx'})
114+
expected_message = '[{"nick": "name", "karma": 3}]'
115+
self.assertEquals(response.content, expected_message)
116+
117+
self.send_wrong_requests('karma_nick')
105118

106119
@patch('irc.views.TOKEN', 'enter')
107120
def test_join(self):
108121
"""
109-
Tests that the join method responds with ok if the correct TOKEN is passed, along with nick, group, offline and channel
110-
Also tests that the response is NO when we send wrong requests through the send_wrong_request function
122+
Tests that the join method responds with ok and a GroupMembers object is created if the correct TOKEN is passed
123+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
111124
"""
112125
response = self.client.post('/irc/join', {'token': 'enter', 'nick': 'name', 'group': 'Psywerx', 'offline': 'false', 'channel': '#psywerx'})
113126
self.assertContains(response, 'ok')
127+
self.assertTrue(GroupMembers.objects.all().exists())
114128

115-
send_wrong_request(self, 'join')
129+
members_set = set((gm.nick, gm.group, gm.channel) for gm in GroupMembers.objects.all())
130+
member_params = ('name', 'Psywerx', '#psywerx')
131+
self.assertIn(member_params, members_set)
132+
133+
self.send_wrong_requests('join')
116134

117135
@patch('irc.views.TOKEN', 'enter')
118136
def test_leave(self):
119137
"""
120-
Tests that the leave method responds with ok if the correct TOKEN is passed, along with nick, group, offline and channel
121-
Also tests that the response is NO when we send wrong requests through the send_wrong_request function
138+
Tests that the leave method responds with ok and the corresponding object is deleted if the correct TOKEN is passed
139+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
122140
"""
141+
self.client.post('/irc/join', {'token': 'enter', 'nick': 'name', 'group': 'Psywerx', 'offline': 'false', 'channel': '#psywerx'})
142+
# some other person leaves - GroupMembers object exists
143+
self.client.post('/irc/leave', {'token': 'enter', 'nick': 'falseName', 'group': 'Psywerx', 'channel': '#psywerx'})
144+
self.assertTrue(GroupMembers.objects.all().exists())
145+
# the joined user leaves - GroupMembers object doesn't exist anymore
123146
response = self.client.post('/irc/leave', {'token': 'enter', 'nick': 'name', 'group': 'Psywerx', 'channel': '#psywerx'})
147+
self.assertFalse(GroupMembers.objects.all().exists())
124148
self.assertContains(response, 'ok')
125149

126-
send_wrong_request(self, 'leave')
150+
self.send_wrong_requests('leave')
151+
152+
@patch('irc.views.TOKEN', 'enter')
153+
def test_leave_all(self):
154+
"""
155+
Tests that the leaveAll method responds with ok and all the GroupMembers objects with the matching name and
156+
channel are deleted if the correct TOKEN is passed
157+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
158+
"""
159+
self.client.post('/irc/join', {'token': 'enter', 'nick': 'name', 'group': 'Psywerx', 'offline': 'false', 'channel': '#psywerx'})
160+
self.client.post('/irc/join', {'token': 'enter', 'nick': 'name', 'group': 'PsywerxTwo', 'offline': 'false', 'channel': '#psywerx'})
161+
response = self.client.post('/irc/leaveAll', {'token': 'enter', 'nick': 'name', 'channel': '#psywerx'})
162+
self.assertContains(response, 'ok')
163+
self.assertFalse(GroupMembers.objects.all().exists())
164+
165+
self.send_wrong_requests('leaveAll')
166+
167+
@patch('irc.views.TOKEN', 'enter')
168+
def test_mention(self):
169+
"""
170+
Tests that the mention method returns all the members in the group
171+
Also tests that the response is NO when we send wrong requests through the send_wrong_requests function
172+
"""
173+
self.client.post('/irc/join', {'token': 'enter', 'nick': 'name', 'group': 'Psywerx', 'offline': 'false', 'channel': '#psywerx'})
174+
self.client.post('/irc/join', {'token': 'enter', 'nick': 'nameTwo', 'group': 'Psywerx', 'offline': 'false', 'channel': '#psywerx'})
175+
response = self.client.post('/irc/mention', {'token': 'enter', 'group': 'Psywerx', 'channel': '#psywerx'})
176+
177+
expected_list = '[["name", "#psywerx", false], ["nameTwo", "#psywerx", false]]'
178+
self.assertEquals(expected_list, response.content)
179+
180+
self.send_wrong_requests('mention')
181+
127182

128183
class IrcModelsTests(TestCase):
129184
fixtures = ['messages.json']
@@ -169,7 +224,7 @@ def test_msg_types_changed(self):
169224

170225
def test_parse_method(self):
171226
"""
172-
Tests every if/else statement in class Irc, method parse(see comments)
227+
Tests every if/else statement in the parse method of class Irc
173228
"""
174229
instance = Irc()
175230
channel = 'channel'
@@ -221,7 +276,16 @@ def test_parse_method(self):
221276
#tests response if message has a link in it
222277
raw = ':nick!name@address TOPIC #test :https://www.youtube.com/'
223278
self.assertEquals(instance.parse(raw, channel), response)
224-
self.assertIn('https://www.youtube.com/', Link.objects.all().__str__())
279+
links_set = set(l.link for l in Link.objects.all())
280+
links_params = ('https://www.youtube.com/')
281+
self.assertIn(links_params, links_set)
282+
self.assertFalse(Repost.objects.all().exists())
283+
284+
#tests response if message contains a link that had been posted before - a Repost object is created
285+
raw = ':nick!name@address TOPIC #test :https://www.youtube.com/'
286+
response = u'REPOST nick nick T 1'
287+
self.assertEquals(instance.parse(raw, channel), response)
288+
self.assertTrue(Repost.objects.all().exists())
225289

226290
def test_join_leave_methods(self):
227291
"""
@@ -235,11 +299,12 @@ def test_join_leave_methods(self):
235299
instance.join(first_user.nick, first_user.group, first_user.offline, first_user.channel)
236300
instance.join(second_user.nick, second_user.group, second_user.offline, second_user.channel)
237301

238-
group_members_objects = GroupMembers.objects.all()
239-
first_user_parameters = '{0} {1} {2}'.format(first_user.nick, first_user.group, first_user.channel)
240-
second_user_parameters = '{0} {1} {2}'.format(second_user.nick, second_user.group, second_user.channel)
241-
self.assertIn(first_user_parameters, group_members_objects.__str__())
242-
self.assertIn(second_user_parameters, group_members_objects.__str__())
302+
group_members_set = set((gm.nick, gm.group, gm.channel) for gm in GroupMembers.objects.all())
303+
first_user_params = (first_user.nick, first_user.group, first_user.channel)
304+
self.assertIn(first_user_params, group_members_set)
305+
second_user_params = (second_user.nick, second_user.group, second_user.channel)
306+
self.assertIn(second_user_params, group_members_set)
243307

244308
instance.leave(second_user.nick, second_user.group, second_user.channel)
245-
self.assertNotIn(second_user_parameters, group_members_objects.__str__())
309+
group_members_set = set((gm.nick, gm.group, gm.channel) for gm in GroupMembers.objects.all())
310+
self.assertNotIn(second_user_params, group_members_set)

irc/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def karma_nick(request):
5050
karma = len(Karma.objects.filter(nick=request.POST['nick'], channel__iexact=request.POST['channel'], time__year=datetime.now().year))
5151
if combined_karma > karma:
5252
karma = ("%s (or %s with his other nicknames - " + ", ".join(nicks) + ")") % (karma, combined_karma)
53+
else:
54+
karma = "%s" %karma
5355
else:
5456
d = defaultdict(int)
5557
dn = {}

local_settings.py.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ DATABASES = {
1515

1616
# Change the password you use locally to access /irc
1717
MAGIC_WORD = "root"
18+
MAGIC_WORD = hashlib.sha224(MAGIC_WORD).hexdigest()
1819

1920
CHANNEL = "#psywerx"
2021

@@ -27,5 +28,3 @@ TIME_ZONE = 'Europe/Ljubljana'
2728
# Language code for this installation. All choices can be found here:
2829
# http://www.i18nguy.com/unicode/language-identifiers.html
2930
LANGUAGE_CODE = 'si'
30-
31-
MAGIC_WORD = hashlib.sha224(MAGIC_WORD).hexdigest()

web/tests.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from django.test import TestCase
99
from .models import *
10+
from web.models import Static
1011

1112
class SimpleTest(TestCase):
1213
def test_basic_addition(self):
@@ -50,4 +51,13 @@ def test_wrong_url(self):
5051
404.html loading
5152
"""
5253
response = self.client.get('/nonexistenturl')
53-
self.assertContains(response, 'The force is wrong with your url...')
54+
self.assertContains(response, 'The force is wrong with your url...')
55+
56+
def test_site_title(self):
57+
"""
58+
Tests that you can set a custom site title and footer
59+
"""
60+
Static.objects.create(title='Custom Header', footer='Custom Footer')
61+
response = self.client.get('/')
62+
self.assertContains(response, 'Custom Header')
63+
self.assertContains(response, 'Custom Footer')

web/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ def index(request):
66
return render_to_response('index.html', context_instance=RequestContext(request))
77

88
def error500(request, template_name = '500.html'):
9-
return render_to_response(template_name, context_instance=RequestContext(request))
9+
return render_to_response(template_name, context_instance=RequestContext(request)) # pragma: no cover
1010

1111
def error404(request, template_name = '404.html'):
1212
return render_to_response(template_name, context_instance=RequestContext(request))

0 commit comments

Comments
 (0)