Passed
Branch master (15e5b6)
by Mathieu
01:29
created

FlaskrTestCase.test_plain_rule()   B

Complexity

Conditions 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 20
rs 8.5454
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
import re
0 ignored issues
show
Unused Code introduced by
The import re seems to be unused.
Loading history...
9
10
from spike import create_app
11
from spike.model import db
0 ignored issues
show
Unused Code introduced by
Unused db imported from spike.model
Loading history...
12
from spike.model.naxsi_rules import ValueTemplates, NaxsiRules, NaxsiRuleSets
0 ignored issues
show
Unused Code introduced by
Unused NaxsiRuleSets imported from spike.model.naxsi_rules
Loading history...
Unused Code introduced by
Unused ValueTemplates imported from spike.model.naxsi_rules
Loading history...
13
import unittest
14
15
16
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...
17
18
    def setUp(self):
19
        app = create_app()
20
        app.config['TESTING'] = True
21
        self.app = app.test_client()
22
23
    def tearDown(self):
24
        pass
25
26
    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...
27
        assert self.app.get('/robots.txt').data == 'User-agent: *\n Disallow: /'
28
29
    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...
30
        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...
31
        assert rv.status_code == 302
32
        assert urlparse(rv.location).path == '/rules'
33
34
    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...
35
        data = {
36
            'msg': 'this is a test message',
37
            'detection': 'DETECTION',
38
            'mz': 'BODY',
39
            'custom_mz_val': '',
40
            'negative': 'checked',
41
            'score_$SQL': 8,
42
            'score': '$SQL',
43
            'rmks': 'this is a test remark',
44
            'ruleset': 'scanner.rules'
45
        }
46
        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...
47
        rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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
        assert ('<li> - OK: created %d : %s</li>' % (rule.sid, rule.msg)) in rv.data
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after assert.
Loading history...
49
        assert rule.msg == data['msg']
50
        assert rule.detection == 'str:' + data['detection']
51
        assert rule.mz == data['mz']
52
        assert rule.score == data['score'] + ':' + str(data['score_$SQL'])
53
        assert rule.rmks == data['rmks']
54
        assert rule.ruleset == data['ruleset']
55
56
    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...
57
        current_sid = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first().sid
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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
        self.test_add_rule()
59
60
        sid = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first().sid
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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...
61
        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...
62
        assert rv.status_code == 302
63
64
        rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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...
65
        assert rule.sid == current_sid
66
67
    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...
68
        self.test_add_rule()
69
70
        _rule = NaxsiRules.query.order_by(NaxsiRules.sid.desc()).first()
0 ignored issues
show
Bug introduced by
The Class NaxsiRules 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...
71
        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...
72
        assert rv.status_code == 200
73
        rdate = strftime("%F - %H:%M", localtime(float(str(_rule.timestamp))))
74
        rmks = "# ".join(_rule.rmks.strip().split("\n"))
75
        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...
76
        negate = 'negative' if _rule.negative == 1 else ''
77
        expected = """
78
#
79
# sid: %s | date: %s
80
#
81
# %s
82
#
83
MainRule %s "%s" "msg:%s" "mz:%s" "s:%s" id:%s ;
84
85
""" % (_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...
86
        assert expected == rv.data
87
88
89
if __name__ == '__main__':
90
    unittest.main()
0 ignored issues
show
Coding Style introduced by
Final newline missing
Loading history...