Skip to content

Commit

Permalink
Merge pull request #33906 from nextcloud/fix/fix-calendar-tests-getTi…
Browse files Browse the repository at this point in the history
…mestamp

Fix Calendar tests mocking a non-existant method.
  • Loading branch information
blizzz authored Sep 8, 2022
2 parents 5555500 + e3a52ce commit c77cc80
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
17 changes: 16 additions & 1 deletion lib/private/Calendar/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,27 @@ public function handleIMipCancel(string $principalUri, string $sender, ?string $
// to the email address in the ORGANIZER.
// We don't want to accept a CANCEL request from just anyone
$organizer = substr($vEvent->{'ORGANIZER'}->getValue(), 7);
if (strcasecmp($sender, $organizer) !== 0 && strcasecmp($replyTo, $organizer) !== 0) {
$isNotOrganizer = ($replyTo !== null) ? (strcasecmp($sender, $organizer) !== 0 && strcasecmp($replyTo, $organizer) !== 0) : (strcasecmp($sender, $organizer) !== 0);
if ($isNotOrganizer) {
$this->logger->warning('Sender must be the ORGANIZER of this event');
return false;
}

//check if the event is in the future
/** @var DateTime $eventTime */
$eventTime = $vEvent->{'DTSTART'};
if ($eventTime->getDateTime()->getTimeStamp() < $this->timeFactory->getTime()) { // this might cause issues with recurrences
$this->logger->warning('Only events in the future are processed');
return false;
}

// Check if we have a calendar to work with
$calendars = $this->getCalendarsForPrincipal($principalUri);
if (empty($calendars)) {
$this->logger->warning('Could not find any calendars for principal ' . $principalUri);
return false;
}

$found = null;
// if the attendee has been found in at least one calendar event with the UID of the iMIP event
// we process it.
Expand Down
71 changes: 37 additions & 34 deletions tests/lib/Calendar/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function testHandleImipReplyWrongMethod(): void {
$this->logger->expects(self::once())
->method('warning');
$this->time->expects(self::never())
->method('getTimestamp');
->method('getTime');

$result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
$this->assertFalse($result);
Expand All @@ -266,7 +266,7 @@ public function testHandleImipReplyOrganizerNotRecipient(): void {
$this->logger->expects(self::once())
->method('warning');
$this->time->expects(self::never())
->method('getTimestamp');
->method('getTime');

$result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
$this->assertFalse($result);
Expand All @@ -280,7 +280,7 @@ public function testHandleImipReplyDateInThePast(): void {
$calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past

$this->time->expects(self::once())
->method('getTimestamp')
->method('getTime')
->willReturn(time());

$this->logger->expects(self::once())
Expand All @@ -301,15 +301,16 @@ public function testHandleImipReplyNoCalendars(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$principalUri = 'principals/user/linus';
$sender = '[email protected]';
$recipient = '[email protected]';
$calendarData = $this->getVCalendarReply();

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$manager->expects(self::once())
->method('getCalendarsForPrincipal')
->willReturn([]);
Expand All @@ -331,16 +332,17 @@ public function testHandleImipReplyEventNotFound(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$calendar = $this->createMock(ICreateFromString::class);
$principalUri = 'principals/user/linus';
$sender = '[email protected]';
$recipient = '[email protected]';
$calendarData = $this->getVCalendarReply();

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$manager->expects(self::once())
->method('getCalendarsForPrincipal')
->willReturn([$calendar]);
Expand All @@ -367,16 +369,17 @@ public function testHandleImipReply(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$calendar = $this->createMock(ICreateFromString::class);
$principalUri = 'principals/user/linus';
$sender = '[email protected]';
$recipient = '[email protected]';
$calendarData = $this->getVCalendarReply();

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$manager->expects(self::once())
->method('getCalendarsForPrincipal')
->willReturn([$calendar]);
Expand All @@ -397,12 +400,12 @@ public function testHandleImipCancelWrongMethod(): void {
$recipient = '[email protected]';
$replyTo = null;
$calendarData = $this->getVCalendarCancel();
$calendarData->VEVENT->METHOD = 'REQUEST';
$calendarData->METHOD = 'REQUEST';

$this->logger->expects(self::once())
->method('warning');
$this->time->expects(self::never())
->method('getTimestamp');
->method('getTime');

$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertFalse($result);
Expand All @@ -414,12 +417,11 @@ public function testHandleImipCancelAttendeeNotRecipient(): void {
$recipient = '[email protected]';
$replyTo = null;
$calendarData = $this->getVCalendarCancel();
$calendarData->VEVENT->METHOD = 'CANCEL';

$this->logger->expects(self::once())
->method('warning');
$this->time->expects(self::never())
->method('getTimestamp');
->method('getTime');

$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertFalse($result);
Expand All @@ -434,7 +436,7 @@ public function testHandleImipCancelDateInThePast(): void {
$calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past

$this->time->expects(self::once())
->method('getTimestamp')
->method('getTime')
->willReturn(time());
$this->logger->expects(self::once())
->method('warning');
Expand All @@ -454,25 +456,26 @@ public function testHandleImipCancelNoCalendars(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$principalUri = 'principals/user/pierre';
$sender = '[email protected]';
$recipient = '[email protected]';
$replyTo = null;
$calendarData = $this->getVCalendarCancel();

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$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);
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertFalse($result);
}

public function testHandleImipCancelOrganiserInReplyTo(): void {
Expand All @@ -486,18 +489,18 @@ public function testHandleImipCancelOrganiserInReplyTo(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$principalUri = 'principals/user/pierre';
$sender = '[email protected]';
$recipient = '[email protected]';
$replyTo = '[email protected]';
$calendar = $this->createMock(ICreateFromString::class);
$calendarData = $this->getVCalendarCancel();
$calendarData->VEVENT->METHOD = 'CANCEL';

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$manager->expects(self::once())
->method('getCalendarsForPrincipal')
->with($principalUri)
Expand All @@ -508,8 +511,8 @@ public function testHandleImipCancelOrganiserInReplyTo(): void {
$calendar->expects(self::once())
->method('handleIMipMessage')
->with('testname.ics', $calendarData->serialize());
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertFalse($result);
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertTrue($result);
}

public function testHandleImipCancel(): void {
Expand All @@ -523,18 +526,18 @@ public function testHandleImipCancel(): void {
])
->setMethods([
'getCalendarsForPrincipal'
]);
])
->getMock();
$principalUri = 'principals/user/pierre';
$sender = '[email protected]';
$recipient = '[email protected]';
$replyTo = null;
$calendar = $this->createMock(ICreateFromString::class);
$calendarData = $this->getVCalendarCancel();
$calendarData->VEVENT->METHOD = 'CANCEL';

$this->time->expects(self::once())
->method('getTimestamp')
->willReturn(202208219);
->method('getTime')
->willReturn(1628374233);
$manager->expects(self::once())
->method('getCalendarsForPrincipal')
->with($principalUri)
Expand All @@ -545,8 +548,8 @@ public function testHandleImipCancel(): void {
$calendar->expects(self::once())
->method('handleIMipMessage')
->with('testname.ics', $calendarData->serialize());
$result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertFalse($result);
$result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
$this->assertTrue($result);
}

private function getVCalendarReply(): Document {
Expand Down

0 comments on commit c77cc80

Please sign in to comment.