summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/imap/test/unit/test_trustSpamAssassin.js
blob: 89c223b20643f5e1b32acccadde7e57162799521 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * This file tests recognizing a message as junk due to
 *  SpamAssassin headers, and marking that as good
 *  without having the message return to the junk folder,
 *  as discussed in bug 540385.
 *
 * adapted from test_filterNeedsBody.js
 */

var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

// Globals
var { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);

var gMessage = "SpamAssassinYes"; // message file used as the test message
var gJunkFolder;

add_setup(function () {
  setupIMAPPump();
  let server = IMAPPump.incomingServer;
  let spamSettings = server.spamSettings;
  server.setBoolValue("useServerFilter", true);
  server.setCharValue("serverFilterName", "SpamAssassin");
  server.setIntValue(
    "serverFilterTrustFlags",
    Ci.nsISpamSettings.TRUST_POSITIVES
  );
  server.setBoolValue("moveOnSpam", true);
  server.setIntValue(
    "moveTargetMode",
    Ci.nsISpamSettings.MOVE_TARGET_MODE_ACCOUNT
  );
  server.setCharValue("spamActionTargetAccount", server.serverURI);

  spamSettings.initialize(server);
});

add_task(async function createJunkFolder() {
  IMAPPump.incomingServer.rootFolder.createSubfolder("Junk", null);
  await PromiseTestUtils.promiseFolderAdded("Junk");
  gJunkFolder = IMAPPump.incomingServer.rootFolder.getChildNamed("Junk");
  Assert.ok(gJunkFolder instanceof Ci.nsIMsgImapMailFolder);
  let listener = new PromiseTestUtils.PromiseUrlListener();
  gJunkFolder.updateFolderWithListener(null, listener);
  await listener.promise;
});

/*
 * Load and update a message in the imap fake server, should move
 *  SpamAssassin-marked junk message to junk folder
 */
add_task(async function loadImapMessage() {
  IMAPPump.mailbox.addMessage(
    new ImapMessage(specForFileName(gMessage), IMAPPump.mailbox.uidnext++, [])
  );
  /*
   * The message matched the SpamAssassin header, so it moved
   *  to the junk folder
   */
  IMAPPump.inbox.updateFolder(null);
  await PromiseTestUtils.promiseFolderNotification(
    gJunkFolder,
    "msgsMoveCopyCompleted"
  );
  let listener = new PromiseTestUtils.PromiseUrlListener();
  gJunkFolder.updateFolderWithListener(null, listener);
  await listener.promise;
});

add_task(function testMessageInJunk() {
  Assert.equal(0, IMAPPump.inbox.getTotalMessages(false));
  Assert.equal(1, gJunkFolder.getTotalMessages(false));
});

add_task(async function markMessageAsGood() {
  /*
   * This is done in the application in nsMsgDBView, which is difficult
   *  to test in xpcshell tests. We aren't really trying to test that here
   *  though, since the point of this test is working with the server-based
   *  filters. So I will simply simulate the operations that would typically
   *  be done by a manual marking of the messages.
   */
  let msgHdr = mailTestUtils.firstMsgHdr(gJunkFolder);
  msgHdr.setStringProperty("junkscoreorigin", "user");
  msgHdr.setStringProperty("junkpercent", "0"); // good percent
  msgHdr.setStringProperty("junkscore", "0"); // good score

  /*
   * Now move this message to the inbox. In bug 540385, the message just
   *  gets moved back to the junk folder again. We'll test that we
   *  are now preventing that.
   */
  MailServices.copy.copyMessages(
    gJunkFolder, // srcFolder
    [msgHdr], // messages
    IMAPPump.inbox, // dstFolder
    true, // isMove
    null, // listener
    null, // msgWindow
    false // allowUndo
  );
  await PromiseTestUtils.promiseFolderNotification(
    IMAPPump.inbox,
    "msgsMoveCopyCompleted"
  );
});

add_task(async function updateFoldersAndCheck() {
  let inboxUrlListener = new PromiseTestUtils.PromiseUrlListener();
  IMAPPump.inbox.updateFolderWithListener(null, inboxUrlListener);
  await inboxUrlListener.promise;
  let junkUrlListener = new PromiseTestUtils.PromiseUrlListener();
  gJunkFolder.updateFolderWithListener(null, junkUrlListener);
  await junkUrlListener.promise;
  // bug 540385 causes this test to fail
  Assert.equal(1, IMAPPump.inbox.getTotalMessages(false));
  Assert.equal(0, gJunkFolder.getTotalMessages(false));
});

add_task(function endTest() {
  teardownIMAPPump();
});

/*
 * helper functions
 */

// given a test file, return the file uri spec
function specForFileName(aFileName) {
  let file = do_get_file("../../../data/" + aFileName);
  let msgfileuri = Services.io.newFileURI(file).QueryInterface(Ci.nsIFileURL);
  return msgfileuri.spec;
}

// quick shorthand for output of a line of text.
function dl(text) {
  dump(text + "\n");
}