InvitationResponseControllerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Tests\Unit\DAV\Controller;
  25. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  26. use OCA\DAV\CalDAV\Schedule\Plugin;
  27. use OCA\DAV\Controller\InvitationResponseController;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. use OCP\IRequest;
  33. use Sabre\VObject\ITip\Message;
  34. use Test\TestCase;
  35. class InvitationResponseControllerTest extends TestCase {
  36. /** @var InvitationResponseController */
  37. private $controller;
  38. /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
  39. private $dbConnection;
  40. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  41. private $request;
  42. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  43. private $timeFactory;
  44. /** @var InvitationResponseServer|\PHPUnit_Framework_MockObject_MockObject */
  45. private $responseServer;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->dbConnection = $this->createMock(IDBConnection::class);
  49. $this->request = $this->createMock(IRequest::class);
  50. $this->timeFactory = $this->createMock(ITimeFactory::class);
  51. $this->responseServer = $this->getMockBuilder(InvitationResponseServer::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->controller = new InvitationResponseController(
  55. 'appName',
  56. $this->request,
  57. $this->dbConnection,
  58. $this->timeFactory,
  59. $this->responseServer
  60. );
  61. }
  62. public function testAccept() {
  63. $this->buildQueryExpects('TOKEN123', [
  64. 'id' => 0,
  65. 'uid' => 'this-is-the-events-uid',
  66. 'recurrenceid' => null,
  67. 'attendee' => 'mailto:attendee@foo.bar',
  68. 'organizer' => 'mailto:organizer@foo.bar',
  69. 'sequence' => null,
  70. 'token' => 'TOKEN123',
  71. 'expiration' => 420000,
  72. ], 1337);
  73. $expected = <<<EOF
  74. BEGIN:VCALENDAR
  75. VERSION:2.0
  76. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  77. METHOD:REPLY
  78. BEGIN:VEVENT
  79. ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
  80. ORGANIZER:mailto:organizer@foo.bar
  81. UID:this-is-the-events-uid
  82. SEQUENCE:0
  83. REQUEST-STATUS:2.0;Success
  84. DTSTAMP:19700101T002217Z
  85. END:VEVENT
  86. END:VCALENDAR
  87. EOF;
  88. $expected = preg_replace('~\R~u', "\r\n", $expected);
  89. $called = false;
  90. $this->responseServer->expects($this->once())
  91. ->method('handleITipMessage')
  92. ->will($this->returnCallback(function(Message $iTipMessage) use (&$called, $expected) {
  93. $called = true;
  94. $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
  95. $this->assertEquals('VEVENT', $iTipMessage->component);
  96. $this->assertEquals('REPLY', $iTipMessage->method);
  97. $this->assertEquals(null, $iTipMessage->sequence);
  98. $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
  99. $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
  100. $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
  101. $this->assertEquals($expected, $iTipMessage->message->serialize());
  102. }));
  103. $response = $this->controller->accept('TOKEN123');
  104. $this->assertInstanceOf(TemplateResponse::class, $response);
  105. $this->assertEquals('schedule-response-success', $response->getTemplateName());
  106. $this->assertEquals([], $response->getParams());
  107. $this->assertTrue($called);
  108. }
  109. public function testAcceptSequence() {
  110. $this->buildQueryExpects('TOKEN123', [
  111. 'id' => 0,
  112. 'uid' => 'this-is-the-events-uid',
  113. 'recurrenceid' => null,
  114. 'attendee' => 'mailto:attendee@foo.bar',
  115. 'organizer' => 'mailto:organizer@foo.bar',
  116. 'sequence' => 1337,
  117. 'token' => 'TOKEN123',
  118. 'expiration' => 420000,
  119. ], 1337);
  120. $expected = <<<EOF
  121. BEGIN:VCALENDAR
  122. VERSION:2.0
  123. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  124. METHOD:REPLY
  125. BEGIN:VEVENT
  126. ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
  127. ORGANIZER:mailto:organizer@foo.bar
  128. UID:this-is-the-events-uid
  129. SEQUENCE:1337
  130. REQUEST-STATUS:2.0;Success
  131. DTSTAMP:19700101T002217Z
  132. END:VEVENT
  133. END:VCALENDAR
  134. EOF;
  135. $expected = preg_replace('~\R~u', "\r\n", $expected);
  136. $called = false;
  137. $this->responseServer->expects($this->once())
  138. ->method('handleITipMessage')
  139. ->will($this->returnCallback(function(Message $iTipMessage) use (&$called, $expected) {
  140. $called = true;
  141. $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
  142. $this->assertEquals('VEVENT', $iTipMessage->component);
  143. $this->assertEquals('REPLY', $iTipMessage->method);
  144. $this->assertEquals(1337, $iTipMessage->sequence);
  145. $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
  146. $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
  147. $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
  148. $this->assertEquals($expected, $iTipMessage->message->serialize());
  149. }));
  150. $response = $this->controller->accept('TOKEN123');
  151. $this->assertInstanceOf(TemplateResponse::class, $response);
  152. $this->assertEquals('schedule-response-success', $response->getTemplateName());
  153. $this->assertEquals([], $response->getParams());
  154. $this->assertTrue($called);
  155. }
  156. public function testAcceptRecurrenceId() {
  157. $this->buildQueryExpects('TOKEN123', [
  158. 'id' => 0,
  159. 'uid' => 'this-is-the-events-uid',
  160. 'recurrenceid' => "RECURRENCE-ID;TZID=Europe/Berlin:20180726T150000\n",
  161. 'attendee' => 'mailto:attendee@foo.bar',
  162. 'organizer' => 'mailto:organizer@foo.bar',
  163. 'sequence' => null,
  164. 'token' => 'TOKEN123',
  165. 'expiration' => 420000,
  166. ], 1337);
  167. $expected = <<<EOF
  168. BEGIN:VCALENDAR
  169. VERSION:2.0
  170. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  171. METHOD:REPLY
  172. BEGIN:VEVENT
  173. ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
  174. ORGANIZER:mailto:organizer@foo.bar
  175. UID:this-is-the-events-uid
  176. SEQUENCE:0
  177. REQUEST-STATUS:2.0;Success
  178. RECURRENCE-ID;TZID=Europe/Berlin:20180726T150000
  179. DTSTAMP:19700101T002217Z
  180. END:VEVENT
  181. END:VCALENDAR
  182. EOF;
  183. $expected = preg_replace('~\R~u', "\r\n", $expected);
  184. $called = false;
  185. $this->responseServer->expects($this->once())
  186. ->method('handleITipMessage')
  187. ->will($this->returnCallback(function(Message $iTipMessage) use (&$called, $expected) {
  188. $called = true;
  189. $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
  190. $this->assertEquals('VEVENT', $iTipMessage->component);
  191. $this->assertEquals('REPLY', $iTipMessage->method);
  192. $this->assertEquals(0, $iTipMessage->sequence);
  193. $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
  194. $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
  195. $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
  196. $this->assertEquals($expected, $iTipMessage->message->serialize());
  197. }));
  198. $response = $this->controller->accept('TOKEN123');
  199. $this->assertInstanceOf(TemplateResponse::class, $response);
  200. $this->assertEquals('schedule-response-success', $response->getTemplateName());
  201. $this->assertEquals([], $response->getParams());
  202. $this->assertTrue($called);
  203. }
  204. public function testAcceptTokenNotFound() {
  205. $this->buildQueryExpects('TOKEN123', null, 1337);
  206. $response = $this->controller->accept('TOKEN123');
  207. $this->assertInstanceOf(TemplateResponse::class, $response);
  208. $this->assertEquals('schedule-response-error', $response->getTemplateName());
  209. $this->assertEquals([], $response->getParams());
  210. }
  211. public function testAcceptExpiredToken() {
  212. $this->buildQueryExpects('TOKEN123', [
  213. 'id' => 0,
  214. 'uid' => 'this-is-the-events-uid',
  215. 'recurrenceid' => null,
  216. 'attendee' => 'mailto:attendee@foo.bar',
  217. 'organizer' => 'mailto:organizer@foo.bar',
  218. 'sequence' => null,
  219. 'token' => 'TOKEN123',
  220. 'expiration' => 42,
  221. ], 1337);
  222. $response = $this->controller->accept('TOKEN123');
  223. $this->assertInstanceOf(TemplateResponse::class, $response);
  224. $this->assertEquals('schedule-response-error', $response->getTemplateName());
  225. $this->assertEquals([], $response->getParams());
  226. }
  227. public function testDecline() {
  228. $this->buildQueryExpects('TOKEN123', [
  229. 'id' => 0,
  230. 'uid' => 'this-is-the-events-uid',
  231. 'recurrenceid' => null,
  232. 'attendee' => 'mailto:attendee@foo.bar',
  233. 'organizer' => 'mailto:organizer@foo.bar',
  234. 'sequence' => null,
  235. 'token' => 'TOKEN123',
  236. 'expiration' => 420000,
  237. ], 1337);
  238. $expected = <<<EOF
  239. BEGIN:VCALENDAR
  240. VERSION:2.0
  241. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  242. METHOD:REPLY
  243. BEGIN:VEVENT
  244. ATTENDEE;PARTSTAT=DECLINED:mailto:attendee@foo.bar
  245. ORGANIZER:mailto:organizer@foo.bar
  246. UID:this-is-the-events-uid
  247. SEQUENCE:0
  248. REQUEST-STATUS:2.0;Success
  249. DTSTAMP:19700101T002217Z
  250. END:VEVENT
  251. END:VCALENDAR
  252. EOF;
  253. $expected = preg_replace('~\R~u', "\r\n", $expected);
  254. $called = false;
  255. $this->responseServer->expects($this->once())
  256. ->method('handleITipMessage')
  257. ->will($this->returnCallback(function(Message $iTipMessage) use (&$called, $expected) {
  258. $called = true;
  259. $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
  260. $this->assertEquals('VEVENT', $iTipMessage->component);
  261. $this->assertEquals('REPLY', $iTipMessage->method);
  262. $this->assertEquals(null, $iTipMessage->sequence);
  263. $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
  264. $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
  265. $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
  266. $this->assertEquals($expected, $iTipMessage->message->serialize());
  267. }));
  268. $response = $this->controller->decline('TOKEN123');
  269. $this->assertInstanceOf(TemplateResponse::class, $response);
  270. $this->assertEquals('schedule-response-success', $response->getTemplateName());
  271. $this->assertEquals([], $response->getParams());
  272. $this->assertTrue($called);
  273. }
  274. public function testOptions() {
  275. $response = $this->controller->options('TOKEN123');
  276. $this->assertInstanceOf(TemplateResponse::class, $response);
  277. $this->assertEquals('schedule-response-options', $response->getTemplateName());
  278. $this->assertEquals(['token' => 'TOKEN123'], $response->getParams());
  279. }
  280. public function testProcessMoreOptionsResult() {
  281. $this->request->expects($this->at(0))
  282. ->method('getParam')
  283. ->with('partStat')
  284. ->will($this->returnValue('TENTATIVE'));
  285. $this->request->expects($this->at(1))
  286. ->method('getParam')
  287. ->with('guests')
  288. ->will($this->returnValue('7'));
  289. $this->request->expects($this->at(2))
  290. ->method('getParam')
  291. ->with('comment')
  292. ->will($this->returnValue('Foo bar Bli blub'));
  293. $this->buildQueryExpects('TOKEN123', [
  294. 'id' => 0,
  295. 'uid' => 'this-is-the-events-uid',
  296. 'recurrenceid' => null,
  297. 'attendee' => 'mailto:attendee@foo.bar',
  298. 'organizer' => 'mailto:organizer@foo.bar',
  299. 'sequence' => null,
  300. 'token' => 'TOKEN123',
  301. 'expiration' => 420000,
  302. ], 1337);
  303. $expected = <<<EOF
  304. BEGIN:VCALENDAR
  305. VERSION:2.0
  306. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  307. METHOD:REPLY
  308. BEGIN:VEVENT
  309. ATTENDEE;PARTSTAT=TENTATIVE;X-RESPONSE-COMMENT=Foo bar Bli blub;X-NUM-GUEST
  310. S=7:mailto:attendee@foo.bar
  311. ORGANIZER:mailto:organizer@foo.bar
  312. UID:this-is-the-events-uid
  313. SEQUENCE:0
  314. REQUEST-STATUS:2.0;Success
  315. DTSTAMP:19700101T002217Z
  316. COMMENT:Foo bar Bli blub
  317. END:VEVENT
  318. END:VCALENDAR
  319. EOF;
  320. $expected = preg_replace('~\R~u', "\r\n", $expected);
  321. $called = false;
  322. $this->responseServer->expects($this->once())
  323. ->method('handleITipMessage')
  324. ->will($this->returnCallback(function(Message $iTipMessage) use (&$called, $expected) {
  325. $called = true;
  326. $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
  327. $this->assertEquals('VEVENT', $iTipMessage->component);
  328. $this->assertEquals('REPLY', $iTipMessage->method);
  329. $this->assertEquals(null, $iTipMessage->sequence);
  330. $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
  331. $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
  332. $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
  333. $this->assertEquals($expected, $iTipMessage->message->serialize());
  334. }));
  335. $response = $this->controller->processMoreOptionsResult('TOKEN123');
  336. $this->assertInstanceOf(TemplateResponse::class, $response);
  337. $this->assertEquals('schedule-response-success', $response->getTemplateName());
  338. $this->assertEquals([], $response->getParams());
  339. $this->assertTrue($called);
  340. }
  341. private function buildQueryExpects($token, $return, $time) {
  342. $queryBuilder = $this->createMock(IQueryBuilder::class);
  343. $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class);
  344. $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class);
  345. $this->dbConnection->expects($this->once())
  346. ->method('getQueryBuilder')
  347. ->with()
  348. ->will($this->returnValue($queryBuilder));
  349. $queryBuilder->method('expr')
  350. ->will($this->returnValue($expr));
  351. $queryBuilder->method('createNamedParameter')
  352. ->will($this->returnValueMap([
  353. [$token, \PDO::PARAM_STR, null, 'namedParameterToken']
  354. ]));
  355. $stmt->expects($this->once())
  356. ->method('fetch')
  357. ->with(\PDO::FETCH_ASSOC)
  358. ->will($this->returnValue($return));
  359. $expr->expects($this->once())
  360. ->method('eq')
  361. ->with('token', 'namedParameterToken')
  362. ->will($this->returnValue('EQ STATEMENT'));
  363. $this->dbConnection->expects($this->once())
  364. ->method('getQueryBuilder')
  365. ->with()
  366. ->will($this->returnValue($queryBuilder));
  367. $queryBuilder->expects($this->at(0))
  368. ->method('select')
  369. ->with('*')
  370. ->will($this->returnValue($queryBuilder));
  371. $queryBuilder->expects($this->at(1))
  372. ->method('from')
  373. ->with('calendar_invitations')
  374. ->will($this->returnValue($queryBuilder));
  375. $queryBuilder->expects($this->at(4))
  376. ->method('where')
  377. ->with('EQ STATEMENT')
  378. ->will($this->returnValue($queryBuilder));
  379. $queryBuilder->expects($this->at(5))
  380. ->method('execute')
  381. ->with()
  382. ->will($this->returnValue($stmt));
  383. $this->timeFactory->method('getTime')
  384. ->will($this->returnValue($time));
  385. }
  386. }