~sirn/fanboi2

ref: 06f8c07f0bdf242fc448cab0a7a5f02c403d63ef fanboi2/fanboi2/tests/test_utils.py -rw-r--r-- 23.5 KiB
06f8c07fKridsada Thanabulpong Use constant-time comparison function in CSRF check. 4 years ago
                                                                                
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import unittest
import unittest.mock
from fanboi2.models import redis_conn
from fanboi2.tests import DummyRedis, RegistryMixin, CacheMixin
from pyramid import testing


class TestRequestSerializer(unittest.TestCase):

    def _getTargetFunction(self):
        from fanboi2.utils import serialize_request
        return serialize_request

    def test_serialize(self):
        request = testing.DummyRequest()
        request.application_url = 'http://www.example.com/'
        request.referrer = 'http://www.example.com/'
        request.remote_addr = '127.0.0.1'
        request.url = 'http://www.example.com/foobar'
        request.user_agent = 'Mock/1.0'
        self.assertEqual(
            self._getTargetFunction()(request),
            {
                'application_url': 'http://www.example.com/',
                'referrer': 'http://www.example.com/',
                'remote_addr': '127.0.0.1',
                'url': 'http://www.example.com/foobar',
                'user_agent': 'Mock/1.0',
            }
        )

    def test_serialize_dict(self):
        request = {'foo': 1}
        self.assertEqual(self._getTargetFunction()(request), request)


class TestDnsBl(unittest.TestCase):

    def _makeOne(self, providers=None):
        from fanboi2.utils import Dnsbl
        if providers is None:
            providers = ['xbl.spamhaus.org']
        dnsbl = Dnsbl()
        dnsbl.configure_providers(providers)
        return dnsbl

    def test_init(self):
        dnsbl = self._makeOne()
        self.assertEqual(dnsbl.providers, ['xbl.spamhaus.org'])

    def test_init_no_providers(self):
        dnsbl = self._makeOne(providers=[])
        self.assertEqual(dnsbl.providers, [])

    def test_init_string_providers(self):
        dnsbl = self._makeOne(providers='xbl.spamhaus.org tor.ahbl.org')
        self.assertEqual(dnsbl.providers, ['xbl.spamhaus.org', 'tor.ahbl.org'])

    @unittest.mock.patch('socket.gethostbyname')
    def test_listed(self, lookup_call):
        lookup_call.return_value = '127.0.0.2'
        dnsbl = self._makeOne()
        self.assertEqual(dnsbl.listed('10.0.100.254'), True)
        lookup_call.assert_called_with('254.100.0.10.xbl.spamhaus.org.')

    @unittest.mock.patch('socket.gethostbyname')
    def test_listed_unlisted(self, lookup_call):
        import socket
        lookup_call.side_effect = socket.gaierror('foobar')
        dnsbl = self._makeOne()
        self.assertEqual(dnsbl.listed('10.0.100.1'), False)
        lookup_call.assert_called_with('1.100.0.10.xbl.spamhaus.org.')

    @unittest.mock.patch('socket.gethostbyname')
    def test_listed_invalid(self, lookup_call):
        lookup_call.return_value = '192.168.1.1'
        dnsbl = self._makeOne()
        self.assertEqual(dnsbl.listed('10.0.100.2'), False)

    @unittest.mock.patch('socket.gethostbyname')
    def test_listed_malformed(self, lookup_call):
        lookup_call.return_value = 'foobarbaz'
        dnsbl = self._makeOne()
        self.assertEqual(dnsbl.listed('10.0.100.2'), False)


class TestGeoIP(unittest.TestCase):

    def _makeOne(self, providers=None):
        from fanboi2.utils import GeoIP
        geoip = GeoIP()
        return geoip

    def _makeGeoIP2(self, country_code=None):
        class MockGeoIP2Response(object):
            @property
            def country_code(self):
                return country_code

        class MockGeoIP2(object):
            def country(self, ip_address):
                return MockGeoIP2Response()

        return MockGeoIP2()

    @unittest.mock.patch('geoip2.database.Reader')
    def test_init(self, reader):
        reader.return_value = self._makeGeoIP2()
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')

    @unittest.mock.patch('geoip2.database.Reader')
    def test_init_no_path(self, reader):
        geoip = self._makeOne()
        geoip.configure_geoip2(None)
        assert not reader.called

    @unittest.mock.patch('geoip2.database.Reader')
    def test_init_file_not_found(self, reader):
        reader.side_effect = FileNotFoundError
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')
        assert not geoip.geoip2

    @unittest.mock.patch('geoip2.database.Reader')
    def test_init_invalid_database(self, reader):
        from maxminddb.errors import InvalidDatabaseError
        reader.side_effect = InvalidDatabaseError
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')
        assert not geoip.geoip2

    @unittest.mock.patch('geoip2.database.Reader')
    def test_country_code(self, reader):
        reader.return_value = self._makeGeoIP2(country_code='TH')
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')
        self.assertEqual(geoip.country_code('127.0.0.1'), 'TH')

    @unittest.mock.patch('geoip2.database.Reader')
    def test_country_code_not_inited(self, reader):
        reader.return_value = self._makeGeoIP2()
        geoip = self._makeOne()
        self.assertIsNone(geoip.country_code('127.0.0.1'))

    @unittest.mock.patch('geoip2.database.Reader')
    def test_country_code_address_not_found(self, reader):
        from geoip2.errors import AddressNotFoundError
        class MockNotFoundGeoIP2(object):
            def country(self, ip_address):
                raise AddressNotFoundError
        reader.return_value = MockNotFoundGeoIP2()
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')
        self.assertIsNone(geoip.country_code('127.0.0.1'))

    @unittest.mock.patch('geoip2.database.Reader')
    def test_country_code_not_found(self, reader):
        reader.return_value = self._makeGeoIP2()
        geoip = self._makeOne()
        geoip.configure_geoip2('/tmp/some/path')
        self.assertIsNone(geoip.country_code('127.0.0.1'))


class TestChecklist(unittest.TestCase):

    def _makeOne(self, data=None):
        from fanboi2.utils import Checklist
        checklist = Checklist()
        checklist.configure_checklist(data)
        return checklist

    def test_init(self):
        checklist = self._makeOne(['scope1/', 'scope2/foo,bar', '*/*'])
        self.assertDictEqual(checklist.data, {
            'scope1': [],
            'scope2': ['foo', 'bar'],
            '*': ['*'],
        })

    def test_init_none(self):
        checklist = self._makeOne(None)
        self.assertDictEqual(checklist.data, {})

    def test_init_empty(self):
        checklist = self._makeOne('')
        self.assertDictEqual(checklist.data, {})

    def test_fetch(self):
        checklist = self._makeOne(['scope1/', 'scope2/foo,bar', '*/*'])
        self.assertListEqual(checklist.fetch('scope1'), [])
        self.assertListEqual(checklist.fetch('scope2'), ['foo', 'bar'])
        self.assertListEqual(checklist.fetch('scope3'), ['*'])

    def test_fetch_empty(self):
        checklist = self._makeOne([])
        self.assertListEqual(checklist.fetch('scope1'), ['*'])
        self.assertListEqual(checklist.fetch('scope2'), ['*'])
        self.assertListEqual(checklist.fetch('scope3'), ['*'])

    def test_enabled(self):
        checklist = self._makeOne(['scope1/', 'scope2/foo,bar', '*/*'])
        self.assertFalse(checklist.enabled('scope1', 'foo'))
        self.assertFalse(checklist.enabled('scope1', 'bar'))
        self.assertFalse(checklist.enabled('scope1', 'baz'))
        self.assertTrue(checklist.enabled('scope2', 'foo'))
        self.assertTrue(checklist.enabled('scope2', 'bar'))
        self.assertFalse(checklist.enabled('scope2', 'baz'))
        self.assertTrue(checklist.enabled('scope3', 'foo'))
        self.assertTrue(checklist.enabled('scope3', 'bar'))
        self.assertTrue(checklist.enabled('scope3', 'baz'))

    def test_enabled_empty(self):
        checklist = self._makeOne([])
        self.assertTrue(checklist.enabled('scope1', 'foo'))
        self.assertTrue(checklist.enabled('scope1', 'bar'))
        self.assertTrue(checklist.enabled('scope1', 'baz'))
        self.assertTrue(checklist.enabled('scope2', 'foo'))
        self.assertTrue(checklist.enabled('scope2', 'bar'))
        self.assertTrue(checklist.enabled('scope2', 'baz'))
        self.assertTrue(checklist.enabled('scope3', 'foo'))
        self.assertTrue(checklist.enabled('scope3', 'bar'))
        self.assertTrue(checklist.enabled('scope3', 'baz'))


class TestAkismet(RegistryMixin, unittest.TestCase):

    def _makeOne(self, key='hogehoge'):
        from fanboi2.utils import Akismet
        akismet = Akismet()
        akismet.configure_key(key)
        return akismet

    def _makeResponse(self, content):
        class MockResponse(object):

            def __init__(self, content):
                self.content = content

        return MockResponse(content)

    def test_init(self):
        akismet = self._makeOne()
        self.assertEqual(akismet.key, 'hogehoge')

    # noinspection PyTypeChecker
    def test_init_no_key(self):
        akismet = self._makeOne(key=None)
        self.assertEqual(akismet.key, None)

    @unittest.mock.patch('requests.post')
    def test_spam(self, api_call):
        api_call.return_value = self._makeResponse(b'true')
        request = self._makeRequest()
        akismet = self._makeOne()
        self.assertEqual(akismet.spam(request, 'buy viagra'), True)
        api_call.assert_called_with(
            'https://hogehoge.rest.akismet.com/1.1/comment-check',
            headers=unittest.mock.ANY,
            data=unittest.mock.ANY,
            timeout=unittest.mock.ANY,
        )

    @unittest.mock.patch('requests.post')
    def test_spam_ham(self, api_call):
        api_call.return_value = self._makeResponse(b'false')
        request = self._makeRequest()
        akismet = self._makeOne()
        self.assertEqual(akismet.spam(request, 'Hogehogehogehoge!'), False)
        api_call.assert_called_with(
            'https://hogehoge.rest.akismet.com/1.1/comment-check',
            headers=unittest.mock.ANY,
            data=unittest.mock.ANY,
            timeout=unittest.mock.ANY,
        )

    @unittest.mock.patch('requests.post')
    def test_spam_timeout(self, api_call):
        import requests
        request = self._makeRequest()
        akismet = self._makeOne()
        api_call.side_effect = requests.Timeout('connection timed out')
        self.assertEqual(akismet.spam(request, 'buy viagra'), False)

    # noinspection PyTypeChecker
    @unittest.mock.patch('requests.post')
    def test_spam_no_key(self, api_call):
        request = self._makeRequest()
        akismet = self._makeOne(key=None)
        self.assertEqual(akismet.spam(request, 'buy viagra'), False)
        assert not api_call.called


class TestBlackBoxProxyDetector(unittest.TestCase):

    def _makeOne(self, settings={}):
        from fanboi2.utils.proxy import BlackBoxProxyDetector
        blackbox = BlackBoxProxyDetector(settings)
        return blackbox

    def _makeResponse(self, status_code, content):
        class MockResponse(object):

            def __init__(self, status_code, content):
                self.status_code = status_code
                self.content = content

        return MockResponse(status_code, content)

    def test_init(self):
        blackbox = self._makeOne({'url': 'http://www.example.com/'})
        self.assertEqual(blackbox.url, 'http://www.example.com/')

    def test_init_default(self):
        blackbox = self._makeOne()
        self.assertEqual(
            blackbox.url,
            'http://www.shroomery.org/ythan/proxycheck.php')

    @unittest.mock.patch('requests.get')
    def test_check(self, api_call):
        api_call.return_value = self._makeResponse(200, b'Y')
        blackbox = self._makeOne({'url': 'http://www.example.com/'})
        self.assertEqual(blackbox.check('8.8.8.8'), b'Y')
        api_call.assert_called_with(
            'http://www.example.com/',
            headers=unittest.mock.ANY,
            timeout=unittest.mock.ANY,
            params={'ip': '8.8.8.8'},
        )

    @unittest.mock.patch('requests.get')
    def test_check_timeout(self, api_call):
        import requests
        api_call.side_effect = requests.Timeout('connection timed out')
        blackbox = self._makeOne()
        self.assertIsNone(blackbox.check('8.8.8.8'))

    @unittest.mock.patch('requests.get')
    def test_check_status_error(self, api_call):
        api_call.return_value = self._makeResponse(500, b'Error')
        blackbox = self._makeOne()
        self.assertIsNone(blackbox.check('8.8.8.8'))

    @unittest.mock.patch('requests.get')
    def test_check_response_error(self, api_call):
        api_call.return_value = self._makeResponse(200, b'X')
        blackbox = self._makeOne()
        self.assertIsNone(blackbox.check('8.8.8.8'))

    def test_evaluate(self):
        blackbox = self._makeOne()
        self.assertEqual(blackbox.evaluate(b'Y'), True)

    def test_evaluate_negative(self):
        blackbox = self._makeOne()
        self.assertEqual(blackbox.evaluate(b'N'), False)

    def test_evaluate_unknown(self):
        blackbox = self._makeOne()
        self.assertEqual(blackbox.evaluate(b'Z'), False)


class TestGetIPIntelProxyDetector(unittest.TestCase):

    def _makeOne(self, settings={}):
        from fanboi2.utils.proxy import GetIPIntelProxyDetector
        getipintel = GetIPIntelProxyDetector(settings)
        return getipintel

    def _makeResponse(self, status_code, content):
        class MockResponse(object):

            def __init__(self, status_code, content):
                self.status_code = status_code
                self.content = content

        return MockResponse(status_code, content)

    def test_init(self):
        getipintel = self._makeOne({
            'url': 'http://www.example.com/',
            'email': 'foo@example.com',
            'flags': 'm',
        })
        self.assertEqual(getipintel.url, 'http://www.example.com/')
        self.assertEqual(getipintel.email, 'foo@example.com')
        self.assertEqual(getipintel.flags, 'm')

    def test_init_default(self):
        with self.assertRaises(ValueError):
            self._makeOne()

    def test_init_default_with_email(self):
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertEqual(
            getipintel.url,
            'http://check.getipintel.net/check.php')
        self.assertEqual(getipintel.email, 'foo@example.com')
        self.assertEqual(getipintel.flags, None)

    @unittest.mock.patch('requests.get')
    def test_check(self, api_call):
        api_call.return_value = self._makeResponse(200, b'1')
        getipintel = self._makeOne({
            'url': 'http://www.example.com/',
            'email': 'foo@example.com',
            'flags': 'm',
        })
        self.assertEqual(getipintel.check('8.8.8.8'), b'1')
        api_call.assert_called_with(
            'http://www.example.com/',
            headers=unittest.mock.ANY,
            timeout=unittest.mock.ANY,
            params={
                'ip': '8.8.8.8',
                'contact': 'foo@example.com',
                'flags': 'm',
            },
        )

    @unittest.mock.patch('requests.get')
    def test_check_no_flags(self, api_call):
        api_call.return_value = self._makeResponse(200, b'1')
        getipintel = self._makeOne({
            'url': 'http://www.example.com/',
            'email': 'foo@example.com',
        })
        self.assertEqual(getipintel.check('8.8.8.8'), b'1')
        api_call.assert_called_with(
            'http://www.example.com/',
            headers=unittest.mock.ANY,
            timeout=unittest.mock.ANY,
            params={
                'ip': '8.8.8.8',
                'contact': 'foo@example.com',
            },
        )

    @unittest.mock.patch('requests.get')
    def test_check_timeout(self, api_call):
        import requests
        api_call.side_effect = requests.Timeout('connection timed out')
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertIsNone(getipintel.check('8.8.8.8'))

    @unittest.mock.patch('requests.get')
    def test_check_status_error(self, api_call):
        api_call.return_value = self._makeResponse(500, b'1')
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertIsNone(getipintel.check('8.8.8.8'))

    @unittest.mock.patch('requests.get')
    def test_check_response_error(self, api_call):
        api_call.return_value = self._makeResponse(200, b'-1')
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertIsNone(getipintel.check('8.8.8.8'))

    def test_evaluate(self):
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertEqual(getipintel.evaluate(b'0.9901'), True)

    def test_evaluate_negative(self):
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertEqual(getipintel.evaluate(b'0.99'), False)
        self.assertEqual(getipintel.evaluate(b'0.98'), False)
        self.assertEqual(getipintel.evaluate(b'0'), False)

    def test_evaluate_unknown(self):
        getipintel = self._makeOne({'email': 'foo@example.com'})
        self.assertEqual(getipintel.evaluate(b'-1'), False)


class TestProxyDetector(CacheMixin, RegistryMixin, unittest.TestCase):

    def _makeOne(self, settings={}, key=None):
        from fanboi2.utils import ProxyDetector
        proxy_detector = ProxyDetector(cache_region=self._getRegion())
        proxy_detector.configure_from_config(settings, key)
        return proxy_detector

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector')
    def test_init(self, GetIPIntelProxyDetector, BlackBoxProxyDetector):
        with unittest.mock.patch('fanboi2.utils.proxy.DETECTOR_PROVIDERS', {
                'blackbox': BlackBoxProxyDetector,
                'getipintel': GetIPIntelProxyDetector}):
            proxy_detector = self._makeOne({
                'proxy.providers': ['blackbox', 'getipintel'],
                'proxy.blackbox.url': 'http://www.example.com/blackbox',
                'proxy.getipintel.url': 'http://www.example.com/getipintel',
                'proxy.getipintel.email': 'foo@example.com',
                'proxy.getipintel.flags': 'm',
            }, 'proxy.')
            self.assertEqual(
                proxy_detector.providers,
                ['blackbox', 'getipintel']
            )
            BlackBoxProxyDetector.assert_called_with({
                'url': 'http://www.example.com/blackbox'
            })
            GetIPIntelProxyDetector.assert_called_with({
                'url': 'http://www.example.com/getipintel',
                'email': 'foo@example.com',
                'flags': 'm',
            })

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector.check')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector.check')
    def test_check(self, getipintel_check, blackbox_check):
        blackbox_check.return_value = b'N'
        getipintel_check.return_value = b'1'
        proxy_detector = self._makeOne({
            'providers': ['blackbox', 'getipintel'],
            'getipintel.email': 'foo@example.com',
        })
        self.assertEqual(proxy_detector.detect('8.8.8.8'), True)
        blackbox_check.assert_called_with('8.8.8.8')
        getipintel_check.assert_called_with('8.8.8.8')

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector.check')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector.check')
    def test_check_cached(self, getipintel_check, blackbox_check):
        blackbox_check.return_value = b'N'
        getipintel_check.return_value = b'0'
        proxy_detector = self._makeOne({
            'providers': ['blackbox', 'getipintel'],
            'getipintel.email': 'foo@example.com',
        })
        self.assertEqual(proxy_detector.detect('8.8.8.8'), False)
        self.assertEqual(proxy_detector.detect('8.8.8.8'), False)
        blackbox_check.assert_called_once_with('8.8.8.8')
        getipintel_check.assert_called_once_with('8.8.8.8')

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector.check')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector.check')
    def test_check_first_matched(self, getipintel_check, blackbox_check):
        blackbox_check.return_value = b'Y'
        getipintel_check.side_effective = AssertionError('should not called')
        proxy_detector = self._makeOne({
            'providers': ['blackbox', 'getipintel'],
            'getipintel.email': 'foo@example.com',
        })
        self.assertEqual(proxy_detector.detect('8.8.8.8'), True)
        blackbox_check.assert_called_with('8.8.8.8')
        self.assertFalse(getipintel_check.called)

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector.check')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector.check')
    def test_check_no_matched(self, getipintel_check, blackbox_check):
        blackbox_check.return_value = b'F'
        getipintel_check.return_value = b'0'
        proxy_detector = self._makeOne({
            'providers': ['blackbox', 'getipintel'],
            'getipintel.email': 'foo@example.com',
        })
        self.assertEqual(proxy_detector.detect('8.8.8.8'), False)
        blackbox_check.assert_called_with('8.8.8.8')
        getipintel_check.assert_called_with('8.8.8.8')

    @unittest.mock.patch('fanboi2.utils.proxy.BlackBoxProxyDetector.check')
    @unittest.mock.patch('fanboi2.utils.proxy.GetIPIntelProxyDetector.check')
    def test_check_error_skip(self, getipintel_check, blackbox_check):
        blackbox_check.return_value = None
        getipintel_check.return_value = None
        proxy_detector = self._makeOne({
            'providers': ['blackbox', 'getipintel'],
            'getipintel.email': 'foo@example.com',
        })
        self.assertEqual(proxy_detector.detect('8.8.8.8'), False)
        self.assertEqual(proxy_detector.detect('8.8.8.8'), False)
        self.assertEqual(blackbox_check.call_count, 2)
        self.assertEqual(getipintel_check.call_count, 2)
        blackbox_check.assert_called_with('8.8.8.8')
        getipintel_check.assert_called_with('8.8.8.8')


class TestRateLimiter(unittest.TestCase):

    def setUp(self):
        redis_conn._redis = DummyRedis()

    def tearDown(self):
        redis_conn._redis = None

    def _getTargetClass(self):
        from fanboi2.utils import RateLimiter
        return RateLimiter

    def _getHash(self, text):
        import hashlib
        return hashlib.md5(text.encode('utf8')).hexdigest()

    def _makeRequest(self):
        request = testing.DummyRequest()
        request.remote_addr = '127.0.0.1'
        request.user_agent = 'TestBrowser/1.0'
        request.referrer = 'http://www.example.com/foo'
        testing.setUp(request=request)
        return request

    def test_init(self):
        request = self._makeRequest()
        ratelimit = self._getTargetClass()(request, namespace='foobar')
        self.assertEqual(ratelimit.key,
                         "rate:foobar:%s" % self._getHash('127.0.0.1'))

    def test_init_no_namespace(self):
        request = self._makeRequest()
        ratelimit = self._getTargetClass()(request)
        self.assertEqual(ratelimit.key,
                         "rate:None:%s" % self._getHash('127.0.0.1'))

    def test_limit(self):
        request = self._makeRequest()
        ratelimit = self._getTargetClass()(request, namespace='foobar')
        self.assertFalse(ratelimit.limited())
        self.assertEqual(ratelimit.timeleft(), 0)
        ratelimit.limit(seconds=30)
        self.assertTrue(ratelimit.limited())
        self.assertEqual(ratelimit.timeleft(), 30)

    def test_limit_no_seconds(self):
        request = self._makeRequest()
        ratelimit = self._getTargetClass()(request, namespace='foobar')
        ratelimit.limit()
        self.assertTrue(ratelimit.limited())
        self.assertEqual(ratelimit.timeleft(), 10)