2018-11-04 17:06:30 +00:00
< ? php
/** @ license MIT
* Copyright 2017 J . King , Dustin Wilson et al .
* See LICENSE and AUTHORS files for details */
declare ( strict_types = 1 );
namespace JKingWeb\Arsse\TestCase\User ;
use JKingWeb\Arsse\Arsse ;
use JKingWeb\Arsse\Database ;
use JKingWeb\Arsse\User\Driver as DriverInterface ;
use JKingWeb\Arsse\User\Internal\Driver ;
/** @covers \JKingWeb\Arsse\User\Internal\Driver */
class TestInternal extends \JKingWeb\Arsse\Test\AbstractTest {
2019-10-16 18:42:43 +00:00
public function setUp () : void {
2018-11-23 15:01:17 +00:00
self :: clearData ();
2018-11-23 00:55:54 +00:00
self :: setConf ();
2018-11-04 17:06:30 +00:00
// create a mock database interface
2019-09-05 14:03:32 +00:00
Arsse :: $db = \Phake :: mock ( Database :: class );
2019-09-05 14:13:17 +00:00
\Phake :: when ( Arsse :: $db ) -> begin -> thenReturn ( \Phake :: mock ( \JKingWeb\Arsse\Db\Transaction :: class ));
2018-11-04 17:06:30 +00:00
}
2020-01-20 18:52:48 +00:00
public function testConstruct () : void {
2018-11-04 17:06:30 +00:00
$this -> assertInstanceOf ( DriverInterface :: class , new Driver );
}
2020-01-20 18:52:48 +00:00
public function testFetchDriverName () : void {
2018-11-04 17:06:30 +00:00
$this -> assertTrue ( strlen ( Driver :: driverName ()) > 0 );
}
2018-12-05 22:28:11 +00:00
/**
* @ dataProvider provideAuthentication
2018-11-04 17:06:30 +00:00
* @ group slow
2020-03-01 20:16:50 +00:00
*/
2020-11-09 21:51:30 +00:00
public function testAuthenticateAUser ( string $user , $password , bool $exp ) : void {
\Phake :: when ( Arsse :: $db ) -> userPasswordGet ( " john.doe@example.com " ) -> thenReturn ( '$2y$10$1zbqRJhxM8uUjeSBPp4IhO90xrqK0XjEh9Z16iIYEFRV4U.zeAFom' ); // hash of "secret"
\Phake :: when ( Arsse :: $db ) -> userPasswordGet ( " jane.doe@example.com " ) -> thenReturn ( '$2y$10$bK1ljXfTSyc2D.NYvT.Eq..OpehLRXVbglW.23ihVuyhgwJCd.7Im' ); // hash of "superman"
\Phake :: when ( Arsse :: $db ) -> userPasswordGet ( " owen.hardy@example.com " ) -> thenReturn ( " " );
2020-11-15 21:24:26 +00:00
\Phake :: when ( Arsse :: $db ) -> userPasswordGet ( " kira.nerys@example.com " ) -> thenThrow ( new \JKingWeb\Arsse\User\ExceptionConflict ( " doesNotExist " ));
2020-11-09 21:51:30 +00:00
\Phake :: when ( Arsse :: $db ) -> userPasswordGet ( " 007@example.com " ) -> thenReturn ( null );
2018-11-04 17:06:30 +00:00
$this -> assertSame ( $exp , ( new Driver ) -> auth ( $user , $password ));
}
2019-10-16 18:42:43 +00:00
public function provideAuthentication () : iterable {
2018-11-04 17:06:30 +00:00
$john = " john.doe@example.com " ;
$jane = " jane.doe@example.com " ;
$owen = " owen.hardy@example.com " ;
$kira = " kira.nerys@example.com " ;
2019-03-24 18:42:23 +00:00
$bond = " 007@example.com " ;
2018-11-04 17:06:30 +00:00
return [
2020-11-09 21:51:30 +00:00
[ $john , " secret " , true ],
[ $jane , " superman " , true ],
[ $owen , " " , true ],
[ $kira , " ashalla " , false ],
[ $john , " top secret " , false ],
[ $jane , " clark kent " , false ],
[ $owen , " watchmaker " , false ],
[ $kira , " singha " , false ],
[ $john , " " , false ],
[ $jane , " " , false ],
[ $kira , " " , false ],
[ $bond , " for England " , false ],
[ $bond , " " , false ],
2018-11-04 17:06:30 +00:00
];
}
2020-01-20 18:52:48 +00:00
public function testListUsers () : void {
2018-11-04 17:06:30 +00:00
$john = " john.doe@example.com " ;
$jane = " jane.doe@example.com " ;
2019-09-05 14:03:32 +00:00
\Phake :: when ( Arsse :: $db ) -> userList -> thenReturn ([ $john , $jane ]) -> thenReturn ([ $jane , $john ]);
2018-11-04 17:06:30 +00:00
$driver = new Driver ;
$this -> assertSame ([ $john , $jane ], $driver -> userList ());
$this -> assertSame ([ $jane , $john ], $driver -> userList ());
2019-09-05 14:03:32 +00:00
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 2 )) -> userList ;
2018-11-04 17:06:30 +00:00
}
2020-01-20 18:52:48 +00:00
public function testAddAUser () : void {
2018-11-04 17:06:30 +00:00
$john = " john.doe@example.com " ;
2019-09-05 14:03:32 +00:00
\Phake :: when ( Arsse :: $db ) -> userAdd -> thenReturnCallback ( function ( $user , $pass ) {
2018-11-04 17:06:30 +00:00
return $pass ;
});
$driver = new Driver ;
$this -> assertNull ( $driver -> userAdd ( $john ));
$this -> assertNull ( $driver -> userAdd ( $john , null ));
$this -> assertSame ( " secret " , $driver -> userAdd ( $john , " secret " ));
2019-09-05 14:03:32 +00:00
\Phake :: verify ( Arsse :: $db ) -> userAdd ( $john , " secret " );
\Phake :: verify ( Arsse :: $db ) -> userAdd ;
2018-11-04 17:06:30 +00:00
}
2020-12-25 22:47:36 +00:00
public function testRenameAUser () : void {
$john = " john.doe@example.com " ;
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( true );
$this -> assertTrue (( new Driver ) -> userRename ( $john , " jane.doe@example.com " ));
$this -> assertFalse (( new Driver ) -> userRename ( $john , $john ));
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 2 )) -> userExists ( $john );
}
public function testRenameAMissingUser () : void {
$john = " john.doe@example.com " ;
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( false );
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
( new Driver ) -> userRename ( $john , " jane.doe@example.com " );
}
2020-01-20 18:52:48 +00:00
public function testRemoveAUser () : void {
2018-11-04 17:06:30 +00:00
$john = " john.doe@example.com " ;
2020-11-15 21:24:26 +00:00
\Phake :: when ( Arsse :: $db ) -> userRemove -> thenReturn ( true ) -> thenThrow ( new \JKingWeb\Arsse\User\ExceptionConflict ( " doesNotExist " ));
2018-11-04 17:06:30 +00:00
$driver = new Driver ;
$this -> assertTrue ( $driver -> userRemove ( $john ));
2019-09-05 14:03:32 +00:00
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 1 )) -> userRemove ( $john );
2020-11-15 21:24:26 +00:00
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
2018-11-04 17:06:30 +00:00
try {
$this -> assertFalse ( $driver -> userRemove ( $john ));
} finally {
2019-09-05 14:03:32 +00:00
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 2 )) -> userRemove ( $john );
2018-11-04 17:06:30 +00:00
}
}
2020-01-20 18:52:48 +00:00
public function testSetAPassword () : void {
2018-11-04 17:06:30 +00:00
$john = " john.doe@example.com " ;
2020-12-25 22:47:36 +00:00
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( true );
2018-11-04 17:06:30 +00:00
$this -> assertSame ( " superman " , ( new Driver ) -> userPasswordSet ( $john , " superman " ));
$this -> assertSame ( null , ( new Driver ) -> userPasswordSet ( $john , null ));
2020-11-09 23:14:03 +00:00
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 0 )) -> userPasswordSet ;
2018-11-04 17:06:30 +00:00
}
2019-03-24 18:42:23 +00:00
2020-12-25 22:47:36 +00:00
public function testSetAPasswordForAMssingUser () : void {
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( false );
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
( new Driver ) -> userPasswordSet ( " john.doe@example.com " , " secret " );
}
2020-01-20 18:52:48 +00:00
public function testUnsetAPassword () : void {
2020-11-09 23:14:03 +00:00
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( true );
$this -> assertTrue (( new Driver ) -> userPasswordUnset ( " john.doe@example.com " ));
\Phake :: verify ( Arsse :: $db , \Phake :: times ( 0 )) -> userPasswordUnset ;
2019-03-24 18:42:23 +00:00
}
2020-01-20 18:52:48 +00:00
public function testUnsetAPasswordForAMssingUser () : void {
2020-11-09 23:14:03 +00:00
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( false );
2020-11-15 21:24:26 +00:00
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
2020-11-09 23:14:03 +00:00
( new Driver ) -> userPasswordUnset ( " john.doe@example.com " );
2019-03-24 18:42:23 +00:00
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testGetUserProperties () : void {
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( true );
$this -> assertSame ([], ( new Driver ) -> userPropertiesGet ( " john.doe@example.com " ));
\Phake :: verify ( Arsse :: $db ) -> userExists ( " john.doe@example.com " );
\Phake :: verifyNoFurtherInteraction ( Arsse :: $db );
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testGetPropertiesForAMissingUser () : void {
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( false );
2020-11-15 21:24:26 +00:00
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
2020-11-14 02:41:27 +00:00
try {
( new Driver ) -> userPropertiesGet ( " john.doe@example.com " );
} finally {
\Phake :: verify ( Arsse :: $db ) -> userExists ( " john.doe@example.com " );
\Phake :: verifyNoFurtherInteraction ( Arsse :: $db );
}
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testSetUserProperties () : void {
$in = [ 'admin' => true ];
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( true );
$this -> assertSame ( $in , ( new Driver ) -> userPropertiesSet ( " john.doe@example.com " , $in ));
\Phake :: verify ( Arsse :: $db ) -> userExists ( " john.doe@example.com " );
\Phake :: verifyNoFurtherInteraction ( Arsse :: $db );
}
2020-11-16 15:26:14 +00:00
2020-11-14 02:41:27 +00:00
public function testSetPropertiesForAMissingUser () : void {
\Phake :: when ( Arsse :: $db ) -> userExists -> thenReturn ( false );
2020-11-15 21:24:26 +00:00
$this -> assertException ( " doesNotExist " , " User " , " ExceptionConflict " );
2020-11-14 02:41:27 +00:00
try {
( new Driver ) -> userPropertiesSet ( " john.doe@example.com " , [ 'admin' => true ]);
} finally {
\Phake :: verify ( Arsse :: $db ) -> userExists ( " john.doe@example.com " );
\Phake :: verifyNoFurtherInteraction ( Arsse :: $db );
}
}
2018-11-04 17:06:30 +00:00
}