summaryrefslogtreecommitdiffstats
path: root/comm/mail/test/browser/openpgp/composition/browser_expiredKey.js
blob: bf5c7e195d3471a19131c197822d333c7836928f (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
/* 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/. */

"use strict";

/**
 * Tests for composition when a key is expired.
 */

const { close_compose_window, open_compose_new_mail } = ChromeUtils.import(
  "resource://testing-common/mozmill/ComposeHelpers.jsm"
);

const { get_notification, wait_for_notification_to_show } = ChromeUtils.import(
  "resource://testing-common/mozmill/NotificationBoxHelpers.jsm"
);

const { OpenPGPTestUtils } = ChromeUtils.import(
  "resource://testing-common/mozmill/OpenPGPTestUtils.jsm"
);

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

var gAccount;
var gIdentity;
var gExpiredKeyId;
var gNotExpiredKeyId;

add_setup(async () => {
  gAccount = MailServices.accounts.createAccount();
  gAccount.incomingServer = MailServices.accounts.createIncomingServer(
    "eddie",
    "openpgp.example",
    "imap"
  );

  gIdentity = MailServices.accounts.createIdentity();
  gIdentity.email = "eddie@openpgp.example";
  gAccount.addIdentity(gIdentity);
  MailServices.accounts.defaultAccount = gAccount;
  MailServices.accounts.defaultAccount.defaultIdentity = gIdentity;

  [gExpiredKeyId] = await OpenPGPTestUtils.importPrivateKey(
    window,
    new FileUtils.File(
      getTestFilePath(
        "../data/keys/eddie@openpgp.example-0x15e9357d2c2395c0-secret.asc"
      )
    )
  );
  [gNotExpiredKeyId] = await OpenPGPTestUtils.importPrivateKey(
    window,
    new FileUtils.File(
      getTestFilePath(
        "../data/keys/alice@openpgp.example-0xf231550c4f47e38e-secret.asc"
      )
    )
  );
  // FIXME: ^^^ should use a non-expiring key matching EDDIE!

  registerCleanupFunction(async () => {
    await OpenPGPTestUtils.removeKeyById(gExpiredKeyId, true);
    await OpenPGPTestUtils.removeKeyById(gNotExpiredKeyId, true);
    MailServices.accounts.removeIncomingServer(gAccount.incomingServer, true);
    MailServices.accounts.removeAccount(gAccount, true);
  });
});

add_task(async function testExpiredKeyShowsNotificationBar() {
  Services.wm
    .getMostRecentWindow("mail:3pane")
    .document.getElementById("tabmail")
    .currentAbout3Pane.displayFolder(gAccount.incomingServer.rootFolder);
  info(`Using key ${gExpiredKeyId}`);
  gIdentity.setUnicharAttribute(
    "openpgp_key_id",
    gExpiredKeyId.replace(/^0x/, "")
  );
  let cwc = open_compose_new_mail();

  wait_for_notification_to_show(
    cwc.window,
    "compose-notification-bottom",
    "openpgpSenderKeyExpired"
  );
  let notification = get_notification(
    cwc.window,
    "compose-notification-bottom",
    "openpgpSenderKeyExpired"
  );

  Assert.ok(notification !== null, "notification should be visible");
  Assert.equal(
    notification.messageText.textContent,
    "Your current configuration uses the key 0x15E9357D2C2395C0, which has expired.",
    "correct notification message should be displayed"
  );

  const buttons = notification._buttons;
  Assert.equal(
    buttons[0]["l10n-id"],
    "settings-context-open-account-settings-item2",
    "button0 should be the button to open account settings"
  );
  cwc.window.close();
});

add_task(async function testKeyWithoutExpiryDoesNotShowNotification() {
  Services.wm
    .getMostRecentWindow("mail:3pane")
    .document.getElementById("tabmail")
    .currentAbout3Pane.displayFolder(gAccount.incomingServer.rootFolder);
  info(`Using key ${gNotExpiredKeyId}`);
  gIdentity.setUnicharAttribute(
    "openpgp_key_id",
    gNotExpiredKeyId.replace(/^0x/, "")
  );
  let cwc = open_compose_new_mail();

  // Give it some time to potentially start showing.
  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
  await new Promise(resolve => setTimeout(resolve, 200));
  let notification = get_notification(
    cwc.window,
    "compose-notification-bottom",
    "openpgpSenderKeyExpired"
  );

  Assert.ok(
    notification === null,
    "the expiry warning should not be visible if the key is not expired"
  );
  cwc.window.close();
});