Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / tests / functional / test_search_form_host.py @ b373a5de

History | View | Annotate | Download (5.46 KB)

1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2006-2011 CS-SI
3
# License: GNU GPL v2 <http://www.gnu.org/licenses/gpl-2.0.html>
4

    
5
"""
6
Teste le formulaire de recherche avec un nom d'hôte.
7
"""
8
from nose.tools import assert_true, assert_equal
9
from datetime import datetime
10
import transaction
11

    
12
from vigiboard.tests import TestController
13
from vigilo.models.session import DBSession
14
from vigilo.models.tables import SupItemGroup, Host, Permission, \
15
                                    Event, CorrEvent, StateName, \
16
                                    User, UserGroup, DataPermission
17

    
18
def insert_deps():
19
    """Insère les dépendances nécessaires aux tests."""
20
    timestamp = datetime.now()
21

    
22
    hostgroup = SupItemGroup(name=u'foo', parent=None)
23
    DBSession.add(hostgroup)
24
    DBSession.flush()
25

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

    
40
    hostgroup.supitems.append(host)
41
    DBSession.flush()
42

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

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

    
66
class TestSearchFormHost(TestController):
67
    """Teste la récupération d'événements selon le nom d'hôte."""
68
    def setUp(self):
69
        super(TestSearchFormHost, self).setUp()
70
        perm = Permission.by_permission_name(u'vigiboard-access')
71
        user = User(
72
            user_name=u'user',
73
            fullname=u'',
74
            email=u'some.random@us.er',
75
        )
76
        usergroup = UserGroup(group_name=u'users')
77
        user.usergroups.append(usergroup)
78
        usergroup.permissions.append(perm)
79
        DBSession.add(user)
80
        DBSession.add(usergroup)
81
        DBSession.flush()
82

    
83
    def test_search_host_when_allowed(self):
84
        """Teste la recherche par hôte avec les bons droits."""
85
        # On crée un hôte avec une alerte.
86
        # L'hôte est rattaché à un groupe d'hôtes
87
        # pour lesquel l'utilisateur a les permissions.
88
        hostgroup = insert_deps()
89
        usergroup = UserGroup.by_group_name(u'users')
90
        DBSession.add(DataPermission(
91
            group=hostgroup,
92
            usergroup=usergroup,
93
            access=u'r',
94
        ))
95
        DBSession.flush()
96
        transaction.commit()
97

    
98
        # On envoie une requête avec recherche sur l'hôte créé,
99
        # on s'attend à recevoir 1 résultat.
100
        response = self.app.get('/?host=bar',
101
            extra_environ={'REMOTE_USER': 'user'})
102

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

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

    
113
    def test_search_inexistent_host(self):
114
        """Teste la recherche par hôte sur un hôte inexistant."""
115
        # On envoie une requête avec recherche sur un hôte
116
        # qui n'existe pas, on s'attend à n'obtenir aucun résultat.
117
        transaction.commit()
118
        response = self.app.get('/?host=bad',
119
            extra_environ={'REMOTE_USER': 'user'})
120

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

    
126
        # Il doit y avoir 1 seule colonne dans la ligne de résultats.
127
        # (la colonne contient le texte "Il n'y a aucun événément", traduit)
128
        cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
129
        print "There are %d columns in the result set" % len(cols)
130
        assert_equal(len(cols), 1)
131

    
132
    def test_search_host_when_disallowed(self):
133
        """Teste la recherche par hôte SANS les droits."""
134
        # On NE DONNE PAS l'autorisation aux utilisateurs
135
        # de voir l'alerte, donc elle ne doit jamais apparaître.
136
        insert_deps()
137
        transaction.commit()
138

    
139
        # On envoie une requête avec recherche sur l'hôte créé,
140
        # mais avec un utilisateur ne disposant pas des permissions adéquates.
141
        # On s'attend à n'obtenir aucun résultat.
142
        response = self.app.get('/?host=bar',
143
            extra_environ={'REMOTE_USER': 'user'})
144

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

    
150
        # Il doit y avoir 1 seule colonne dans la ligne de résultats.
151
        # (la colonne contient le texte "Il n'y a aucun événément", traduit)
152
        cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
153
        print "There are %d columns in the result set" % len(cols)
154
        assert_equal(len(cols), 1)