vigiboard / vigiboard / tests / functional / test_search_form_service.py @ 5dbfa80d
History | View | Annotate | Download (6.97 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""
|
3 |
Teste le formulaire de recherche avec un nom de service.
|
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 ServiceGroup, HostGroup, \ |
12 |
Host, Permission, StateName, \ |
13 |
LowLevelService, Event, CorrEvent |
14 |
|
15 |
def insert_deps(): |
16 |
"""Insère les dépendances nécessaires aux tests."""
|
17 |
timestamp = datetime.now() |
18 |
DBSession.add(StateName(statename=u'OK', order=1)) |
19 |
DBSession.add(StateName(statename=u'UNKNOWN', order=1)) |
20 |
DBSession.add(StateName(statename=u'WARNING', order=1)) |
21 |
DBSession.add(StateName(statename=u'CRITICAL', order=1)) |
22 |
DBSession.flush() |
23 |
|
24 |
hostgroup = HostGroup( |
25 |
name=u'foo',
|
26 |
) |
27 |
DBSession.add(hostgroup) |
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 |
servicegroup = ServiceGroup( |
47 |
name=u'foo',
|
48 |
) |
49 |
DBSession.add(servicegroup) |
50 |
|
51 |
service = LowLevelService( |
52 |
host=host, |
53 |
command=u'',
|
54 |
weight=42,
|
55 |
servicename=u'baz',
|
56 |
op_dep=u'&',
|
57 |
) |
58 |
DBSession.add(service) |
59 |
DBSession.flush() |
60 |
|
61 |
servicegroup.services.append(service) |
62 |
DBSession.flush() |
63 |
|
64 |
event = Event( |
65 |
supitem=service, |
66 |
timestamp=timestamp, |
67 |
current_state=StateName.statename_to_value(u'WARNING'),
|
68 |
message=u'Hello world',
|
69 |
) |
70 |
DBSession.add(event) |
71 |
DBSession.flush() |
72 |
|
73 |
correvent = CorrEvent( |
74 |
impact=42,
|
75 |
priority=42,
|
76 |
trouble_ticket=None,
|
77 |
status=u'None',
|
78 |
occurrence=42,
|
79 |
timestamp_active=timestamp, |
80 |
cause=event, |
81 |
) |
82 |
correvent.events.append(event) |
83 |
DBSession.add(correvent) |
84 |
DBSession.flush() |
85 |
return (hostgroup, servicegroup)
|
86 |
|
87 |
class TestSearchFormService(TestController): |
88 |
"""Teste la récupération d'événements selon le groupe de services."""
|
89 |
|
90 |
def test_search_service_when_allowed_by_hostgroup(self): |
91 |
"""
|
92 |
Teste la recherche par service avec des droits implicites
|
93 |
(droits accordés car l'utilisateur a les droits sur l'hôte).
|
94 |
"""
|
95 |
# On crée un service avec une alerte.
|
96 |
# Le service est rattaché à un hôte appartenant
|
97 |
# à un groupe d'hôtes pour lesquels l'utilisateur
|
98 |
# a les permissions.
|
99 |
hostgroup = insert_deps()[0]
|
100 |
print "Adding permission for 'manager' on host group '%s'" % \ |
101 |
hostgroup.name |
102 |
manage = Permission.by_permission_name(u'manage')
|
103 |
manage.hostgroups.append(hostgroup) |
104 |
DBSession.flush() |
105 |
transaction.commit() |
106 |
|
107 |
# On envoie une requête avec recherche sur le service créé,
|
108 |
# on s'attend à recevoir 1 résultat.
|
109 |
response = self.app.get('/?service=baz', |
110 |
extra_environ={'REMOTE_USER': 'manager'}) |
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_service_when_allowed_by_servicegroup(self): |
123 |
"""
|
124 |
Teste la recherche par service avec des droits explicites
|
125 |
(droits accordés car l'utilisateur a explicitement les droits
|
126 |
sur ce service).
|
127 |
"""
|
128 |
# On crée un service avec une alerte.
|
129 |
# Le service est rattaché à un groupe de services
|
130 |
# pour lesquel l'utilisateur a les permissions.
|
131 |
servicegroup = insert_deps()[1]
|
132 |
print "Adding permission for 'manager' on service group '%s'" % \ |
133 |
servicegroup.name |
134 |
manage = Permission.by_permission_name(u'manage')
|
135 |
manage.servicegroups.append(servicegroup) |
136 |
DBSession.flush() |
137 |
transaction.commit() |
138 |
|
139 |
# On envoie une requête avec recherche sur le service créé,
|
140 |
# on s'attend à recevoir 1 résultat.
|
141 |
response = self.app.get('/?service=baz', |
142 |
extra_environ={'REMOTE_USER': 'manager'}) |
143 |
|
144 |
# Il doit y avoir 1 seule ligne de résultats.
|
145 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
146 |
print "There are %d rows in the result set" % len(rows) |
147 |
assert_equal(len(rows), 1) |
148 |
|
149 |
# Il doit y avoir plusieurs colonnes dans la ligne de résultats.
|
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_true(len(cols) > 1) |
153 |
|
154 |
def test_search_inexistent_service(self): |
155 |
"""Teste la recherche par service sur un service inexistant."""
|
156 |
# On envoie une requête avec recherche sur un service
|
157 |
# qui n'existe pas, on s'attend à n'obtenir aucun résultat.
|
158 |
response = self.app.get('/?servicegroup=bad', |
159 |
extra_environ={'REMOTE_USER': 'manager'}) |
160 |
|
161 |
# Il doit y avoir 1 seule ligne de résultats.
|
162 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
163 |
print "There are %d rows in the result set" % len(rows) |
164 |
assert_equal(len(rows), 1) |
165 |
|
166 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
167 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
168 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
169 |
assert_equal(len(cols), 1) |
170 |
|
171 |
def test_search_service_when_disallowed(self): |
172 |
"""Teste la recherche par service SANS les droits."""
|
173 |
# On NE DONNE PAS l'autorisation aux utilisateurs
|
174 |
# de voir l'alerte, donc elle ne doit jamais apparaître.
|
175 |
insert_deps() |
176 |
transaction.commit() |
177 |
|
178 |
# On envoie une requête avec recherche sur le service créé,
|
179 |
# mais avec un utilisateur ne disposant pas des permissions adéquates.
|
180 |
# On s'attend à n'obtenir aucun résultat.
|
181 |
response = self.app.get('/?hostgroup=baz', |
182 |
extra_environ={'REMOTE_USER': 'manager'}) |
183 |
|
184 |
# Il doit y avoir 1 seule ligne de résultats.
|
185 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
186 |
print "There are %d rows in the result set" % len(rows) |
187 |
assert_equal(len(rows), 1) |
188 |
|
189 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
190 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
191 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
192 |
print "There are %d columns in the result set" % len(cols) |
193 |
assert_equal(len(cols), 1) |
194 |
|