23
23
('O' , 'Other' ),
24
24
)
25
25
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
37
35
38
- response = self .client .post (url )
39
- self .assertContains (response , 'NO' )
36
+ response = self .client .get (url )
37
+ self .assertContains (response , 'NO' )
40
38
41
- response = self .client .post (url , { 'token' : 'wrongtoken' } )
42
- self .assertContains (response , 'NO' )
39
+ response = self .client .post (url )
40
+ self .assertContains (response , 'NO' )
43
41
44
- class IrcViewTests (TestCase ):
42
+ response = self .client .post (url , {'token' : 'wrongtoken' })
43
+ self .assertContains (response , 'NO' )
44
+
45
45
def test_login_loads_correctly (self ):
46
46
"""
47
47
Tests that the login page loads correctly
@@ -73,57 +73,112 @@ def test_irc(self):
73
73
def test_irc_bot_add (self ):
74
74
"""
75
75
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
77
77
"""
78
78
response = self .client .post ('/irc/add' , {'token' : 'enter' , 'raw' : ':nick!name@address PRIVMSG #test :Hello!' , 'channel' : '#psywerx' })
79
79
self .assertContains (response , 'OK' )
80
80
81
- send_wrong_request ( self , 'add' )
81
+ self . send_wrong_requests ( 'add' )
82
82
83
83
@patch ('irc.views.TOKEN' , 'enter' )
84
84
def test_karma_add (self ):
85
85
"""
86
86
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
88
88
"""
89
89
response = self .client .post ('/irc/karma' , {'token' : 'enter' , 'nick' : 'nickname' , 'channel' : '#psywerx' })
90
90
self .assertContains (response , 'OK' )
91
91
92
- send_wrong_request ( self , 'karma' )
92
+ self . send_wrong_requests ( 'karma' )
93
93
94
94
@patch ('irc.views.TOKEN' , 'enter' )
95
95
def test_karma_nick (self ):
96
96
"""
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
98
99
"""
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' )
105
118
106
119
@patch ('irc.views.TOKEN' , 'enter' )
107
120
def test_join (self ):
108
121
"""
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
111
124
"""
112
125
response = self .client .post ('/irc/join' , {'token' : 'enter' , 'nick' : 'name' , 'group' : 'Psywerx' , 'offline' : 'false' , 'channel' : '#psywerx' })
113
126
self .assertContains (response , 'ok' )
127
+ self .assertTrue (GroupMembers .objects .all ().exists ())
114
128
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' )
116
134
117
135
@patch ('irc.views.TOKEN' , 'enter' )
118
136
def test_leave (self ):
119
137
"""
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
122
140
"""
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
123
146
response = self .client .post ('/irc/leave' , {'token' : 'enter' , 'nick' : 'name' , 'group' : 'Psywerx' , 'channel' : '#psywerx' })
147
+ self .assertFalse (GroupMembers .objects .all ().exists ())
124
148
self .assertContains (response , 'ok' )
125
149
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
+
127
182
128
183
class IrcModelsTests (TestCase ):
129
184
fixtures = ['messages.json' ]
@@ -169,7 +224,7 @@ def test_msg_types_changed(self):
169
224
170
225
def test_parse_method (self ):
171
226
"""
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
173
228
"""
174
229
instance = Irc ()
175
230
channel = 'channel'
@@ -221,7 +276,16 @@ def test_parse_method(self):
221
276
#tests response if message has a link in it
222
277
raw = ':nick!name@address TOPIC #test :https://www.youtube.com/'
223
278
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 ())
225
289
226
290
def test_join_leave_methods (self ):
227
291
"""
@@ -235,11 +299,12 @@ def test_join_leave_methods(self):
235
299
instance .join (first_user .nick , first_user .group , first_user .offline , first_user .channel )
236
300
instance .join (second_user .nick , second_user .group , second_user .offline , second_user .channel )
237
301
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 )
243
307
244
308
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 )
0 commit comments