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 |
* Auth oauth2 auth functions tests.
|
|
|
21 |
*
|
|
|
22 |
* @package auth_oauth2
|
|
|
23 |
* @category test
|
|
|
24 |
* @copyright 2019 Shamim Rezaie
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
* @coversDefaultClass \auth_oauth2\auth
|
|
|
27 |
*/
|
|
|
28 |
class auth_test extends \advanced_testcase {
|
|
|
29 |
|
|
|
30 |
public function test_get_password_change_info() {
|
|
|
31 |
$this->resetAfterTest();
|
|
|
32 |
|
|
|
33 |
$user = $this->getDataGenerator()->create_user(['auth' => 'oauth2']);
|
|
|
34 |
$auth = get_auth_plugin($user->auth);
|
|
|
35 |
$info = $auth->get_password_change_info($user);
|
|
|
36 |
|
|
|
37 |
$this->assertEqualsCanonicalizing(['subject', 'message'], array_keys($info));
|
|
|
38 |
$this->assertStringContainsString(
|
|
|
39 |
'your password cannot be reset because you are using your account on another site to log in',
|
|
|
40 |
$info['message']);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Test complete_login for oauth2.
|
|
|
45 |
* @covers ::complete_login
|
|
|
46 |
*/
|
|
|
47 |
public function test_oauth2_complete_login(): void {
|
|
|
48 |
global $CFG;
|
|
|
49 |
$this->resetAfterTest();
|
|
|
50 |
$this->setAdminUser();
|
|
|
51 |
$wantsurl = new \moodle_url('/');
|
|
|
52 |
|
|
|
53 |
$issuer = \core\oauth2\api::create_standard_issuer('microsoft');
|
|
|
54 |
|
|
|
55 |
$info = [];
|
|
|
56 |
$info['username'] = 'apple';
|
|
|
57 |
$info['email'] = 'apple@example.com';
|
|
|
58 |
$info['firstname'] = 'Apple';
|
|
|
59 |
$info['lastname'] = 'Fruit';
|
|
|
60 |
$info['url'] = 'http://apple.com/';
|
|
|
61 |
$info['alternamename'] = 'Beatles';
|
|
|
62 |
$info['auth'] = 'oauth2';
|
|
|
63 |
|
|
|
64 |
$user = \auth_oauth2\api::create_new_confirmed_account($info, $issuer);
|
|
|
65 |
$auth = get_auth_plugin($user->auth);
|
|
|
66 |
|
|
|
67 |
// Set up mock data.
|
|
|
68 |
$client = $this->createMock(\core\oauth2\client::class);
|
|
|
69 |
$client->expects($this->once())->method('get_raw_userinfo')->willReturn((object)$info);
|
|
|
70 |
$client->expects($this->once())->method('get_userinfo')->willReturn($info);
|
|
|
71 |
$client->expects($this->once())->method('get_issuer')->willReturn($issuer);
|
|
|
72 |
|
|
|
73 |
$sink = $this->redirectEvents();
|
|
|
74 |
try {
|
|
|
75 |
// Need @ as it will fail at \core\session\manager::login_user for session_regenerate_id.
|
|
|
76 |
@$auth->complete_login($client, $wantsurl);
|
|
|
77 |
} catch (\Exception $e) {
|
|
|
78 |
// This happens as complete login is using 'redirect'.
|
|
|
79 |
$this->assertInstanceOf(\moodle_exception::class, $e);
|
|
|
80 |
}
|
|
|
81 |
$events = $sink->get_events();
|
|
|
82 |
$sink->close();
|
|
|
83 |
|
|
|
84 |
// There are 2 events. First is core\event\user_updated and second is core\event\user_loggedin.
|
|
|
85 |
$event = $events[1];
|
|
|
86 |
$this->assertInstanceOf('core\event\user_loggedin', $event);
|
|
|
87 |
|
|
|
88 |
// Make sure the extra record is in the user_loggedin event.
|
|
|
89 |
$extrauserinfo = $event->other['extrauserinfo'];
|
|
|
90 |
$this->assertEquals($info, $extrauserinfo);
|
|
|
91 |
}
|
|
|
92 |
}
|