RefreshWebcalServiceTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2020, Thomas Citharel <nextcloud@tcit.fr>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  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\BackgroundJob;
  27. use GuzzleHttp\HandlerStack;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  30. use OCP\Http\Client\IClient;
  31. use OCP\Http\Client\IClientService;
  32. use OCP\Http\Client\IResponse;
  33. use OCP\Http\Client\LocalServerException;
  34. use OCP\IConfig;
  35. use OCP\ILogger;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Sabre\VObject;
  38. use Test\TestCase;
  39. class RefreshWebcalServiceTest extends TestCase {
  40. /** @var CalDavBackend | MockObject */
  41. private $caldavBackend;
  42. /** @var IClientService | MockObject */
  43. private $clientService;
  44. /** @var IConfig | MockObject */
  45. private $config;
  46. /** @var ILogger | MockObject */
  47. private $logger;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->caldavBackend = $this->createMock(CalDavBackend::class);
  51. $this->clientService = $this->createMock(IClientService::class);
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->logger = $this->createMock(ILogger::class);
  54. }
  55. /**
  56. * @param string $body
  57. * @param string $contentType
  58. * @param string $result
  59. *
  60. * @dataProvider runDataProvider
  61. */
  62. public function testRun(string $body, string $contentType, string $result) {
  63. $refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
  64. ->setMethods(['getRandomCalendarObjectUri'])
  65. ->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
  66. ->getMock();
  67. $refreshWebcalService
  68. ->method('getRandomCalendarObjectUri')
  69. ->willReturn('uri-1.ics');
  70. $this->caldavBackend->expects($this->once())
  71. ->method('getSubscriptionsForUser')
  72. ->with('principals/users/testuser')
  73. ->willReturn([
  74. [
  75. 'id' => '99',
  76. 'uri' => 'sub456',
  77. '{http://apple.com/ns/ical/}refreshrate' => 'P1D',
  78. '{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
  79. '{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
  80. '{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
  81. 'source' => 'webcal://foo.bar/bla'
  82. ],
  83. [
  84. 'id' => '42',
  85. 'uri' => 'sub123',
  86. '{http://apple.com/ns/ical/}refreshrate' => 'PT1H',
  87. '{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
  88. '{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
  89. '{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
  90. 'source' => 'webcal://foo.bar/bla2'
  91. ],
  92. ]);
  93. $client = $this->createMock(IClient::class);
  94. $response = $this->createMock(IResponse::class);
  95. $this->clientService->expects($this->once())
  96. ->method('newClient')
  97. ->with()
  98. ->willReturn($client);
  99. $this->config->expects($this->once())
  100. ->method('getAppValue')
  101. ->with('dav', 'webcalAllowLocalAccess', 'no')
  102. ->willReturn('no');
  103. $client->expects($this->once())
  104. ->method('get')
  105. ->with('https://foo.bar/bla2', $this->callback(function ($obj) {
  106. return $obj['allow_redirects']['redirects'] === 10 && $obj['handler'] instanceof HandlerStack;
  107. }))
  108. ->willReturn($response);
  109. $response->expects($this->once())
  110. ->method('getBody')
  111. ->with()
  112. ->willReturn($body);
  113. $response->expects($this->once())
  114. ->method('getHeader')
  115. ->with('Content-Type')
  116. ->willReturn($contentType);
  117. $this->caldavBackend->expects($this->once())
  118. ->method('purgeAllCachedEventsForSubscription')
  119. ->with(42);
  120. $this->caldavBackend->expects($this->once())
  121. ->method('createCalendarObject')
  122. ->with(42, 'uri-1.ics', $result, 1);
  123. $refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function runDataProvider():array {
  129. return [
  130. [
  131. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  132. 'text/calendar;charset=utf8',
  133. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
  134. ],
  135. [
  136. '["vcalendar",[["prodid",{},"text","-//Example Corp.//Example Client//EN"],["version",{},"text","2.0"]],[["vtimezone",[["last-modified",{},"date-time","2004-01-10T03:28:45Z"],["tzid",{},"text","US/Eastern"]],[["daylight",[["dtstart",{},"date-time","2000-04-04T02:00:00"],["rrule",{},"recur",{"freq":"YEARLY","byday":"1SU","bymonth":4}],["tzname",{},"text","EDT"],["tzoffsetfrom",{},"utc-offset","-05:00"],["tzoffsetto",{},"utc-offset","-04:00"]],[]],["standard",[["dtstart",{},"date-time","2000-10-26T02:00:00"],["rrule",{},"recur",{"freq":"YEARLY","byday":"1SU","bymonth":10}],["tzname",{},"text","EST"],["tzoffsetfrom",{},"utc-offset","-04:00"],["tzoffsetto",{},"utc-offset","-05:00"]],[]]]],["vevent",[["dtstamp",{},"date-time","2006-02-06T00:11:21Z"],["dtstart",{"tzid":"US/Eastern"},"date-time","2006-01-02T14:00:00"],["duration",{},"duration","PT1H"],["recurrence-id",{"tzid":"US/Eastern"},"date-time","2006-01-04T12:00:00"],["summary",{},"text","Event #2"],["uid",{},"text","12345"]],[]]]]',
  137. 'application/calendar+json',
  138. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VTIMEZONE\r\nLAST-MODIFIED:20040110T032845Z\r\nTZID:US/Eastern\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000404T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4\r\nTZNAME:EDT\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nDTSTART:20001026T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=10\r\nTZNAME:EST\r\nTZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTAMP:20060206T001121Z\r\nDTSTART;TZID=US/Eastern:20060102T140000\r\nDURATION:PT1H\r\nRECURRENCE-ID;TZID=US/Eastern:20060104T120000\r\nSUMMARY:Event #2\r\nUID:12345\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
  139. ],
  140. [
  141. '<?xml version="1.0" encoding="utf-8" ?><icalendar xmlns="urn:ietf:params:xml:ns:icalendar-2.0"><vcalendar><properties><prodid><text>-//Example Inc.//Example Client//EN</text></prodid><version><text>2.0</text></version></properties><components><vevent><properties><dtstamp><date-time>2006-02-06T00:11:21Z</date-time></dtstamp><dtstart><parameters><tzid><text>US/Eastern</text></tzid></parameters><date-time>2006-01-04T14:00:00</date-time></dtstart><duration><duration>PT1H</duration></duration><recurrence-id><parameters><tzid><text>US/Eastern</text></tzid></parameters><date-time>2006-01-04T12:00:00</date-time></recurrence-id><summary><text>Event #2 bis</text></summary><uid><text>12345</text></uid></properties></vevent></components></vcalendar></icalendar>',
  142. 'application/calendar+xml',
  143. "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject " . VObject\Version::VERSION . "//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nDTSTAMP:20060206T001121Z\r\nDTSTART;TZID=US/Eastern:20060104T140000\r\nDURATION:PT1H\r\nRECURRENCE-ID;TZID=US/Eastern:20060104T120000\r\nSUMMARY:Event #2 bis\r\nUID:12345\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"
  144. ]
  145. ];
  146. }
  147. /**
  148. * @dataProvider runLocalURLDataProvider
  149. *
  150. * @param string $source
  151. */
  152. public function testRunLocalURL($source) {
  153. $refreshWebcalService = new RefreshWebcalService(
  154. $this->caldavBackend,
  155. $this->clientService,
  156. $this->config,
  157. $this->logger
  158. );
  159. $this->caldavBackend->expects($this->once())
  160. ->method('getSubscriptionsForUser')
  161. ->with('principals/users/testuser')
  162. ->willReturn([
  163. [
  164. 'id' => 42,
  165. 'uri' => 'sub123',
  166. 'refreshreate' => 'P1H',
  167. 'striptodos' => 1,
  168. 'stripalarms' => 1,
  169. 'stripattachments' => 1,
  170. 'source' => $source
  171. ],
  172. ]);
  173. $client = $this->createMock(IClient::class);
  174. $this->clientService->expects($this->once())
  175. ->method('newClient')
  176. ->with()
  177. ->willReturn($client);
  178. $this->config->expects($this->once())
  179. ->method('getAppValue')
  180. ->with('dav', 'webcalAllowLocalAccess', 'no')
  181. ->willReturn('no');
  182. $client->expects($this->once())
  183. ->method('get')
  184. ->willThrowException(new LocalServerException());
  185. $this->logger->expects($this->once())
  186. ->method('logException')
  187. ->with($this->isInstanceOf(LocalServerException::class), $this->anything());
  188. $refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
  189. }
  190. public function runLocalURLDataProvider():array {
  191. return [
  192. ['localhost/foo.bar'],
  193. ['localHost/foo.bar'],
  194. ['random-host/foo.bar'],
  195. ['[::1]/bla.blub'],
  196. ['[::]/bla.blub'],
  197. ['192.168.0.1'],
  198. ['172.16.42.1'],
  199. ['[fdf8:f53b:82e4::53]/secret.ics'],
  200. ['[fe80::200:5aee:feaa:20a2]/secret.ics'],
  201. ['[0:0:0:0:0:0:10.0.0.1]/secret.ics'],
  202. ['[0:0:0:0:0:ffff:127.0.0.0]/secret.ics'],
  203. ['10.0.0.1'],
  204. ['another-host.local'],
  205. ['service.localhost'],
  206. ];
  207. }
  208. public function testInvalidUrl() {
  209. $refreshWebcalService = new RefreshWebcalService($this->caldavBackend,
  210. $this->clientService, $this->config, $this->logger);
  211. $this->caldavBackend->expects($this->once())
  212. ->method('getSubscriptionsForUser')
  213. ->with('principals/users/testuser')
  214. ->willReturn([
  215. [
  216. 'id' => 42,
  217. 'uri' => 'sub123',
  218. 'refreshreate' => 'P1H',
  219. 'striptodos' => 1,
  220. 'stripalarms' => 1,
  221. 'stripattachments' => 1,
  222. 'source' => '!@#$'
  223. ],
  224. ]);
  225. $client = $this->createMock(IClient::class);
  226. $this->clientService->expects($this->once())
  227. ->method('newClient')
  228. ->with()
  229. ->willReturn($client);
  230. $this->config->expects($this->once())
  231. ->method('getAppValue')
  232. ->with('dav', 'webcalAllowLocalAccess', 'no')
  233. ->willReturn('no');
  234. $client->expects($this->never())
  235. ->method('get');
  236. $refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
  237. }
  238. }