AppleProvisioningPluginTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\Provisioning\Apple;
  7. use OCA\DAV\Provisioning\Apple\AppleProvisioningPlugin;
  8. use OCA\Theming\ThemingDefaults;
  9. use OCP\IL10N;
  10. use OCP\IRequest;
  11. use OCP\IURLGenerator;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. use Test\TestCase;
  15. class AppleProvisioningPluginTest extends TestCase {
  16. /** @var \Sabre\DAV\Server|\PHPUnit\Framework\MockObject\MockObject */
  17. protected $server;
  18. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  19. protected $userSession;
  20. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  21. protected $urlGenerator;
  22. /** @var ThemingDefaults|\PHPUnit\Framework\MockObject\MockObject */
  23. protected $themingDefaults;
  24. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  25. protected $request;
  26. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $l10n;
  28. /** @var \Sabre\HTTP\RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
  29. protected $sabreRequest;
  30. /** @var \Sabre\HTTP\ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
  31. protected $sabreResponse;
  32. /** @var AppleProvisioningPlugin */
  33. protected $plugin;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->server = $this->createMock(\Sabre\DAV\Server::class);
  37. $this->userSession = $this->createMock(IUserSession::class);
  38. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  39. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  40. $this->request = $this->createMock(IRequest::class);
  41. $this->l10n = $this->createMock(IL10N::class);
  42. $this->plugin = new AppleProvisioningPlugin($this->userSession,
  43. $this->urlGenerator,
  44. $this->themingDefaults,
  45. $this->request,
  46. $this->l10n,
  47. function () {
  48. return 'generated-uuid';
  49. }
  50. );
  51. $this->sabreRequest = $this->createMock(\Sabre\HTTP\RequestInterface::class);
  52. $this->sabreResponse = $this->createMock(\Sabre\HTTP\ResponseInterface::class);
  53. }
  54. public function testInitialize(): void {
  55. $server = $this->createMock(\Sabre\DAV\Server::class);
  56. $plugin = new AppleProvisioningPlugin($this->userSession,
  57. $this->urlGenerator, $this->themingDefaults, $this->request, $this->l10n,
  58. function (): void {
  59. });
  60. $server->expects($this->once())
  61. ->method('on')
  62. ->with('method:GET', [$plugin, 'httpGet'], 90);
  63. $plugin->initialize($server);
  64. }
  65. public function testHttpGetOnHttp(): void {
  66. $this->sabreRequest->expects($this->once())
  67. ->method('getPath')
  68. ->with()
  69. ->willReturn('provisioning/apple-provisioning.mobileconfig');
  70. $user = $this->createMock(IUser::class);
  71. $this->userSession->expects($this->once())
  72. ->method('getUser')
  73. ->willReturn($user);
  74. $this->request->expects($this->once())
  75. ->method('getServerProtocol')
  76. ->wilLReturn('http');
  77. $this->themingDefaults->expects($this->once())
  78. ->method('getName')
  79. ->willReturn('InstanceName');
  80. $this->l10n->expects($this->once())
  81. ->method('t')
  82. ->with('Your %s needs to be configured to use HTTPS in order to use CalDAV and CardDAV with iOS/macOS.', ['InstanceName'])
  83. ->willReturn('LocalizedErrorMessage');
  84. $this->sabreResponse->expects($this->once())
  85. ->method('setStatus')
  86. ->with(200);
  87. $this->sabreResponse->expects($this->once())
  88. ->method('setHeader')
  89. ->with('Content-Type', 'text/plain; charset=utf-8');
  90. $this->sabreResponse->expects($this->once())
  91. ->method('setBody')
  92. ->with('LocalizedErrorMessage');
  93. $returnValue = $this->plugin->httpGet($this->sabreRequest, $this->sabreResponse);
  94. $this->assertFalse($returnValue);
  95. }
  96. public function testHttpGetOnHttps(): void {
  97. $this->sabreRequest->expects($this->once())
  98. ->method('getPath')
  99. ->with()
  100. ->willReturn('provisioning/apple-provisioning.mobileconfig');
  101. $user = $this->createMock(IUser::class);
  102. $user->expects($this->once())
  103. ->method('getUID')
  104. ->willReturn('userName');
  105. $this->userSession->expects($this->once())
  106. ->method('getUser')
  107. ->willReturn($user);
  108. $this->request->expects($this->once())
  109. ->method('getServerProtocol')
  110. ->wilLReturn('https');
  111. $this->urlGenerator->expects($this->once())
  112. ->method('getBaseUrl')
  113. ->willReturn('https://nextcloud.tld/nextcloud');
  114. $this->themingDefaults->expects($this->once())
  115. ->method('getName')
  116. ->willReturn('InstanceName');
  117. $this->l10n->expects($this->exactly(2))
  118. ->method('t')
  119. ->withConsecutive(
  120. ['Configures a CalDAV account'],
  121. ['Configures a CardDAV account'],
  122. )
  123. ->willReturnOnConsecutiveCalls(
  124. 'LocalizedConfiguresCalDAV',
  125. 'LocalizedConfiguresCardDAV',
  126. );
  127. $this->sabreResponse->expects($this->once())
  128. ->method('setStatus')
  129. ->with(200);
  130. $this->sabreResponse->expects($this->exactly(2))
  131. ->method('setHeader')
  132. ->withConsecutive(
  133. ['Content-Disposition', 'attachment; filename="userName-apple-provisioning.mobileconfig"'],
  134. ['Content-Type', 'application/xml; charset=utf-8'],
  135. );
  136. $this->sabreResponse->expects($this->once())
  137. ->method('setBody')
  138. ->with(<<<EOF
  139. <?xml version="1.0" encoding="UTF-8"?>
  140. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  141. <plist version="1.0">
  142. <dict>
  143. <key>PayloadContent</key>
  144. <array>
  145. <dict>
  146. <key>CalDAVAccountDescription</key>
  147. <string>InstanceName</string>
  148. <key>CalDAVHostName</key>
  149. <string>nextcloud.tld</string>
  150. <key>CalDAVUsername</key>
  151. <string>userName</string>
  152. <key>CalDAVUseSSL</key>
  153. <true/>
  154. <key>CalDAVPort</key>
  155. <integer>443</integer>
  156. <key>PayloadDescription</key>
  157. <string>LocalizedConfiguresCalDAV</string>
  158. <key>PayloadDisplayName</key>
  159. <string>InstanceName CalDAV</string>
  160. <key>PayloadIdentifier</key>
  161. <string>tld.nextcloud.generated-uuid</string>
  162. <key>PayloadType</key>
  163. <string>com.apple.caldav.account</string>
  164. <key>PayloadUUID</key>
  165. <string>generated-uuid</string>
  166. <key>PayloadVersion</key>
  167. <integer>1</integer>
  168. </dict>
  169. <dict>
  170. <key>CardDAVAccountDescription</key>
  171. <string>InstanceName</string>
  172. <key>CardDAVHostName</key>
  173. <string>nextcloud.tld</string>
  174. <key>CardDAVUsername</key>
  175. <string>userName</string>
  176. <key>CardDAVUseSSL</key>
  177. <true/>
  178. <key>CardDAVPort</key>
  179. <integer>443</integer>
  180. <key>PayloadDescription</key>
  181. <string>LocalizedConfiguresCardDAV</string>
  182. <key>PayloadDisplayName</key>
  183. <string>InstanceName CardDAV</string>
  184. <key>PayloadIdentifier</key>
  185. <string>tld.nextcloud.generated-uuid</string>
  186. <key>PayloadType</key>
  187. <string>com.apple.carddav.account</string>
  188. <key>PayloadUUID</key>
  189. <string>generated-uuid</string>
  190. <key>PayloadVersion</key>
  191. <integer>1</integer>
  192. </dict>
  193. </array>
  194. <key>PayloadDisplayName</key>
  195. <string>InstanceName</string>
  196. <key>PayloadIdentifier</key>
  197. <string>tld.nextcloud.generated-uuid</string>
  198. <key>PayloadRemovalDisallowed</key>
  199. <false/>
  200. <key>PayloadType</key>
  201. <string>Configuration</string>
  202. <key>PayloadUUID</key>
  203. <string>generated-uuid</string>
  204. <key>PayloadVersion</key>
  205. <integer>1</integer>
  206. </dict>
  207. </plist>
  208. EOF
  209. );
  210. $returnValue = $this->plugin->httpGet($this->sabreRequest, $this->sabreResponse);
  211. $this->assertFalse($returnValue);
  212. }
  213. }