Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.39 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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