InvitationResponseControllerTest.php 14 KB

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