選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

MailSettingsControllerTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @copyright 2014 Lukas Reschke lukas@nextcloud.com
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Settings\Tests\Controller;
  30. use OC\Mail\Message;
  31. use OC\User\User;
  32. use OCA\Settings\Controller\MailSettingsController;
  33. use OCP\AppFramework\Http;
  34. use OCP\IConfig;
  35. use OCP\IL10N;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use OCP\Mail\IEMailTemplate;
  39. use OCP\Mail\IMailer;
  40. /**
  41. * @package Tests\Settings\Controller
  42. */
  43. class MailSettingsControllerTest extends \Test\TestCase {
  44. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  45. private $config;
  46. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  47. private $userSession;
  48. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  49. private $mailer;
  50. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  51. private $l;
  52. /** @var MailSettingsController */
  53. private $mailController;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->l = $this->createMock(IL10N::class);
  57. $this->config = $this->createMock(IConfig::class);
  58. $this->userSession = $this->createMock(IUserSession::class);
  59. $this->mailer = $this->createMock(IMailer::class);
  60. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  61. $request = $this->createMock(IRequest::class);
  62. $this->mailController = new MailSettingsController(
  63. 'settings',
  64. $request,
  65. $this->l,
  66. $this->config,
  67. $this->userSession,
  68. $this->mailer,
  69. 'no-reply@nextcloud.com'
  70. );
  71. }
  72. public function testSetMailSettings() {
  73. $this->config->expects($this->exactly(2))
  74. ->method('setSystemValues')
  75. ->withConsecutive(
  76. [[
  77. 'mail_domain' => 'nextcloud.com',
  78. 'mail_from_address' => 'demo@nextcloud.com',
  79. 'mail_smtpmode' => 'smtp',
  80. 'mail_smtpsecure' => 'ssl',
  81. 'mail_smtphost' => 'mx.nextcloud.org',
  82. 'mail_smtpauthtype' => 'NTLM',
  83. 'mail_smtpauth' => 1,
  84. 'mail_smtpport' => '25',
  85. 'mail_sendmailmode' => null,
  86. ]],
  87. [[
  88. 'mail_domain' => 'nextcloud.com',
  89. 'mail_from_address' => 'demo@nextcloud.com',
  90. 'mail_smtpmode' => 'smtp',
  91. 'mail_smtpsecure' => 'ssl',
  92. 'mail_smtphost' => 'mx.nextcloud.org',
  93. 'mail_smtpauthtype' => 'NTLM',
  94. 'mail_smtpauth' => null,
  95. 'mail_smtpport' => '25',
  96. 'mail_smtpname' => null,
  97. 'mail_smtppassword' => null,
  98. 'mail_sendmailmode' => null,
  99. ]]
  100. );
  101. // With authentication
  102. $response = $this->mailController->setMailSettings(
  103. 'nextcloud.com',
  104. 'demo@nextcloud.com',
  105. 'smtp',
  106. 'ssl',
  107. 'mx.nextcloud.org',
  108. 'NTLM',
  109. 1,
  110. '25',
  111. null
  112. );
  113. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  114. // Without authentication (testing the deletion of the stored password)
  115. $response = $this->mailController->setMailSettings(
  116. 'nextcloud.com',
  117. 'demo@nextcloud.com',
  118. 'smtp',
  119. 'ssl',
  120. 'mx.nextcloud.org',
  121. 'NTLM',
  122. 0,
  123. '25',
  124. null
  125. );
  126. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  127. }
  128. public function testStoreCredentials() {
  129. $this->config
  130. ->expects($this->once())
  131. ->method('setSystemValues')
  132. ->with([
  133. 'mail_smtpname' => 'UsernameToStore',
  134. 'mail_smtppassword' => 'PasswordToStore',
  135. ]);
  136. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  137. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  138. }
  139. public function testSendTestMail() {
  140. $user = $this->createMock(User::class);
  141. $user->expects($this->any())
  142. ->method('getUID')
  143. ->willReturn('Werner');
  144. $user->expects($this->any())
  145. ->method('getDisplayName')
  146. ->willReturn('Werner Brösel');
  147. $this->l->expects($this->any())
  148. ->method('t')
  149. ->willReturnCallback(function ($text, $parameters = []) {
  150. return vsprintf($text, $parameters);
  151. });
  152. $this->userSession
  153. ->expects($this->any())
  154. ->method('getUser')
  155. ->willReturn($user);
  156. // Ensure that it fails when no mail address has been specified
  157. $response = $this->mailController->sendTestMail();
  158. $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
  159. $this->assertSame('You need to set your user email before being able to send test emails.', $response->getData());
  160. // If no exception is thrown it should work
  161. $this->config
  162. ->expects($this->any())
  163. ->method('getUserValue')
  164. ->willReturn('mail@example.invalid');
  165. $this->mailer->expects($this->once())
  166. ->method('createMessage')
  167. ->willReturn($this->createMock(Message::class));
  168. $emailTemplate = $this->createMock(IEMailTemplate::class);
  169. $this->mailer
  170. ->expects($this->once())
  171. ->method('createEMailTemplate')
  172. ->willReturn($emailTemplate);
  173. $response = $this->mailController->sendTestMail();
  174. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  175. }
  176. }