| 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 yii\web\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use vxm\mfa\VerifyAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Class VerifyActionTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Vuong Minh <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | class VerifyActionTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function testBeforeAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $controller = $this->mockController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $controller->runAction('verify'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->assertNotNull(Yii::$app->response->headers->get('location')); // login required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @depends testBeforeAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function testRenderView() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $identity = Identity::findIdentity('user1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         Yii::$app->user->login($identity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $controller = $this->mockController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $result = $controller->runAction('verify'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertEquals('hello world!', trim($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @depends testBeforeAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function testInValidOtp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $identity = Identity::findIdentity('user1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         Yii::$app->user->login($identity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $controller = $this->mockController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $_POST['_method'] = 'POST'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         Yii::$app->request->setBodyParams([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             Yii::$app->request->csrfParam => Yii::$app->request->csrfToken, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'OtpForm' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 'otp' => 'abcd', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $controller->runAction('verify'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertTrue(Yii::$app->user->getIsGuest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // test retry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         /** @var VerifyAction $action */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $action = $controller->createAction('verify'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $action->retry = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $action->runWithParams([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $data = Yii::$app->user->getIdentityLoggedIn(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertNull($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @depends testBeforeAction | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     public function testValidOtp() | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $identity = Identity::findIdentity('user1'); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         Yii::$app->user->login($identity); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $controller = $this->mockController(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $_POST['_method'] = 'POST'; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         Yii::$app->request->setBodyParams([ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             Yii::$app->request->csrfParam => Yii::$app->request->csrfToken, | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             'OtpForm' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |                 'otp' => Yii::$app->user->generateOtpByIdentityLoggedIn(), | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $controller->runAction('verify'); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertFalse(Yii::$app->user->getIsGuest()); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     protected function mockController() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return new class('test', Yii::$app) extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             public $layout = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             public function actions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     'verify' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                         'class' => 'vxm\mfa\VerifyAction', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                         'viewFile' => '@vxm/test/unit/mfa/verify-action-view.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                         'retry' => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                     ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |