MailSettingsControllerTest.php 5.7 KB

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