vigiboard / vigiboard / tests / functional / test_search_form_host.py @ 5dbfa80d
History | View | Annotate | Download (4.94 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.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 |
hostgroup = HostGroup( |
24 |
name=u'foo',
|
25 |
) |
26 |
DBSession.add(hostgroup) |
27 |
DBSession.flush() |
28 |
|
29 |
host = Host( |
30 |
name=u'bar',
|
31 |
checkhostcmd=u'',
|
32 |
description=u'',
|
33 |
hosttpl=u'',
|
34 |
mainip=u'127.0.0.1',
|
35 |
snmpport=42,
|
36 |
snmpcommunity=u'public',
|
37 |
snmpversion=u'3',
|
38 |
weight=42,
|
39 |
) |
40 |
DBSession.add(host) |
41 |
DBSession.flush() |
42 |
|
43 |
hostgroup.hosts.append(host) |
44 |
DBSession.flush() |
45 |
|
46 |
event = Event( |
47 |
supitem=host, |
48 |
timestamp=timestamp, |
49 |
current_state=StateName.statename_to_value(u'WARNING'),
|
50 |
message=u'Hello world',
|
51 |
) |
52 |
DBSession.add(event) |
53 |
DBSession.flush() |
54 |
|
55 |
correvent = CorrEvent( |
56 |
impact=42,
|
57 |
priority=42,
|
58 |
trouble_ticket=None,
|
59 |
status=u'None',
|
60 |
occurrence=42,
|
61 |
timestamp_active=timestamp, |
62 |
cause=event, |
63 |
) |
64 |
correvent.events.append(event) |
65 |
DBSession.add(correvent) |
66 |
DBSession.flush() |
67 |
return hostgroup
|
68 |
|
69 |
class TestSearchFormHost(TestController): |
70 |
"""Teste la récupération d'événements selon le nom d'hôte."""
|
71 |
|
72 |
def test_search_host_when_allowed(self): |
73 |
"""Teste la recherche par hôte avec les bons droits."""
|
74 |
# On crée un hôte avec une alerte.
|
75 |
# L'hôte est rattaché à un groupe d'hôtes
|
76 |
# pour lesquel l'utilisateur a les permissions.
|
77 |
hostgroup = insert_deps() |
78 |
manage = Permission.by_permission_name(u'manage')
|
79 |
manage.hostgroups.append(hostgroup) |
80 |
DBSession.flush() |
81 |
transaction.commit() |
82 |
|
83 |
# On envoie une requête avec recherche sur l'hôte créé,
|
84 |
# on s'attend à recevoir 1 résultat.
|
85 |
response = self.app.get('/?host=bar', |
86 |
extra_environ={'REMOTE_USER': 'manager'}) |
87 |
|
88 |
# Il doit y avoir 1 seule ligne de résultats.
|
89 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
90 |
print "There are %d rows in the result set" % len(rows) |
91 |
assert_equal(len(rows), 1) |
92 |
|
93 |
# Il doit y avoir plusieurs colonnes dans la ligne de résultats.
|
94 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
95 |
print "There are %d columns in the result set" % len(cols) |
96 |
assert_true(len(cols) > 1) |
97 |
|
98 |
def test_search_inexistent_host(self): |
99 |
"""Teste la recherche par hôte sur un hôte inexistant."""
|
100 |
# On envoie une requête avec recherche sur un hôte
|
101 |
# qui n'existe pas, on s'attend à n'obtenir aucun résultat.
|
102 |
response = self.app.get('/?host=bad', |
103 |
extra_environ={'REMOTE_USER': 'manager'}) |
104 |
|
105 |
# Il doit y avoir 1 seule ligne de résultats.
|
106 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
107 |
print "There are %d rows in the result set" % len(rows) |
108 |
assert_equal(len(rows), 1) |
109 |
|
110 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
111 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
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_equal(len(cols), 1) |
115 |
|
116 |
def test_search_host_when_disallowed(self): |
117 |
"""Teste la recherche par hôte SANS les droits."""
|
118 |
# On NE DONNE PAS l'autorisation aux utilisateurs
|
119 |
# de voir l'alerte, donc elle ne doit jamais apparaître.
|
120 |
insert_deps() |
121 |
transaction.commit() |
122 |
|
123 |
# On envoie une requête avec recherche sur l'hôte créé,
|
124 |
# mais avec un utilisateur ne disposant pas des permissions adéquates.
|
125 |
# On s'attend à n'obtenir aucun résultat.
|
126 |
response = self.app.get('/?host=bar', |
127 |
extra_environ={'REMOTE_USER': 'manager'}) |
128 |
|
129 |
# Il doit y avoir 1 seule ligne de résultats.
|
130 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
131 |
print "There are %d rows in the result set" % len(rows) |
132 |
assert_equal(len(rows), 1) |
133 |
|
134 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
135 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
136 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
137 |
print "There are %d columns in the result set" % len(cols) |
138 |
assert_equal(len(cols), 1) |
139 |
|