Skip to content
This repository was archived by the owner on Aug 9, 2021. It is now read-only.

Commit cd2c08a

Browse files
committed
test(agent): enhance tests
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 5e06e95 commit cd2c08a

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

inc/agent.class.php

+4
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,12 @@ public function canUpdateItem() {
393393
return parent::canUpdateItem();
394394
}
395395

396+
// The user has a guest profile
396397
if (!$this->checkEntity(true)) {
397398
return false;
398399
}
399400

401+
// Only the account of the device can update
400402
return $_SESSION['glpiID'] == $this->fields[User::getForeignKeyField()];
401403
}
402404

@@ -485,6 +487,8 @@ public function prepareInputForAdd($input) {
485487
public function prepareInputForUpdate($input) {
486488
$config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']);
487489
if ($_SESSION['glpiactiveprofile']['id'] == $config['guest_profiles_id']) {
490+
// a guest profile is the device itself.
491+
// @see self::canUpdateItem()
488492
return $this->prepareInputForUpdateFromDevice($input);
489493
}
490494

tests/src/Flyvemdm/Tests/CommonTestCase.php

+8
Original file line numberDiff line numberDiff line change
@@ -688,4 +688,12 @@ protected function asserLastMqttlog(
688688
}
689689
return 0;
690690
}
691+
692+
protected function assertInvitationLogHasMessage(\PluginFlyvemdmInvitation $invitation, $message) {
693+
$invitationId = $invitation->getID();
694+
$fk = \PluginFlyvemdmInvitation::getForeignKeyField();
695+
$invitationLog = new \PluginFlyvemdmInvitationLog();
696+
$found = $invitationLog->find("`$fk` = '$invitationId' AND `event` = '$message'");
697+
return count($found);
698+
}
691699
}

tests/suite-integration/PluginFlyvemdmAgent.php

+28-11
Original file line numberDiff line numberDiff line change
@@ -127,37 +127,43 @@ protected function providerInvalidEnrollAgent() {
127127
'invitationToken' => 'bad token',
128128
],
129129
'expected' => 'Invitation token invalid',
130+
'invitationlog' => false,
130131
],
131132
'without MDM type' => [
132133
'data' => [
133134
'mdmType' => null,
134135
],
135136
'expected' => 'MDM type missing',
137+
'invitationlog' => true,
136138
],
137139
'with bad MDM type' => [
138140
'data' => [
139141
'mdmType' => 'alien MDM',
140142
],
141143
'expected' => 'unknown MDM type',
144+
'invitationlog' => true,
142145
],
143146
'with bad version' => [
144147
'data' => [
145148
'version' => 'bad version',
146149
],
147150
'expected' => 'Bad agent version',
151+
'invitationlog' => true,
148152
],
149153
'with a too low version' => [
150154
'data' => [
151155
'version' => '1.9.0',
152156
],
153157
'expected' => 'The agent version is too low',
158+
'invitationlog' => true,
154159
],
155160
'without inventory' => [
156161
'data' => [
157162
'version' => $version,
158163
'inventory' => '',
159164
],
160165
'expected' => 'Device inventory XML is mandatory',
166+
'invitationlog' => true,
161167
],
162168
'with invalid inventory' => [
163169
'data' => [
@@ -166,6 +172,7 @@ protected function providerInvalidEnrollAgent() {
166172
'inventory' => $inventory,
167173
],
168174
'expected' => 'Inventory XML is not well formed',
175+
'invitationlog' => true,
169176
],
170177
];
171178
}
@@ -175,12 +182,17 @@ protected function providerInvalidEnrollAgent() {
175182
* @tags testInvalidEnrollAgent
176183
* @param array $data
177184
* @param string $expected
185+
* @param boolean $invitationlog Is an invitation log entry expected ?
178186
*/
179-
public function testInvalidEnrollAgent(array $data, $expected) {
187+
public function testInvalidEnrollAgent(array $data, $expected, $invitationlog) {
180188
$dbUtils = new \DbUtils;
181189
$invitationlogTable = \PluginFlyvemdmInvitationlog::getTable();
182190
$expectedLogCount = $dbUtils->countElementsInTable($invitationlogTable);
183191
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());
192+
193+
//Check there is no event for the invitation
194+
$this->integer($this->assertInvitationLogHasMessage($invitation, $expected))->isEqualTo(0);
195+
184196
$invitationToken = (isset($data['invitationToken'])) ? $data['invitationToken'] : $invitation->getField('invitation_token');
185197
$serial = (key_exists('serial', $data)) ? $data['serial'] : $serial;
186198
$mdmType = (key_exists('mdmType', $data)) ? $data['mdmType'] : 'android';
@@ -194,15 +206,10 @@ public function testInvalidEnrollAgent(array $data, $expected) {
194206
->isTrue(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
195207
$this->array($_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR])->contains($expected);
196208

197-
// Check registered log
198-
// previous enrolment could generate a new log
199-
$invitationLog = new \PluginFlyvemdmInvitationlog();
200-
$fk = \PluginFlyvemdmInvitation::getForeignKeyField();
201-
$inviationId = $invitation->getID();
202-
$expectedLogCount += count($invitationLog->find("`$fk` = '$inviationId'"));
203-
204-
$total = $dbUtils->countElementsInTable($invitationlogTable);
205-
$this->integer($total)->isEqualTo($expectedLogCount);
209+
// Check that an event was created for the invitation
210+
if ($invitationlog) {
211+
$this->integer($this->assertInvitationLogHasMessage($invitation, $expected))->isGreaterThan(0);
212+
}
206213
}
207214

208215
/**
@@ -382,7 +389,7 @@ public function testUnenrollAgent() {
382389
$this->boolean($agent->isNewItem())
383390
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
384391

385-
sleep(2);
392+
$this->login('glpi', 'glpi');
386393

387394
// Find the last existing ID of logged MQTT messages
388395
$log = new \PluginFlyvemdmMqttlog();
@@ -446,6 +453,8 @@ public function testDeviceOnlineChange() {
446453
$this->boolean($agent->isNewItem())
447454
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
448455

456+
$this->login('glpi', 'glpi');
457+
449458
$this->deviceOnlineStatus($agent, 'true', 1);
450459

451460
$this->deviceOnlineStatus($agent, 'false', 0);
@@ -581,6 +590,8 @@ public function testPingRequest() {
581590
// Get enrolment data to enable the agent's MQTT account
582591
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();
583592

593+
$this->login('glpi', 'glpi');
594+
584595
// Find the last existing ID of logged MQTT messages
585596
$log = new \PluginFlyvemdmMqttlog();
586597
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
@@ -616,6 +627,8 @@ public function testGeolocateRequest() {
616627
// Get enrolment data to enable the agent's MQTT account
617628
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();
618629

630+
$this->login('glpi', 'glpi');
631+
619632
// Find the last existing ID of logged MQTT messages
620633
$log = new \PluginFlyvemdmMqttlog();
621634
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
@@ -647,6 +660,8 @@ public function testInventoryRequest() {
647660
$this->boolean($agent->isNewItem())
648661
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
649662

663+
$this->login('glpi', 'glpi');
664+
650665
// Get enrolment data to enable the agent's MQTT account
651666
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();
652667

@@ -684,6 +699,8 @@ public function testLockAndWipe() {
684699
$this->boolean($agent->isNewItem())
685700
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
686701

702+
$this->login('glpi', 'glpi');
703+
687704
// Test lock and wipe are unset after enrollment
688705
$this->integer((int) $agent->getField('lock'))->isEqualTo(0);
689706
$this->integer((int) $agent->getField('wipe'))->isEqualTo(0);

0 commit comments

Comments
 (0)