From 4ca4b0279372cd6fe11f717ed697f905ecb1e4a2 Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Thu, 23 Jun 2022 22:17:53 +0200 Subject: Support iMIP invitations from Mail Signed-off-by: Anna Larch --- tests/lib/Calendar/ManagerTest.php | 377 ++++++++++++++++++++++++++++++++++++- 1 file changed, 376 insertions(+), 1 deletion(-) (limited to 'tests/lib/Calendar') diff --git a/tests/lib/Calendar/ManagerTest.php b/tests/lib/Calendar/ManagerTest.php index a4d9d45fdb2..8b99c21ae41 100644 --- a/tests/lib/Calendar/ManagerTest.php +++ b/tests/lib/Calendar/ManagerTest.php @@ -25,10 +25,14 @@ namespace Test\Calendar; use OC\AppFramework\Bootstrap\Coordinator; use OC\Calendar\Manager; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\Calendar\ICalendar; +use OCP\Calendar\ICreateFromString; use PHPUnit\Framework\MockObject\MockObject; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; +use Sabre\VObject\Document; +use Sabre\VObject\Reader; use Test\TestCase; class ManagerTest extends TestCase { @@ -45,17 +49,22 @@ class ManagerTest extends TestCase { /** @var Manager */ private $manager; + /** @var ITimeFactory|ITimeFactory&MockObject|MockObject */ + private $time; + protected function setUp(): void { parent::setUp(); $this->coordinator = $this->createMock(Coordinator::class); $this->container = $this->createMock(ContainerInterface::class); $this->logger = $this->createMock(LoggerInterface::class); + $this->time = $this->createMock(ITimeFactory::class); $this->manager = new Manager( $this->coordinator, $this->container, - $this->logger + $this->logger, + $this->time, ); } @@ -231,4 +240,370 @@ class ManagerTest extends TestCase { $isEnabled = $this->manager->isEnabled(); $this->assertTrue($isEnabled); } + + public function testHandleImipReplyWrongMethod(): void { + $principalUri = 'principals/user/linus'; + $sender = 'pierre@general-store.com'; + $recipient = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + $calendarData->METHOD = 'REQUEST'; + + $this->logger->expects(self::once()) + ->method('warning'); + $this->time->expects(self::never()) + ->method('getTimestamp'); + + $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipReplyOrganizerNotRecipient(): void { + $principalUri = 'principals/user/linus'; + $recipient = 'pierre@general-store.com'; + $sender = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + + $this->logger->expects(self::once()) + ->method('warning'); + $this->time->expects(self::never()) + ->method('getTimestamp'); + + $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipReplyDateInThePast(): void { + $principalUri = 'principals/user/linus'; + $sender = 'pierre@general-store.com'; + $recipient = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(time()); + + $this->logger->expects(self::once()) + ->method('warning'); + + $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipReplyNoCalendars(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $principalUri = 'principals/user/linus'; + $sender = 'pierre@general-store.com'; + $recipient = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->willReturn([]); + $this->logger->expects(self::once()) + ->method('warning'); + + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipReplyEventNotFound(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $calendar = $this->createMock(ICreateFromString::class); + $principalUri = 'principals/user/linus'; + $sender = 'pierre@general-store.com'; + $recipient = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->willReturn([$calendar]); + $calendar->expects(self::once()) + ->method('search') + ->willReturn([]); + $this->logger->expects(self::once()) + ->method('info'); + $calendar->expects(self::never()) + ->method('handleIMipMessage'); + + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipReply(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $calendar = $this->createMock(ICreateFromString::class); + $principalUri = 'principals/user/linus'; + $sender = 'pierre@general-store.com'; + $recipient = 'linus@stardew-tent-living.com'; + $calendarData = $this->getVCalendarReply(); + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->willReturn([$calendar]); + $calendar->expects(self::once()) + ->method('search') + ->willReturn([['uri' => 'testname.ics']]); + $calendar->expects(self::once()) + ->method('handleIMipMessage') + ->with('testname.ics', $calendarData->serialize()); + + $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize()); + $this->assertTrue($result); + } + + public function testHandleImipCancelWrongMethod(): void { + $principalUri = 'principals/user/pierre'; + $sender = 'linus@stardew-tent-living.com'; + $recipient = 'pierre@general-store.com'; + $replyTo = null; + $calendarData = $this->getVCalendarCancel(); + $calendarData->VEVENT->METHOD = 'REQUEST'; + + $this->logger->expects(self::once()) + ->method('warning'); + $this->time->expects(self::never()) + ->method('getTimestamp'); + + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipCancelAttendeeNotRecipient(): void { + $principalUri = '/user/admin'; + $sender = 'linus@stardew-tent-living.com'; + $recipient = 'leah@general-store.com'; + $replyTo = null; + $calendarData = $this->getVCalendarCancel(); + $calendarData->VEVENT->METHOD = 'CANCEL'; + + $this->logger->expects(self::once()) + ->method('warning'); + $this->time->expects(self::never()) + ->method('getTimestamp'); + + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipCancelDateInThePast(): void { + $principalUri = 'principals/user/pierre'; + $sender = 'linus@stardew-tent-living.com'; + $recipient = 'pierre@general-store.com'; + $replyTo = null; + $calendarData = $this->getVCalendarCancel(); + $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(time()); + $this->logger->expects(self::once()) + ->method('warning'); + + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipCancelNoCalendars(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $principalUri = 'principals/user/pierre'; + $sender = 'linus@stardew-tent-living.com'; + $recipient = 'pierre@general-store.com'; + $replyTo = null; + $calendarData = $this->getVCalendarCancel(); + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->with($principalUri) + ->willReturn([]); + $this->logger->expects(self::once()) + ->method('warning'); + + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertTrue($result); + } + + public function testHandleImipCancelOrganiserInReplyTo(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $principalUri = 'principals/user/pierre'; + $sender = 'clint@stardew-blacksmiths.com'; + $recipient = 'pierre@general-store.com'; + $replyTo = 'linus@stardew-tent-living.com'; + $calendar = $this->createMock(ICreateFromString::class); + $calendarData = $this->getVCalendarCancel(); + $calendarData->VEVENT->METHOD = 'CANCEL'; + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->with($principalUri) + ->willReturn([$calendar]); + $calendar->expects(self::once()) + ->method('search') + ->willReturn([['uri' => 'testname.ics']]); + $calendar->expects(self::once()) + ->method('handleIMipMessage') + ->with('testname.ics', $calendarData->serialize()); + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + public function testHandleImipCancel(): void { + /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */ + $manager = $this->getMockBuilder(Manager::class) + ->setConstructorArgs([ + $this->coordinator, + $this->container, + $this->logger, + $this->time + ]) + ->setMethods([ + 'getCalendarsForPrincipal' + ]); + $principalUri = 'principals/user/pierre'; + $sender = 'linus@stardew-tent-living.com'; + $recipient = 'pierre@general-store.com'; + $replyTo = null; + $calendar = $this->createMock(ICreateFromString::class); + $calendarData = $this->getVCalendarCancel(); + $calendarData->VEVENT->METHOD = 'CANCEL'; + + $this->time->expects(self::once()) + ->method('getTimestamp') + ->willReturn(202208219); + $manager->expects(self::once()) + ->method('getCalendarsForPrincipal') + ->with($principalUri) + ->willReturn([$calendar]); + $calendar->expects(self::once()) + ->method('search') + ->willReturn([['uri' => 'testname.ics']]); + $calendar->expects(self::once()) + ->method('handleIMipMessage') + ->with('testname.ics', $calendarData->serialize()); + $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize()); + $this->assertFalse($result); + } + + private function getVCalendarReply(): Document { + $data = <<