mirror of
https://github.com/moodle/moodle.git
synced 2025-08-11 03:46:42 +02:00
MDL-76722 user: Add new update_user_device_public_key webservice
This commit is contained in:
parent
7c843b380b
commit
9bc236d48d
7 changed files with 381 additions and 0 deletions
70
user/tests/devicekey_test.php
Normal file
70
user/tests/devicekey_test.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core_user;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Tests for the devicekey class.
|
||||
*
|
||||
* @package core_user
|
||||
* @covers \core_user\devicekey
|
||||
*/
|
||||
class devicekey_test extends \advanced_testcase {
|
||||
/**
|
||||
* Helper to create a device record.
|
||||
*
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_device_record(): stdClass {
|
||||
global $USER, $DB;
|
||||
|
||||
$device = (object) [
|
||||
'appid' => 'com.moodle.moodlemobile',
|
||||
'name' => 'occam',
|
||||
'model' => 'Nexus 4',
|
||||
'platform' => 'Android',
|
||||
'version' => '4.2.2',
|
||||
'pushid' => 'apushdkasdfj4835',
|
||||
'uuid' => 'ABCDE3723ksdfhasfaasef859',
|
||||
'userid' => $USER->id,
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
];
|
||||
$device->id = $DB->insert_record('user_devices', $device);
|
||||
|
||||
return $device;
|
||||
}
|
||||
|
||||
public function test_update_device_public_key_no_device(): void {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$device = $this->create_device_record();
|
||||
|
||||
$devicekeypair = sodium_crypto_box_keypair();
|
||||
$publickey = sodium_bin2base64(
|
||||
sodium_crypto_box_publickey($devicekeypair),
|
||||
SODIUM_BASE64_VARIANT_ORIGINAL
|
||||
);
|
||||
|
||||
$this->assertTrue(devicekey::update_device_public_key($device->uuid, $device->appid, $publickey));
|
||||
$this->assertEquals($publickey, $DB->get_field('user_devices', 'publickey', ['id' => $device->id]));
|
||||
}
|
||||
}
|
125
user/tests/external/update_user_device_public_key_test.php
vendored
Normal file
125
user/tests/external/update_user_device_public_key_test.php
vendored
Normal file
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace core_user\external;
|
||||
|
||||
use core_external\external_api;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Tests for the devicekey class.
|
||||
*
|
||||
* @package core_user
|
||||
* @covers \core_user\external\update_user_device_public_key
|
||||
*/
|
||||
class update_user_device_public_key_test extends \advanced_testcase {
|
||||
/**
|
||||
* Helper to create a device record.
|
||||
*
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_device_record(): stdClass {
|
||||
global $USER, $DB;
|
||||
|
||||
$device = (object) [
|
||||
'appid' => 'com.moodle.moodlemobile',
|
||||
'name' => 'occam',
|
||||
'model' => 'Nexus 4',
|
||||
'platform' => 'Android',
|
||||
'version' => '4.2.2',
|
||||
'pushid' => 'apushdkasdfj4835',
|
||||
'uuid' => 'ABCDE3723ksdfhasfaasef859',
|
||||
'userid' => $USER->id,
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
];
|
||||
$device->id = $DB->insert_record('user_devices', $device);
|
||||
|
||||
return $device;
|
||||
}
|
||||
|
||||
public function test_execute(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$device = $this->create_device_record();
|
||||
|
||||
$devicekeypair = sodium_crypto_box_keypair();
|
||||
$publickey = sodium_bin2base64(
|
||||
sodium_crypto_box_publickey($devicekeypair),
|
||||
SODIUM_BASE64_VARIANT_ORIGINAL
|
||||
);
|
||||
|
||||
// Test sending a key to a valid device.
|
||||
$result = update_user_device_public_key::execute(
|
||||
$device->uuid,
|
||||
$device->appid,
|
||||
$publickey,
|
||||
);
|
||||
|
||||
$result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
|
||||
$this->assertTrue($result['status']);
|
||||
$this->assertEmpty($result['warnings']);
|
||||
}
|
||||
|
||||
public function test_execute_with_invalid_device_appid(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$device = $this->create_device_record();
|
||||
|
||||
$devicekeypair = sodium_crypto_box_keypair();
|
||||
$publickey = sodium_bin2base64(
|
||||
sodium_crypto_box_publickey($devicekeypair),
|
||||
SODIUM_BASE64_VARIANT_ORIGINAL
|
||||
);
|
||||
|
||||
// Invalid appid.
|
||||
$result = update_user_device_public_key::execute(
|
||||
$device->uuid,
|
||||
'invalidappid',
|
||||
$publickey,
|
||||
);
|
||||
|
||||
$result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
|
||||
$this->assertFalse($result['status']);
|
||||
$this->assertNotEmpty($result['warnings']);
|
||||
}
|
||||
|
||||
public function test_execute_with_invalid_device_uuid(): void {
|
||||
$this->resetAfterTest();
|
||||
$this->setAdminUser();
|
||||
|
||||
$device = $this->create_device_record();
|
||||
|
||||
$devicekeypair = sodium_crypto_box_keypair();
|
||||
$publickey = sodium_bin2base64(
|
||||
sodium_crypto_box_publickey($devicekeypair),
|
||||
SODIUM_BASE64_VARIANT_ORIGINAL
|
||||
);
|
||||
|
||||
// Invalid appid.
|
||||
$result = update_user_device_public_key::execute(
|
||||
'invaliduuid',
|
||||
$device->appid,
|
||||
$publickey,
|
||||
);
|
||||
|
||||
$result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
|
||||
$this->assertFalse($result['status']);
|
||||
$this->assertNotEmpty($result['warnings']);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue