MailSettingsControllerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * @copyright 2014 Lukas Reschke lukas@nextcloud.com
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * This file is licensed under the Affero General Public License version 3 or
  10. * later.
  11. * See the COPYING-README file.
  12. */
  13. namespace Tests\Settings\Controller;
  14. use OC\Mail\Message;
  15. use OC\Settings\Controller\MailSettingsController;
  16. use OCP\AppFramework\Http;
  17. use OCP\IConfig;
  18. use OCP\IL10N;
  19. use OCP\IRequest;
  20. use OCP\IUserSession;
  21. use OCP\Mail\IEMailTemplate;
  22. use OCP\Mail\IMailer;
  23. use OC\User\User;
  24. /**
  25. * @package Tests\Settings\Controller
  26. */
  27. class MailSettingsControllerTest extends \Test\TestCase {
  28. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  29. private $config;
  30. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  31. private $userSession;
  32. /** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
  33. private $mailer;
  34. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  35. private $l;
  36. /** @var MailSettingsController */
  37. private $mailController;
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->l = $this->createMock(IL10N::class);
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->userSession = $this->createMock(IUserSession::class);
  43. $this->mailer = $this->createMock(IMailer::class);
  44. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */
  45. $request = $this->createMock(IRequest::class);
  46. $this->mailController = new MailSettingsController(
  47. 'settings',
  48. $request,
  49. $this->l,
  50. $this->config,
  51. $this->userSession,
  52. $this->mailer,
  53. 'no-reply@nextcloud.com'
  54. );
  55. }
  56. public function testSetMailSettings() {
  57. $this->config->expects($this->exactly(2))
  58. ->method('setSystemValues')
  59. ->withConsecutive(
  60. [[
  61. 'mail_domain' => 'nextcloud.com',
  62. 'mail_from_address' => 'demo@nextcloud.com',
  63. 'mail_smtpmode' => 'smtp',
  64. 'mail_smtpsecure' => 'ssl',
  65. 'mail_smtphost' => 'mx.nextcloud.org',
  66. 'mail_smtpauthtype' => 'NTLM',
  67. 'mail_smtpauth' => 1,
  68. 'mail_smtpport' => '25',
  69. 'mail_sendmailmode' => null,
  70. ]],
  71. [[
  72. 'mail_domain' => 'nextcloud.com',
  73. 'mail_from_address' => 'demo@nextcloud.com',
  74. 'mail_smtpmode' => 'smtp',
  75. 'mail_smtpsecure' => 'ssl',
  76. 'mail_smtphost' => 'mx.nextcloud.org',
  77. 'mail_smtpauthtype' => 'NTLM',
  78. 'mail_smtpauth' => null,
  79. 'mail_smtpport' => '25',
  80. 'mail_smtpname' => null,
  81. 'mail_smtppassword' => null,
  82. 'mail_sendmailmode' => null,
  83. ]]
  84. );
  85. // With authentication
  86. $response = $this->mailController->setMailSettings(
  87. 'nextcloud.com',
  88. 'demo@nextcloud.com',
  89. 'smtp',
  90. 'ssl',
  91. 'mx.nextcloud.org',
  92. 'NTLM',
  93. 1,
  94. '25',
  95. null
  96. );
  97. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  98. // Without authentication (testing the deletion of the stored password)
  99. $response = $this->mailController->setMailSettings(
  100. 'nextcloud.com',
  101. 'demo@nextcloud.com',
  102. 'smtp',
  103. 'ssl',
  104. 'mx.nextcloud.org',
  105. 'NTLM',
  106. 0,
  107. '25',
  108. null
  109. );
  110. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  111. }
  112. public function testStoreCredentials() {
  113. $this->config
  114. ->expects($this->once())
  115. ->method('setSystemValues')
  116. ->with([
  117. 'mail_smtpname' => 'UsernameToStore',
  118. 'mail_smtppassword' => 'PasswordToStore',
  119. ]);
  120. $response = $this->mailController->storeCredentials('UsernameToStore', 'PasswordToStore');
  121. $this->assertSame(Http::STATUS_OK, $response->getStatus());
  122. }
  123. public function testSendTestMail() {
  124. $user = $this->createMock(User::class);
  125. $user->expects($this->any())
  126. ->method('getUID')
  127. ->will($this->returnValue('Werner'));
  128. $user->expects($this->any())
  129. ->method('getDisplayName')
  130. ->will($this->returnValue('Werner Brösel'));
  131. $this->l->expects($this->any())
  132. ->method('t')
  133. ->willReturnCallback(function($text, $parameters = []) {
  134. return vsprintf($text, $parameters);
  135. });
  136. $this->userSession
  137. ->expects($this->any())
  138. ->method('getUser')
  139. ->will($this->returnValue($user));
  140. // Ensure that it fails when no mail address has been specified
  141. $response = $this->mailController->sendTestMail();
  142. $this->assertSame(Http::STATUS_BAD_REQUEST, $response->getStatus());
  143. $this->assertSame('You need to set your user email before being able to send test emails.', $response->getData());
  144. // If no exception is thrown it should work
  145. $this->config
  146. ->expects($this->any())
  147. ->method('getUserValue')
  148. ->will($this->returnValue('mail@example.invalid'));
  149. $this->mailer->expects($this->once())
  150. ->method('createMessage')
  151. ->willReturn($this->createMock(Message::class));
  152. $emailTemplate = $this->createMock(IEMailTemplate::class);
  153. $this->mailer
  154. ->expects($this->once())
  155. ->method('createEMailTemplate')
  156. ->willReturn($emailTemplate);
  157. $response = $this->mailController->sendTestMail();
  158. $this->assertSame(Http::STATUS_OK, $response->getStatus(), $response->getData());
  159. }
  160. }