Skip to content

Commit

Permalink
Merge pull request #117 from silinternational/feature/search-by-group…
Browse files Browse the repository at this point in the history
…-aliases

Search by group aliases, and other alias based functionality
  • Loading branch information
mtompset authored Dec 10, 2024
2 parents e5971be + 5bdd908 commit 24e6a2f
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 57 deletions.
70 changes: 62 additions & 8 deletions SilMock/Google/Service/Directory/Resource/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Google\Service\Directory\Group as GoogleDirectory_Group;
use Google\Service\Directory\Groups as GoogleDirectory_Groups;
use Google\Service\Directory\Alias as GoogleDirectory_GroupAlias;
use SilMock\Google\Service\DbClass;
use SilMock\Google\Service\Directory\ObjectUtils;

Expand All @@ -20,23 +21,47 @@ public function delete(string $groupKey)
$groupRecords = $this->getRecords();
foreach ($groupRecords as $groupRecord) {
$groupRecordData = json_decode($groupRecord['data'], true);
if ($groupRecordData['email'] === $groupKey) {
$keysToCheck = $groupRecordData['aliases'];
$keysToCheck[] = $groupRecordData['email'];
if (in_array($groupKey, $keysToCheck)) {
$this->deleteRecordById($groupRecord['id']);
$groupAliasesObject = new GroupsAliases($this->dbFile);
$groupAliasesObject->deletedByGroup($groupRecordData['email']);
}
}
}

public function get(string $groupKey): ?GoogleDirectory_Group
{
$matchingGroupKey = null;
$groupRecords = $this->getRecords();
foreach ($groupRecords as $groupRecord) {
$groupRecordData = json_decode($groupRecord['data'], true);
$keysToCheck = $groupRecordData['aliases'];
$keysToCheck[] = $groupRecordData['email'];
if (in_array($groupKey, $keysToCheck)) {
$matchingGroupKey = $groupRecordData['email'];
}
}
if ($matchingGroupKey === null) {
return null;
}
$mockGroupsObject = new Groups($this->dbFile);
$groupsObject = $mockGroupsObject->listGroups();
$groups = $groupsObject->getGroups();
$matchedGroup = null;
foreach ($groups as $group) {
if (mb_strtolower($group->getEmail()) === mb_strtolower($groupKey)) {
return $group;
if (mb_strtolower($group->getEmail()) === mb_strtolower($matchingGroupKey)) {
$matchedGroup = $group;
break;
}
}
return null;
if ($matchedGroup !== null) {
$mockGroupsAliasesObject = new GroupsAliases($this->dbFile);
$aliases = $mockGroupsAliasesObject->listGroupsAliases($matchedGroup->getEmail());
$matchedGroup->setAliases($aliases->getAliases());
}
return $matchedGroup;
}

/**
Expand All @@ -49,12 +74,14 @@ public function insert(GoogleDirectory_Group $postBody, $optParams = [])
$postBody['id'] = $postBody['id'] ?? $id;
$dataAsJson = json_encode(get_object_vars($postBody));
$this->addRecord($dataAsJson);
} else {
throw new Exception(
"Cannot group.insert an existing group: " . $postBody->getEmail()
);
}

$newGroup = new GoogleDirectory_Group();
ObjectUtils::initialize($newGroup, $postBody);

return $newGroup;
// This should leave aliases as is.
return $this->get($postBody->getEmail());
}

/**
Expand Down Expand Up @@ -104,4 +131,31 @@ protected function isNewGroup(string $groupKey): bool
$uppercaseGroupEmailAddress = mb_strtoupper($groupKey);
return ! in_array($uppercaseGroupEmailAddress, $groupEmailAddresses);
}

public function update(string $groupKey, GoogleDirectory_Group $postBody, $optParams = []): GoogleDirectory_Group
{
if ($this->isNewGroup($postBody->getEmail())) {
throw new Exception("Group '{$groupKey}' does not exist.");
}
$group = $this->get($groupKey);

// remember aliases, because they don't change.
$aliases = $group->getAliases();

// update by deleting and reinserting, deletion causes a loss of aliases
$this->delete($groupKey);
ObjectUtils::initialize($group, $postBody);
$this->insert($group);

// re-add the aliases
$mockGroupAliasesObject = new GroupsAliases($this->dbFile);
foreach ($aliases as $alias) {
$aliasObject = new GoogleDirectory_GroupAlias();
$aliasObject->setAlias($alias);
$aliasObject->setPrimaryEmail($group->getEmail());
$mockGroupAliasesObject->insert($groupKey, $aliasObject);
}

return $this->get($groupKey);
}
}
10 changes: 10 additions & 0 deletions SilMock/Google/Service/Directory/Resource/GroupsAliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public function __construct(?string $dbFile = null)
parent::__construct($dbFile, 'directory', 'groupsaliases');
}

public function deletedByGroup(string $groupKey): void
{
foreach ($this->getRecords() as $aliasRecord) {
$aliasRecordData = json_decode($aliasRecord['data'], true);
if ($aliasRecordData['primaryEmail'] === $groupKey) {
$this->deleteRecordById($aliasRecord['id']);
}
}
}

public function delete(string $groupKey, string $alias, array $optParams = [])
{
$groupAliasRecords = $this->getRecords();
Expand Down
168 changes: 159 additions & 9 deletions SilMock/tests/Google/Service/Directory/Resource/GroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Google\Service\Directory\Group as GoogleDirectory_Group;
use Google\Service\Directory\Alias as GoogleDirectory_GroupAlias;
use PHPUnit\Framework\TestCase;
use SilMock\Google\Service\Directory as GoogleMock_Directory;

Expand All @@ -13,13 +14,28 @@ class GroupsTest extends TestCase
// They are very dependent on order run.
// groups.insert, groups.listGroups, members.insert, members.listMembers
public string $dataFile = DATAFILE5;
public const GROUP_EMAIL_ADDRESS = '[email protected]';
public const GROUP_EMAIL_ADDRESS = '[email protected]';
public const GROUP_ALIAS_ADDRESS = '[email protected]';

protected function deleteGroupAndAliasesIfTheyExists(string $groupName)
{
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$group = $mockGoogleServiceDirectory->groups->get($groupName);
if ($group !== null) {
$mockGoogleServiceDirectory->groups->delete($groupName);
}
$group = $mockGoogleServiceDirectory->groups->get($groupName);
self::assertNull($group, "Failed to clean up previous tests.");
}

public function testInsert()
{
$this->deleteGroupAndAliasesIfTheyExists(self::GROUP_EMAIL_ADDRESS);
$group = new GoogleDirectory_Group();
$group->setEmail(self::GROUP_EMAIL_ADDRESS);
$group->setAliases([]);
// See https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups#Group
// setting the aliases will not add, remove, or change aliases at all.
$group->setAliases([self::GROUP_ALIAS_ADDRESS]);
$group->setName('Sample Group');
$group->setDescription('A Sample Group used for testing');

Expand All @@ -35,14 +51,50 @@ public function testInsert()
);
}
self::assertTrue($addedGroup instanceof GoogleDirectory_Group);
self::assertEmpty($addedGroup->getAliases(), "Expecting no group aliases inserted by group.insert");
}

public function testUpdate()
{
$this->deleteGroupAndAliasesIfTheyExists(self::GROUP_EMAIL_ADDRESS . 'update');
$group = new GoogleDirectory_Group();
$group->setEmail(self::GROUP_EMAIL_ADDRESS . 'update');
// See https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups#Group
// setting the aliases will not add, remove, or change aliases at all.
$group->setAliases([self::GROUP_ALIAS_ADDRESS . 'update']);
$group->setName('Sample Group Update');
$group->setDescription('A Sample Group used for testing update');

$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
try {
$addedGroup = $mockGoogleServiceDirectory->groups->insert($group);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the groups.insert method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertTrue($addedGroup instanceof GoogleDirectory_Group);
self::assertEmpty($addedGroup->getAliases(), "Expecting no group aliases inserted by group.insert");

// See https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups#Group
// setting the aliases will not add, remove, or change aliases at all.
$group->setAliases([self::GROUP_ALIAS_ADDRESS . 'update-change']);
$updatedGroup = $mockGoogleServiceDirectory->groups->update($group->getEmail(), $group);
self::assertTrue($updatedGroup instanceof GoogleDirectory_Group);
self::assertEmpty($updatedGroup->getAliases(), "Expecting no group aliases changed by group.update");
}

public function testDelete()
protected function deleteTestSetup()
{
// Set update a deletable email address
$group = new GoogleDirectory_Group();
$group->setEmail(self::GROUP_EMAIL_ADDRESS . 'delete');
$group->setAliases([]);
// See https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups#Group
// setting the aliases will not add, remove, or change aliases at all.
$group->setAliases([self::GROUP_ALIAS_ADDRESS . 'delete']);
$group->setName('Sample Deletable Group');
$group->setDescription('A Sample Deletable Group used for testing');

Expand All @@ -58,6 +110,32 @@ public function testDelete()
);
}
self::assertTrue($addedGroup instanceof GoogleDirectory_Group);
self::assertEmpty($addedGroup->getAliases(), "Expecting no group aliases");

// Set update a deletable email address
$groupAlias = new GoogleDirectory_GroupAlias();
$groupAlias->setPrimaryEmail(self::GROUP_EMAIL_ADDRESS . 'delete');
$groupAlias->setAlias(self::GROUP_ALIAS_ADDRESS . 'delete');

try {
$addedGroupAliases = $mockGoogleServiceDirectory->groups_aliases->insert(
self::GROUP_EMAIL_ADDRESS . 'delete',
$groupAlias
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the groups_aliases.insert method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertTrue($addedGroupAliases instanceof GoogleDirectory_GroupAlias);
}

public function testDeleteByName()
{
$this->deleteTestSetup();

// Now try to delete it
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
Expand All @@ -66,7 +144,7 @@ public function testDelete()
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the groups.delete method to function, but got: %s',
'Was expecting the groups.delete method to function for name, but got: %s',
$exception->getMessage()
)
);
Expand All @@ -76,31 +154,103 @@ public function testDelete()
$group = $mockGoogleServiceDirectory->groups->get(self::GROUP_EMAIL_ADDRESS . 'delete');
self::assertNull(
$group,
'Was expecting the group to be deleted, but found something'
'Was expecting the group to be deleted by name, but found something'
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting to confirm the group was deleted, but got: %s',
'Was expecting to confirm the group was deleted by name, but got: %s',
$exception->getMessage()
)
);
}
try {
$groupAliases = $mockGoogleServiceDirectory->groups_aliases->listGroupsAliases(
self::GROUP_EMAIL_ADDRESS . 'delete'
);
self::assertEmpty(
$groupAliases->getAliases(),
'Was expecting the group aliases to be deleted by name, but found something'
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting to confirm the group aliases were also deleted by name, but got: %s',
$exception->getMessage()
)
);
}
}

public function testDeleteByAlias()
{
$this->deleteTestSetup();

// Now try to delete it
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
try {
$mockGoogleServiceDirectory->groups->delete(self::GROUP_ALIAS_ADDRESS . 'delete');
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the groups.delete method to function for aliases, but got: %s',
$exception->getMessage()
)
);
}

try {
$group = $mockGoogleServiceDirectory->groups->get(self::GROUP_EMAIL_ADDRESS . 'delete');
self::assertNull(
$group,
'Was expecting the group to be deleted by alias, but found something'
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting to confirm the group was deleted by alias, but got: %s',
$exception->getMessage()
)
);
}
try {
$groupAliases = $mockGoogleServiceDirectory->groups_aliases->listGroupsAliases(self::GROUP_EMAIL_ADDRESS . 'delete');
self::assertEmpty(
$groupAliases->getAliases(),
'Was expecting the group aliases to be deleted by name, but found something'
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting to confirm the group aliases were also deleted by name, but got: %s',
$exception->getMessage()
)
);
}
}

public function testGet()
public function testGetByName()
{
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$group = $mockGoogleServiceDirectory->groups->get(self::GROUP_EMAIL_ADDRESS);
self::assertInstanceOf(GoogleDirectory_Group::class, $group);
self::assertEquals(self::GROUP_EMAIL_ADDRESS, $group->getEmail());
}

public function testGetByAlias()
{
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$group = $mockGoogleServiceDirectory->groups->get(self::GROUP_ALIAS_ADDRESS);
self::assertInstanceOf(GoogleDirectory_Group::class, $group);
self::assertEquals(self::GROUP_EMAIL_ADDRESS, $group->getEmail());
}

public function testListGroups()
{
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$groups = [];
try {
$groups = $mockGoogleServiceDirectory->groups->listGroups(self::GROUP_EMAIL_ADDRESS);
$groups = $mockGoogleServiceDirectory->groups->listGroups();
} catch (Exception $exception) {
self::fail(
sprintf(
Expand Down
Loading

0 comments on commit 24e6a2f

Please sign in to comment.