vigiboard / vigiboard / tests / functional / test_search_form_hostgroup.py @ 53fda08d
History | View | Annotate | Download (5.34 KB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
"""
|
3 |
Teste le formulaire de recherche avec un groupe d'hôtes.
|
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 vigiboard.model import DBSession, HostGroup, Host, Permission, \ |
11 |
LowLevelService, Event, CorrEvent, StateName |
12 |
|
13 |
def insert_deps(): |
14 |
"""Insère les dépendances nécessaires aux tests."""
|
15 |
timestamp = datetime.now() |
16 |
DBSession.add(StateName(statename=u'OK', order=1)) |
17 |
DBSession.add(StateName(statename=u'UNKNOWN', order=1)) |
18 |
DBSession.add(StateName(statename=u'WARNING', order=1)) |
19 |
DBSession.add(StateName(statename=u'CRITICAL', order=1)) |
20 |
DBSession.flush() |
21 |
|
22 |
hostgroup = HostGroup( |
23 |
name=u'foo',
|
24 |
) |
25 |
DBSession.add(hostgroup) |
26 |
DBSession.flush() |
27 |
|
28 |
host = Host( |
29 |
name=u'bar',
|
30 |
checkhostcmd=u'',
|
31 |
description=u'',
|
32 |
hosttpl=u'',
|
33 |
mainip=u'127.0.0.1',
|
34 |
snmpport=42,
|
35 |
snmpcommunity=u'public',
|
36 |
snmpversion=u'3',
|
37 |
weight=42,
|
38 |
) |
39 |
DBSession.add(host) |
40 |
DBSession.flush() |
41 |
|
42 |
hostgroup.hosts.append(host) |
43 |
DBSession.flush() |
44 |
|
45 |
event = Event( |
46 |
supitem=host, |
47 |
timestamp=timestamp, |
48 |
current_state=StateName.statename_to_value(u'WARNING'),
|
49 |
message=u'Hello world',
|
50 |
) |
51 |
DBSession.add(event) |
52 |
DBSession.flush() |
53 |
|
54 |
correvent = CorrEvent( |
55 |
impact=42,
|
56 |
priority=42,
|
57 |
trouble_ticket=None,
|
58 |
status=u'None',
|
59 |
occurrence=42,
|
60 |
timestamp_active=timestamp, |
61 |
cause=event, |
62 |
) |
63 |
correvent.events.append(event) |
64 |
DBSession.add(correvent) |
65 |
DBSession.flush() |
66 |
return hostgroup
|
67 |
|
68 |
class TestSearchFormHostGroup(TestController): |
69 |
"""Teste la récupération d'événements selon le groupe d'hôtes."""
|
70 |
|
71 |
def test_search_hostgroup_when_allowed(self): |
72 |
"""Teste la recherche par hostgroup avec les bons droits d'accès."""
|
73 |
# On crée un groupe d'hôte appelé 'foo',
|
74 |
# contenant un hôte 'bar', ainsi qu'un événement
|
75 |
# et un événement corrélé sur cet hôte.
|
76 |
# De plus, on donne l'autorisation aux utilisateurs
|
77 |
# ayant la permission 'manage' de voir cette alerte.
|
78 |
hostgroup = insert_deps() |
79 |
manage = Permission.by_permission_name(u'manage')
|
80 |
manage.hostgroups.append(hostgroup) |
81 |
DBSession.flush() |
82 |
transaction.commit() |
83 |
|
84 |
# On envoie une requête avec recherche sur le groupe d'hôtes créé,
|
85 |
# on s'attend à recevoir 1 résultat.
|
86 |
response = self.app.get('/?hostgroup=foo', |
87 |
extra_environ={'REMOTE_USER': 'manager'}) |
88 |
|
89 |
# Il doit y avoir 1 seule ligne de résultats.
|
90 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
91 |
print "There are %d rows in the result set" % len(rows) |
92 |
assert_equal(len(rows), 1) |
93 |
|
94 |
# Il doit y avoir plusieurs colonnes dans la ligne de résultats.
|
95 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
96 |
print "There are %d columns in the result set" % len(cols) |
97 |
assert_true(len(cols) > 1) |
98 |
|
99 |
def test_search_inexistent_hostgroup(self): |
100 |
"""Teste la recherche par hostgroup sur un groupe inexistant."""
|
101 |
# On envoie une requête avec recherche sur un groupe d'hôtes
|
102 |
# qui n'existe pas, on s'attend à n'obtenir aucun résultat.
|
103 |
response = self.app.get('/?hostgroup=foot', |
104 |
extra_environ={'REMOTE_USER': 'manager'}) |
105 |
|
106 |
# Il doit y avoir 1 seule ligne de résultats.
|
107 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
108 |
print "There are %d rows in the result set" % len(rows) |
109 |
assert_equal(len(rows), 1) |
110 |
|
111 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
112 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
113 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
114 |
print "There are %d columns in the result set" % len(cols) |
115 |
assert_equal(len(cols), 1) |
116 |
|
117 |
def test_search_hostgroup_when_disallowed(self): |
118 |
"""Teste la recherche par hostgroup SANS les droits d'accès."""
|
119 |
# On crée un groupe d'hôte appelé 'foo',
|
120 |
# contenant un hôte 'bar', ainsi qu'un événement
|
121 |
# et un événement corrélé sur cet hôte.
|
122 |
# MAIS, on NE DONNE PAS l'autorisation aux utilisateurs
|
123 |
# de voir cette alerte, donc elle ne doit jamais apparaître.
|
124 |
insert_deps() |
125 |
transaction.commit() |
126 |
|
127 |
# On envoie une requête avec recherche sur le groupe d'hôtes créé,
|
128 |
# mais avec un utilisateur ne disposant pas des permissions adéquates.
|
129 |
# On s'attend à n'obtenir aucun résultat.
|
130 |
response = self.app.get('/?hostgroup=foo', |
131 |
extra_environ={'REMOTE_USER': 'manager'}) |
132 |
|
133 |
# Il doit y avoir 1 seule ligne de résultats.
|
134 |
rows = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr')
|
135 |
print "There are %d rows in the result set" % len(rows) |
136 |
assert_equal(len(rows), 1) |
137 |
|
138 |
# Il doit y avoir 1 seule colonne dans la ligne de résultats.
|
139 |
# (la colonne contient le texte "Il n'y a aucun événément", traduit)
|
140 |
cols = response.lxml.xpath('//table[@class="vigitable"]/tbody/tr/td')
|
141 |
print "There are %d columns in the result set" % len(cols) |
142 |
assert_equal(len(cols), 1) |
143 |
|