123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- <?php
- declare(strict_types=1);
- /**
- * @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
- *
- * @author Georg Ehrke <oc.list@georgehrke.com>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace OCA\DAV\Tests\Unit\DAV\Controller;
- use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
- use OCA\DAV\CalDAV\Schedule\Plugin;
- use OCA\DAV\Controller\InvitationResponseController;
- use OCP\AppFramework\Http\TemplateResponse;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\IDBConnection;
- use OCP\IRequest;
- use Sabre\VObject\ITip\Message;
- use Test\TestCase;
- class InvitationResponseControllerTest extends TestCase {
- /** @var InvitationResponseController */
- private $controller;
- /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
- private $dbConnection;
- /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
- private $request;
- /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
- private $timeFactory;
- /** @var InvitationResponseServer|\PHPUnit_Framework_MockObject_MockObject */
- private $responseServer;
- protected function setUp(): void {
- parent::setUp();
- $this->dbConnection = $this->createMock(IDBConnection::class);
- $this->request = $this->createMock(IRequest::class);
- $this->timeFactory = $this->createMock(ITimeFactory::class);
- $this->responseServer = $this->getMockBuilder(InvitationResponseServer::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->controller = new InvitationResponseController(
- 'appName',
- $this->request,
- $this->dbConnection,
- $this->timeFactory,
- $this->responseServer
- );
- }
- public function testAccept() {
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => null,
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => null,
- 'token' => 'TOKEN123',
- 'expiration' => 420000,
- ], 1337);
- $expected = <<<EOF
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
- METHOD:REPLY
- BEGIN:VEVENT
- ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
- ORGANIZER:mailto:organizer@foo.bar
- UID:this-is-the-events-uid
- SEQUENCE:0
- REQUEST-STATUS:2.0;Success
- DTSTAMP:19700101T002217Z
- END:VEVENT
- END:VCALENDAR
- EOF;
- $expected = preg_replace('~\R~u', "\r\n", $expected);
- $called = false;
- $this->responseServer->expects($this->once())
- ->method('handleITipMessage')
- ->willReturnCallback(function(Message $iTipMessage) use (&$called, $expected) {
- $called = true;
- $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
- $this->assertEquals('VEVENT', $iTipMessage->component);
- $this->assertEquals('REPLY', $iTipMessage->method);
- $this->assertEquals(null, $iTipMessage->sequence);
- $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
- $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
- $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
- $this->assertEquals($expected, $iTipMessage->message->serialize());
- });
- $response = $this->controller->accept('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-success', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- $this->assertTrue($called);
- }
- public function testAcceptSequence() {
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => null,
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => 1337,
- 'token' => 'TOKEN123',
- 'expiration' => 420000,
- ], 1337);
- $expected = <<<EOF
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
- METHOD:REPLY
- BEGIN:VEVENT
- ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
- ORGANIZER:mailto:organizer@foo.bar
- UID:this-is-the-events-uid
- SEQUENCE:1337
- REQUEST-STATUS:2.0;Success
- DTSTAMP:19700101T002217Z
- END:VEVENT
- END:VCALENDAR
- EOF;
- $expected = preg_replace('~\R~u', "\r\n", $expected);
- $called = false;
- $this->responseServer->expects($this->once())
- ->method('handleITipMessage')
- ->willReturnCallback(function(Message $iTipMessage) use (&$called, $expected) {
- $called = true;
- $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
- $this->assertEquals('VEVENT', $iTipMessage->component);
- $this->assertEquals('REPLY', $iTipMessage->method);
- $this->assertEquals(1337, $iTipMessage->sequence);
- $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
- $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
- $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
- $this->assertEquals($expected, $iTipMessage->message->serialize());
- });
- $response = $this->controller->accept('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-success', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- $this->assertTrue($called);
- }
- public function testAcceptRecurrenceId() {
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => "RECURRENCE-ID;TZID=Europe/Berlin:20180726T150000\n",
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => null,
- 'token' => 'TOKEN123',
- 'expiration' => 420000,
- ], 1337);
- $expected = <<<EOF
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
- METHOD:REPLY
- BEGIN:VEVENT
- ATTENDEE;PARTSTAT=ACCEPTED:mailto:attendee@foo.bar
- ORGANIZER:mailto:organizer@foo.bar
- UID:this-is-the-events-uid
- SEQUENCE:0
- REQUEST-STATUS:2.0;Success
- RECURRENCE-ID;TZID=Europe/Berlin:20180726T150000
- DTSTAMP:19700101T002217Z
- END:VEVENT
- END:VCALENDAR
- EOF;
- $expected = preg_replace('~\R~u', "\r\n", $expected);
- $called = false;
- $this->responseServer->expects($this->once())
- ->method('handleITipMessage')
- ->willReturnCallback(function(Message $iTipMessage) use (&$called, $expected) {
- $called = true;
- $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
- $this->assertEquals('VEVENT', $iTipMessage->component);
- $this->assertEquals('REPLY', $iTipMessage->method);
- $this->assertEquals(0, $iTipMessage->sequence);
- $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
- $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
- $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
- $this->assertEquals($expected, $iTipMessage->message->serialize());
- });
- $response = $this->controller->accept('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-success', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- $this->assertTrue($called);
- }
- public function testAcceptTokenNotFound() {
- $this->buildQueryExpects('TOKEN123', null, 1337);
- $response = $this->controller->accept('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-error', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- }
- public function testAcceptExpiredToken() {
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => null,
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => null,
- 'token' => 'TOKEN123',
- 'expiration' => 42,
- ], 1337);
- $response = $this->controller->accept('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-error', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- }
- public function testDecline() {
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => null,
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => null,
- 'token' => 'TOKEN123',
- 'expiration' => 420000,
- ], 1337);
- $expected = <<<EOF
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
- METHOD:REPLY
- BEGIN:VEVENT
- ATTENDEE;PARTSTAT=DECLINED:mailto:attendee@foo.bar
- ORGANIZER:mailto:organizer@foo.bar
- UID:this-is-the-events-uid
- SEQUENCE:0
- REQUEST-STATUS:2.0;Success
- DTSTAMP:19700101T002217Z
- END:VEVENT
- END:VCALENDAR
- EOF;
- $expected = preg_replace('~\R~u', "\r\n", $expected);
- $called = false;
- $this->responseServer->expects($this->once())
- ->method('handleITipMessage')
- ->willReturnCallback(function(Message $iTipMessage) use (&$called, $expected) {
- $called = true;
- $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
- $this->assertEquals('VEVENT', $iTipMessage->component);
- $this->assertEquals('REPLY', $iTipMessage->method);
- $this->assertEquals(null, $iTipMessage->sequence);
- $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
- $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
- $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
- $this->assertEquals($expected, $iTipMessage->message->serialize());
- });
- $response = $this->controller->decline('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-success', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- $this->assertTrue($called);
- }
- public function testOptions() {
- $response = $this->controller->options('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-options', $response->getTemplateName());
- $this->assertEquals(['token' => 'TOKEN123'], $response->getParams());
- }
- public function testProcessMoreOptionsResult() {
- $this->request->expects($this->at(0))
- ->method('getParam')
- ->with('partStat')
- ->willReturn('TENTATIVE');
- $this->request->expects($this->at(1))
- ->method('getParam')
- ->with('guests')
- ->willReturn('7');
- $this->request->expects($this->at(2))
- ->method('getParam')
- ->with('comment')
- ->willReturn('Foo bar Bli blub');
- $this->buildQueryExpects('TOKEN123', [
- 'id' => 0,
- 'uid' => 'this-is-the-events-uid',
- 'recurrenceid' => null,
- 'attendee' => 'mailto:attendee@foo.bar',
- 'organizer' => 'mailto:organizer@foo.bar',
- 'sequence' => null,
- 'token' => 'TOKEN123',
- 'expiration' => 420000,
- ], 1337);
- $expected = <<<EOF
- BEGIN:VCALENDAR
- VERSION:2.0
- PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
- METHOD:REPLY
- BEGIN:VEVENT
- ATTENDEE;PARTSTAT=TENTATIVE;X-RESPONSE-COMMENT=Foo bar Bli blub;X-NUM-GUEST
- S=7:mailto:attendee@foo.bar
- ORGANIZER:mailto:organizer@foo.bar
- UID:this-is-the-events-uid
- SEQUENCE:0
- REQUEST-STATUS:2.0;Success
- DTSTAMP:19700101T002217Z
- COMMENT:Foo bar Bli blub
- END:VEVENT
- END:VCALENDAR
- EOF;
- $expected = preg_replace('~\R~u', "\r\n", $expected);
- $called = false;
- $this->responseServer->expects($this->once())
- ->method('handleITipMessage')
- ->willReturnCallback(function(Message $iTipMessage) use (&$called, $expected) {
- $called = true;
- $this->assertEquals('this-is-the-events-uid', $iTipMessage->uid);
- $this->assertEquals('VEVENT', $iTipMessage->component);
- $this->assertEquals('REPLY', $iTipMessage->method);
- $this->assertEquals(null, $iTipMessage->sequence);
- $this->assertEquals('mailto:attendee@foo.bar', $iTipMessage->sender);
- $this->assertEquals('mailto:organizer@foo.bar', $iTipMessage->recipient);
- $iTipMessage->scheduleStatus = '1.2;Message delivered locally';
- $this->assertEquals($expected, $iTipMessage->message->serialize());
- });
- $response = $this->controller->processMoreOptionsResult('TOKEN123');
- $this->assertInstanceOf(TemplateResponse::class, $response);
- $this->assertEquals('schedule-response-success', $response->getTemplateName());
- $this->assertEquals([], $response->getParams());
- $this->assertTrue($called);
- }
- private function buildQueryExpects($token, $return, $time) {
- $queryBuilder = $this->createMock(IQueryBuilder::class);
- $stmt = $this->createMock(\Doctrine\DBAL\Driver\Statement::class);
- $expr = $this->createMock(\OCP\DB\QueryBuilder\IExpressionBuilder::class);
- $this->dbConnection->expects($this->once())
- ->method('getQueryBuilder')
- ->with()
- ->willReturn($queryBuilder);
- $queryBuilder->method('expr')
- ->willReturn($expr);
- $queryBuilder->method('createNamedParameter')
- ->willReturnMap([
- [$token, \PDO::PARAM_STR, null, 'namedParameterToken']
- ]);
- $stmt->expects($this->once())
- ->method('fetch')
- ->with(\PDO::FETCH_ASSOC)
- ->willReturn($return);
- $expr->expects($this->once())
- ->method('eq')
- ->with('token', 'namedParameterToken')
- ->willReturn('EQ STATEMENT');
- $this->dbConnection->expects($this->once())
- ->method('getQueryBuilder')
- ->with()
- ->willReturn($queryBuilder);
- $queryBuilder->expects($this->at(0))
- ->method('select')
- ->with('*')
- ->willReturn($queryBuilder);
- $queryBuilder->expects($this->at(1))
- ->method('from')
- ->with('calendar_invitations')
- ->willReturn($queryBuilder);
- $queryBuilder->expects($this->at(4))
- ->method('where')
- ->with('EQ STATEMENT')
- ->willReturn($queryBuilder);
- $queryBuilder->expects($this->at(5))
- ->method('execute')
- ->with()
- ->willReturn($stmt);
- $this->timeFactory->method('getTime')
- ->willReturn($time);
- }
- }
|