Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / tests / functional / test_search_form_misc.py @ 5dbfa80d

History | View | Annotate | Download (4.72 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
Teste le formulaire de recherche avec divers champs.
4
"""
5
from nose.tools import assert_true, assert_equal
6
from datetime import datetime
7
import transaction
8

    
9
from vigiboard.tests import TestController
10
from vigilo.models.configure import DBSession
11
from vigilo.models import HostGroup, Host, Permission, \
12
                            Event, CorrEvent, StateName
13

    
14
def insert_deps():
15
    """Insère les dépendances nécessaires aux tests."""
16
    timestamp = datetime.now()
17
    DBSession.add(StateName(statename=u'OK', order=1))
18
    DBSession.add(StateName(statename=u'UNKNOWN', order=1))
19
    DBSession.add(StateName(statename=u'WARNING', order=1))
20
    DBSession.add(StateName(statename=u'CRITICAL', order=1))
21
    DBSession.flush()
22

    
23
    host = Host(
24
        name=u'bar',
25
        checkhostcmd=u'',
26
        description=u'',
27
        hosttpl=u'',
28
        mainip=u'127.0.0.1',
29
        snmpport=42,
30
        snmpcommunity=u'public',
31
        snmpversion=u'3',
32
        weight=42,
33
    )
34
    DBSession.add(host)
35
    DBSession.flush()
36

    
37
    hostgroup = HostGroup(
38
        name=u'foo',
39
    )
40
    hostgroup.hosts.append(host)
41
    DBSession.add(hostgroup)
42
    DBSession.flush()
43

    
44
    event = Event(
45
        supitem=host,
46
        timestamp=timestamp,
47
        current_state=StateName.statename_to_value(u'WARNING'),
48
        message=u'Hello world',
49
    )
50
    DBSession.add(event)
51
    DBSession.flush()
52

    
53
    correvent = CorrEvent(
54
        impact=42,
55
        priority=42,
56
        trouble_ticket=u'FOO BAR BAZ',
57
        status=u'None',
58
        occurrence=42,
59
        timestamp_active=timestamp,
60
        cause=event,
61
    )
62
    correvent.events.append(event)
63
    DBSession.add(correvent)
64
    DBSession.flush()
65

    
66
    # On attribut les permissions.
67
    manage = Permission.by_permission_name(u'manage')
68
    manage.hostgroups.append(hostgroup)
69
    DBSession.flush()
70
    return timestamp
71

    
72

    
73
class TestSearchFormMisc(TestController):
74
    """Teste la récupération d'événements selon le nom d'hôte."""
75
    def test_search_by_output(self):
76
        """Teste la recherche sur le message issu de Nagios."""
77
        insert_deps()
78
        transaction.commit()
79

    
80
        # Permet également de vérifier que la recherche est
81
        # insensible à la casse.
82
        response = self.app.get('/?output=hello',
83
            extra_environ={'REMOTE_USER': 'manager'})
84

    
85
        # Il doit y avoir 1 seule ligne de résultats.
86
        rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
87
        print "There are %d rows in the result set" % len(rows)
88
        assert_equal(len(rows), 1)
89

    
90
        # Il doit y avoir plusieurs colonnes dans la ligne de résultats.
91
        cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
92
        print "There are %d columns in the result set" % len(cols)
93
        assert_true(len(cols) > 1)
94

    
95
    def test_search_by_trouble_ticket(self):
96
        """Teste la recherche sur le ticket d'incident."""
97
        insert_deps()
98
        transaction.commit()
99

    
100
        # Permet également de vérifier que la recherche est
101
        # insensible à la casse.
102
        response = self.app.get('/?trouble_ticket=bar',
103
            extra_environ={'REMOTE_USER': 'manager'})
104
        transaction.commit()
105

    
106
        # Il doit y avoir 1 seule ligne de résultats.
107
        rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
108
        print "There are %d rows in the result set" % len(rows)
109
        assert_equal(len(rows), 1)
110

    
111
        # Il doit y avoir plusieurs colonnes dans la ligne de résultats.
112
        cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
113
        print "There are %d columns in the result set" % len(cols)
114
        assert_true(len(cols) > 1)
115

    
116
    def test_search_by_dates(self):
117
        """Teste la recherche par dates."""
118
        timestamp = insert_deps()
119
        transaction.commit()
120
        from_date = timestamp.strftime('%Y-%m-%d %I:%M:%S %p')
121
        to_date = datetime.max.strftime('%Y-%m-%d %I:%M:%S %p')
122

    
123
        # Permet également de vérifier que la recherche
124
        # par date est inclusive.
125
        response = self.app.get(
126
            '/?from_date=%(from_date)s&to_date=%(to_date)s' % {
127
                'from_date': from_date,
128
                'to_date': to_date,
129
            },
130
            extra_environ={'REMOTE_USER': 'manager'})
131
        transaction.commit()
132

    
133
        # Il doit y avoir 1 seule ligne de résultats.
134
        rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
135
        print "There are %d rows in the result set" % len(rows)
136
        assert_equal(len(rows), 1)
137

    
138
        # Il doit y avoir plusieurs colonnes dans la ligne de résultats.
139
        cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
140
        print "There are %d columns in the result set" % len(cols)
141
        assert_true(len(cols) > 1)
142