Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AntiSpamServiceIntegrationTest.php « Service « Integration « tests - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f5d14a934ff3cf653bd1a725842aa1f8b49a925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

/**
 * @copyright 2021 Anna Larch <anna@nextcloud.com>
 *
 * @copyright 2021 Anna Larch <anna@nextcloud.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Mail\Tests\Integration\Service;

use Horde_Imap_Client;
use OC;
use OCA\Mail\Account;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Contracts\IMailManager;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper;
use OCA\Mail\Service\AntiSpamService;
use OCA\Mail\Service\Sync\SyncService;
use OCA\Mail\Tests\Integration\Framework\ImapTest;
use OCA\Mail\Tests\Integration\Framework\ImapTestAccount;

class AntiSpamServiceIntegrationTest extends TestCase {
	use ImapTest,
		ImapTestAccount;

	/** @var AntiSpamService */
	private $service;

	public function setUp():void {
		parent::setUp();
		$this->service = OC::$server->get(AntiSpamService::class);
		$this->service->setSpamEmail('spam@domain.tld');
		$this->service->setHamEmail('notspam@domain.tld');
	}

	public function tearDown(): void {
		$this->resetImapAccount();
		$this->service->deleteConfig();
	}

	public function testFlagJunkWithSpamReportActive(): void {
		// First, set up account and retrieve sync token
		$this->resetImapAccount();
		$account = $this->createTestAccount();

		/** @var SyncService $syncService */
		$syncService = OC::$server->get(SyncService::class);
		/** @var ImapMessageMapper $imapMessageMapper */
		$imapMessageMapper = OC::$server->get(ImapMessageMapper::class);
		/** @var MessageMapper $messageMapper */
		$messageMapper = OC::$server->get(MessageMapper::class);
		/** @var IMailManager $mailManager */
		$mailManager = OC::$server->get(IMailManager::class);
		$mailBoxes = $mailManager->getMailboxes(new Account($account));
		$inbox = null;
		foreach ($mailBoxes as $mailBox) {
			if ($mailBox->getName() === 'INBOX') {
				$inbox = $mailBox;
				break;
			}
		}

		// Second, put a new message into the mailbox
		$message = $this->getMessageBuilder()
			->from('buffington@domain.tld')
			->to('user@domain.tld')
			->finish();
		$newUid = $this->saveMessage($inbox->getName(), $message, $account);

		// sync in between creating and flagging otherwise it can't be found
		$syncService->syncMailbox(
			new Account($account),
			$inbox,
			Horde_Imap_Client::SYNC_NEWMSGSUIDS | Horde_Imap_Client::SYNC_FLAGSUIDS | Horde_Imap_Client::SYNC_VANISHEDUIDS,
			null,
			false
		);

		// now we flag this message as junk
		$mailManager->flagMessage(new Account($account), $inbox->getName(), $newUid, 'junk', true);

		// if everything runs through, we can assert the run has been fine,
		// but we can't really test if Listener and Transmission have actually sent the message
		$this->addToAssertionCount(1);

		// now we flag this message as not junk
		$mailManager->flagMessage(new Account($account), $inbox->getName(), $newUid, 'notjunk', true);

		// same as before
		$this->addToAssertionCount(1);
	}
}