| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SimpleSAML\Module\monitor\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \SimpleSAML_Configuration as ApplicationConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \SimpleSAML\Module\monitor\TestCase as TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \SimpleSAML\Module\monitor\TestData as TestData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use \SimpleSAML\Module\monitor\State as State; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Tests for TestCase\Ldap\Bind | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class TestLdapBindTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function testBindSuccesful() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $authSourceData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'search.username' => 'testuser', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'search.password' => 'password', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $connectionMock = $this->getMock('LdapConnection', array('bind')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $connectionMock->expects($this->once())->method('bind')->will($this->returnValue(true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $confTest = new TestCase\AuthSource\Ldap\Bind( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             new TestData([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 'authSourceData' => ApplicationConfiguration::loadFromArray($authSourceData), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 'connection' => $connectionMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $testResult = $confTest->getTestResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->assertEquals(State::OK, $testResult->getState()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function testBindFailed() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $authSourceData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'search.username' => 'testuser', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'search.password' => 'password', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $connectionMock = $this->getMock('LdapConnection', array('bind')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $connectionMock->expects($this->once())->method('bind')->will($this->throwException(new \Exception())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $confTest = new TestCase\AuthSource\Ldap\Bind( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             new TestData([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'authSourceData' => ApplicationConfiguration::loadFromArray($authSourceData), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'connection' => $connectionMock, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $testResult = $confTest->getTestResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertEquals(State::FATAL, $testResult->getState()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function testAuthenticationFailed() | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $authSourceData = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'search.username' => 'testuser', | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             'search.password' => 'password', | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $connectionMock = $this->getMock('LdapConnection', array('bind')); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $connectionMock->expects($this->once())->method('bind')->will($this->returnValue(false)); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $confTest = new TestCase\AuthSource\Ldap\Bind( | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             new TestData([ | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 'authSourceData' => ApplicationConfiguration::loadFromArray($authSourceData), | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 'connection' => $connectionMock, | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             ]) | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $testResult = $confTest->getTestResult(); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $this->assertEquals(State::ERROR, $testResult->getState()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  |