1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-mfa |
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace vxm\test\unit\mfa; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
use vxm\mfa\IdentityInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BehaviorTest |
16
|
|
|
* |
17
|
|
|
* @author Vuong Minh <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class BehaviorTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
public function testLoggedIn() |
24
|
|
|
{ |
25
|
|
|
$identity = Identity::findIdentity('user1'); |
26
|
|
|
Yii::$app->user->login($identity); |
27
|
|
|
$data = Yii::$app->user->getIdentityLoggedIn(); |
28
|
|
|
|
29
|
|
|
$this->assertNotEmpty($data); |
30
|
|
|
$this->assertTrue($data[0] instanceof IdentityInterface); |
31
|
|
|
$this->assertEquals(0, $data[1]); |
32
|
|
|
$this->assertNotNull(Yii::$app->response->headers->get('location')); // verify required |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @depends testLoggedIn |
37
|
|
|
*/ |
38
|
|
|
public function testSwitchIdentityLoggedIn() |
39
|
|
|
{ |
40
|
|
|
$identity = Identity::findIdentity('user1'); |
41
|
|
|
Yii::$app->user->login($identity); |
42
|
|
|
Yii::$app->user->switchIdentityLoggedIn(); |
43
|
|
|
|
44
|
|
|
$this->assertFalse(Yii::$app->user->getIsGuest()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @depends testSwitchIdentityLoggedIn |
49
|
|
|
*/ |
50
|
|
|
public function testQRCodeUri() |
51
|
|
|
{ |
52
|
|
|
$identity = Identity::findIdentity('user1'); |
53
|
|
|
Yii::$app->user->login($identity); |
54
|
|
|
Yii::$app->user->switchIdentityLoggedIn(); |
55
|
|
|
|
56
|
|
|
$this->assertNotEmpty(Yii::$app->user->getQRCodeUri([ |
57
|
|
|
'label' => 'vxm' |
58
|
|
|
])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @depends testLoggedIn |
63
|
|
|
*/ |
64
|
|
|
public function testGenerateOtpByIdentityLoggedIn() |
65
|
|
|
{ |
66
|
|
|
$identity = Identity::findIdentity('user1'); |
67
|
|
|
Yii::$app->user->login($identity); |
68
|
|
|
|
69
|
|
|
$otp = Yii::$app->user->generateOtpByIdentityLoggedIn(); |
70
|
|
|
$this->assertNotEmpty($otp); |
71
|
|
|
$this->assertTrue(strlen($otp) === Yii::$app->user->otp->digits); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @depends testLoggedIn |
76
|
|
|
*/ |
77
|
|
|
public function testValidateOtpByIdentityLoggedIn() |
78
|
|
|
{ |
79
|
|
|
$identity = Identity::findIdentity('user1'); |
80
|
|
|
Yii::$app->user->login($identity); |
81
|
|
|
|
82
|
|
|
$otp = Yii::$app->user->generateOtpByIdentityLoggedIn(); |
83
|
|
|
$this->assertTrue(Yii::$app->user->validateOtpByIdentityLoggedIn($otp)); |
84
|
|
|
$this->assertFalse(Yii::$app->user->validateOtpByIdentityLoggedIn('abcd')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @depends testGenerateOtpByIdentityLoggedIn |
89
|
|
|
* @depends testValidateOtpByIdentityLoggedIn |
90
|
|
|
*/ |
91
|
|
|
public function testSetOtp() |
92
|
|
|
{ |
93
|
|
|
$identity = Identity::findIdentity('user1'); |
94
|
|
|
Yii::$app->user->login($identity); |
95
|
|
|
Yii::$app->user->setOtp([ |
96
|
|
|
'digits' => 8 |
97
|
|
|
]); |
98
|
|
|
$otp = Yii::$app->user->generateOtpByIdentityLoggedIn(); |
99
|
|
|
$this->assertTrue(strlen($otp) === 8); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|