1
0

MailSettingsControllerTest.php 5.7 KB

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\IURLGenerator;
  38. use OCP\IUserSession;
  39. use OCP\Mail\IEMailTemplate;
  40. use OCP\Mail\IMailer;
  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_smtpauth' => 1,
  88. 'mail_smtpport' => '25',
  89. 'mail_sendmailmode' => null,
  90. ]],
  91. [[
  92. 'mail_domain' => 'nextcloud.com',
  93. 'mail_from_address' => 'demo@nextcloud.com',
  94. 'mail_smtpmode' => 'smtp',
  95. 'mail_smtpsecure' => 'ssl',
  96. 'mail_smtphost' => 'mx.nextcloud.org',
  97. 'mail_smtpauth' => null,
  98. 'mail_smtpport' => '25',
  99. 'mail_smtpname' => null,
  100. 'mail_smtppassword' => null,
  101. 'mail_sendmailmode' => null,
  102. ]]
  103. );
  104. // With authentication
  105. $response = $this->mailController->setMailSettings(
  106. 'nextcloud.com',
  107. 'demo@nextcloud.com',
  108. 'smtp',
  109. 'ssl',
  110. 'mx.nextcloud.org',
  111. 1,
  112. '25',
  113. null
  114. );
  115. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  116. // Without authentication (testing the deletion of the stored password)
  117. $response = $this->mailController->setMailSettings(
  118. 'nextcloud.com',
  119. 'demo@nextcloud.com',
  120. 'smtp',
  121. 'ssl',
  122. 'mx.nextcloud.org',
  123. 0,
  124. '25',
  125. null
  126. );
  127. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  128. }
  129. public function testStoreCredentials() {
  130. $this->config
  131. ->expects($this->once())
  132. ->method('setSystemValues')
  133. ->with([
  134. 'mail_smtpname' => 'UsernameToStore',
  135. 'mail_smtppassword' => 'PasswordToStore',
  136. ]);
  137. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  138. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  139. }
  140. public function testSendTestMail() {
  141. $user = $this->createMock(User::class);
  142. $user->expects($this->any())
  143. ->method('getUID')
  144. ->willReturn('Werner');
  145. $user->expects($this->any())
  146. ->method('getDisplayName')
  147. ->willReturn('Werner Brösel');
  148. $this->l->expects($this->any())
  149. ->method('t')
  150. ->willReturnCallback(function ($text, $parameters = []) {
  151. return vsprintf($text, $parameters);
  152. });
  153. $this->userSession
  154. ->expects($this->any())
  155. ->method('getUser')
  156. ->willReturn($user);
  157. // Ensure that it fails when no mail address has been specified
  158. $response = $this->mailController->sendTestMail();
  159. $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
  160. $this->assertSame('You need to set your account email before being able to send test emails. Go to for that.', $response->getData());
  161. // If no exception is thrown it should work
  162. $this->config
  163. ->expects($this->any())
  164. ->method('getUserValue')
  165. ->willReturn('mail@example.invalid');
  166. $this->mailer->expects($this->once())
  167. ->method('createMessage')
  168. ->willReturn($this->createMock(Message::class));
  169. $emailTemplate = $this->createMock(IEMailTemplate::class);
  170. $this->mailer
  171. ->expects($this->once())
  172. ->method('createEMailTemplate')
  173. ->willReturn($emailTemplate);
  174. $response = $this->mailController->sendTestMail();
  175. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  176. }
  177. }