test_commands.py 13.6 KB
Newer Older
Mindiell's avatar
Mindiell committed
1
2
3
4
5
6
7
# encoding: utf-8
"""
Testing commands of wantzel
"""

from mock import Mock
import unittest
8
import sqlite3
Mindiell's avatar
Mindiell committed
9

10
import config
Mindiell's avatar
Mindiell committed
11
12
13
from messages import messages
from wantzel import DEBUG, get_cursor, Utils, Wantzel

14
LOG_FILE = "tests/wantzel.log"
Mindiell's avatar
Mindiell committed
15
16
LOG_LEVEL = DEBUG

17
18
19
20
21
config.dbuser      = "root"
config.dbpassword  = "root"
config.dbserver    = "localhost"
config.dbname      = "wantzel_test"
config.sqlite_db   = "tests/db_test.sqlite3"
Mindiell's avatar
Mindiell committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35


class TestWantzel(unittest.TestCase):
    def setUp(self):
        self.wantzel = Wantzel()
        # Faking send_message on IRC
        self.wantzel.send_message = Mock()
        # Faking topic change on IRC
        self.wantzel.irc = Mock()
        self.wantzel.irc.client = Mock()
        self.wantzel.irc.client.topic = Mock()
        cursor = get_cursor()
        # clearing datas
        cursor.execute("DELETE FROM presse")
36
37
38
39
        connection = sqlite3.connect(config.sqlite_db)
        connection.execute("DELETE FROM moderator")
        connection.execute("INSERT INTO moderator (name) VALUES ('admin')")
        connection.commit()
Mindiell's avatar
Mindiell committed
40
41
42
43
44

    def tearDown(self):
        # Destroying database
        cursor = get_cursor()
        cursor.execute("DELETE FROM presse")
45
46
47
        connection = sqlite3.connect(config.sqlite_db)
        connection.execute("DELETE FROM moderator")
        connection.commit()
Mindiell's avatar
Mindiell committed
48

49
    def testHelpAnswerIsInQuery(self):
Mindiell's avatar
Mindiell committed
50
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~help")
51
        self.wantzel.send_message.assert_called_once_with("test", messages["help"])
Mindiell's avatar
Mindiell committed
52

53
    def testSubHelpAnswersAreInQuery(self):
Mindiell's avatar
Mindiell committed
54
55
56
        commands = ['help', 'rp', 'status', 'stats', 'kill', 'admin']
        for command in commands:
            self.wantzel.on_privmsg("test!test.me", "#test_channel", "~help %s" % command)
57
            self.wantzel.send_message.assert_called_with("test", messages["help_%s" % command])
Mindiell's avatar
Mindiell committed
58

59
    def testRpCommandWithNoUrlDoesNothing(self):
Mindiell's avatar
Mindiell committed
60
61
62
63
64
65
66
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp nothing")
        self.wantzel.send_message.assert_not_called()
        cursor = get_cursor()
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(0, int(rows[0][0]))

67
    def testRpCommandWithNewArticleSavesIt(self):
Mindiell's avatar
Mindiell committed
68
69
70
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.send_message.assert_called_once_with(
            "#test_channel",
71
            messages["master_rp_new_article"] % "test"
Mindiell's avatar
Mindiell committed
72
73
74
75
76
77
78
79
80
        )
        cursor = get_cursor()
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))

81
82
83
84
85
86
87
88
    def testDoubleRpCommandWithAKnownArticleIsNotModifyingNote(self):
        cursor = get_cursor()
        # Adding article for first time
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        # Then, just add a point
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
89
            messages["master_rp_known_article"] % "test"
90
91
92
93
94
95
96
97
98
        )
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))

    def testRpCommandWithAKnownArticle(self):
Mindiell's avatar
Mindiell committed
99
100
101
102
103
104
105
        cursor = get_cursor()
        # Adding article for first time
        self.wantzel.on_privmsg("first_test!test.me", "#test_channel", "~rp http://test.me/article")
        # Then, just add a point
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
106
            messages["master_rp_known_article"] % "test"
Mindiell's avatar
Mindiell committed
107
108
109
110
111
112
113
114
        )
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(2, int(rows[0][0]))

115
    def testRpCommandWithAPublishableArticle(self):
Mindiell's avatar
Mindiell committed
116
117
118
119
120
121
122
123
        cursor = get_cursor()
        # Adding article for first time
        self.wantzel.on_privmsg("first_test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.on_privmsg("second_test!test.me", "#test_channel", "~rp http://test.me/article")
        # Then, add a third point
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
124
            messages["master_rp_taken_article"] % "test"
Mindiell's avatar
Mindiell committed
125
126
127
128
129
130
131
132
133
        )
        self.wantzel.irc.client.topic.assert_called_once_with("#lqdn-rp", messages["topic"] % 1)
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(3, int(rows[0][0]))

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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    def testStatusCommandForAnUnknownArticle(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~status http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["status_unknown_article"] % "test"
        )

    def testStatusCommandForAKnownArticle(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        for i in range(1, 5):
            self.wantzel.on_privmsg("test!test.me", "#test_channel", "~status http://test.me/article")
            self.wantzel.send_message.assert_called_with(
                "#test_channel",
                "test: note %s / non publié" % i
            )
            self.wantzel.on_privmsg("other_test!test.me", "#test_channel", "~rp http://test.me/article")

    def testStatusCommandForAPublishedArticle(self):
        cursor = get_cursor()
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~status http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            "test: note 1 / non publié"
        )
        for i in range(1, 5):
            cursor.execute("UPDATE presse SET nid=%s WHERE url='%s'" % (
                i,
                "http://test.me/article",
            ))
            self.wantzel.on_privmsg("test!test.me", "#test_channel", "~status http://test.me/article")
            self.wantzel.send_message.assert_called_with(
                "#test_channel",
                "test: note 1 / publié (https://laquadrature.net/node/%s)" % i
            )

    def testStatsCommandWithNoArticle(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~stats")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["stats_bravo"] % 15
        )

    def testStatsCommandWithOneArticle(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~stats")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            "note>=0: 1j:1, 3j:1, 7j:1, 15j:1"
        )

    def testStatsCommandWithSomeOldArticles(self):
        cursor = get_cursor()
        # Old article
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/old_article")
        cursor.execute("""
            UPDATE presse
            SET datec=(NOW()-INTERVAL 5 DAY)
            WHERE url='%s'""" % "http://test.me/old_article"
        )
        # New article
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/new_article")
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~stats")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            "note>=0: 1j:1, 3j:1, 7j:2, 15j:2"
        )

    def testKillCommandWithoutCredentials(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~kill http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["not_moderator"]
        )

    def testKillCommandWithAnUnknownArticle(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~kill http://test.me/unknown_article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["kill_none"] % "http://test.me/unknown_article"
        )

    def testKillCommandWithAKnownArticle(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~kill http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["kill_done"] % "http://test.me/article"
        )
        cursor = get_cursor()
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(-100, int(rows[0][0]))

    def testAdminListCommandWithoutCredentials(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["not_moderator"]
        )

    def testAdminListCommand(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin"
        )

    def testAdminAddCommandWithoutCredentials(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~admin add test")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["not_moderator"]
        )

    def testAdminAddCommandWithOneName(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin add toto")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, toto"
        )

    def testAdminAddCommandWithMultipleName(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin add tata, titi,tutu, toto")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, tata, titi, toto, tutu"
        )

    def testAdminDelCommandWithoutCredentials(self):
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~admin del test")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["not_moderator"]
        )

    def testAdminDelCommandWithOneName(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin add tata, titi,tutu, toto")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, tata, titi, toto, tutu"
        )
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin del titi")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, tata, toto, tutu"
        )

    def testAdminDelCommandWithMultipleName(self):
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin add tata, titi,tutu, toto")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, tata, titi, toto, tutu"
        )
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin del toto, tutu,titi")
        self.wantzel.on_privmsg("admin!test.me", "#test_channel", "~admin list")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
            messages["admin_list"] % "admin, tata"
        )



    def notest(self):
        # Adding article for first time
        self.wantzel.on_privmsg("first_test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.on_privmsg("second_test!test.me", "#test_channel", "~rp http://test.me/article")
        # Then, test its status
        self.wantzel.on_privmsg("test!test.me", "#test_channel", "~rp http://test.me/article")
        self.wantzel.send_message.assert_called_with(
            "#test_channel",
310
            messages["master_rp_taken_article"] % "test"
311
312
313
314
315
316
317
318
319
        )
        self.wantzel.irc.client.topic.assert_called_once_with("#lqdn-rp", messages["topic"] % 1)
        cursor.execute("SELECT COUNT(*) FROM presse")
        rows = cursor.fetchall()
        self.assertEqual(1, int(rows[0][0]))
        cursor.execute("SELECT note FROM presse WHERE url='%s'" % "http://test.me/article")
        rows = cursor.fetchall()
        self.assertEqual(3, int(rows[0][0]))