~fgallina/python-timeline-django/no-query-fix

« back to all changes in this revision

Viewing changes to timeline_django/tests/test_filters.py

  • Committer: Tarmac
  • Author(s): James Westby
  • Date: 2012-02-27 10:58:29 UTC
  • mfrom: (8.3.2 obscure-session-info)
  • Revision ID: tarmac@server-5390-20120227105829-xutkpmwzw0svpn6t
Tags: 0.0.1
[r=jml] Add filters to redact session and user queries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2012, Canonical Ltd
 
2
#
 
3
# This program is free software: you can redistribute it and/or modify
 
4
# it under the terms of the GNU Lesser General Public License as published by
 
5
# the Free Software Foundation, version 3 only.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU Lesser General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU Lesser General Public License
 
13
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
# GNU Lesser General Public License version 3 (see the file LICENSE).
 
15
 
 
16
import oops
 
17
import oops_timeline
 
18
from testtools import TestCase
 
19
import timeline
 
20
 
 
21
from .. import filters
 
22
 
 
23
 
 
24
class FilterSessionInfoTests(TestCase):
 
25
 
 
26
    def test_leaves_most_queries_be(self):
 
27
        query = 'some query'
 
28
        result = filters.filter_session_info(query)
 
29
        self.assertEqual(query, result)
 
30
 
 
31
    def test_redacts_django_session(self):
 
32
        query = 'select * from django_session'
 
33
        result = filters.filter_session_info(query)
 
34
        self.assertEqual('select <session query redacted>', result)
 
35
 
 
36
 
 
37
class FilterUserInfoTests(TestCase):
 
38
 
 
39
    def test_leaves_most_queries_be(self):
 
40
        query = 'some query'
 
41
        result = filters.filter_user_info(query)
 
42
        self.assertEqual(query, result)
 
43
 
 
44
    def test_redacts_django_session(self):
 
45
        query = 'select * from auth_user'
 
46
        result = filters.filter_user_info(query)
 
47
        self.assertEqual('select <user query redacted>', result)
 
48
 
 
49
 
 
50
class FilterSessionQueryTests(TestCase):
 
51
 
 
52
    def test_does_nothing_no_timeline(self):
 
53
        report = {}
 
54
        context = {}
 
55
        filters.filter_session_query(report, context)
 
56
        self.assertEqual({}, report)
 
57
        self.assertEqual({}, context)
 
58
 
 
59
    def test_filter_session_query(self):
 
60
        query = 'select * from django_session'
 
61
        test_timeline = [(0, 0, 'some-category', query, 'other')]
 
62
        report = dict(timeline=test_timeline)
 
63
        filters.filter_session_query(report, {})
 
64
        new_timeline = report['timeline']
 
65
        self.assertEqual(
 
66
                [(0, 0, 'some-category', filters.filter_session_info(query),
 
67
                    'other')],
 
68
                new_timeline)
 
69
 
 
70
 
 
71
class FilterUserQueryTests(TestCase):
 
72
 
 
73
    def test_does_nothing_no_timeline(self):
 
74
        report = {}
 
75
        context = {}
 
76
        filters.filter_user_query(report, context)
 
77
        self.assertEqual({}, report)
 
78
        self.assertEqual({}, context)
 
79
 
 
80
    def test_filter_user_query(self):
 
81
        query = 'select * from auth_user'
 
82
        test_timeline = [(0, 0, 'some-category', query, 'other')]
 
83
        report = dict(timeline=test_timeline)
 
84
        filters.filter_user_query(report, {})
 
85
        new_timeline = report['timeline']
 
86
        self.assertEqual(
 
87
                [(0, 0, 'some-category', filters.filter_user_info(query),
 
88
                    'other')],
 
89
                new_timeline)
 
90
 
 
91
 
 
92
class FakeConfig(object):
 
93
 
 
94
    def __init__(self):
 
95
        self.on_create = []
 
96
 
 
97
 
 
98
class InstallHooksTests(TestCase):
 
99
 
 
100
    def test_installs_filter_session_query(self):
 
101
        config = FakeConfig()
 
102
        filters.install_hooks(config)
 
103
        self.assertIn(filters.filter_session_query, config.on_create)
 
104
 
 
105
    def test_installs_filter_user_query(self):
 
106
        config = FakeConfig()
 
107
        filters.install_hooks(config)
 
108
        self.assertIn(filters.filter_user_query, config.on_create)
 
109
 
 
110
 
 
111
class IntegrationTests(TestCase):
 
112
 
 
113
    def test_publishing_oops_redacts_timeline(self):
 
114
        config = oops.Config()
 
115
        oops_timeline.install_hooks(config)
 
116
        filters.install_hooks(config)
 
117
        user_query = 'select * from auth_user'
 
118
        session_query = 'select * from django_session'
 
119
        test_timeline = timeline.Timeline()
 
120
        test_timeline.start('some-category', user_query).finish()
 
121
        test_timeline.start('some-other-category', session_query).finish()
 
122
        test_context = dict(timeline=test_timeline)
 
123
        test_oops = config.create(test_context)
 
124
        self.assertEqual(
 
125
            [filters.filter_user_info(user_query),
 
126
             filters.filter_session_info(session_query),
 
127
            ],
 
128
            [action[3] for action in test_oops['timeline']])