AppleProvisioningPluginTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Nils Wittenbrink <nilswittenbrink@web.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Provisioning\Apple;
  28. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  29. use OCA\Theming\ThemingDefaults;
  30. use OCP\IL10N;
  31. use OCP\IRequest;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserSession;
  35. use Test\TestCase;
  36. class AppleProvisioningPluginTest extends TestCase {
  37. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $server;
  39. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $userSession;
  41. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  42. protected $urlGenerator;
  43. /** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
  44. protected $themingDefaults;
  45. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  46. protected $request;
  47. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  48. protected $l10n;
  49. /** @var \Sabre\HTTP\RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
  50. protected $sabreRequest;
  51. /** @var \Sabre\HTTP\ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
  52. protected $sabreResponse;
  53. /** @var AppleProvisioningPlugin */
  54. protected $plugin;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  58. $this->userSession = $this->createMock(IUserSession::class);
  59. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  60. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  61. $this->request = $this->createMock(IRequest::class);
  62. $this->l10n = $this->createMock(IL10N::class);
  63. $this->plugin = new AppleProvisioningPlugin($this->userSession,
  64. $this->urlGenerator,
  65. $this->themingDefaults,
  66. $this->request,
  67. $this->l10n,
  68. function () {
  69. return 'generated-uuid';
  70. }
  71. );
  72. $this->sabreRequest = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  73. $this->sabreResponse = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  74. }
  75. public function testInitialize(): void {
  76. $server = $this->createMock(\Sabre\DAV\Server::class);
  77. $plugin = new AppleProvisioningPlugin($this->userSession,
  78. $this->urlGenerator, $this->themingDefaults, $this->request, $this->l10n,
  79. function (): void {
  80. });
  81. $server->expects($this->once())
  82. ->method('on')
  83. ->with('method:GET', [$plugin, 'httpGet'], 90);
  84. $plugin->initialize($server);
  85. }
  86. public function testHttpGetOnHttp(): void {
  87. $this->sabreRequest->expects($this->once())
  88. ->method('getPath')
  89. ->with()
  90. ->willReturn('provisioning/apple-provisioning.mobileconfig');
  91. $user = $this->createMock(IUser::class);
  92. $this->userSession->expects($this->once())
  93. ->method('getUser')
  94. ->willReturn($user);
  95. $this->request->expects($this->once())
  96. ->method('getServerProtocol')
  97. ->wilLReturn('http');
  98. $this->themingDefaults->expects($this->once())
  99. ->method('getName')
  100. ->willReturn('InstanceName');
  101. $this->l10n->expects($this->once())
  102. ->method('t')
  103. ->with('Your %s needs to be configured to use HTTPS in order to use CalDAV and CardDAV with iOS/macOS.', ['InstanceName'])
  104. ->willReturn('LocalizedErrorMessage');
  105. $this->sabreResponse->expects($this->once())
  106. ->method('setStatus')
  107. ->with(200);
  108. $this->sabreResponse->expects($this->once())
  109. ->method('setHeader')
  110. ->with('Content-Type', 'text/plain; charset=utf-8');
  111. $this->sabreResponse->expects($this->once())
  112. ->method('setBody')
  113. ->with('LocalizedErrorMessage');
  114. $returnValue = $this->plugin->httpGet($this->sabreRequest, $this->sabreResponse);
  115. $this->assertFalse($returnValue);
  116. }
  117. public function testHttpGetOnHttps(): void {
  118. $this->sabreRequest->expects($this->once())
  119. ->method('getPath')
  120. ->with()
  121. ->willReturn('provisioning/apple-provisioning.mobileconfig');
  122. $user = $this->createMock(IUser::class);
  123. $user->expects($this->once())
  124. ->method('getUID')
  125. ->willReturn('userName');
  126. $this->userSession->expects($this->once())
  127. ->method('getUser')
  128. ->willReturn($user);
  129. $this->request->expects($this->once())
  130. ->method('getServerProtocol')
  131. ->wilLReturn('https');
  132. $this->urlGenerator->expects($this->once())
  133. ->method('getBaseUrl')
  134. ->willReturn('https://nextcloud.tld/nextcloud');
  135. $this->themingDefaults->expects($this->once())
  136. ->method('getName')
  137. ->willReturn('InstanceName');
  138. $this->l10n->expects($this->exactly(2))
  139. ->method('t')
  140. ->withConsecutive(
  141. ['Configures a CalDAV account'],
  142. ['Configures a CardDAV account'],
  143. )
  144. ->willReturnOnConsecutiveCalls(
  145. 'LocalizedConfiguresCalDAV',
  146. 'LocalizedConfiguresCardDAV',
  147. );
  148. $this->sabreResponse->expects($this->once())
  149. ->method('setStatus')
  150. ->with(200);
  151. $this->sabreResponse->expects($this->exactly(2))
  152. ->method('setHeader')
  153. ->withConsecutive(
  154. ['Content-Disposition', 'attachment; filename="userName-apple-provisioning.mobileconfig"'],
  155. ['Content-Type', 'application/xml; charset=utf-8'],
  156. );
  157. $this->sabreResponse->expects($this->once())
  158. ->method('setBody')
  159. ->with(<<<EOF
  160. <?xml version="1.0" encoding="UTF-8"?>
  161. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  162. <plist version="1.0">
  163. <dict>
  164. <key>PayloadContent</key>
  165. <array>
  166. <dict>
  167. <key>CalDAVAccountDescription</key>
  168. <string>InstanceName</string>
  169. <key>CalDAVHostName</key>
  170. <string>nextcloud.tld</string>
  171. <key>CalDAVUsername</key>
  172. <string>userName</string>
  173. <key>CalDAVUseSSL</key>
  174. <true/>
  175. <key>CalDAVPort</key>
  176. <integer>443</integer>
  177. <key>PayloadDescription</key>
  178. <string>LocalizedConfiguresCalDAV</string>
  179. <key>PayloadDisplayName</key>
  180. <string>InstanceName CalDAV</string>
  181. <key>PayloadIdentifier</key>
  182. <string>tld.nextcloud.generated-uuid</string>
  183. <key>PayloadType</key>
  184. <string>com.apple.caldav.account</string>
  185. <key>PayloadUUID</key>
  186. <string>generated-uuid</string>
  187. <key>PayloadVersion</key>
  188. <integer>1</integer>
  189. </dict>
  190. <dict>
  191. <key>CardDAVAccountDescription</key>
  192. <string>InstanceName</string>
  193. <key>CardDAVHostName</key>
  194. <string>nextcloud.tld</string>
  195. <key>CardDAVUsername</key>
  196. <string>userName</string>
  197. <key>CardDAVUseSSL</key>
  198. <true/>
  199. <key>CardDAVPort</key>
  200. <integer>443</integer>
  201. <key>PayloadDescription</key>
  202. <string>LocalizedConfiguresCardDAV</string>
  203. <key>PayloadDisplayName</key>
  204. <string>InstanceName CardDAV</string>
  205. <key>PayloadIdentifier</key>
  206. <string>tld.nextcloud.generated-uuid</string>
  207. <key>PayloadType</key>
  208. <string>com.apple.carddav.account</string>
  209. <key>PayloadUUID</key>
  210. <string>generated-uuid</string>
  211. <key>PayloadVersion</key>
  212. <integer>1</integer>
  213. </dict>
  214. </array>
  215. <key>PayloadDisplayName</key>
  216. <string>InstanceName</string>
  217. <key>PayloadIdentifier</key>
  218. <string>tld.nextcloud.generated-uuid</string>
  219. <key>PayloadRemovalDisallowed</key>
  220. <false/>
  221. <key>PayloadType</key>
  222. <string>Configuration</string>
  223. <key>PayloadUUID</key>
  224. <string>generated-uuid</string>
  225. <key>PayloadVersion</key>
  226. <integer>1</integer>
  227. </dict>
  228. </plist>
  229. EOF
  230. );
  231. $returnValue = $this->plugin->httpGet($this->sabreRequest, $this->sabreResponse);
  232. $this->assertFalse($returnValue);
  233. }
  234. }