Skip to content

Commit

Permalink
fixup! WIP - add check-group command in user_ldap
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Jul 20, 2023
1 parent 4cf25c2 commit 8521643
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 57 deletions.
1 change: 1 addition & 0 deletions apps/user_ldap/lib/Group_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace OCA\User_LDAP;

use OC\ServerNotAvailableException;
use OCP\Group\Backend\IDeleteGroupBackend;
use OCP\Group\Backend\IGetDisplayNameBackend;
use OCP\Group\Backend\INamedBackend;
Expand Down
2 changes: 0 additions & 2 deletions apps/user_ldap/lib/Jobs/UpdateGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -54,7 +53,6 @@ public function __construct(
private IGroupManager $groupManager,
private IUserManager $userManager,
private LoggerInterface $logger,
private IDBConnection $dbc,
private GroupMembershipMapper $groupMembershipMapper,
IConfig $config,
ITimeFactory $timeFactory,
Expand Down
77 changes: 22 additions & 55 deletions apps/user_ldap/tests/Jobs/UpdateGroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,24 @@
*/
namespace OCA\user_ldap\tests\Jobs;

use OCA\User_LDAP\Db\GroupMembership;
use OCA\User_LDAP\Db\GroupMembershipMapper;
use OCA\User_LDAP\Group_Proxy;
use OCA\User_LDAP\Jobs\UpdateGroups;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\UserAddedEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
use function serialize;

class UpdateGroupsTest extends TestCase {

/** @var Group_Proxy|MockObject */
protected $groupBackend;
/** @var IEventDispatcher|MockObject */
Expand All @@ -58,8 +54,8 @@ class UpdateGroupsTest extends TestCase {
protected $userManager;
/** @var LoggerInterface|MockObject */
protected $logger;
/** @var IDBConnection|MockObject */
protected $dbc;
/** @var GroupMembershipMapper|MockObject */
protected $groupMembershipMapper;
/** @var IConfig|MockObject */
protected $config;
/** @var ITimeFactory|MockObject */
Expand All @@ -73,7 +69,7 @@ public function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->dbc = $this->createMock(IDBConnection::class);
$this->groupMembershipMapper = $this->createMock(GroupMembershipMapper::class);
$this->config = $this->createMock(IConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);

Expand All @@ -83,20 +79,20 @@ public function setUp(): void {
$this->groupManager,
$this->userManager,
$this->logger,
$this->dbc,
$this->groupMembershipMapper,
$this->config,
$this->timeFactory
);
}

public function testHandleKnownGroups(): void {
$knownGroups = [
'emptyGroup' => serialize([]),
'stableGroup' => serialize(['userA', 'userC', 'userE']),
'groupWithAdditions' => serialize(['userA', 'userC', 'userE']),
'groupWithRemovals' => serialize(['userA', 'userC', 'userDeleted', 'userE']),
'groupWithAdditionsAndRemovals' => serialize(['userA', 'userC', 'userE']),
'vanishedGroup' => serialize(['userB', 'userDeleted'])
'emptyGroup' => [],
'stableGroup' => ['userA', 'userC', 'userE'],
'groupWithAdditions' => ['userA', 'userC', 'userE'],
'groupWithRemovals' => ['userA', 'userC', 'userDeleted', 'userE'],
'groupWithAdditionsAndRemovals' => ['userA', 'userC', 'userE'],
'vanishedGroup' => ['userB', 'userDeleted'],
];
$knownGroupsDB = [];
foreach ($knownGroups as $gid => $members) {
Expand All @@ -115,45 +111,16 @@ public function testHandleKnownGroups(): void {
];
$groups = array_intersect(array_keys($knownGroups), array_keys($actualGroups));

/** @var IQueryBuilder|MockObject $updateQb */
$updateQb = $this->createMock(IQueryBuilder::class);
$updateQb->expects($this->once())
->method('update')
->willReturn($updateQb);
$updateQb->expects($this->once())
->method('set')
->willReturn($updateQb);
$updateQb->expects($this->once())
->method('where')
->willReturn($updateQb);
// three groups need to be updated
$updateQb->expects($this->exactly(3))
->method('setParameters');
$updateQb->expects($this->exactly(3))
->method('executeStatement');
$updateQb->expects($this->any())
->method('expr')
->willReturn($this->createMock(IExpressionBuilder::class));

$stmt = $this->createMock(IResult::class);
$stmt->expects($this->once())
->method('fetchAll')
->willReturn($knownGroupsDB);

$selectQb = $this->createMock(IQueryBuilder::class);
$selectQb->expects($this->once())
->method('select')
->willReturn($selectQb);
$selectQb->expects($this->once())
->method('from')
->willReturn($selectQb);
$selectQb->expects($this->once())
->method('executeQuery')
->willReturn($stmt);

$this->dbc->expects($this->any())
->method('getQueryBuilder')
->willReturnOnConsecutiveCalls($updateQb, $selectQb);
$this->groupMembershipMapper->expects($this->never())
->method('getKnownGroups');
$this->groupMembershipMapper->expects($this->exactly(5))
->method('findGroupMemberships')
->willReturnCallback(
fn ($group) => array_map(
fn ($userid) => GroupMembership::fromParams(['groupid' => $group,'userid' => $userid]),
$knownGroups[$group]
)
);

$this->groupBackend->expects($this->any())
->method('usersInGroup')
Expand Down

0 comments on commit 8521643

Please sign in to comment.