summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/test/unit/test_attachmentChecker.js
blob: b72781923156aace54fdeb4179681fe2860e0b0d (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

/*
 * Test suite for the attachmentChecker class
 *
 * Currently tested:
 * - getAttachmentKeywords function.
 */

// Globals

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

/*
 * UTILITIES
 */

function assert(aBeTrue, aWhy) {
  if (!aBeTrue) {
    do_throw(aWhy);
  }
}

function assert_equal(aA, aB, aWhy) {
  assert(
    aA == aB,
    aWhy +
      " (" +
      unescape(encodeURIComponent(aA)) +
      " != " +
      unescape(encodeURIComponent(aB)) +
      ")."
  );
}

/*
 * TESTS
 */

function test_getAttachmentKeywords(desc, mailData, keywords, expected) {
  let result = AttachmentChecker.getAttachmentKeywords(mailData, keywords);
  assert_equal(result, expected, desc + " not equal!");
}

var tests = [
  // Desc, mail body Data, keywords to search for, expected keywords found.
  ["Simple keyword", "latte.ca", "latte", "latte"],
  ["Extension", "testing document.pdf", ".pdf", "document.pdf"],
  [
    "Two Extensions",
    "testing document.pdf and test.pdf",
    ".pdf",
    "document.pdf,test.pdf",
  ],
  [
    "Two+one Extensions",
    "testing document.pdf and test.pdf and again document.pdf",
    ".pdf",
    "document.pdf,test.pdf",
  ],
  ["Url", "testing http://document.pdf", ".pdf", ""],
  ["Both", "testing http://document.pdf test.pdf", ".pdf", "test.pdf"],
  ["Greek", "This is a Θεωρία test", "Θεωρία,is", "Θεωρία,is"],
  ["Greek missing", "This a Θεωρίαω test", "Θεωρία", ""],
  ["Greek and punctuation", "This a:Θεωρία-test", "Θεωρία", "Θεωρία"],
  ["Greek and Japanese", "This a 添Θεωρία付 test", "Θεωρία", "Θεωρία"],
  ["Japanese", "This is 添付! test", "Θεωρία,添付", "添付"],
  ["More Japanese", "添付mailを送る", "添付,cv", "添付"],
  ["Japanese and English", "添付mailを送る", "添付,mail", "添付,mail"],
  ["Japanese and English Mixed", "添付mailを送る", "添付mail", "添付mail"],
  ["Japanese and English Mixed missing", "添付mailing", "添付mail", ""],
  ["Japanese trailers", "This is 添添付付! test", "Θεωρία,添付", "添付"],
  ["Multi-lang", "cv添付Θεωρία", "Θεωρία,添付,cv", "Θεωρία,添付,cv"],
  [
    "Should match",
    "I've attached the http/test.pdf file",
    ".pdf",
    "http/test.pdf",
  ],
  ["Should still fail", "a https://test.pdf a", ".pdf", ""],
  ["Should match Japanese", "a test.添付 a", ".添付", "test.添付"],
  ["Should match Greek", "a test.Θεωρία a", ".Θεωρία", "test.Θεωρία"],
  ["Should match once", "a test.pdf.doc a", ".pdf,.doc", "test.pdf.doc"],
  [
    "Should not match kw in url",
    "see https://example.org/attachment.cgi?id=1 test",
    "attachment",
    "",
  ],
  [
    "Should not match kw in url ending with kw",
    "https://example.org/attachment",
    "attachment",
    "",
  ],
  [
    "Should match CV and attachment",
    "got my CV as attachment",
    "CV,attachment",
    "CV,attachment",
  ],
];

function run_test() {
  do_test_pending();

  for (var i in tests) {
    if (typeof tests[i] == "function") {
      tests[i]();
    } else {
      test_getAttachmentKeywords.apply(null, tests[i]);
    }
  }

  do_test_finished();
}