diff options
Diffstat (limited to 'comm/mailnews/compose/test/unit/test_smtpPasswordFailure1.js')
-rw-r--r-- | comm/mailnews/compose/test/unit/test_smtpPasswordFailure1.js | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/comm/mailnews/compose/test/unit/test_smtpPasswordFailure1.js b/comm/mailnews/compose/test/unit/test_smtpPasswordFailure1.js new file mode 100644 index 0000000000..b7d7f1ef43 --- /dev/null +++ b/comm/mailnews/compose/test/unit/test_smtpPasswordFailure1.js @@ -0,0 +1,151 @@ +/** + * This test checks to see if the smtp password failure is handled correctly. + * The steps are: + * - Have an invalid password in the password database. + * - Check we get a prompt asking what to do. + * - Check retry does what it should do. + * - Check cancel does what it should do. + * + * XXX Due to problems with the fakeserver + smtp not using one connection for + * multiple sends, the rest of this test is in test_smtpPasswordFailure2.js. + */ + +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +/* import-globals-from ../../../test/resources/alertTestUtils.js */ +/* import-globals-from ../../../test/resources/passwordStorage.js */ +load("../../../resources/alertTestUtils.js"); +load("../../../resources/passwordStorage.js"); + +var server; +var attempt = 0; + +var kIdentityMail = "identity@foo.invalid"; +var kSender = "from@foo.invalid"; +var kTo = "to@foo.invalid"; +var kUsername = "testsmtp"; +// Login information needs to match the login information stored in the signons +// json file. +var kInvalidPassword = "smtptest"; +var kValidPassword = "smtptest1"; + +/* exported alert, confirmEx */ +// for alertTestUtils.js +function alert(aDialogText, aText) { + // The first few attempts may prompt about the password problem, the last + // attempt shouldn't. + Assert.ok(attempt < 4); + + // Log the fact we've got an alert, but we don't need to test anything here. + dump("Alert Title: " + aDialogText + "\nAlert Text: " + aText + "\n"); +} + +function confirmExPS( + parent, + aDialogTitle, + aText, + aButtonFlags, + aButton0Title, + aButton1Title, + aButton2Title, + aCheckMsg, + aCheckState +) { + switch (++attempt) { + // First attempt, retry. + case 1: + dump("\nAttempting retry\n"); + return 0; + // Second attempt, cancel. + case 2: + dump("\nCancelling login attempt\n"); + return 1; + default: + do_throw("unexpected attempt number " + attempt); + return 1; + } +} + +add_task(async function () { + function createHandler(d) { + var handler = new SMTP_RFC2821_handler(d); + // Username needs to match the login information stored in the signons json + // file. + handler.kUsername = kUsername; + handler.kPassword = kValidPassword; + handler.kAuthRequired = true; + return handler; + } + server = setupServerDaemon(createHandler); + + // Prepare files for passwords (generated by a script in bug 1018624). + await setupForPassword("signons-mailnews1.8.json"); + + registerAlertTestUtils(); + + // Test file + var testFile = do_get_file("data/message1.eml"); + + // Ensure we have at least one mail account + localAccountUtils.loadLocalMailAccount(); + + server.start(); + var smtpServer = getBasicSmtpServer(server.port); + var identity = getSmtpIdentity(kIdentityMail, smtpServer); + + // Handle the server in a try/catch/finally loop so that we always will stop + // the server if something fails. + try { + // This time with auth + test = "Auth sendMailMessage"; + + smtpServer.authMethod = Ci.nsMsgAuthMethod.passwordCleartext; + smtpServer.socketType = Ci.nsMsgSocketType.plain; + smtpServer.username = kUsername; + + dump("Send\n"); + + MailServices.smtp.sendMailMessage( + testFile, + kTo, + identity, + kSender, + null, + null, + null, + null, + false, + "", + {}, + {} + ); + + server.performTest(); + + dump("End Send\n"); + + Assert.equal(attempt, 2); + + // Check that we haven't forgetton the login even though we've retried and cancelled. + let logins = Services.logins.findLogins( + "smtp://localhost", + null, + "smtp://localhost" + ); + + Assert.equal(logins.length, 1); + Assert.equal(logins[0].username, kUsername); + Assert.equal(logins[0].password, kInvalidPassword); + } catch (e) { + do_throw(e); + } finally { + server.stop(); + + var thread = gThreadManager.currentThread; + while (thread.hasPendingEvents()) { + thread.processNextEvent(true); + } + } +}); |