Author: Alexandru Stanoi
Date: 2007-03-29 14:42:31 +0200 (Thu, 29 Mar 2007)
New Revision: 4785

Log:
- Some code and tests (not complete) to see better how Authentication works.

Added:
   experimental/Authentication/src/authentication.php
   experimental/Authentication/src/authentication_autoload.php
   experimental/Authentication/src/exceptions/
   experimental/Authentication/src/filters/
   experimental/Authentication/src/filters/group/
   experimental/Authentication/src/filters/group/group_filter.php
   experimental/Authentication/src/filters/htpasswd/
   experimental/Authentication/src/filters/htpasswd/htpasswd_filter.php
   experimental/Authentication/src/interfaces/
   experimental/Authentication/src/interfaces/authentication_filter.php
   experimental/Authentication/src/options/
   experimental/Authentication/src/options/authentication_options.php
   experimental/Authentication/src/options/filter_options.php
   experimental/Authentication/tests/filters/
   experimental/Authentication/tests/filters/group/
   experimental/Authentication/tests/filters/group/group_test.php
   experimental/Authentication/tests/filters/htpasswd/
   experimental/Authentication/tests/filters/htpasswd/data/
   experimental/Authentication/tests/filters/htpasswd/data/.htpasswd
   experimental/Authentication/tests/filters/htpasswd/data/.htpasswd_empty
   experimental/Authentication/tests/filters/htpasswd/htpasswd_test.php
   experimental/Authentication/tests/general/
   experimental/Authentication/tests/general/authentication_test.php
   experimental/Authentication/tests/suite.php

Added: experimental/Authentication/src/authentication.php
===================================================================
--- experimental/Authentication/src/authentication.php  2007-03-29 12:41:27 UTC 
(rev 4784)
+++ experimental/Authentication/src/authentication.php  2007-03-29 12:42:31 UTC 
(rev 4785)
@@ -0,0 +1,99 @@
+<?php
+/**
+ * File containing the ezcAuthentication class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * Container for authentication filters.
+ *
+ * @package Authentication
+ * @version //autogen//
+ * @mainclass
+ */
+class ezcAuthentication
+{
+    /**
+     * Status returned by the run() method when the filter has completed
+     * successfully and it doesn't need authentication from other filters.
+     */
+    const STATUS_STOP = 1;
+
+    /**
+     * Status returned by the run() method when the filter has completed
+     * successfully and it needs authentication from other filters.
+     */
+    const STATUS_CONTINUE = 2;
+
+    /**
+     * Status returned by the run() method if the filter has completed
+     * with an error.
+     */
+    const STATUS_ERROR = 4;
+
+    /**
+     * The filter queue of the authentication process.
+     * 
+     * @var array(ezcAuthenticationFilter)
+     */
+    private $filters;
+
+    /**
+     * Options for the Authentication object.
+     * 
+     * @var ezcAuthenticationOptions
+     */
+    private $options;
+
+    /**
+     * Creates a new object of this class.
+     *
+     * @param array(string=>mixed) $options Options for this class
+     */
+    public function __construct( array $options = array() )
+    {
+        $this->options = new ezcAuthenticationOptions( $options );
+        $this->filters = array();
+    }
+
+    /**
+     * Adds an authentication filter at the end of the filter list.
+     *
+     * @param ezcAuthenticationFilter $filter The authentication filter to add
+     */
+    public function addFilter( ezcAuthenticationFilter $filter )
+    {
+        $this->filters[] = $filter;
+    }
+
+    /**
+     * Runs through all the filters in the filter queue.
+     *
+     * @return bool
+     */
+    public function run()
+    {
+        foreach ( $this->filters as $filter )
+        {
+            $status = $filter->run();
+            if ( $status === self::STATUS_CONTINUE )
+            {
+            }
+            if ( $status === self::STATUS_STOP )
+            {
+                break;
+            }
+            if ( $status === self::STATUS_ERROR )
+            {
+                return false;
+            }
+        }
+        return true;
+    }
+}
+?>


Property changes on: experimental/Authentication/src/authentication.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/authentication_autoload.php
===================================================================
--- experimental/Authentication/src/authentication_autoload.php 2007-03-29 
12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/authentication_autoload.php 2007-03-29 
12:42:31 UTC (rev 4785)
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Autoloader definition for the ezcAuthentication component.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+return array(
+
+    // authentication class
+    'ezcAuthentication'                       => 
'Authentication/authentication.php',
+
+    // authentication options
+    'ezcAuthenticationOptions'                => 
'Authentication/options/authentication_options.php',
+
+    // authentication filter interface + options
+    'ezcAuthenticationFilter'                 => 
'Authentication/interfaces/authentication_filter.php',
+    'ezcAuthenticationFilterOptions'          => 
'Authentication/options/filter_options.php',
+
+    // authentication filters + options
+    'ezcAuthenticationGroupFilter'            => 
'Authentication/filters/group/group_filter.php',
+    'ezcAuthenticationHtpasswdFilter'         => 
'Authentication/filters/htpasswd/htpasswd_filter.php',
+
+    );
+?>


Property changes on: experimental/Authentication/src/authentication_autoload.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/filters/group/group_filter.php
===================================================================
--- experimental/Authentication/src/filters/group/group_filter.php      
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/filters/group/group_filter.php      
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,62 @@
+<?php
+/**
+ * File containing the ezcAuthenticationGroupFilter class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * Group authentication filters together, where only one filter needs to 
succeed
+ * in order for the group to succeed.
+ *
+ * @package Authentication
+ * @version //autogen//
+ * @mainclass
+ */
+class ezcAuthenticationGroupFilter extends ezcAuthenticationFilter
+{
+    /**
+     * Authentication filters, where only one filter needs to succeed in order 
for
+     * the group to succeed.
+     * 
+     * @var array(ezcAuthenticationFilter)
+     */
+    private $filters;
+
+    /**
+     * Creates a new object of this class.
+     *
+     * @param array(ezcAuthenticationFilter) $filters Authentication filters
+     * @param array(string=>mixed) $options Options for the authentication 
filter
+     */
+    public function __construct( array $filters, array $options = array() )
+    {
+        $this->filters = $filters;
+        $this->options = new ezcAuthenticationFilterOptions( $options );
+    }
+
+    /**
+     * Runs the filter and returns a status code when finished.
+     *
+     * @return int
+     */
+    public function run()
+    {
+        foreach ( $this->filters as $filter )
+        {
+            $status = $filter->run();
+            if ( $status === ezcAuthentication::STATUS_CONTINUE ||
+                 $status === ezcAuthentication::STATUS_STOP )
+            {
+                return $this->options->continue ? 
ezcAuthentication::STATUS_CONTINUE :
+                                                  
ezcAuthentication::STATUS_STOP;
+            }
+        }
+        return ezcAuthentication::STATUS_ERROR;
+    }
+}
+?>


Property changes on: 
experimental/Authentication/src/filters/group/group_filter.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/filters/htpasswd/htpasswd_filter.php
===================================================================
--- experimental/Authentication/src/filters/htpasswd/htpasswd_filter.php        
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/filters/htpasswd/htpasswd_filter.php        
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,101 @@
+<?php
+/**
+ * File containing the ezcAuthenticationHtpasswdFilter class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * Filter to authenticate against a Unix htpasswd file.
+ *
+ * @property string $file
+ *                  The path and file name of the htpasswd file to use.
+ * @property string $user
+ *                  The username.
+ * @property string $password
+ *                  The password of the user.
+ *
+ * @package Authentication
+ * @version //autogen//
+ * @mainclass
+ */
+class ezcAuthenticationHtpasswdFilter extends ezcAuthenticationFilter
+{
+    /**
+     * The path and file name of the htpasswd file to use.
+     * 
+     * @var string
+     */
+    private $file;
+
+    /**
+     * The user name to authenticate.
+     * 
+     * @var string
+     */
+    private $user;
+
+    /**
+     * The password of the user.
+     * 
+     * @var string
+     */
+    private $password;
+
+    /**
+     * Creates a new object of this class.
+     *
+     * @param array(string) $properties Values with which to initialize this 
object
+     * @param array(string=>mixed) $options Options for this class
+     */
+    public function __construct( array $properties, array $options = array() )
+    {
+        $this->file = $properties["file"];
+        $this->user = $properties["user"];
+        $this->password = $properties["password"];
+        $this->options = new ezcAuthenticationFilterOptions( $options );
+    }
+
+    /**
+     * Runs the filter and returns a status code when finished.
+     *
+     * @return int
+     */
+    public function run()
+    {
+        if ( !file_exists( $this->file ) )
+        {
+            throw new ezcBaseFileNotFoundException( $this->file );
+        }
+        $fh = @fopen( $this->file, 'r' );
+        if ( $fh === false )
+        {
+            throw new ezcBaseFilePermissionException( $this->file, 
ezcBaseFileException::READ );
+        }
+        $found = false;
+        while ( $line = fgets( $fh ) )
+        {
+            $parts = split( ':', $line );
+            if ( count( $parts ) >= 2 && $parts[0] === $this->user )
+            {
+                $found = true;
+                break;
+            }
+        }
+        fclose( $fh );
+        if ( $found )
+        {
+            if ( trim( $parts[1] ) === crypt( $this->password, $parts[1] ) )
+            {
+                return $this->options->continue ? 
ezcAuthentication::STATUS_CONTINUE :
+                                                  
ezcAuthentication::STATUS_STOP;
+            }
+        }
+        return ezcAuthentication::STATUS_ERROR;
+    }
+}
+?>


Property changes on: 
experimental/Authentication/src/filters/htpasswd/htpasswd_filter.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/interfaces/authentication_filter.php
===================================================================
--- experimental/Authentication/src/interfaces/authentication_filter.php        
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/interfaces/authentication_filter.php        
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,34 @@
+<?php
+/**
+ * File containing the ezcAuthenticationFilter class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * The main class of the Authentication package.
+ *
+ * @package Authentication
+ * @version //autogen//
+ */
+abstract class ezcAuthenticationFilter
+{
+    /**
+     * Options for authentication filters.
+     * 
+     * @var ezcAuthenticationFilterOptions
+     */
+    protected $options;
+
+    /**
+     * Runs the filter and returns a status code when finished.
+     *
+     * @return int
+     */
+    abstract public function run();
+}
+?>


Property changes on: 
experimental/Authentication/src/interfaces/authentication_filter.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/options/authentication_options.php
===================================================================
--- experimental/Authentication/src/options/authentication_options.php  
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/options/authentication_options.php  
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,54 @@
+<?php
+/**
+ * File containing the ezcAuthenticationOptions class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * Class containing the basic options for the authentication.
+ *
+ * @package Authentication
+ * @version //autogen//
+ */
+class ezcAuthenticationOptions extends ezcBaseOptions
+{
+    /**
+     * Constructs an object with the specified values.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if $options contains a property not defined
+     * @throws ezcBaseValueException
+     *         if $options contains a property with a value not allowed
+     * @param array(string=>mixed) $options
+     */
+    public function __construct( array $options = array() )
+    {
+        parent::__construct( $options );
+    }
+
+    /**
+     * Sets the option $name to $value.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if the property $name is not defined
+     * @throws ezcBaseValueException
+     *         if $value is not correct for the property $name
+     * @param string $name
+     * @param mixed $value
+     * @ignore
+     */
+    public function __set( $name, $value )
+    {
+        switch ( $name )
+        {
+            default:
+                throw new ezcBasePropertyNotFoundException( $name );
+        }
+    }
+}
+?>


Property changes on: 
experimental/Authentication/src/options/authentication_options.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/src/options/filter_options.php
===================================================================
--- experimental/Authentication/src/options/filter_options.php  2007-03-29 
12:41:27 UTC (rev 4784)
+++ experimental/Authentication/src/options/filter_options.php  2007-03-29 
12:42:31 UTC (rev 4785)
@@ -0,0 +1,68 @@
+<?php
+/**
+ * File containing the ezcAuthenticationFilterOptions class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ */
+
+/**
+ * Class containing the basic options for authentication filters.
+ *
+ * @property bool $continue
+ *           Specifies whether the filter should stop the authentication 
process
+ *           if it succeeds, or not.
+ *
+ * @package Authentication
+ * @version //autogen//
+ */
+class ezcAuthenticationFilterOptions extends ezcBaseOptions
+{
+    /**
+     * Constructs an object with the specified values.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if $options contains a property not defined
+     * @throws ezcBaseValueException
+     *         if $options contains a property with a value not allowed
+     * @param array(string=>mixed) $options
+     */
+    public function __construct( array $options = array() )
+    {
+        $this->continue = true;
+
+        parent::__construct( $options );
+    }
+
+    /**
+     * Sets the option $name to $value.
+     *
+     * @throws ezcBasePropertyNotFoundException
+     *         if the property $name is not defined
+     * @throws ezcBaseValueException
+     *         if $value is not correct for the property $name
+     * @param string $name
+     * @param mixed $value
+     * @ignore
+     */
+    public function __set( $name, $value )
+    {
+        switch ( $name )
+        {
+            case 'continue':
+                if ( !is_bool( $value ) )
+                {
+                    throw new ezcBaseValueException( $name, $value, 'bool' );
+                }
+                $this->properties[$name] = $value;
+                break;
+
+            default:
+                throw new ezcBasePropertyNotFoundException( $name );
+        }
+    }
+}
+?>


Property changes on: experimental/Authentication/src/options/filter_options.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/tests/filters/group/group_test.php
===================================================================
--- experimental/Authentication/tests/filters/group/group_test.php      
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/tests/filters/group/group_test.php      
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,128 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+
+/**
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+class ezcAuthenticationGroupTest extends ezcTestCase
+{
+    private static $path = null;
+
+    public static function suite()
+    {
+        self::$path = dirname( __FILE__ ) . '/../htpasswd/data/.htpasswd';
+        return new PHPUnit_Framework_TestSuite( "ezcAuthenticationGroupTest" );
+    }
+
+    public function testGroupHtpasswdPass()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' )
+                        )
+                    )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testGroupHtpasswFail()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'wrong password' )
+                        )
+                    )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+
+    public function testGroupHtpasswdPassHtpasswdPass()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' )
+                        ),
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' )
+                        ),
+                    )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testGroupHtpasswdPassHtpasswdFail()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' )
+                        ),
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'wrong password' )
+                        ),
+                    )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testGroupHtpasswdFailHtpasswdPass()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'wrong password' )
+                        ),
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' )
+                        ),
+                    )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testGroupHtpasswdFailHtpasswdFail()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationGroupFilter(
+                array(
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'wrong password' )
+                        ),
+                    new ezcAuthenticationHtpasswdFilter(
+                        array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'wrong password' )
+                        ),
+                    )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+}
+?>


Property changes on: 
experimental/Authentication/tests/filters/group/group_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/tests/filters/htpasswd/data/.htpasswd
===================================================================
--- experimental/Authentication/tests/filters/htpasswd/data/.htpasswd   
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/tests/filters/htpasswd/data/.htpasswd   
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,2 @@
+john.doe:wpeE20wyWHnLE
+ola.nordmann:vwSb5b6IsXI6w

Added: experimental/Authentication/tests/filters/htpasswd/data/.htpasswd_empty
===================================================================
--- experimental/Authentication/tests/filters/htpasswd/data/.htpasswd_empty     
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/tests/filters/htpasswd/data/.htpasswd_empty     
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1 @@
+

Added: experimental/Authentication/tests/filters/htpasswd/htpasswd_test.php
===================================================================
--- experimental/Authentication/tests/filters/htpasswd/htpasswd_test.php        
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/tests/filters/htpasswd/htpasswd_test.php        
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,196 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+
+/**
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+class ezcAuthenticationHtpasswdTest extends ezcTestCase
+{
+    private static $path = null;
+
+    public static function suite()
+    {
+        self::$path = dirname( __FILE__ ) . '/data/.htpasswd';
+        return new PHPUnit_Framework_TestSuite( 
"ezcAuthenticationHtpasswdTest" );
+    }
+
+    public function testHtpasswdCorrectCredentials()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'foobar' )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testHtpasswdCorrectCredentialsStop()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'foobar' ),
+                array( 'continue' => false )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testHtpasswdCorrectCredentialsIncorrectFail()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'foobar' )
+                )
+            );
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'wrong password' )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+
+    public function testHtpasswdCorrectCredentialsStopIncorrectFail()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'foobar' ),
+                array( 'continue' => false )
+                )
+            );
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'wrong password' )
+                )
+            );
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testHtpasswdIncorrectUsername()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'no such user', 
'password' => 'foobar' )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+
+    public function testHtpasswdIncorrectPassword()
+    {
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => self::$path, 'user' => 'john.doe', 'password' 
=> 'wrong password' )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+
+    public function testHtpasswdFileEmpty()
+    {
+        $path = dirname( __FILE__ ) . '/data/.htpasswd_empty';
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => $path, 'user' => 'john.doe', 'password' => 
'foobar' )
+                )
+            );
+        $this->assertEquals( false, $authentication->run() );
+    }
+
+    public function testHtpasswdFileNotFound()
+    {
+        $path = dirname( __FILE__ ) . '/data/.htpassw';
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => $path, 'user' => 'john.doe', 'password' => 
'foobar' )
+                )
+            );
+        try
+        {
+            $authentication->run();
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBaseFileNotFoundException $e )
+        {
+            $this->assertEquals( "The file '{$path}' could not be found.", 
$e->getMessage() );
+        }
+    }
+
+    public function testHtpasswdFileNoPermission()
+    {
+        $tempDir = $this->createTempDir( 'ezcMailComposerTest' );
+        $path = $tempDir . "/htpasswd_unreadable";
+        $fh = fopen( $path, "wb" );
+        fwrite( $fh, "john.doe:wpeE20wyWHnLE" );
+        fclose( $fh );
+        chmod( $path, 0 );
+        $authentication = new ezcAuthentication();
+        $authentication->addFilter(
+            new ezcAuthenticationHtpasswdFilter(
+                array( 'file' => $path, 'user' => 'john.doe', 'password' => 
'foobar' )
+                )
+            );
+        try
+        {
+            $authentication->run();
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBaseFilePermissionException $e )
+        {
+            $this->assertEquals( "The file '{$path}' can not be opened for 
reading.", $e->getMessage() );
+        }
+        $this->removeTempDir();
+    }
+
+    public function testHtpasswdWrongOptions()
+    {
+        $authentication = new ezcAuthentication();
+        try
+        {
+            $authentication->addFilter(
+                new ezcAuthenticationHtpasswdFilter(
+                    array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' ),
+                    array( 'continue' => 'wrong option value' )
+                    )
+                );
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBaseValueException $e )
+        {
+            $this->assertEquals( "The value 'wrong option value' that you were 
trying to assign to setting 'continue' is invalid. Allowed values are: bool.", 
$e->getMessage() );
+        }
+
+        try
+        {
+            $authentication->addFilter(
+                new ezcAuthenticationHtpasswdFilter(
+                    array( 'file' => self::$path, 'user' => 'john.doe', 
'password' => 'foobar' ),
+                    array( 'wrong option' => 'wrong option value' )
+                    )
+                );
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            $this->assertEquals( "No such property name 'wrong option'.", 
$e->getMessage() );
+        }
+    }
+}
+?>


Property changes on: 
experimental/Authentication/tests/filters/htpasswd/htpasswd_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/tests/general/authentication_test.php
===================================================================
--- experimental/Authentication/tests/general/authentication_test.php   
2007-03-29 12:41:27 UTC (rev 4784)
+++ experimental/Authentication/tests/general/authentication_test.php   
2007-03-29 12:42:31 UTC (rev 4785)
@@ -0,0 +1,44 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+
+/**
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+class ezcAuthenticationTest extends ezcTestCase
+{
+    public static function suite()
+    {
+        return new PHPUnit_Framework_TestSuite( "ezcAuthenticationTest" );
+    }
+
+    public function testGeneralNoFilters()
+    {
+        $authentication = new ezcAuthentication();
+        $this->assertEquals( true, $authentication->run() );
+    }
+
+    public function testGeneralWrongOptions()
+    {
+        try
+        {
+            $authentication = new ezcAuthentication(
+                array ( 'wrong option' => 'wrong option value' )
+                );
+            $this->fail( "Expected exception was not thrown" );
+        }
+        catch ( ezcBasePropertyNotFoundException $e )
+        {
+            $this->assertEquals( "No such property name 'wrong option'.", 
$e->getMessage() );
+        }
+    }
+}
+?>


Property changes on: 
experimental/Authentication/tests/general/authentication_test.php
___________________________________________________________________
Name: svn:eol-style
   + native

Added: experimental/Authentication/tests/suite.php
===================================================================
--- experimental/Authentication/tests/suite.php 2007-03-29 12:41:27 UTC (rev 
4784)
+++ experimental/Authentication/tests/suite.php 2007-03-29 12:42:31 UTC (rev 
4785)
@@ -0,0 +1,43 @@
+<?php
+/**
+ * File containing the ezcAuthenticationSuite class.
+ *
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @filesource
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+
+/**
+ * Including the tests
+ */
+require_once( "general/authentication_test.php" );
+require_once( "filters/htpasswd/htpasswd_test.php" );
+require_once( "filters/group/group_test.php" );
+
+/**
+ * @package Authentication
+ * @version //autogen//
+ * @subpackage Tests
+ */
+class ezcAuthenticationSuite extends PHPUnit_Framework_TestSuite
+{
+    public function __construct()
+    {
+        parent::__construct();
+        $this->setName( "Authentication" );
+        ezcBase::addClassRepository( '/home/as/dev/ezcomponents/experimental', 
'/home/as/dev/ezcomponents/experimental/autoload' );
+        
+        $this->addTest( ezcAuthenticationTest::suite() );
+        $this->addTest( ezcAuthenticationHtpasswdTest::suite() );
+        $this->addTest( ezcAuthenticationGroupTest::suite() );
+    }
+
+    public static function suite()
+    {
+        return new ezcAuthenticationSuite();
+    }
+}
+?>


Property changes on: experimental/Authentication/tests/suite.php
___________________________________________________________________
Name: svn:eol-style
   + native

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to