Test Failed
Push — master ( 889ab2...b787f4 )
by -
01:35
created

FlaskrTestCase.test_add_rule()   B

Complexity

Conditions 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 24
rs 8.9713
1
from time import strftime, localtime
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
3
try:
4
    from urlparse import urlparse
5
except ImportError:  # python3
6
    from urllib.parse import urlparse
7
8
from spike import create_app
9
from spike.model import db
10
from spike.model.naxsi_rules import  NaxsiRules
11
import unittest
12
13
14
class FlaskrTestCase(unittest.TestCase):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
15
16
    def setUp(self):
17
        app = create_app()
18
        db.init_app(app)
19
        app.config['TESTING'] = True
20
        self.app = app.test_client()
21
22
    def tearDown(self):
23
        pass
24
25
    def test_robotstxt(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
26
        assert self.app.get('/robots.txt').data == 'User-agent: *\n Disallow: /'
27
28
    def test_redirect_root(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
29
        rv = self.app.get('/', follow_redirects=False)
0 ignored issues
show
Coding Style Naming introduced by
The name rv does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
30
        self.assertEqual(rv.status_code, 302)
31
        self.assertEqual(urlparse(rv.location).path, '/rules')
32
33
    def test_add_rule(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
34
        data = {
35
            'msg': 'this is a test message',
36
            'detection': 'DETECTION',
37
            'mz': 'BODY',
38
            'custom_mz_val': '',
39
            'negative': 'checked',
40
            'score_$SQL': 8,
41
            'score': '$SQL',
42
            'rmks': 'this is a test remark',
43
            'ruleset': 'scanner.rules'
44
        }
45
        rv = self.app.post('/rules/new', data=data, follow_redirects=True)
0 ignored issues
show
Coding Style Naming introduced by
The name rv does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
46
        rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
47
48
        self.assertIn(('<li> - OK: created %d : %s</li>' % (rule.sid, rule.msg)), rv.data)
49
        self.assertEqual(rule.msg, data['msg'])
50
        self.assertEqual(rule.detection, 'str:' + data['detection'])
51
        self.assertEqual(rule.mz, data['mz'])
52
        self.assertEqual(rule.score, data['score'] + ':' + str(data['score_$SQL']))
53
        self.assertEqual(rule.rmks, data['rmks'])
54
        self.assertEqual(rule.ruleset, data['ruleset'])
55
56
        db.session.delete(NaxsiRules.query.filter(rule.sid == NaxsiRules.sid).first())
57
58
    def test_del_rule(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
59
        current_sid = int(NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first().sid)
60
        db.session.add(NaxsiRules(u'POUET', 'str:test', u'BODY', u'$SQL:8', current_sid+1, u'web_server.rules',
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (111/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
61
         u'f hqewifueiwf hueiwhf uiewh fiewh fhw', '1', True, 1457101045))
62
63
        sid = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first().sid
64
        rv = self.app.get('/rules/del/%d' % sid)
0 ignored issues
show
Coding Style Naming introduced by
The name rv does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
65
        self.assertEqual(rv.status_code, 302)
66
67
        rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
68
        self.assertEqual(rule.sid, current_sid)
69
70
    def test_plain_rule(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
71
        _rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
72
        rv = self.app.get('/rules/plain/%d' % _rule.sid)
0 ignored issues
show
Coding Style Naming introduced by
The name rv does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
73
        self.assertEqual(rv.status_code, 200)
74
        rdate = strftime("%F - %H:%M", localtime(float(str(_rule.timestamp))))
75
        rmks = "# ".join(_rule.rmks.strip().split("\n"))
76
        detect = _rule.detection.lower() if _rule.detection.startswith("str:") else _rule.detection
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (99/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
77
        negate = 'negative' if _rule.negative == 1 else ''
78
        expected = """
79
#
80
# sid: %s | date: %s
81
#
82
# %s
83
#
84
MainRule %s "%s" "msg:%s" "mz:%s" "s:%s" id:%s ;
85
86
""" % (_rule.sid, rdate, rmks, negate, detect, _rule.msg, _rule.mz, _rule.score, _rule.sid)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (91/90).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
87
        self.assertEqual(expected, rv.data)
88
        db.session.delete(NaxsiRules.query.filter(_rule.sid == NaxsiRules.sid).first())
89
90
91
if __name__ == '__main__':
92
    unittest.main()
93