Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobia De Koninck <tobia@ledfan.be>2017-10-03 15:53:33 +0300
committerTobia De Koninck <tobia@ledfan.be>2018-02-10 12:35:47 +0300
commite8136e41f92660d8ac4c6ecfecfb5373371f5fe6 (patch)
tree2f6c96a7dd4a1bfc90f216147b6e841320645017 /tests/unit
parentdf86a4c2bacee7012ed64bffcaadec6102918d5f (diff)
Update tests
Signed-off-by: Tobia De Koninck <tobia@ledfan.be>
Diffstat (limited to 'tests/unit')
-rw-r--r--tests/unit/HooksTest.php31
-rw-r--r--tests/unit/NewContentContainerTest.php4
-rw-r--r--tests/unit/RosterPushTest.php3
-rw-r--r--tests/unit/UserTest.php20
-rw-r--r--tests/unit/controller/HttpBindControllerTest.php31
-rw-r--r--tests/unit/stanzahandlers/IQTest.php107
-rw-r--r--tests/unit/stanzahandlers/MessageTest.php38
-rw-r--r--tests/unit/stanzahandlers/PresenceTest.php1
8 files changed, 153 insertions, 82 deletions
diff --git a/tests/unit/HooksTest.php b/tests/unit/HooksTest.php
index 2724d25..2c67ed8 100644
--- a/tests/unit/HooksTest.php
+++ b/tests/unit/HooksTest.php
@@ -3,9 +3,12 @@
namespace OCA\OJSXC;
+use OCA\OJSXC\AppInfo\Application;
use OCA\OJSXC\Db\Presence;
use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\StanzaMapper;
+use OCP\IGroup;
+use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit_Framework_MockObject_MockObject;
@@ -62,7 +65,7 @@ class HooksTest extends TestCase
$this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->disableOriginalConstructor()->getMock();
- $this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->setMethods(['listen', 'isBackendUsed', 'addBackend', 'clearBackends', 'get', 'groupExists', 'createGroup', 'search', 'getUserGroups', 'getUserGroupIds', 'displayNamesInGroup', 'isAdmin', 'isInGroup'])->getMock();
+ $this->groupManager = $this->getMockBuilder('OCP\IGroupManager')->disableOriginalConstructor()->setMethods(['listen', 'isBackendUsed', 'addBackend', 'clearBackends', 'get', 'groupExists', 'createGroup', 'search', 'getUserGroups', 'getUserGroupIds', 'displayNamesInGroup', 'isAdmin', 'isInGroup', 'getBackends'])->getMock();
$this->hooks = new Hooks(
$this->userManager,
@@ -165,4 +168,30 @@ class HooksTest extends TestCase
$hooks->onChangeUser($user, 'displayName', 'abc');
}
+
+ public function testOnAddUserToGroup()
+ {
+ $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
+ $group = $this->getMockBuilder(IGroup::class)->disableOriginalConstructor()->getMock();
+
+ $this->rosterPush->expects($this->once())->method('createOrUpdateRosterItem')->with($user);
+ $this->rosterPush->expects($this->once())->method('addUserToGroup')->with($user, $group);
+
+ $this->hooks->onAddUserToGroup($group, $user);
+ }
+
+ public function testOnRemoveUserFromGroup()
+ {
+ $user = $this->getMockBuilder(IUser::class)->disableOriginalConstructor()->getMock();
+ $group = $this->getMockBuilder(IGroup::class)->disableOriginalConstructor()->getMock();
+
+ if (Application::contactsStoreApiSupported()) {
+ $user->expects($this->once())->method('getUID')->willReturn('uid1');
+ $this->rosterPush->expects($this->once())->method('removeRosterItemForUsersInGroup')->with($group, 'uid1');
+ }
+
+ $this->rosterPush->expects($this->once())->method('removeUserFromGroup')->with($user, $group);
+
+ $this->hooks->onRemoveUserFromGroup($group, $user);
+ }
}
diff --git a/tests/unit/NewContentContainerTest.php b/tests/unit/NewContentContainerTest.php
index 6ee637f..69df627 100644
--- a/tests/unit/NewContentContainerTest.php
+++ b/tests/unit/NewContentContainerTest.php
@@ -20,7 +20,7 @@ class NewContentContainerTest extends TestCase
$this->newContentContainer = new NewContentContainer();
}
- public function testProvider()
+ public function datatestProvider()
{
$stanza1 = new Stanza();
$stanza1->setFrom('test@own.dev');
@@ -40,7 +40,7 @@ class NewContentContainerTest extends TestCase
}
/**
- * @dataProvider testProvider
+ * @dataProvider datatestProvider
*/
public function test($stanzas, $count)
{
diff --git a/tests/unit/RosterPushTest.php b/tests/unit/RosterPushTest.php
index fafd14d..48ed08d 100644
--- a/tests/unit/RosterPushTest.php
+++ b/tests/unit/RosterPushTest.php
@@ -5,6 +5,7 @@ namespace OCA\OJSXC;
use OCA\OJSXC\Db\IQRosterPush;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCP\IDBConnection;
+use OCP\IGroup;
use OCP\IUserManager;
use OCP\IUserSession;
use PHPUnit\Framework\TestCase;
@@ -264,4 +265,6 @@ class RosterPushTest extends TestCase
$this->rosterPush->createOrUpdateRosterItem($user1);
}
+
+ // removeRosterItemForUsersInGroup, addUserToGroup and removeUserFromGroup covered by integration tests
}
diff --git a/tests/unit/UserTest.php b/tests/unit/UserTest.php
new file mode 100644
index 0000000..ae77bbe
--- /dev/null
+++ b/tests/unit/UserTest.php
@@ -0,0 +1,20 @@
+<?php
+
+
+use OCA\OJSXC\User;
+use PHPUnit\Framework\TestCase;
+
+class UserTest extends TestCase
+{
+ public function test()
+ {
+ $user1 = new User(" test @ 'abc", 'Test123', null);
+
+ $this->assertEquals($user1->getFullName(), 'Test123');
+ $this->assertEquals($user1->getUid(), '_ojsxc_esc_space_test_ojsxc_esc_space__ojsxc_esc_at__ojsxc_esc_space__ojsxc_squote_space_abc');
+ $user1->setUid('test1');
+ $user1->setFullName('test2');
+ $this->assertEquals($user1->getUid(), 'test1');
+ $this->assertEquals($user1->getFullName(), 'test2');
+ }
+}
diff --git a/tests/unit/controller/HttpBindControllerTest.php b/tests/unit/controller/HttpBindControllerTest.php
index 6d08c75..0345132 100644
--- a/tests/unit/controller/HttpBindControllerTest.php
+++ b/tests/unit/controller/HttpBindControllerTest.php
@@ -1,12 +1,11 @@
<?php
+
namespace OCA\OJSXC\Controller;
-use OCA\OJSXC\Db\Message;
use OCA\OJSXC\Db\Presence;
use OCA\OJSXC\Db\Stanza;
-use OCA\OJSXC\Db\StanzaMapper;
+use OCA\OJSXC\Exceptions\TerminateException;
use OCA\OJSXC\Http\XMPPResponse;
-use OCA\OJSXC\StanzaHandlers\IQ;
use OCA\OJSXC\StanzaLogger;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit\Framework\TestCase;
@@ -569,4 +568,30 @@ XML;
$this->assertEquals($expResponse, $response);
$this->assertEquals($expResponse->render(), $response->render());
}
+
+
+ public function testTerminateException()
+ {
+ $ex = new TerminateException();
+
+ $body = '<body rid=\'897878985\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'test@test.local/internal\' to=\'test.local\' type=\'get\'
+ xmlns=\'jabber:client\' id=\'3b300f7c-1a38-4c1b-b66a-582895d12e47:sendIQ\'>
+ <query xmlns=\'http://jabber.org/protocol/disco#info\'
+ node=\'undefined#undefined\'/>
+ </iq></body>';
+ $this->setUpController($body);
+ $this->mockLock();
+
+ $this->iqHandler->expects($this->once())
+ ->method('handle')
+ ->will($this->throwException($ex));
+
+ $expResponse = new XMPPResponse($this->stanzaLogger);
+ $expResponse->terminate();
+
+ $response = $this->controller->index();
+
+ $this->assertEquals($expResponse, $response);
+ $this->assertEquals($expResponse->render(), $response->render());
+ }
}
diff --git a/tests/unit/stanzahandlers/IQTest.php b/tests/unit/stanzahandlers/IQTest.php
index 5b31da0..7aabd81 100644
--- a/tests/unit/stanzahandlers/IQTest.php
+++ b/tests/unit/stanzahandlers/IQTest.php
@@ -3,6 +3,7 @@
namespace OCA\OJSXC\StanzaHandlers;
use OCA\OJSXC\Db\IQRoster;
+use OCA\OJSXC\Exceptions\TerminateException;
use OCA\OJSXC\IUserProvider;
use OCA\OJSXC\User;
use OCP\IConfig;
@@ -66,11 +67,6 @@ class IQTest extends TestCase
->method('getFullName')
->will($this->returnValue('John'));
-// $user1->expects($this->any())
-// ->method('isEnabled')
-// ->will($this->returnValue(true));
-// TOOD
-
$user2 = $this->getMockBuilder(User::class)->disableOriginalConstructor()->getMock();
$user2->expects($this->any())
->method('getUID')
@@ -80,11 +76,6 @@ class IQTest extends TestCase
->method('getFullName')
->will($this->returnValue('Richard'));
- $user2->expects($this->any())
- ->method('isEnabled')
- ->will($this->returnValue(true));
-
-
$expected1 = new IQRoster();
$expected1->setType('result');
$expected1->setTo('john');
@@ -197,76 +188,46 @@ class IQTest extends TestCase
}
}
- public function testIqRosterWithDisabledUsers()
+ public function testTerminateExceptionDiscoItems()
{
- $user1 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
- $user1->expects($this->any())
- ->method('getUID')
- ->will($this->returnValue('jan'));
-
- $user1->expects($this->any())
- ->method('getDisplayName')
- ->will($this->returnValue('Jan'));
+ $stanza = ['name' => '{jabber:client}iq',
+ 'value' => [0 => [
+ 'name' => '{http://jabber.org/protocol/disco#items}query',
+ 'value' => null,
+ 'attributes' => [
+ 'node' => 'undefined#undefined',
+ ],
+ ]],
+ ];
- $user1->expects($this->any())
- ->method('isEnabled')
+ $this->userProvider->expects($this->once())
+ ->method('isUserExcluded')
+ ->with('john')
->will($this->returnValue(true));
- $user2 = $this->getMockBuilder('OCP\IUser')->disableOriginalConstructor()->getMock();
- $user2->expects($this->any())
- ->method('getUID')
- ->will($this->returnValue('richard'));
-
- $user2->expects($this->any())
- ->method('getDisplayName')
- ->will($this->returnValue('Richard'));
-
- $user2->expects($this->any())
- ->method('isEnabled')
- ->will($this->returnValue(false));
-
- $expected = new IQRoster();
- $expected->setType('result');
- $expected->setTo('john');
- $expected->setQid('f9a26583-3c59-4f09-89be-964ce265fbfd:sendIQ');
- $expected->addItem('jan@localhost', 'Jan');
+ $this->expectException(TerminateException::class);
+ $this->iq->handle($stanza);
+ }
- $this->config->expects($this->once())
- ->method('getSystemValue')
- ->with('debug')
- ->will($this->returnValue(false));
+ public function testTerminateExceptionDiscoInfo()
+ {
+ $stanza = ['name' => '{jabber:client}iq',
+ 'value' => [0 => [
+ 'name' => '{http://jabber.org/protocol/disco#info}query',
+ 'value' => null,
+ 'attributes' => [
+ 'node' => 'undefined#undefined',
+ ],
+ ]],
+ ];
- $this->userManager->expects($this->once())
- ->method('search')
- ->with('')
- ->will($this->returnValue([$user1, $user2]));
- $result = $this->iq->handle(
- [
- 'name' => '{jabber:client}iq',
- 'value' =>
- [
- 0 =>
- [
- 'name' => '{jabber:iq:roster}query',
- 'value' => null,
- 'attributes' => []
- ]
- ],
- 'attributes' =>
- [
- 'type' => 'get',
- 'id' => 'f9a26583-3c59-4f09-89be-964ce265fbfd:sendIQ',
- ],
- ]
- );
+ $this->userProvider->expects($this->once())
+ ->method('isUserExcluded')
+ ->with('john')
+ ->will($this->returnValue(true));
- $this->assertEquals($expected->getFrom(), $result->getFrom());
- $this->assertEquals($expected->getId(), $result->getId());
- $this->assertEquals($expected->getItems(), $result->getItems());
- $this->assertEquals($expected->getQid(), $result->getQid());
- $this->assertEquals($expected->getTo(), $result->getTo());
- $this->assertEquals($expected->getType(), $result->getType());
- $this->assertEquals($expected->getStanza(), $result->getStanza());
+ $this->expectException(TerminateException::class);
+ $this->iq->handle($stanza);
}
}
diff --git a/tests/unit/stanzahandlers/MessageTest.php b/tests/unit/stanzahandlers/MessageTest.php
index 681d189..455636c 100644
--- a/tests/unit/stanzahandlers/MessageTest.php
+++ b/tests/unit/stanzahandlers/MessageTest.php
@@ -6,6 +6,7 @@ use OCA\OJSXC\Db\Message as MessageEntity;
use PHPUnit\Framework\TestCase;
use OCA\OJSXC\Db\MessageMapper;
use OCA\OJSXC\IUserProvider;
+use OCP\ILogger;
use PHPUnit_Framework_TestCase;
use PHPUnit_Framework_MockObject_MockObject;
@@ -37,13 +38,19 @@ class MessageTest extends TestCase
*/
private $userProvider;
+ /**
+ * @var PHPUnit_Framework_MockObject_MockObject | ILogger
+ */
+ private $logger;
+
public function setUp()
{
$this->host = 'localhost';
$this->userId = 'john';
$this->messageMapper = $this->getMockBuilder('OCA\OJSXC\Db\MessageMapper')->disableOriginalConstructor()->getMock();
$this->userProvider = $this->getMockBuilder('OCA\OJSXC\IUserProvider')->disableOriginalConstructor()->getMock();
- $this->message = new Message($this->userId, $this->host, $this->messageMapper, $this->userProvider);
+ $this->logger = $this->getMockBuilder('OCP\ILogger')->disableOriginalConstructor()->getMock();
+ $this->message = new Message($this->userId, $this->host, $this->messageMapper, $this->userProvider, $this->logger);
}
public function messageProvider()
@@ -99,7 +106,34 @@ class MessageTest extends TestCase
$this->userProvider->expects($this->once())
->method('hasUserByUID')
->with('derp')
- ->willReturn(true); // TODO test return false
+ ->willReturn(true);
+
+ $this->message->handle($stanza);
+ }
+
+ public function testNotAllowedToChat()
+ {
+ $this->messageMapper->expects($this->never())
+ ->method('insert');
+
+ $this->userProvider->expects($this->once())
+ ->method('hasUserByUID')
+ ->with('derp')
+ ->willReturn(false);
+
+ $stanza = [
+ 'name' => '{jabber:client}message',
+ 'value' =>
+ [
+ '{jabber:client}body' => 'abcèé³e¹³€{ë',
+ '{urn:xmpp:receipts}request' => null,
+ ],
+ 'attributes' =>
+ [
+ 'to' => 'derp@own.dev',
+ 'type' => 'chat',
+ ],
+ ];
$this->message->handle($stanza);
}
diff --git a/tests/unit/stanzahandlers/PresenceTest.php b/tests/unit/stanzahandlers/PresenceTest.php
index f5c6cf9..82a197d 100644
--- a/tests/unit/stanzahandlers/PresenceTest.php
+++ b/tests/unit/stanzahandlers/PresenceTest.php
@@ -6,7 +6,6 @@ use OCA\OJSXC\StanzaHandlers\Presence;
use OCA\OJSXC\Db\Presence as PresenceEntity;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;
-use PHPUnit_Framework_TestCase;
class PresenceTest extends TestCase
{