Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

vigiboard / vigiboard / tests / functional / test_search_form_host.py @ 6f6efdcd

History | View | Annotate | Download (5.36 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 Host, Permission, \
12
                                    Event, CorrEvent, StateName, \
13
                                    User, UserGroup, DataPermission
14
from vigilo.models.demo.functions import *
15

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

    
20
    hostgroup = add_supitemgroup(name=u'foo')
21

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

    
36
    hostgroup.supitems.append(host)
37
    DBSession.flush()
38

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

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

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

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

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

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

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

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

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

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

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

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

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

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