summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/mime/test/unit/test_hidden_attachments.js
blob: 1203c9a8ea5ee0db95827cd3b12343b653b008bb (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* 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 creates some messages with attachments of different types and
 * checks that libmime emits (or doesn't emit) the attachments as appropriate.
 */

var { MessageGenerator, SyntheticMessageSet } = 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 { TestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TestUtils.sys.mjs"
);

var messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
var messageGenerator = new MessageGenerator();
var messageInjection = new MessageInjection({ mode: "local" });
var inbox = messageInjection.getInboxFolder();

add_task(async function test_without_attachment() {
  await test_message_attachments({});
});

/* Attachments with Content-Disposition: attachment */
// inline-able attachment with a name
add_task(
  async function test_content_disposition_attachment_inlineable_attachment_with_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "ubik.txt",
          disposition: "attachment",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: attachment */
// inline-able attachment with no name
add_task(
  async function test_content_disposition_attachment_inlineable_attachment_no_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "",
          disposition: "attachment",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: attachment */
// non-inline-able attachment with a name
add_task(
  async function test_content_disposition_attachment_non_inlineable_attachment_with_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "ubik.ubk",
          disposition: "attachment",
          contentType: "application/x-ubik",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: attachment */
// non-inline-able attachment with no name
add_task(
  async function test_content_disposition_attachment_non_inlineable_attachment_no_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "",
          disposition: "attachment",
          contentType: "application/x-ubik",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: inline */
// inline-able attachment with a name
add_task(
  async function test_content_disposition_inline_inlineable_attachment_with_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "ubik.txt",
          disposition: "inline",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: inline */
// inline-able attachment with no name
add_task(
  async function test_content_disposition_inline_inlineable_attachment_no_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "",
          disposition: "inline",
          format: "",
          shouldShow: false,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: inline */
// non-inline-able attachment with a name
add_task(
  async function test_content_disposition_inline_non_inlineable_attachment_with_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "ubik.ubk",
          disposition: "inline",
          contentType: "application/x-ubik",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

/* Attachments with Content-Disposition: inline */
// non-inline-able attachment with no name
add_task(
  async function test_content_disposition_inline_non_inlineable_attachment_no_name() {
    await test_message_attachments({
      attachments: [
        {
          body: "attachment",
          filename: "",
          disposition: "inline",
          contentType: "application/x-ubik",
          format: "",
          shouldShow: true,
        },
      ],
    });
  }
);

async function test_message_attachments(info) {
  let synMsg = messageGenerator.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({
    onStopRequest(request, status) {
      request.QueryInterface(Ci.nsIMailChannel);
      let expectedAttachments = (info.attachments || [])
        .filter(i => i.shouldShow)
        .map(i => i.filename);
      Assert.equal(request.attachments.length, expectedAttachments.length);

      for (let i = 0; i < request.attachments.length; i++) {
        // If the expected attachment's name is empty, we probably generated a
        // name like "Part 1.2", so don't bother checking that the names match
        // (they won't).
        if (expectedAttachments[i]) {
          Assert.equal(
            request.attachments[i].getProperty("displayName"),
            expectedAttachments[i]
          );
        }
      }
    },
  });
  msgService.streamMessage(
    msgURI,
    streamListener,
    null,
    null,
    true, // have them create the converter
    // additional uri payload, note that "header=" is prepended automatically
    "filter",
    false
  );

  await streamListener.promise;
}