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

Commit

Permalink
test(agent): enhance tests
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Oct 19, 2018
1 parent 1f48459 commit 74072ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 4 additions & 0 deletions inc/agent.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,12 @@ public function canUpdateItem() {
return parent::canUpdateItem();
}

// The user has a guest profile
if (!$this->checkEntity(true)) {
return false;
}

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

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

Expand Down
8 changes: 8 additions & 0 deletions tests/src/Flyvemdm/Tests/CommonTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,12 @@ protected function asserLastMqttlog(
}
return 0;
}

protected function assertInvitationLogHasMessage(\PluginFlyvemdmInvitation $invitation, $message) {
$invitationId = $invitation->getID();
$fk = \PluginFlyvemdmInvitation::getForeignKeyField();
$invitationLog = new \PluginFlyvemdmInvitationLog();
$found = $invitationLog->find("`$fk` = '$invitationId' AND `event` = '$message'");
return count($found);
}
}
39 changes: 28 additions & 11 deletions tests/suite-integration/PluginFlyvemdmAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,37 +127,43 @@ protected function providerInvalidEnrollAgent() {
'invitationToken' => 'bad token',
],
'expected' => 'Invitation token invalid',
'invitationlog' => false,
],
'without MDM type' => [
'data' => [
'mdmType' => null,
],
'expected' => 'MDM type missing',
'invitationlog' => true,
],
'with bad MDM type' => [
'data' => [
'mdmType' => 'alien MDM',
],
'expected' => 'unknown MDM type',
'invitationlog' => true,
],
'with bad version' => [
'data' => [
'version' => 'bad version',
],
'expected' => 'Bad agent version',
'invitationlog' => true,
],
'with a too low version' => [
'data' => [
'version' => '1.9.0',
],
'expected' => 'The agent version is too low',
'invitationlog' => true,
],
'without inventory' => [
'data' => [
'version' => $version,
'inventory' => '',
],
'expected' => 'Device inventory XML is mandatory',
'invitationlog' => true,
],
'with invalid inventory' => [
'data' => [
Expand All @@ -166,6 +172,7 @@ protected function providerInvalidEnrollAgent() {
'inventory' => $inventory,
],
'expected' => 'Inventory XML is not well formed',
'invitationlog' => true,
],
];
}
Expand All @@ -175,12 +182,17 @@ protected function providerInvalidEnrollAgent() {
* @tags testInvalidEnrollAgent
* @param array $data
* @param string $expected
* @param boolean $invitationlog Is an invitation log entry expected ?
*/
public function testInvalidEnrollAgent(array $data, $expected) {
public function testInvalidEnrollAgent(array $data, $expected, $invitationlog) {
$dbUtils = new \DbUtils;
$invitationlogTable = \PluginFlyvemdmInvitationlog::getTable();
$expectedLogCount = $dbUtils->countElementsInTable($invitationlogTable);
list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField());

//Check there is no event for the invitation
$this->integer($this->assertInvitationLogHasMessage($invitation, $expected))->isEqualTo(0);

$invitationToken = (isset($data['invitationToken'])) ? $data['invitationToken'] : $invitation->getField('invitation_token');
$serial = (key_exists('serial', $data)) ? $data['serial'] : $serial;
$mdmType = (key_exists('mdmType', $data)) ? $data['mdmType'] : 'android';
Expand All @@ -194,15 +206,10 @@ public function testInvalidEnrollAgent(array $data, $expected) {
->isTrue(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));
$this->array($_SESSION['MESSAGE_AFTER_REDIRECT'][ERROR])->contains($expected);

// Check registered log
// previous enrolment could generate a new log
$invitationLog = new \PluginFlyvemdmInvitationlog();
$fk = \PluginFlyvemdmInvitation::getForeignKeyField();
$inviationId = $invitation->getID();
$expectedLogCount += count($invitationLog->find("`$fk` = '$inviationId'"));

$total = $dbUtils->countElementsInTable($invitationlogTable);
$this->integer($total)->isEqualTo($expectedLogCount);
// Check that an event was created for the invitation
if ($invitationlog) {
$this->integer($this->assertInvitationLogHasMessage($invitation, $expected))->isGreaterThan(0);
}
}

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

sleep(2);
$this->login('glpi', 'glpi');

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

$this->login('glpi', 'glpi');

$this->deviceOnlineStatus($agent, 'true', 1);

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

$this->login('glpi', 'glpi');

// Find the last existing ID of logged MQTT messages
$log = new \PluginFlyvemdmMqttlog();
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
Expand Down Expand Up @@ -616,6 +627,8 @@ public function testGeolocateRequest() {
// Get enrolment data to enable the agent's MQTT account
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();

$this->login('glpi', 'glpi');

// Find the last existing ID of logged MQTT messages
$log = new \PluginFlyvemdmMqttlog();
$lastLogId = \PluginFlyvemdmCommon::getMax($log, '', 'id');
Expand Down Expand Up @@ -647,6 +660,8 @@ public function testInventoryRequest() {
$this->boolean($agent->isNewItem())
->isFalse(json_encode($_SESSION['MESSAGE_AFTER_REDIRECT'], JSON_PRETTY_PRINT));

$this->login('glpi', 'glpi');

// Get enrolment data to enable the agent's MQTT account
$this->boolean($agent->getFromDB($agent->getID()))->isTrue();

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

$this->login('glpi', 'glpi');

// Test lock and wipe are unset after enrollment
$this->integer((int) $agent->getField('lock'))->isEqualTo(0);
$this->integer((int) $agent->getField('wipe'))->isEqualTo(0);
Expand Down

0 comments on commit 74072ff

Please sign in to comment.