ref: 795764886b3c887a6e4988efe77eea8f7665d71a fanboi2/fanboi2/tests/test_integrations_admin_banwords.py -rw-r--r-- 13.1 KiB View raw
                                                                                
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
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
310
311
312
313
314
315
316
317
318
319
320
import unittest
import unittest.mock

from webob.multidict import MultiDict

from . import IntegrationMixin


class TestIntegrationAdminBanwords(IntegrationMixin, unittest.TestCase):
    def test_banwords_get(self):
        from ..interfaces import IBanwordQueryService
        from ..models import Banword
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banwords_get
        from . import mock_service

        banword1 = self._make(Banword(expr="https?:\\/\\/bit\\.ly"))
        banword2 = self._make(Banword(expr="https?:\\/\\/goo\\.gl"))
        self._make(Banword(expr="https?:\\/\\/youtu\\.be", active=False))
        self._make(Banword(expr="https?:\\/\\/example\\.com", active=False))
        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        response = banwords_get(request)
        self.assertEqual(response["banwords"], [banword2, banword1])

    def test_banwords_inactive_get(self):
        from ..interfaces import IBanwordQueryService
        from ..models import Banword
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banwords_inactive_get
        from . import mock_service

        self._make(Banword(expr="https?:\\/\\/bit\\.ly"))
        self._make(Banword(expr="https?:\\/\\/goo\\.gl"))
        banword3 = self._make(Banword(expr="https?:\\/\\/youtu\\.be", active=False))
        banword4 = self._make(Banword(expr="https?:\\/\\/example\\.com", active=False))
        self.dbsession.commit()
        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        response = banwords_inactive_get(request)
        self.assertEqual(response["banwords"], [banword4, banword3])

    def test_banword_new_get(self):
        from ..forms import AdminBanwordForm
        from ..views.admin import banword_new_get

        self.request.method = "GET"
        response = banword_new_get(self.request)
        self.assertIsInstance(response["form"], AdminBanwordForm)

    def test_banword_new_post(self):
        from ..interfaces import IBanwordCreateService
        from ..models import Banword
        from ..services import BanwordCreateService
        from ..views.admin import banword_new_post
        from . import mock_service

        request = mock_service(
            self.request,
            {
                "db": self.dbsession,
                IBanwordCreateService: BanwordCreateService(self.dbsession),
            },
        )
        request.method = "POST"
        request.content_type = "application/x-www-form-urlencoded"
        request.POST = MultiDict({})
        request.POST["expr"] = "https?:\\/\\/bit\\.ly/"
        request.POST["description"] = "Violation of galactic law"
        request.POST["scope"] = "board:foo"
        request.POST["active"] = "1"
        request.POST["csrf_token"] = request.session.get_csrf_token()
        self.config.add_route("admin_banword", "/admin/banwords/{banword}")
        response = banword_new_post(request)
        banword = self.dbsession.query(Banword).first()
        self.assertEqual(response.location, "/admin/banwords/%s" % banword.id)
        self.assertEqual(self.dbsession.query(Banword).count(), 1)
        self.assertEqual(banword.expr, "https?:\\/\\/bit\\.ly/")
        self.assertEqual(banword.description, "Violation of galactic law")
        self.assertEqual(banword.scope, "board:foo")
        self.assertTrue(banword.active)

    def test_banword_new_post_bad_csrf(self):
        from pyramid.csrf import BadCSRFToken
        from ..views.admin import banword_new_post

        self.request.method = "POST"
        self.request.content_type = "application/x-www-form-urlencoded"
        self.request.POST = MultiDict({})
        self.request.POST["ip_address"] = "10.0.1.0/24"
        self.request.POST["description"] = "Violation of galactic law"
        self.request.POST["duration"] = 30
        self.request.POST["scope"] = "board:galaxy_far_away"
        self.request.POST["active"] = "1"
        with self.assertRaises(BadCSRFToken):
            banword_new_post(self.request)

    def test_banword_new_post_invalid_expr(self):
        from ..interfaces import IBanwordCreateService
        from ..models import Banword
        from ..services import BanwordCreateService
        from ..views.admin import banword_new_post
        from . import mock_service

        request = mock_service(
            self.request, {IBanwordCreateService: BanwordCreateService(self.dbsession)}
        )
        request.method = "POST"
        request.content_type = "application/x-www-form-urlencoded"
        request.POST = MultiDict({})
        request.POST["expr"] = "(?y)"
        request.POST["description"] = "Violation of galactic law"
        request.POST["scope"] = "board:foo"
        request.POST["active"] = "1"
        request.POST["csrf_token"] = request.session.get_csrf_token()
        response = banword_new_post(request)
        self.assertEqual(self.dbsession.query(Banword).count(), 0)
        self.assertEqual(response["form"].expr.data, "(?y)")
        self.assertDictEqual(
            response["form"].errors, {"expr": ["Must be a valid regular expression."]}
        )

    def test_banword_get(self):
        from ..models import Banword
        from ..interfaces import IBanwordQueryService
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banword_get
        from . import mock_service

        banword = self._make(Banword(expr="https?:\\/\\/bit\\.ly"))
        self.dbsession.commit()
        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        request.matchdict["banword"] = str(banword.id)
        response = banword_get(request)
        self.assertEqual(response["banword"], banword)

    def test_banword_get_not_found(self):
        from sqlalchemy.orm.exc import NoResultFound
        from ..interfaces import IBanwordQueryService
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banword_get
        from . import mock_service

        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        request.matchdict["banword"] = "-1"
        with self.assertRaises(NoResultFound):
            banword_get(request)

    def test_banword_edit_get(self):
        from ..models import Banword
        from ..interfaces import IBanwordQueryService
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banword_edit_get
        from . import mock_service

        banword = self._make(
            Banword(
                expr="https?:\\/\\/bit\\.ly", description="no shortlinks", active=True
            )
        )
        self.dbsession.commit()
        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        request.matchdict["banword"] = str(banword.id)
        response = banword_edit_get(request)
        self.assertEqual(response["banword"], banword)
        self.assertEqual(response["form"].expr.data, "https?:\\/\\/bit\\.ly")
        self.assertEqual(response["form"].description.data, "no shortlinks")
        self.assertTrue(response["form"].active.data)

    def test_banword_edit_get_not_found(self):
        from sqlalchemy.orm.exc import NoResultFound
        from ..interfaces import IBanwordQueryService
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banword_edit_get
        from . import mock_service

        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "GET"
        request.matchdict["banword"] = "-1"
        with self.assertRaises(NoResultFound):
            banword_edit_get(request)

    def test_banword_edit_post(self):
        from ..models import Banword
        from ..interfaces import IBanwordQueryService, IBanwordUpdateService
        from ..services import BanwordQueryService, BanwordUpdateService, ScopeService
        from ..views.admin import banword_edit_post
        from . import mock_service

        banword = self._make(
            Banword(
                expr="https?:\\/\\/bit\\.ly",
                description="no shortlinks",
                scope="board:foo",
                active=True,
            )
        )
        self.dbsession.commit()
        request = mock_service(
            self.request,
            {
                IBanwordQueryService: BanwordQueryService(
                    self.dbsession, ScopeService()
                ),
                IBanwordUpdateService: BanwordUpdateService(self.dbsession),
            },
        )
        request.method = "POST"
        request.matchdict["banword"] = str(banword.id)
        request.content_type = "application/x-www-form-urlencoded"
        request.POST = MultiDict({})
        request.POST["expr"] = "https?:\\/\\/(bit\\.ly|goo\\.gl)"
        request.POST["description"] = "violation of galactic law"
        request.POST["scope"] = "board:bar"
        request.POST["active"] = ""
        request.POST["csrf_token"] = request.session.get_csrf_token()
        self.config.add_route("admin_banword", "/admin/banwords/{banword}")
        response = banword_edit_post(request)
        self.assertEqual(response.location, "/admin/banwords/%s" % banword.id)
        self.assertEqual(banword.expr, "https?:\\/\\/(bit\\.ly|goo\\.gl)")
        self.assertEqual(banword.description, "violation of galactic law")
        self.assertEqual(banword.scope, "board:bar")
        self.assertFalse(banword.active)

    def test_banword_edit_post_not_found(self):
        from sqlalchemy.orm.exc import NoResultFound
        from ..interfaces import IBanwordQueryService
        from ..services import BanwordQueryService, ScopeService
        from ..views.admin import banword_edit_post
        from . import mock_service

        request = mock_service(
            self.request,
            {IBanwordQueryService: BanwordQueryService(self.dbsession, ScopeService())},
        )
        request.method = "POST"
        request.matchdict["banword"] = "-1"
        request.content_type = "application/x-www-form-urlencoded"
        request.POST = MultiDict({})
        request.POST["csrf_token"] = request.session.get_csrf_token()
        with self.assertRaises(NoResultFound):
            banword_edit_post(request)

    def test_banword_edit_post_bad_csrf(self):
        from pyramid.csrf import BadCSRFToken
        from ..models import Banword
        from ..views.admin import banword_edit_post

        banword = self._make(Banword(expr="https?:\\/\\/bit\\.ly"))
        self.request.method = "POST"
        self.request.matchdict["banword"] = str(banword.id)
        self.request.content_type = "application/x-www-form-urlencoded"
        self.request.POST = MultiDict({})
        self.request.POST["expr"] = "https?:\\/\\/(bit\\.ly|goo\\.gl)"
        self.request.POST["description"] = "violation of galactic law"
        self.request.POST["scope"] = "board:foo"
        self.request.POST["active"] = ""
        with self.assertRaises(BadCSRFToken):
            banword_edit_post(self.request)

    def test_banword_edit_post_invalid_expr(self):
        from ..models import Banword
        from ..interfaces import IBanwordQueryService, IBanwordUpdateService
        from ..services import BanwordQueryService, BanwordUpdateService, ScopeService
        from ..views.admin import banword_edit_post
        from . import mock_service

        banword = self._make(
            Banword(
                expr="https?:\\/\\/bit\\.ly",
                description="no shortlinks",
                scope="board:foo",
                active=True,
            )
        )
        self.dbsession.commit()
        request = mock_service(
            self.request,
            {
                IBanwordQueryService: BanwordQueryService(
                    self.dbsession, ScopeService()
                ),
                IBanwordUpdateService: BanwordUpdateService(self.dbsession),
            },
        )
        request.method = "POST"
        request.matchdict["banword"] = str(banword.id)
        request.content_type = "application/x-www-form-urlencoded"
        request.POST = MultiDict({})
        request.POST["expr"] = "(?y)"
        request.POST["csrf_token"] = request.session.get_csrf_token()
        response = banword_edit_post(request)
        self.assertEqual(banword.expr, "https?:\\/\\/bit\\.ly")
        self.assertEqual(response["banword"], banword)
        self.assertEqual(response["form"].expr.data, "(?y)")
        self.assertDictEqual(
            response["form"].errors, {"expr": ["Must be a valid regular expression."]}
        )