Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / tests / functional / test_search_form_host.py @ 0dcb87f7

History | View | Annotate | Download (5.58 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
Teste le formulaire de recherche avec un nom d'hôte.
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.session import DBSession
11
from vigilo.models.tables import SupItemGroup, Host, Permission, \
12
                                    Event, CorrEvent, StateName, \
13
                                    User, UserGroup, DataPermission
14
from vigilo.models.tables.grouphierarchy import GroupHierarchy
15

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

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

    
26
    DBSession.add(GroupHierarchy(
27
        parent=hostgroup,
28
        child=hostgroup,
29
        hops=0,
30
    ))
31
    DBSession.flush()
32

    
33
    host = Host(
34
        name=u'bar',
35
        checkhostcmd=u'',
36
        description=u'',
37
        hosttpl=u'',
38
        address=u'127.0.0.1',
39
        snmpport=42,
40
        snmpcommunity=u'public',
41
        snmpversion=u'3',
42
        weight=42,
43
    )
44
    DBSession.add(host)
45
    DBSession.flush()
46

    
47
    hostgroup.supitems.append(host)
48
    DBSession.flush()
49

    
50
    event = Event(
51
        supitem=host,
52
        timestamp=timestamp,
53
        current_state=StateName.statename_to_value(u'WARNING'),
54
        message=u'Hello world',
55
    )
56
    DBSession.add(event)
57
    DBSession.flush()
58

    
59
    correvent = CorrEvent(
60
        impact=42,
61
        priority=42,
62
        trouble_ticket=None,
63
        status=u'None',
64
        occurrence=42,
65
        timestamp_active=timestamp,
66
        cause=event,
67
    )
68
    correvent.events.append(event)
69
    DBSession.add(correvent)
70
    DBSession.flush()
71
    return hostgroup
72

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

    
92
    def test_search_host_when_allowed(self):
93
        """Teste la recherche par hôte avec les bons droits."""
94
        # On crée un hôte avec une alerte.
95
        # L'hôte est rattaché à un groupe d'hôtes
96
        # pour lesquel l'utilisateur a les permissions.
97
        hostgroup = insert_deps()
98
        usergroup = UserGroup.by_group_name(u'users')
99
        DBSession.add(DataPermission(
100
            group=hostgroup,
101
            usergroup=usergroup,
102
            access=u'r',
103
        ))
104
        DBSession.flush()
105
        transaction.commit()
106

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

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

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

    
122
    def test_search_inexistent_host(self):
123
        """Teste la recherche par hôte sur un hôte inexistant."""
124
        # On envoie une requête avec recherche sur un hôte
125
        # qui n'existe pas, on s'attend à n'obtenir aucun résultat.
126
        transaction.commit()
127
        response = self.app.get('/?host=bad',
128
            extra_environ={'REMOTE_USER': 'user'})
129

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

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

    
141
    def test_search_host_when_disallowed(self):
142
        """Teste la recherche par hôte SANS les droits."""
143
        # On NE DONNE PAS l'autorisation aux utilisateurs
144
        # de voir l'alerte, donc elle ne doit jamais apparaître.
145
        insert_deps()
146
        transaction.commit()
147

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

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

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