diff options
Diffstat (limited to 'tests/integration')
-rw-r--r-- | tests/integration/ContactsStoreUserProviderTest.php | 281 | ||||
-rw-r--r-- | tests/integration/RosterPushTest.php | 174 | ||||
-rw-r--r-- | tests/integration/db/PresenceMapperTest.php | 53 |
3 files changed, 506 insertions, 2 deletions
diff --git a/tests/integration/ContactsStoreUserProviderTest.php b/tests/integration/ContactsStoreUserProviderTest.php new file mode 100644 index 0000000..30c2dfe --- /dev/null +++ b/tests/integration/ContactsStoreUserProviderTest.php @@ -0,0 +1,281 @@ +<?php + +namespace OCA\OJSXC; + +use OC\Contacts\ContactsMenu\ContactsStore; +use OCA\DAV\CardDAV\AddressBookImpl; +use OCA\OJSXC\AppInfo\Application; +use OCP\Contacts\ContactsMenu\IContactsStore; +use OCP\Contacts\IManager; +use OCP\IConfig; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\IUserSession; +use OCA\DAV\AppInfo\Application as DavApp; +use OCA\DAV\CardDAV\CardDavBackend; +use PHPUnit\Framework\Constraint\IsEqual; +use Sabre\VObject\Component\VCard; +use OCA\OJSXC\Utility\TestCase; + +class ContactsStoreUserProviderTest extends TestCase +{ + + /** + * @var ContactsStoreUserProvider + */ + private $contactsStoreUserProvider; + + /** + * @var IContactsStore + */ + private $contactsStore; + + /** + * @var IUserSession + */ + private $userSession; + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var IGroupManager + */ + private $groupManager; + + /** + * @var IConfig + */ + private $config; + + /** + * @var CardDavBackend + */ + private $cardDavBackend; + + /** + * @var IManager + */ + private $contactsManager; + + public function setUp() + { + if (!Application::contactsStoreApiSupported()) { + $this->markTestSkipped(); + return; + } + $this->config = \OC::$server->getConfig(); + $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); + foreach (\OC::$server->getUserManager()->search('') as $user) { + $user->delete(); + } + + $users[] = \OC::$server->getUserManager()->createUser('admin', 'admin'); + $users[] = \OC::$server->getUserManager()->createUser('derp', 'derp'); + $users[] = \OC::$server->getUserManager()->createUser('derpina', 'derpina'); + $users[] = \OC::$server->getUserManager()->createUser('herp', 'herp'); + $users[] = \OC::$server->getUserManager()->createUser('foo', 'foo'); + + $currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest'); + $this->userSession = \OC::$server->getUserSession(); + $this->userSession->setUser($currentUser); + $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->contactsStore = \OC::$server->query(ContactsStore::class); + $this->contactsManager = \OC::$server->getContactsManager(); + + /** @var \OCA\DAV\CardDAV\SyncService $syncService */ + $syncService = \OC::$server->query('CardDAVSyncService'); + $syncService->getLocalSystemAddressBook(); + $syncService->updateUser($currentUser); + + + $davApp = new DavApp(); + + $this->cardDavBackend = $davApp->getContainer()->query(CardDavBackend::class); + + // create some contacts + $vCard = new VCard(); + $vCard->VERSION = '3.0'; + $vCard->UID = 'Test1'; + $vCard->FN = 'Test1'; + + $id = $this->setupAddressBook('autotest'); + $this->cardDavBackend->createCard($id, 'Alice.Test1.vcf', $vCard->serialize()); + + foreach ($users as $user) { + $syncService->updateUser($user); + } + + $davApp->setupSystemContactsProvider($this->contactsManager); + \OC_User::setIncognitoMode(false); + \OC::$server->getDatabaseConnection()->executeQuery("DELETE FROM *PREFIX*ojsxc_stanzas"); + + + $this->contactsStoreUserProvider = new ContactsStoreUserProvider( + $this->contactsStore, + $this->userSession, + $this->userManager, + $this->groupManager, + $this->config + ); + } + + private function setupAddressBook($userId) + { + $addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$userId"); + foreach ($addressBooks as $addressBookInfo) { + $this->cardDavBackend->deleteAddressBook($addressBookInfo['id']); + } + $addressBookId = $this->cardDavBackend->createAddressBook('principals/users/' . $userId, 'principals/users/' . $userId, []); + + $addressBooks = $this->cardDavBackend->getAddressBooksForUser("principals/users/$userId"); + + foreach ($addressBooks as $addressBookInfo) { + $addressBook = new \OCA\DAV\CardDAV\AddressBook($this->cardDavBackend, $addressBookInfo, \OC::$server->getL10N('dav')); + $this->contactsManager->registerAddressBook( + new AddressBookImpl( + $addressBook, + $addressBookInfo, + $this->cardDavBackend, + \OC::$server->getURLGenerator() + ) + ); + } + + return $addressBookId; + } + + protected function tearDown() + { + $config = \OC::$server->getConfig(); + $config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); + $config->setAppValue('core', 'shareapi_exclude_groups', 'no'); + $config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([])); + foreach (\OC::$server->getUserManager()->search('') as $user) { + $user->delete(); + } + } + + public function testNormalSituation() + { + + // no special settings set + $derp = new User('derp', 'derp', $this->contactsStore->findOne($this->userManager->get('autotest'), 6, 'derp')); + $derpina = new User('derpina', 'derpina', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'derpina')[0]); + $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'herp')[0]); + $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); + $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); + $autotest = new User('autotest', 'autotest', $this->contactsStore->getContacts($this->userManager->get('derpina'), 'autotest')[0]); + + $expected = [ + $foo, + $admin, + $derp, + $derpina, + $herp + ]; + + $this->assertCanonicalizeEquals($expected, $this->contactsStoreUserProvider->getAllUsers()); + $this->assertCanonicalizeEquals([$autotest, $derp, $herp, $foo, $admin], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derpina')); + $this->assertCanonicalizeEquals([$autotest, $derp, $herp, $foo, $admin], $this->contactsStoreUserProvider->getAllUsersForUser($derpina)); + + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'derpina')); + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($derp, $derpina)); + $this->assertTrue($this->contactsStoreUserProvider->hasUserByUID('derpina')); + $this->assertTrue($this->contactsStoreUserProvider->hasUser($derpina)); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina')); + } + + public function testGroupsOnly() + { + $this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null); + $group1 = $this->groupManager->createGroup('group1'); + $group2 = $this->groupManager->createGroup('group2'); + $group1->addUser($this->userManager->get('derp')); + $group1->addUser($this->userManager->get('foo')); + $group2->addUser($this->userManager->get('derpina')); + $group2->addUser($this->userManager->get('herp')); + + $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes'); + + // no special settings set + $derp = new User('derp', 'derp', $this->contactsStore->getContacts($this->userManager->get('foo'), 'derp')[0]); + $derpina = new User('derpina', 'derpina', $this->contactsStore->getContacts($this->userManager->get('herp'), 'derpina')[0]); + $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('derpina'), 'herp')[0]); + $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('derp'), 'foo')[0]); + + $this->assertCanonicalizeEquals([], $this->contactsStoreUserProvider->getAllUsers()); // running as autotest -> not in any group + $this->assertCanonicalizeEquals([$herp], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derpina')); + $this->assertCanonicalizeEquals([$herp], $this->contactsStoreUserProvider->getAllUsersForUser($derpina)); + $this->assertCanonicalizeEquals([$foo], $this->contactsStoreUserProvider->getAllUsersForUserByUID('derp')); + $this->assertCanonicalizeEquals([$foo], $this->contactsStoreUserProvider->getAllUsersForUser($derp)); + + $this->assertFalse($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'derpina')); + $this->assertFalse($this->contactsStoreUserProvider->hasUserForUser($derp, $derpina)); + $this->assertFalse($this->contactsStoreUserProvider->hasUserForUserByUID('derpina', 'derp')); + $this->assertFalse($this->contactsStoreUserProvider->hasUserForUser($derpina, $derp)); + + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('derp', 'foo')); + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($derp, $foo)); + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUserByUID('foo', 'derp')); + $this->assertTrue($this->contactsStoreUserProvider->hasUserForUser($foo, $derp)); + + $this->assertFalse($this->contactsStoreUserProvider->hasUserByUID('derpina')); + $this->assertFalse($this->contactsStoreUserProvider->hasUser($derpina)); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('derpina')); + } + + public function testExcluded() + { + $this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null); + $groupExcluded = $this->groupManager->createGroup('excluded'); + $this->config->setAppValue('core', 'shareapi_exclude_groups', 'yes'); + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode(['excluded'])); + $groupExcluded->addUser($this->userManager->get('derp')); + $groupExcluded->addUser($this->userManager->get('derpina')); + + $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'herp')[0]); + $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); + $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); + $autotest = new User('autotest', 'autotest', $this->contactsStore->getContacts($this->userManager->get('admin'), 'autotest')[0]); + + $this->assertCanonicalizeEquals([$admin, $herp, $foo], $this->contactsStoreUserProvider->getAllUsers()); + $this->assertTrue($this->contactsStoreUserProvider->isUserExcluded('derp')); + $this->assertTrue($this->contactsStoreUserProvider->isUserExcluded('derpina')); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('autotest')); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('admin')); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('foo')); + $this->assertFalse($this->contactsStoreUserProvider->isUserExcluded('herp')); + + $this->config->setAppValue('core', 'shareapi_exclude_groups', 'no'); + $this->config->setAppValue('core', 'shareapi_exclude_groups_list', json_encode([])); + } + + public function testDisabled() + { + $this->setValueOfPrivateProperty($this->contactsStoreUserProvider, 'cache', null); + $derpina = new User('derpina', 'derpina', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'derpina')[0]); + $herp = new User('herp', 'herp', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'herp')[0]); + $foo = new User('foo', 'foo', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'foo')[0]); + $admin = new User('admin', 'admin', $this->contactsStore->getContacts($this->userManager->get('autotest'), 'admin')[0]); + + $this->userManager->get('derp')->setEnabled(false); + + $expected = [ + $admin, + $derpina, + $herp, + $foo + ]; + + $this->assertCanonicalizeEquals($expected, $this->contactsStoreUserProvider->getAllUsers()); + } + + private function assertCanonicalizeEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $ignoreCase = false) + { + $this->assertEquals($expected, $actual, $message, $delta, $maxDepth, true, $ignoreCase); + } +} diff --git a/tests/integration/RosterPushTest.php b/tests/integration/RosterPushTest.php new file mode 100644 index 0000000..7b46b11 --- /dev/null +++ b/tests/integration/RosterPushTest.php @@ -0,0 +1,174 @@ +<?php + +namespace OCA\OJSXC; + +use OCA\OJSXC\AppInfo\Application; +use OCA\OJSXC\Db\IQRosterPushMapper; +use OCP\Contacts\ContactsMenu\IContactsStore; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IGroupManager; +use OCP\IUserManager; +use OCP\IUserSession; +use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\MockObject\MockObject; + +class RosterPushTest extends TestCase +{ + + /** + * @var RosterPush + */ + private $rosterPush; + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var IGroupManager + */ + private $groupManager; + + /** + * @var IUserSession + */ + private $userSession; + + /** + * @var MockObject | IQRosterPushMapper + */ + private $iqRosterPushMapper; + + /** + * @var IDBConnection + */ + private $dbConnection; + + /** + * @var IUserProvider + */ + private $userProvider; + + /** + * @var IConfig + */ + private $config; + + /** + * @var MockObject | IContactsStore + */ + private $contactsStore; + + public function setUp() + { + if (!Application::contactsStoreApiSupported()) { + $this->markTestSkipped(); + return; + } + + $this->userManager = \OC::$server->getUserManager(); + $this->groupManager = \OC::$server->getGroupManager(); + $this->userSession = \OC::$server->getUserSession(); + $this->iqRosterPushMapper = $this->getMockBuilder(IQRosterPushMapper::class)->disableOriginalConstructor()->getMock(); + $this->dbConnection = \OC::$server->getDatabaseConnection(); + $this->config = \OC::$server->getConfig(); + $this->contactsStore = $this->getMockBuilder(IContactsStore::class)->disableOriginalConstructor()->getMock(); + + $this->userProvider = new ContactsStoreUserProvider( + $this->contactsStore, + $this->userSession, + $this->userManager, + $this->groupManager, + $this->config + ); + + $this->rosterPush = new RosterPush( + $this->userManager, + $this->userSession, + 'localhost', + $this->iqRosterPushMapper, + $this->dbConnection, + $this->userProvider + ); + + foreach (\OC::$server->getUserManager()->search('') as $user) { + $user->delete(); + } + foreach (\OC::$server->getGroupManager()->search('') as $group) { + $group->delete(); + } + } + + public function testRemoveRosterItemForUsersInGroup() + { + $group1 = $this->groupManager->createGroup('group1'); + $group2 = $this->groupManager->createGroup('group2'); + $group3 = $this->groupManager->createGroup('group3'); + $user1 = $this->userManager->createUser('user1', 'user1'); + $user2 = $this->userManager->createUser('user2', 'user2'); + $user3 = $this->userManager->createUser('user3', 'user3'); + $user4 = $this->userManager->createUser('user4', 'user4'); + + $group1->addUser($user1); + $group1->addUser($user2); + $group1->addUser($user3); + $group2->addUser($user1); + $group2->addUser($user2); + $group3->addUser($user4); + + // remove $user1 from $group1 + // when no special settings are set this should result in no rosterMessages + $this->iqRosterPushMapper->expects($this->never())->method('insert'); + $this->contactsStore->expects($this->at(0)) + ->method('findOne') + ->with($user2, 0, 'user1') + ->willReturn([$user2]); + $this->contactsStore->expects($this->at(1)) + ->method('findOne') + ->with($user3, 0, 'user1') + ->willReturn([$user3]); + $this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1'); + } + public function testRemoveRosterItemForUsersInGroupOwnGroups() + { + $group1 = $this->groupManager->createGroup('group1'); + $group2 = $this->groupManager->createGroup('group2'); + $group3 = $this->groupManager->createGroup('group3'); + $user1 = $this->userManager->createUser('user1', 'user1'); + $user2 = $this->userManager->createUser('user2', 'user2'); + $user3 = $this->userManager->createUser('user3', 'user3'); + $user4 = $this->userManager->createUser('user4', 'user4'); + + $group1->addUser($user1); + $group1->addUser($user2); + $group1->addUser($user3); + $group2->addUser($user1); + $group2->addUser($user2); + $group3->addUser($user4); + + $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes'); + + // remove $user1 from $group1 + // users can only chat with users in their groups + // $user2 should still be reachable by $group2 + // $user3 should be become unreachable + // for $user4 nothing changes + $this->iqRosterPushMapper->expects($this->once())->method('insert'); + + $this->contactsStore->expects($this->at(0)) + ->method('findOne') + ->with($user2, 0, 'user1') + ->willReturn([$user2]); + + $this->contactsStore->expects($this->at(1)) + ->method('findOne') + ->with($user3, 0, 'user1') + ->willReturn(null); + + $this->rosterPush->removeRosterItemForUsersInGroup($group1, 'user1'); + + $this->config->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); + } +} diff --git a/tests/integration/db/PresenceMapperTest.php b/tests/integration/db/PresenceMapperTest.php index 5ecf4fb..79b8748 100644 --- a/tests/integration/db/PresenceMapperTest.php +++ b/tests/integration/db/PresenceMapperTest.php @@ -70,7 +70,7 @@ class PresenceMapperTest extends MapperTestUtility $currentUser = \OC::$server->getUserManager()->createUser('autotest', 'autotest'); \OC::$server->getUserSession()->setUser($currentUser); - if (Application::contactsStoreApiSupporetd()) { + if (Application::contactsStoreApiSupported()) { /** @var \OCA\DAV\CardDAV\SyncService $syncService */ $syncService = \OC::$server->query('CardDAVSyncService'); $syncService->getLocalSystemAddressBook(); @@ -268,7 +268,7 @@ class PresenceMapperTest extends MapperTestUtility ] ]; } - + /** * @dataProvider getPresenceProvider * @param $inputs @@ -343,6 +343,55 @@ class PresenceMapperTest extends MapperTestUtility $this->assertEquals($expected, $result); } + public function testGetConnectedUsersIfUserHasNot() + { + $this->setValueOfPrivateProperty($this->mapper, 'connectedUsers', []); + $this->setupContactsStoreAPI(); + if (!Application::contactsStoreApiSupported()) { + $this->markTestSkipped(); + } + + $group = \OC::$server->getGroupManager()->createGroup('group1'); + $group->addUser(\OC::$server->getUserManager()->get('derp')); + + $group2 = \OC::$server->getGroupManager()->createGroup('group2'); + $group2->addUser(\OC::$server->getUserManager()->get('autotest')); + $group2->addUser(\OC::$server->getUserManager()->get('foo')); + $group2->addUser(\OC::$server->getUserManager()->get('admin')); + + \OC::$server->getConfig()->setAppValue('core', 'shareapi_only_share_with_group_members', 'yes'); + + $input1 = new PresenceEntity(); + $input1->setPresence('online'); + $input1->setUserid('foo'); + $input1->setLastActive(23434475); + + $input2 = new PresenceEntity(); + $input2->setPresence('online'); + $input2->setUserid('derp'); + $input2->setLastActive(23434475); + + $this->mapper->setPresence($input1); + $this->mapper->setPresence($input2); + + $expected = ['foo']; + + $result = $this->mapper->getConnectedUsers(); + + $this->assertCount(count($expected), $result); + sort($expected); + sort($result); + $this->assertEquals($expected, $result); + + \OC::$server->getConfig()->setAppValue('core', 'shareapi_only_share_with_group_members', 'no'); + + $group->removeUser(\OC::$server->getUserManager()->get('derp')); + $group->delete(); + $group2->removeUser(\OC::$server->getUserManager()->get('autotest')); + $group2->removeUser(\OC::$server->getUserManager()->get('admin')); + $group2->delete(); + } + public function updatePresenceProvider() { $input1 = new PresenceEntity(); |