MailSettingsControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. use OCP\IURLGenerator;
  41. /**
  42. * @package Tests\Settings\Controller
  43. */
  44. class MailSettingsControllerTest extends \Test\TestCase {
  45. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  46. private $config;
  47. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  48. private $userSession;
  49. /** @var IMailer|\PHPUnit\Framework\MockObject\MockObject */
  50. private $mailer;
  51. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  52. private $l;
  53. /** @var IURLGenerator */
  54. private $urlGenerator;
  55. /** @var MailSettingsController */
  56. private $mailController;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->l = $this->createMock(IL10N::class);
  60. $this->config = $this->createMock(IConfig::class);
  61. $this->userSession = $this->createMock(IUserSession::class);
  62. $this->mailer = $this->createMock(IMailer::class);
  63. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  64. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  65. $request = $this->createMock(IRequest::class);
  66. $this->mailController = new MailSettingsController(
  67. 'settings',
  68. $request,
  69. $this->l,
  70. $this->config,
  71. $this->userSession,
  72. $this->urlGenerator,
  73. $this->mailer,
  74. 'no-reply@nextcloud.com'
  75. );
  76. }
  77. public function testSetMailSettings() {
  78. $this->config->expects($this->exactly(2))
  79. ->method('setSystemValues')
  80. ->withConsecutive(
  81. [[
  82. 'mail_domain' => 'nextcloud.com',
  83. 'mail_from_address' => 'demo@nextcloud.com',
  84. 'mail_smtpmode' => 'smtp',
  85. 'mail_smtpsecure' => 'ssl',
  86. 'mail_smtphost' => 'mx.nextcloud.org',
  87. 'mail_smtpauthtype' => 'NTLM',
  88. 'mail_smtpauth' => 1,
  89. 'mail_smtpport' => '25',
  90. 'mail_sendmailmode' => null,
  91. ]],
  92. [[
  93. 'mail_domain' => 'nextcloud.com',
  94. 'mail_from_address' => 'demo@nextcloud.com',
  95. 'mail_smtpmode' => 'smtp',
  96. 'mail_smtpsecure' => 'ssl',
  97. 'mail_smtphost' => 'mx.nextcloud.org',
  98. 'mail_smtpauthtype' => 'NTLM',
  99. 'mail_smtpauth' => null,
  100. 'mail_smtpport' => '25',
  101. 'mail_smtpname' => null,
  102. 'mail_smtppassword' => null,
  103. 'mail_sendmailmode' => null,
  104. ]]
  105. );
  106. // With authentication
  107. $response = $this->mailController->setMailSettings(
  108. 'nextcloud.com',
  109. 'demo@nextcloud.com',
  110. 'smtp',
  111. 'ssl',
  112. 'mx.nextcloud.org',
  113. 'NTLM',
  114. 1,
  115. '25',
  116. null
  117. );
  118. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  119. // Without authentication (testing the deletion of the stored password)
  120. $response = $this->mailController->setMailSettings(
  121. 'nextcloud.com',
  122. 'demo@nextcloud.com',
  123. 'smtp',
  124. 'ssl',
  125. 'mx.nextcloud.org',
  126. 'NTLM',
  127. 0,
  128. '25',
  129. null
  130. );
  131. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  132. }
  133. public function testStoreCredentials() {
  134. $this->config
  135. ->expects($this->once())
  136. ->method('setSystemValues')
  137. ->with([
  138. 'mail_smtpname' => 'UsernameToStore',
  139. 'mail_smtppassword' => 'PasswordToStore',
  140. ]);
  141. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  142. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  143. }
  144. public function testSendTestMail() {
  145. $user = $this->createMock(User::class);
  146. $user->expects($this->any())
  147. ->method('getUID')
  148. ->willReturn('Werner');
  149. $user->expects($this->any())
  150. ->method('getDisplayName')
  151. ->willReturn('Werner Brösel');
  152. $this->l->expects($this->any())
  153. ->method('t')
  154. ->willReturnCallback(function ($text, $parameters = []) {
  155. return vsprintf($text, $parameters);
  156. });
  157. $this->userSession
  158. ->expects($this->any())
  159. ->method('getUser')
  160. ->willReturn($user);
  161. // Ensure that it fails when no mail address has been specified
  162. $response = $this->mailController->sendTestMail();
  163. $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
  164. $this->assertSame('You need to set your user email before being able to send test emails. Go to for that.', $response->getData());
  165. // If no exception is thrown it should work
  166. $this->config
  167. ->expects($this->any())
  168. ->method('getUserValue')
  169. ->willReturn('mail@example.invalid');
  170. $this->mailer->expects($this->once())
  171. ->method('createMessage')
  172. ->willReturn($this->createMock(Message::class));
  173. $emailTemplate = $this->createMock(IEMailTemplate::class);
  174. $this->mailer
  175. ->expects($this->once())
  176. ->method('createEMailTemplate')
  177. ->willReturn($emailTemplate);
  178. $response = $this->mailController->sendTestMail();
  179. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  180. }
  181. }