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

HttpBindControllerTest.php « controller « unit « tests - github.com/nextcloud/jsxc.nextcloud.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9366da7ec5d237a2c1875d0323118bba24ec46e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace OCA\OJSXC\Controller;

use OCA\OJSXC\Db\StanzaMapper;
use OCA\OJSXC\Http\XMPPResponse;
use OCA\OJSXC\StanzaHandlers\IQ;
use OCP\AppFramework\Db\DoesNotExistException;
use PHPUnit_Framework_TestCase;

class HttpBindControllerTest extends PHPUnit_Framework_TestCase {

	/**
	 * @var HttpBindController
	 */
	private $controller;

	/**
	 * @var PHPUnit_Framework_MockObject_MockObject
	 */
	private $stanzaMapper;

	/**
	 * @var PHPUnit_Framework_MockObject_MockObject
	 */
	private $iqHandler;

	private $userId = 'john';

	public function setUp() {
	}

	/**
	 * Helper function to set up the controller. This can't be done in the setUp,
	 * since the requestBody is different for every test.
	 * @param $requestBody
	 */
	private function setUpController($requestBody) {
		$request = $this->getMockBuilder('OCP\IRequest')->getMock();
		$session = $this->getMockBuilder('OCP\ISession')->getMock();
		$this->stanzaMapper = $this->getMockBuilder('OCA\OJSXC\Db\StanzaMapper')->getMock();

		$this->iqHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\IQ')->getMock();
		$messageHandler = $this->getMockBuilder('OCA\OJSXC\StanzaHandlers\Message')->getMock();

		$this->controller = new HttpBindController(
			'ojsxc',
			$request,
			$this->userId,
			$session,
			$this->stanzaMapper,
			$this->iqHandler,
			$messageHandler,
			'localhost',
			$requestBody,
			0,
			10
		);
	}

	/**
	 * When invalid XML, just start long polling.
	 */
	public function testInvalidXML() {
		$ex = new DoesNotExistException();
		$expResponse = new XMPPResponse();

		$this->setUpController('<x>');
		$this->stanzaMapper->expects($this->exactly(10))
			->method('findByTo')
			->with('john@localhost')
			->will($this->throwException($ex));

		$response = $this->controller->index();
		$this->assertEquals($expResponse, $response);
	}

	public function IQProvider() {
		return [
			[
				'<body rid=\'897878733\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>',
				'<body xmlns="http://jabber.org/protocol/httpbind"><iq to="admin@localhost" type="result" id="2:sendIQ"><query xmlns="jabber:iq:roster"><item jid="derp@localhost" name="derp"></item></query></iq></body>',
				$this->once()
			],
			[
				'<body rid=\'897878734\' xmlns=\'http://jabber.org/protocol/httpbind\' sid=\'7862\'><iq from=\'admin@localhost\' to=\'localhost\' type=\'get\' xmlns=\'jabber:client\' id=\'1:sendIQ\'><query xmlns=\'http://jabber.org/protocol/disco#info\' node=\'undefined#undefined\'/></iq><iq type=\'get\' xmlns=\'jabber:client\' id=\'2:sendIQ\'><query xmlns=\'jabber:iq:roster\'/></iq><iq type=\'get\' to=\'admin@localhost\' xmlns=\'jabber:client\' id=\'3:sendIQ\'><vCard xmlns=\'vcard-temp\'/></iq></body>',
				null,
				$this->exactly(10)
			]
		];
	}

	/**
	 * @dataProvider IQProvider
	 */
	public function testIQHandler($body, $result, $pollCount) {
		$ex = new DoesNotExistException();
		$this->setUpController($body);

		$expResponse = new XMPPResponse();
		$expResponse->write($result);

		$this->iqHandler->expects($this->any()) // FIXME
			->method('handle')
			->will($this->returnValue($result));

		$this->stanzaMapper->expects($pollCount)
			->method('findByTo')
			->with('john@localhost')
			->will($this->throwException($ex));


		$response = $this->controller->index();
		$this->assertEquals($expResponse, $response);

	}
	
}