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
namespace auth_oauth2;
18
 
19
/**
20
 * External auth oauth2 API tests.
21
 *
22
 * @package     auth_oauth2
23
 * @copyright   2017 Damyon Wiese
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 25
 *
26
 * @covers \auth_oauth2\api
1 efrain 27
 */
1441 ariadna 28
final class api_test extends \advanced_testcase {
1 efrain 29
 
30
    /**
31
     * Test the cleaning of orphaned linked logins for all issuers.
32
     */
11 efrain 33
    public function test_clean_orphaned_linked_logins(): void {
1 efrain 34
        $this->resetAfterTest();
35
        $this->setAdminUser();
36
 
37
        $issuer = \core\oauth2\api::create_standard_issuer('google');
38
        \core\oauth2\api::create_standard_issuer('microsoft');
39
 
40
        $user = $this->getDataGenerator()->create_user();
41
        $info = [];
42
        $info['username'] = 'banana';
43
        $info['email'] = 'banana@example.com';
44
        \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
45
 
46
        \core\oauth2\api::delete_issuer($issuer->get('id'));
47
 
48
        $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
49
        $this->assertCount(1, $linkedlogins);
50
 
51
        \auth_oauth2\api::clean_orphaned_linked_logins();
52
 
53
        $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
54
        $this->assertCount(0, $linkedlogins);
55
 
56
        $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
57
        $this->assertFalse($match);
58
    }
59
 
60
    /**
61
     * Test the cleaning of orphaned linked logins for a specific issuer.
62
     */
11 efrain 63
    public function test_clean_orphaned_linked_logins_with_issuer_id(): void {
1 efrain 64
        $this->resetAfterTest();
65
        $this->setAdminUser();
66
 
67
        $issuer1 = \core\oauth2\api::create_standard_issuer('google');
68
        $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft');
69
 
70
        $user1 = $this->getDataGenerator()->create_user();
71
        $info = [];
72
        $info['username'] = 'banana';
73
        $info['email'] = 'banana@example.com';
74
        \auth_oauth2\api::link_login($info, $issuer1, $user1->id, false);
75
 
76
        $user2 = $this->getDataGenerator()->create_user();
77
        $info = [];
78
        $info['username'] = 'apple';
79
        $info['email'] = 'apple@example.com';
80
        \auth_oauth2\api::link_login($info, $issuer2, $user2->id, false);
81
 
82
        \core\oauth2\api::delete_issuer($issuer1->get('id'));
83
 
84
        \auth_oauth2\api::clean_orphaned_linked_logins($issuer1->get('id'));
85
 
86
        $linkedlogins = \auth_oauth2\api::get_linked_logins($user1->id, $issuer1);
87
        $this->assertCount(0, $linkedlogins);
88
 
89
        $linkedlogins = \auth_oauth2\api::get_linked_logins($user2->id, $issuer2);
90
        $this->assertCount(1, $linkedlogins);
91
    }
92
 
93
    /**
94
     * Test creating a new confirmed account.
95
     * Including testing that user profile fields are correctly set.
96
     *
97
     * @covers \auth_oauth2\api::create_new_confirmed_account
98
     */
11 efrain 99
    public function test_create_new_confirmed_account(): void {
1 efrain 100
        global $DB;
101
        $this->resetAfterTest();
102
        $this->setAdminUser();
103
 
104
        $issuer = \core\oauth2\api::create_standard_issuer('microsoft');
105
 
106
        $info = [];
107
        $info['username'] = 'apple';
108
        $info['email'] = 'apple@example.com';
109
        $info['firstname'] = 'Apple';
110
        $info['lastname'] = 'Fruit';
111
        $info['alternatename'] = 'Beatles';
112
        $info['idnumber'] = '123456';
113
        $info['city'] = 'Melbourne';
114
        $info['country'] = 'AU';
115
        $info['institution'] = 'ACME Inc';
116
        $info['department'] = 'Misc Explosives';
117
 
118
        $createduser = \auth_oauth2\api::create_new_confirmed_account($info, $issuer);
119
 
120
        // Get actual user record from DB to check.
121
        $userdata = $DB->get_record('user', ['id' => $createduser->id]);
122
 
123
        // Confirm each value supplied from issuers is saved into the user record.
124
        foreach ($info as $key => $value) {
125
            $this->assertEquals($value, $userdata->$key);
126
        }
127
 
128
        // Explicitly test the user is confirmed.
129
        $this->assertEquals(1, $userdata->confirmed);
130
    }
131
 
132
    /**
133
     * Test auto-confirming linked logins.
134
     */
11 efrain 135
    public function test_linked_logins(): void {
1 efrain 136
        $this->resetAfterTest();
137
 
138
        $this->setAdminUser();
139
        $issuer = \core\oauth2\api::create_standard_issuer('google');
140
 
141
        $user = $this->getDataGenerator()->create_user();
1441 ariadna 142
        $this->setUser($user);
1 efrain 143
 
144
        $info = [];
145
        $info['username'] = 'banana';
146
        $info['email'] = 'banana@example.com';
147
 
148
        \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
149
 
150
        // Try and match a user with a linked login.
151
        $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
152
 
153
        $this->assertEquals($user->id, $match->get('userid'));
154
        $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
155
        \auth_oauth2\api::delete_linked_login($linkedlogins[0]->get('id'));
156
 
157
        $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
158
        $this->assertFalse($match);
159
 
160
        $info = [];
161
        $info['username'] = 'apple';
162
        $info['email'] = 'apple@example.com';
163
        $info['firstname'] = 'Apple';
164
        $info['lastname'] = 'Fruit';
165
        $info['url'] = 'http://apple.com/';
166
        $info['alternamename'] = 'Beatles';
167
 
168
        $newuser = \auth_oauth2\api::create_new_confirmed_account($info, $issuer);
169
 
170
        $match = \auth_oauth2\api::match_username_to_user('apple', $issuer);
171
 
172
        $this->assertEquals($newuser->id, $match->get('userid'));
173
    }
174
 
175
    /**
1441 ariadna 176
     * Test that we cannot deleted a linked login for another user
177
     */
178
    public function test_delete_linked_login_other_user(): void {
179
        $this->resetAfterTest();
180
 
181
        $this->setAdminUser();
182
        $issuer = \core\oauth2\api::create_standard_issuer('google');
183
 
184
        $user = $this->getDataGenerator()->create_user();
185
 
186
        api::link_login([
187
            'username' => 'banana',
188
            'email' => 'banana@example.com',
189
        ], $issuer, $user->id);
190
 
191
        /** @var linked_login $linkedlogin */
192
        $linkedlogin = api::get_linked_logins($user->id)[0];
193
 
194
        // We are logged in as a different user, so cannot delete this.
195
        $this->expectException(\dml_missing_record_exception::class);
196
        api::delete_linked_login($linkedlogin->get('id'));
197
    }
198
 
199
    /**
1 efrain 200
     * Test that is_enabled correctly identifies when the plugin is enabled.
201
     */
11 efrain 202
    public function test_is_enabled(): void {
1 efrain 203
        $this->resetAfterTest();
204
 
205
        set_config('auth', 'manual,oauth2');
206
        $this->assertTrue(\auth_oauth2\api::is_enabled());
207
    }
208
 
209
    /**
210
     * Test that is_enabled correctly identifies when the plugin is disabled.
211
     */
11 efrain 212
    public function test_is_enabled_disabled(): void {
1 efrain 213
        $this->resetAfterTest();
214
 
215
        set_config('auth', 'manual');
216
        $this->assertFalse(\auth_oauth2\api::is_enabled());
217
    }
218
 
219
    /**
220
     * Test creating a user via the send confirm account email method.
221
     * Including testing that user profile fields are correctly set.
222
     *
223
     * @covers \auth_oauth2\api::send_confirm_account_email
224
     */
11 efrain 225
    public function test_send_confirm_account_email(): void {
1 efrain 226
        global $DB;
227
        $this->resetAfterTest();
228
        $this->setAdminUser();
229
 
230
        $issuer = \core\oauth2\api::create_standard_issuer('microsoft');
231
 
232
        $info = [];
233
        $info['username'] = 'apple';
234
        $info['email'] = 'apple@example.com';
235
        $info['firstname'] = 'Apple';
236
        $info['lastname'] = 'Fruit';
237
        $info['alternatename'] = 'Beatles';
238
        $info['idnumber'] = '123456';
239
        $info['city'] = 'Melbourne';
240
        $info['country'] = 'AU';
241
        $info['institution'] = 'ACME Inc';
242
        $info['department'] = 'Misc Explosives';
243
 
244
        $createduser = \auth_oauth2\api::send_confirm_account_email($info, $issuer);
245
 
246
        // Get actual user record from DB to check.
247
        $userdata = $DB->get_record('user', ['id' => $createduser->id]);
248
 
249
        // Confirm each value supplied from issuers is saved into the user record.
250
        foreach ($info as $key => $value) {
251
            $this->assertEquals($value, $userdata->$key);
252
        }
253
 
254
        // Explicitly test the user is not yet confirmed.
255
        $this->assertEquals(0, $userdata->confirmed);
256
    }
1441 ariadna 257
 
258
    /**
259
     * Test case for checking the email greetings in OAuth2 confirmation emails.
260
     */
261
    public function test_email_greetings(): void {
262
        $this->resetAfterTest();
263
        $this->setAdminUser();
264
 
265
        $issuer = \core\oauth2\api::create_standard_issuer('microsoft');
266
 
267
        $userinfo = [];
268
        $userinfo['username'] = 'apple';
269
        $userinfo['email'] = 'apple@example.com';
270
        $userinfo['firstname'] = 'Apple';
271
        $userinfo['lastname'] = 'Fruit';
272
        $sink = $this->redirectEmails(); // Make sure we are redirecting emails.
273
        \auth_oauth2\api::send_confirm_account_email($userinfo, $issuer);
274
        $result = $sink->get_messages();
275
        $sink->close();
276
        // Test greetings.
277
        $this->assertStringContainsString('Hi ' . $userinfo['firstname'], quoted_printable_decode($result[0]->body));
278
 
279
        $userinfo = [];
280
        $userinfo['username'] = 'banana';
281
        $userinfo['email'] = 'banana@example.com';
282
        $userinfo['firstname'] = 'Banana';
283
        $userinfo['lastname'] = 'Fruit';
284
        $user = $this->getDataGenerator()->create_user();
285
        $sink = $this->redirectEmails(); // Make sure we are redirecting emails.
286
        \auth_oauth2\api::send_confirm_link_login_email($userinfo, $issuer, $user->id);
287
        $result = $sink->get_messages();
288
        $sink->close();
289
        // Test greetings.
290
        $this->assertStringContainsString('Hi ' . $user->firstname, quoted_printable_decode($result[0]->body));
291
    }
1 efrain 292
}