Completed
Push — master ( 00fa6c...ba46ed )
by -
01:48
created

FlaskrTestCase.__create_whitelist()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
1
import unittest
2
from time import time
3
4
from spike import create_app
5
from spike.model import db
6
from spike.model.naxsi_whitelist import NaxsiWhitelist
7
8
try:
9
    from urlparse import urlparse
0 ignored issues
show
Unused Code introduced by
Unused urlparse imported from urlparse
Loading history...
10
except ImportError:  # python3
11
    from urllib.parse import urlparse
12
13
14
class FlaskrTestCase(unittest.TestCase):
15
    def setUp(self):
16
        app = create_app()
17
        db.init_app(app)
18
        app.config['TESTING'] = True
19
        self.app = app.test_client()
20
        self.wid = self.__create_whitelist()
21
22
    def tearDown(self):
23
        db.session.delete(NaxsiWhitelist.query.filter(NaxsiWhitelist.id == self.wid).first())
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
24
        db.session.commit()
25
26
    def __create_whitelist(self):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
27
        _wlist = NaxsiWhitelist(wid='wl:42', timestamp=int(time()), whitelistset='WORDPRESS', mz='BODY', active=1,
28
                               negative=False)
29
        db.session.add(_wlist)
30
        db.session.commit()
31
        return NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first().id
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
32
33
    def test_index(self):
34
        rv = self.app.get('/whitelists/')
35
        self.assertEqual(rv.status_code, 200)
36
37
    def test_plain(self):
38
        _id = NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first().id
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
39
40
        rv = self.app.get('/whitelists/plain/%d' % (_id + 1))
41
        self.assertEqual(rv.status_code, 302)
42
43
        rv = self.app.get('/whitelists/plain/%d' % _id)
44
        self.assertIn('BasicRule  wl:wl:42 "mz:BODY";', str(rv.data))
45
46
    def test_view(self):
47
        _id = NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first().id
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
48
49
        rv = self.app.get('/whitelists/view/%d' % (_id + 1))
50
        self.assertEqual(rv.status_code, 302)
51
52
    def test_del(self):
53
        wlist = NaxsiWhitelist(wid='wl:42', timestamp=int(time()), whitelistset='WORDPRESS', mz='BODY', active=1,
54
                               negative=False)
55
        db.session.add(wlist)
56
        db.session.commit()
57
        _id = NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first().id
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
58
59
        rv = self.app.get('/whitelists/del/%d' % (_id + 1))
60
        self.assertEqual(302, rv.status_code)
61
62
        rv = self.app.get('/whitelists/del/%d' % _id, follow_redirects=True)
63
        self.assertIn('Successfully deleted %d' % _id, str(rv.data))
64
65
    def test_new(self):
66
        rv = self.app.get('/whitelists/new')
67
        self.assertEqual(rv.status_code, 200)
68
69
        rv = self.app.post('/whitelists/new', data={'wid':'wl:42',
70
                                                    'mz':'BODY', 'custom_mz_val':'',  'whitelistset': 'WORDPRESS'})
0 ignored issues
show
Coding Style introduced by
Exactly one space required after comma
'mz':'BODY', 'custom_mz_val':'', 'whitelistset': 'WORDPRESS'})
^
Loading history...
71
        self.assertEqual(rv.status_code, 200)
72
        _wlist = NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
73
        self.assertEqual(_wlist.mz, 'BODY')
74
        self.assertEqual(_wlist.negative, 0)
75
        self.assertEqual(_wlist.wid, 'wl:42')
76
77
        rv = self.app.post('/whitelists/new', data={'mz': 'BODY', 'custom_mz_val': '', 'whitelistset': 'WORDPRESS'})
78
        self.assertIn('Please enter a wid', str(rv.data))
79
        rv = self.app.post('/whitelists/new', data={'mz': 'BODY', 'custom_mz_val': '', 'wid':'wl:42'})
80
        self.assertIn('Please enter a whitelistset', str(rv.data))
81
82
        rv = self.app.post('/whitelists/new', data={'mz': 'BODY', 'custom_mz_val': '', 'wid': 'wl:abcdef',
83
                                                    'whitelistset': 'WORDPRESS'}, follow_redirects=True)
84
        self.assertIn('Illegal character in the whitelist id.', str(rv.data))
85
86
        db.session.delete(NaxsiWhitelist.query.order_by(NaxsiWhitelist.id.desc()).first())
0 ignored issues
show
Bug introduced by
The Class NaxsiWhitelist does not seem to have a member named query.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
87
        db.session.commit()
88
89
    def test_generate(self):
90
        rv = self.app.get('/whitelists/generate')
91
        self.assertEqual(rv.status_code, 200)
92
93
        rv = self.app.post('/whitelists/generate')
94
        self.assertEqual(rv.status_code, 200)
95
        self.assertIn('Please input nxlogs', str(rv.data))
96
97
        rv = self.app.post('/whitelists/generate', data={'nxlogs': 'pouet,lol'})
98
        self.assertEqual(rv.status_code, 200)
99
        self.assertIn('string "ip=" not found.', str(rv.data))
100
101
        rv = self.app.post('/whitelists/generate', data={'nxlogs': 'ip=1234'})
102
        self.assertEqual(rv.status_code, 200)
103
        self.assertIn('string "," not found.', str(rv.data))
104
105
        logs = "2013/11/10 07:36:19 [error] 8278#0: *5932 NAXSI_FMT: ip=X.X.X.X&server=Y.Y.Y.Y&"\
106
                "uri=/phpMyAdmin-2.8.2/scripts/setup.php&learning=0&vers=0.52&total_processed=472&total_blocked=204&"\
107
                "block=0&cscore0=$UWA&score0=8&zone0=HEADERS&id0=42000227&var_name0=user-agent, client: X.X.X.X,"\
108
                'server: blog.memze.ro, request: "GET /phpMyAdmin-2.8.2/scripts/setup.php HTTP/1.1", host: "X.X.X.X"'
109
        rv = self.app.post('/whitelists/generate', data={'nxlogs': logs})
110
        self.assertEqual(rv.status_code, 200)
111
        self.assertIn('BasicRule wl:42000227 "mz:user-agent:HEADERS"', str(rv.data))
112
113