forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-75171 Auth: extend user logged in event
Co-authored-by: Heena Agheda <[email protected]>
- Loading branch information
1 parent
17ee072
commit a6a7b16
Showing
5 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
* @category test | ||
* @copyright 2019 Shamim Rezaie | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @coversDefaultClass \auth_oauth2\auth | ||
*/ | ||
class auth_test extends \advanced_testcase { | ||
|
||
|
@@ -38,4 +39,54 @@ public function test_get_password_change_info() { | |
'your password cannot be reset because you are using your account on another site to log in', | ||
$info['message']); | ||
} | ||
|
||
/** | ||
* Test complete_login for oauth2. | ||
* @covers ::complete_login | ||
*/ | ||
public function test_oauth2_complete_login(): void { | ||
global $CFG; | ||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
$wantsurl = new \moodle_url('/'); | ||
|
||
$issuer = \core\oauth2\api::create_standard_issuer('microsoft'); | ||
|
||
$info = []; | ||
$info['username'] = 'apple'; | ||
$info['email'] = '[email protected]'; | ||
$info['firstname'] = 'Apple'; | ||
$info['lastname'] = 'Fruit'; | ||
$info['url'] = 'http://apple.com/'; | ||
$info['alternamename'] = 'Beatles'; | ||
$info['auth'] = 'oauth2'; | ||
|
||
$user = \auth_oauth2\api::create_new_confirmed_account($info, $issuer); | ||
$auth = get_auth_plugin($user->auth); | ||
|
||
// Set up mock data. | ||
$client = $this->createMock(\core\oauth2\client::class); | ||
$client->expects($this->once())->method('get_raw_userinfo')->willReturn((object)$info); | ||
$client->expects($this->once())->method('get_userinfo')->willReturn($info); | ||
$client->expects($this->once())->method('get_issuer')->willReturn($issuer); | ||
|
||
$sink = $this->redirectEvents(); | ||
try { | ||
// Need @ as it will fail at \core\session\manager::login_user for session_regenerate_id. | ||
@$auth->complete_login($client, $wantsurl); | ||
} catch (\Exception $e) { | ||
// This happens as complete login is using 'redirect'. | ||
$this->assertInstanceOf(\moodle_exception::class, $e); | ||
} | ||
$events = $sink->get_events(); | ||
$sink->close(); | ||
|
||
// There are 2 events. First is core\event\user_updated and second is core\event\user_loggedin. | ||
$event = $events[1]; | ||
$this->assertInstanceOf('core\event\user_loggedin', $event); | ||
|
||
// Make sure the extra record is in the user_loggedin event. | ||
$extrauserinfo = $event->other['extrauserinfo']; | ||
$this->assertEquals($info, $extrauserinfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters