AppleProvisioningPluginTest.php 7.9 KB

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