Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | 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
 * Privacy test for the authentication oauth2
18
 *
19
 * @package    auth_oauth2
20
 * @category   test
21
 * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace auth_oauth2\privacy;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use auth_oauth2\privacy\provider;
29
use core_privacy\local\request\approved_contextlist;
30
use core_privacy\local\request\writer;
31
use core_privacy\tests\provider_testcase;
32
use core_privacy\local\request\approved_userlist;
33
 
34
/**
35
 * Privacy test for the authentication oauth2
36
 *
37
 * @package    auth_oauth2
38
 * @category   test
39
 * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
1441 ariadna 42
final class provider_test extends provider_testcase {
1 efrain 43
    /**
44
     * Set up method.
45
     */
46
    public function setUp(): void {
1441 ariadna 47
        parent::setUp();
1 efrain 48
        $this->resetAfterTest();
49
        $this->setAdminUser();
50
    }
51
 
52
    /**
53
     * Check that a user context is returned if there is any user data for this user.
54
     */
11 efrain 55
    public function test_get_contexts_for_userid(): void {
1 efrain 56
        $user = $this->getDataGenerator()->create_user();
57
        $this->assertEmpty(provider::get_contexts_for_userid($user->id));
58
 
59
        $issuer = \core\oauth2\api::create_standard_issuer('google');
60
        $info = [];
61
        $info['username'] = 'gina';
62
        $info['email'] = 'gina@example.com';
63
        \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
64
 
65
        $contextlist = provider::get_contexts_for_userid($user->id);
66
        // Check that we only get back one context.
67
        $this->assertCount(1, $contextlist);
68
 
69
        // Check that a context is returned is the expected.
70
        $usercontext = \context_user::instance($user->id);
71
        $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
72
    }
73
 
74
    /**
75
     * Test that user data is exported correctly.
76
     */
11 efrain 77
    public function test_export_user_data(): void {
1 efrain 78
        $user = $this->getDataGenerator()->create_user();
79
        $issuer = \core\oauth2\api::create_standard_issuer('google');
80
        $info = [];
81
        $info['username'] = 'gina';
82
        $info['email'] = 'gina@example.com';
83
        \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
84
        $usercontext = \context_user::instance($user->id);
85
 
86
        $writer = writer::with_context($usercontext);
87
        $this->assertFalse($writer->has_any_data());
88
        $approvedlist = new approved_contextlist($user, 'auth_oauth2', [$usercontext->id]);
89
        provider::export_user_data($approvedlist);
90
        $data = $writer->get_data([get_string('privacy:metadata:auth_oauth2', 'auth_oauth2'), $issuer->get('name')]);
91
        $this->assertEquals($info['username'], $data->username);
92
        $this->assertEquals($info['email'], $data->email);
93
    }
94
 
95
    /**
96
     * Test deleting all user data for a specific context.
97
     */
11 efrain 98
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 99
        global $DB;
100
 
101
        $user1 = $this->getDataGenerator()->create_user();
102
        $issuer1 = \core\oauth2\api::create_standard_issuer('google');
103
        $info = [];
104
        $info['username'] = 'gina';
105
        $info['email'] = 'gina@example.com';
106
        \auth_oauth2\api::link_login($info, $issuer1, $user1->id, false);
107
        $user1context = \context_user::instance($user1->id);
108
 
109
        $user2 = $this->getDataGenerator()->create_user();
110
        $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft');
111
        $info = [];
112
        $info['username'] = 'jerry';
113
        $info['email'] = 'jerry@example.com';
114
        \auth_oauth2\api::link_login($info, $issuer2, $user2->id, false);
115
        $user2context = \context_user::instance($user2->id);
116
 
117
        // Get all oauth2 accounts.
118
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', array());
119
        // There should be two.
120
        $this->assertCount(2, $oauth2accounts);
121
 
122
        // Delete everything for the first user context.
123
        provider::delete_data_for_all_users_in_context($user1context);
124
 
125
        // Get all oauth2 accounts match with user1.
126
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', ['userid' => $user1->id]);
127
        $this->assertCount(0, $oauth2accounts);
128
 
129
        // Get all oauth2 accounts.
130
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', array());
131
        // There should be one.
132
        $this->assertCount(1, $oauth2accounts);
133
    }
134
 
135
    /**
136
     * This should work identical to the above test.
137
     */
11 efrain 138
    public function test_delete_data_for_user(): void {
1 efrain 139
        global $DB;
140
 
141
        $user1 = $this->getDataGenerator()->create_user();
142
        $issuer1 = \core\oauth2\api::create_standard_issuer('google');
143
        $info = [];
144
        $info['username'] = 'gina';
145
        $info['email'] = 'gina@example.com';
146
        \auth_oauth2\api::link_login($info, $issuer1, $user1->id, false);
147
        $user1context = \context_user::instance($user1->id);
148
 
149
        $user2 = $this->getDataGenerator()->create_user();
150
        $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft');
151
        $info = [];
152
        $info['username'] = 'jerry';
153
        $info['email'] = 'jerry@example.com';
154
        \auth_oauth2\api::link_login($info, $issuer2, $user2->id, false);
155
        $user2context = \context_user::instance($user2->id);
156
 
157
        // Get all oauth2 accounts.
158
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', array());
159
        // There should be two.
160
        $this->assertCount(2, $oauth2accounts);
161
 
162
        // Delete everything for the first user.
163
        $approvedlist = new approved_contextlist($user1, 'auth_oauth2', [$user1context->id]);
164
        provider::delete_data_for_user($approvedlist);
165
 
166
        // Get all oauth2 accounts match with user1.
167
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', ['userid' => $user1->id]);
168
        $this->assertCount(0, $oauth2accounts);
169
 
170
        // Get all oauth2 accounts.
171
        $oauth2accounts = $DB->get_records('auth_oauth2_linked_login', array());
172
        // There should be one user.
173
        $this->assertCount(1, $oauth2accounts);
174
    }
175
 
176
    /**
177
     * Test that only users with a user context are fetched.
178
     */
11 efrain 179
    public function test_get_users_in_context(): void {
1 efrain 180
        $this->resetAfterTest();
181
 
182
        $component = 'auth_oauth2';
183
        // Create a user.
184
        $user = $this->getDataGenerator()->create_user();
185
        $usercontext = \context_user::instance($user->id);
186
 
187
        // The list of users should not return anything yet (related data still haven't been created).
188
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
189
        provider::get_users_in_context($userlist);
190
        $this->assertCount(0, $userlist);
191
 
192
        $issuer = \core\oauth2\api::create_standard_issuer('google');
193
        $info = [];
194
        $info['username'] = 'gina';
195
        $info['email'] = 'gina@example.com';
196
        \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
197
 
198
        // The list of users for user context should return the user.
199
        provider::get_users_in_context($userlist);
200
        $this->assertCount(1, $userlist);
201
        $expected = [$user->id];
202
        $actual = $userlist->get_userids();
203
        $this->assertEquals($expected, $actual);
204
 
205
        // The list of users for system context should not return any users.
206
        $systemcontext = \context_system::instance();
207
        $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
208
        provider::get_users_in_context($userlist);
209
        $this->assertCount(0, $userlist);
210
    }
211
 
212
    /**
213
     * Test that data for users in approved userlist is deleted.
214
     */
11 efrain 215
    public function test_delete_data_for_users(): void {
1 efrain 216
        $this->resetAfterTest();
217
 
218
        $component = 'auth_oauth2';
219
        // Create user1.
220
        $user1 = $this->getDataGenerator()->create_user();
221
        $usercontext1 = \context_user::instance($user1->id);
222
        // Create user2.
223
        $user2 = $this->getDataGenerator()->create_user();
224
        $usercontext2 = \context_user::instance($user2->id);
225
 
226
        $issuer1 = \core\oauth2\api::create_standard_issuer('google');
227
        $info1 = [];
228
        $info1['username'] = 'gina1';
229
        $info1['email'] = 'gina@example1.com';
230
        \auth_oauth2\api::link_login($info1, $issuer1, $user1->id, false);
231
 
232
        $issuer2 = \core\oauth2\api::create_standard_issuer('google');
233
        $info2 = [];
234
        $info2['username'] = 'gina2';
235
        $info2['email'] = 'gina@example2.com';
236
        \auth_oauth2\api::link_login($info2, $issuer2, $user2->id, false);
237
 
238
        // The list of users for usercontext1 should return user1.
239
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
240
        provider::get_users_in_context($userlist1);
241
        $this->assertCount(1, $userlist1);
242
        $expected = [$user1->id];
243
        $actual = $userlist1->get_userids();
244
        $this->assertEquals($expected, $actual);
245
 
246
        // The list of users for usercontext2 should return user2.
247
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
248
        provider::get_users_in_context($userlist2);
249
        $this->assertCount(1, $userlist2);
250
        $expected = [$user2->id];
251
        $actual = $userlist2->get_userids();
252
        $this->assertEquals($expected, $actual);
253
 
254
        // Add userlist1 to the approved user list.
255
        $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
256
 
257
        // Delete user data using delete_data_for_user for usercontext1.
258
        provider::delete_data_for_users($approvedlist);
259
 
260
        // Re-fetch users in usercontext1 - The user list should now be empty.
261
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
262
        provider::get_users_in_context($userlist1);
263
        $this->assertCount(0, $userlist1);
264
        // Re-fetch users in usercontext2 - The user list should not be empty (user2).
265
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
266
        provider::get_users_in_context($userlist2);
267
        $this->assertCount(1, $userlist2);
268
 
269
        // User data should be only removed in the user context.
270
        $systemcontext = \context_system::instance();
271
        // Add userlist2 to the approved user list in the system context.
272
        $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
273
        // Delete user1 data using delete_data_for_user.
274
        provider::delete_data_for_users($approvedlist);
275
        // Re-fetch users in usercontext2 - The user list should not be empty (user2).
276
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
277
        provider::get_users_in_context($userlist2);
278
        $this->assertCount(1, $userlist2);
279
    }
280
}