summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/mime/test/unit/test_message_attachment.js
blob: 653a7078af4b62b0e7af7e8d28da708f8b8d2014 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* 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 test verifies that we generate proper attachment filenames.
 */

var {
  MessageGenerator,
  SyntheticMessageSet,
  SyntheticPartMultiMixed,
  SyntheticPartLeaf,
} = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageGenerator.jsm"
);
var { MessageInjection } = ChromeUtils.import(
  "resource://testing-common/mailnews/MessageInjection.jsm"
);
var { PromiseTestUtils } = ChromeUtils.import(
  "resource://testing-common/mailnews/PromiseTestUtils.jsm"
);

var messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);

// Create a message generator
var msgGen = new MessageGenerator();
var messageInjection = new MessageInjection({ mode: "local" });
var inbox = messageInjection.getInboxFolder();
var msgWindow = Cc["@mozilla.org/messenger/msgwindow;1"].createInstance(
  Ci.nsIMsgWindow
);

// The attachments need to have some content or the stream converter won't
// display them inline. In the case of the email attachment it must have
// trailing CRLFs or it will fail to parse.
const TEXT_ATTACHMENT = "inline text attachment";
const EMAIL_ATTACHMENT = "Subject: fake email\r\n\r\n";
const HTML_ATTACHMENT = "<html><body></body></html>";

add_setup(function () {
  Services.prefs.setBoolPref("mail.inline_attachments.text", true);
});

// Unnamed email attachment.
add_task(async function test_unnamed_email_attachment() {
  await test_message_attachments({
    attachments: [
      {
        body: TEXT_ATTACHMENT,
        filename: "test.txt",
        format: "",
      },
      {
        body: EMAIL_ATTACHMENT,
        expectedFilename: "ForwardedMessage.eml",
        contentType: "message/rfc822",
      },
    ],
  });
});

// Named email attachment.
add_task(async function test_named_email_attachment() {
  await test_message_attachments({
    attachments: [
      {
        body: TEXT_ATTACHMENT,
        filename: "test.txt",
        format: "",
      },
      {
        body: EMAIL_ATTACHMENT,
        filename: "Attached Message",
        contentType: "message/rfc822",
      },
    ],
  });
});

// Escaped html attachment.
add_task(async function test_foo() {
  await test_message_attachments({
    attachments: [
      {
        body: TEXT_ATTACHMENT,
        filename: "test.html",
        format: "",
      },
      {
        body: HTML_ATTACHMENT,
        filename:
          "<iframe src=&quote;http://www.example.com&quote></iframe>.htm",
        expectedFilename:
          "&lt;iframe src=&amp;quote;http://www.example.com&amp;quote&gt;&lt;/iframe&gt;.htm",
        contentType: "text/html;",
      },
    ],
  });
});

// No named email attachment with subject header.
add_task(async function test_no_named_email_attachment_with_subject_header() {
  await test_message_attachments({
    attachments: [
      {
        body: "",
        expectedFilename: "testSubject.eml",
      },
    ],
    bodyPart: new SyntheticPartMultiMixed([
      new SyntheticPartLeaf("plain body text"),
      msgGen.makeMessage({
        subject: "=?UTF-8?B?dGVzdFN1YmplY3Q=?=", // This string is 'testSubject'.
        charset: "UTF-8",
      }),
    ]),
  });
});

async function test_message_attachments(info) {
  let synMsg = msgGen.makeMessage(info);
  let synSet = new SyntheticMessageSet([synMsg]);
  await messageInjection.addSetsToFolders([inbox], [synSet]);

  let msgURI = synSet.getMsgURI(0);
  let msgService = MailServices.messageServiceFromURI(msgURI);

  let streamListener = new PromiseTestUtils.PromiseStreamListener();

  msgService.streamMessage(
    msgURI,
    streamListener,
    msgWindow,
    null,
    true, // Have them create the converter.
    "header=filter",
    false
  );

  let streamedData = await streamListener.promise;

  // Check that the attachments' filenames are as expected. Just use a regex
  // here because it's simple.
  let regex1 =
    /<legend class="moz-mime-attachment-header-name">(.*?)<\/legend>/gi;

  for (let attachment of info.attachments) {
    let match = regex1.exec(streamedData);
    Assert.notEqual(match, null);
    Assert.equal(match[1], attachment.expectedFilename || attachment.filename);
  }
  Assert.equal(regex1.exec(streamedData), null);

  // Check the attachments' filenames are listed for printing.
  let regex2 = /<td class="moz-mime-attachment-file">(.*?)<\/td>/gi;

  for (let attachment of info.attachments) {
    let match = regex2.exec(streamedData);
    Assert.notEqual(match, null);
    Assert.equal(match[1], attachment.expectedFilename || attachment.filename);
  }
  Assert.equal(regex2.exec(streamedData), null);
}

add_task(function endTest() {
  messageInjection.teardownMessageInjection();
});