Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core_user\external;
18
 
19
use core_external\external_api;
20
use stdClass;
21
 
22
/**
23
 * Tests for the devicekey class.
24
 *
25
 * @package core_user
26
 * @covers \core_user\external\update_user_device_public_key
27
 */
28
class update_user_device_public_key_test extends \advanced_testcase {
29
    /**
30
     * Helper to create a device record.
31
     *
32
     * @return stdClass
33
     */
34
    protected function create_device_record(): stdClass {
35
        global $USER, $DB;
36
 
37
        $device = (object) [
38
            'appid' => 'com.moodle.moodlemobile',
39
            'name' => 'occam',
40
            'model' => 'Nexus 4',
41
            'platform' => 'Android',
42
            'version' => '4.2.2',
43
            'pushid' => 'apushdkasdfj4835',
44
            'uuid' => 'ABCDE3723ksdfhasfaasef859',
45
            'userid' => $USER->id,
46
            'timecreated' => time(),
47
            'timemodified' => time(),
48
        ];
49
        $device->id = $DB->insert_record('user_devices', $device);
50
 
51
        return $device;
52
    }
53
 
54
    public function test_execute(): void {
55
        $this->resetAfterTest();
56
        $this->setAdminUser();
57
 
58
        $device = $this->create_device_record();
59
 
60
        $devicekeypair = sodium_crypto_box_keypair();
61
        $publickey = sodium_bin2base64(
62
            sodium_crypto_box_publickey($devicekeypair),
63
            SODIUM_BASE64_VARIANT_ORIGINAL
64
        );
65
 
66
        // Test sending a key to a valid device.
67
        $result = update_user_device_public_key::execute(
68
            $device->uuid,
69
            $device->appid,
70
            $publickey,
71
        );
72
 
73
        $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
74
        $this->assertTrue($result['status']);
75
        $this->assertEmpty($result['warnings']);
76
    }
77
 
78
    public function test_execute_with_invalid_device_appid(): void {
79
        $this->resetAfterTest();
80
        $this->setAdminUser();
81
 
82
        $device = $this->create_device_record();
83
 
84
        $devicekeypair = sodium_crypto_box_keypair();
85
        $publickey = sodium_bin2base64(
86
            sodium_crypto_box_publickey($devicekeypair),
87
            SODIUM_BASE64_VARIANT_ORIGINAL
88
        );
89
 
90
        // Invalid appid.
91
        $result = update_user_device_public_key::execute(
92
            $device->uuid,
93
            'invalidappid',
94
            $publickey,
95
        );
96
 
97
        $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
98
        $this->assertFalse($result['status']);
99
        $this->assertNotEmpty($result['warnings']);
100
    }
101
 
102
    public function test_execute_with_invalid_device_uuid(): void {
103
        $this->resetAfterTest();
104
        $this->setAdminUser();
105
 
106
        $device = $this->create_device_record();
107
 
108
        $devicekeypair = sodium_crypto_box_keypair();
109
        $publickey = sodium_bin2base64(
110
            sodium_crypto_box_publickey($devicekeypair),
111
            SODIUM_BASE64_VARIANT_ORIGINAL
112
        );
113
 
114
        // Invalid appid.
115
        $result = update_user_device_public_key::execute(
116
            'invaliduuid',
117
            $device->appid,
118
            $publickey,
119
        );
120
 
121
        $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
122
        $this->assertFalse($result['status']);
123
        $this->assertNotEmpty($result['warnings']);
124
    }
125
}