InvitationResponseControllerTest.php 16 KB

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