Removed dep on API
[ninja.git] / test / unit_test / tests / recurring_downtime_permission_Test.php
blob9760fdbd82b5d2fc690ccf24372b32592fa3ae12
1 <?php
2 class Recurring_downtime_permission_Test extends PHPUnit_Framework_TestCase
4 public function createDowntime($data)
6 foreach (ScheduleDate_Model::$valid_fields as $field) {
7 $this->assertArrayHasKey($field, $data, "Incomplete data array");
9 $this->assertContains($data['downtime_type'], ScheduleDate_Model::$valid_types);
10 $sd = new ScheduleDate_Model();
11 $id = false;
12 $this->assertTrue($sd->edit_schedule($data, $id));
13 $this->created[] = $id;
16 public function tearDown()
18 $this->auth = Auth::instance(array('session_key' => false))->force_user(new Op5User_AlwaysAuth());
19 $this->auth->set_authorized_for('host_view_all', true);
20 $this->auth->set_authorized_for('service_view_all', true);
21 $this->auth->set_authorized_for('hostgroup_view_all', true);
22 $this->auth->set_authorized_for('servicegroup_view_all', true);
23 $this->auth->set_authorized_for('host_edit_all', true);
24 $this->auth->set_authorized_for('service_edit_all', true);
25 $this->auth->set_authorized_for('hostgroup_edit_all', true);
26 $this->auth->set_authorized_for('servicegroup_edit_all', true);
27 $sd = new ScheduleDate_Model();
28 foreach ($this->created as $id) {
29 $this->assertTrue($sd->delete_schedule($id));
31 $db = Database::instance();
32 $res = $db->query("SELECT * FROM recurring_downtime");
33 $this->assertCount(0, $res);
34 $res = $db->query("SELECT * FROM recurring_downtime_objects");
35 $this->assertCount(0, $res);
38 public function setUp()
40 $this->auth = Auth::instance(array('session_key' => false))->force_user(new Op5User_AlwaysAuth());
41 $this->created = array();
44 $this->createDowntime(array(
45 'author' => 'me',
46 'downtime_type' => 'hosts',
47 'objects' => array('monitor'),
48 'comment' => 'devs break all the hosts',
49 'start_time' => '08:00', // PHP better not call that octal.
50 'end_time' => '17:00',
51 'duration' => false,
52 'fixed' => true,
53 'weekdays' => array(1,2,3,4,5,6,0),
54 'months' => array(1,2,3,4,5,6,7,8,9,10,11,12)
55 ));
56 $this->createDowntime(array(
57 'author' => 'me',
58 'downtime_type' => 'services',
59 'objects' => array('host_down;service ok'),
60 'comment' => 'devs break all the services',
61 'start_time' => '9:00',
62 'end_time' => '18:00',
63 'duration' => false,
64 'fixed' => true,
65 'weekdays' => array(1,2,3,4,5,6,0),
66 'months' => array(1,2,3,4,5,6,7,8,9,10,11,12)
67 ));
68 $this->createDowntime(array(
69 'author' => 'me',
70 'downtime_type' => 'hostgroups',
71 'objects' => array('hostgroup_up'),
72 'comment' => 'devs break all the hostgroups',
73 'start_time' => '10:00',
74 'end_time' => '12:00',
75 'duration' => '5:00',
76 'fixed' => false,
77 'weekdays' => array(1,2,3,4,5,6,0),
78 'months' => array(1,2,3,4,5,6,7,8,9,10,11,12)
79 ));
80 $this->createDowntime(array(
81 'author' => 'me',
82 'downtime_type' => 'servicegroups',
83 'objects' => array('servicegroup_ok'),
84 'comment' => 'devs break all the servicegroups',
85 'start_time' => '11:00',
86 'end_time' => '13:00',
87 'duration' => '05:00',
88 'fixed' => false,
89 'weekdays' => array(1,2,3,4,5,6,0),
90 'months' => array(1,2,3,4,5,6,7,8,9,10,11,12)
91 ));
94 public function testUnlimited()
96 $this->auth->set_authorized_for('host_view_all', true);
97 $this->auth->set_authorized_for('service_view_all', true);
98 $this->auth->set_authorized_for('hostgroup_view_all', true);
99 $this->auth->set_authorized_for('servicegroup_view_all', true);
100 $this->auth->set_authorized_for('host_edit_all', true);
101 $stats = RecurringDowntimePool_Model::all();
102 $this->assertCount(4, $stats);
104 $one = $stats->it(false)->current()->export();
105 $one['author'] = 'you';
106 $sd = new ScheduleDate_Model();
107 $id = $one['id'];
108 unset($this->created[array_search($id, $this->created)]);
109 $this->assertTrue($sd->edit_schedule($one, $id));
111 $this->assertTrue($sd->delete_schedule($id));
112 $stats = RecurringDowntimePool_Model::all();
113 $this->assertCount(3, $stats);
116 public function testReadOnly()
118 $this->auth->set_authorized_for('host_view_all', true);
119 $this->auth->set_authorized_for('service_view_all', true);
120 $this->auth->set_authorized_for('hostgroup_view_all', true);
121 $this->auth->set_authorized_for('servicegroup_view_all', true);
122 $this->auth->set_authorized_for('host_edit_all', false);
123 $this->auth->set_authorized_for('service_edit_all', false);
124 $this->auth->set_authorized_for('hostgroup_edit_all', false);
125 $this->auth->set_authorized_for('servicegroup_edit_all', false);
126 $this->auth->set_authorized_for('host_edit_contact', false);
127 $this->auth->set_authorized_for('service_edit_contact', false);
128 $this->auth->set_authorized_for('hostgroup_edit_contact', false);
129 $this->auth->set_authorized_for('servicegroup_edit_contact', false);
130 $stats = RecurringDowntimePool_Model::all();
131 $this->assertCount(4, $stats);
133 $one = $stats->it(false)->current()->export();
134 $one['author'] = 'you';
135 $sd = new ScheduleDate_Model();
136 $id = $one['id'];
137 $this->assertFalse($sd->edit_schedule($one, $id));
139 $this->assertFalse($sd->delete_schedule($id));
140 $stats = RecurringDowntimePool_Model::all();
141 $this->assertCount(4, $stats);
142 $one = $stats->it(array('author'))->current();
143 $this->assertEquals($one->get_author(), 'me');
146 public function testLimitedNoHost()
148 $this->auth->set_authorized_for('host_view_all', false);
149 $this->auth->set_authorized_for('service_view_all', true);
150 $this->auth->set_authorized_for('hostgroup_view_all', true);
151 $this->auth->set_authorized_for('servicegroup_view_all', true);
152 $stats = RecurringDowntimePool_Model::all();
153 $this->assertCount(3, $stats);
156 public function testLimitedNoService()
158 $this->auth->set_authorized_for('host_view_all', true);
159 $this->auth->set_authorized_for('service_view_all', false);
160 $this->auth->set_authorized_for('hostgroup_view_all', true);
161 $this->auth->set_authorized_for('servicegroup_view_all', true);
162 $stats = RecurringDowntimePool_Model::all();
163 $this->assertCount(3, $stats);
166 public function testLimitedNothing()
168 $this->auth->set_authorized_for('host_view_all', false);
169 $this->auth->set_authorized_for('service_view_all', false);
170 $this->auth->set_authorized_for('hostgroup_view_all', false);
171 $this->auth->set_authorized_for('servicegroup_view_all', false);
172 $stats = RecurringDowntimePool_Model::all();
173 $this->assertCount(0, $stats);
176 public function testLimitedHost()
178 $this->auth = Auth::instance(array('session_key' => false))->force_user(new Op5User(array('username' => 'limited')));
179 $stats = RecurringDowntimePool_Model::all();
180 $this->assertCount(1, $stats);
181 $obj = $stats->it(array('downtime_type', 'objects', 'start_time'))->current();
182 $this->assertEquals('hosts', $obj->get_downtime_type());
183 $this->assertEquals(array('monitor'), $obj->get_objects());
184 $this->assertEquals(60 * 60 * 8 /* 08:00 as seconds */, $obj->get_start_time());