From 5e0bbd9cc28a2da9c056fc0e06ad5feb139b63eb Mon Sep 17 00:00:00 2001 From: Saravanamuthu Aka CF Mitrah Date: Thu, 21 Jul 2022 10:11:06 -0500 Subject: [PATCH] LDEV-4028 Cleanup the mail testcase and modified to use greenmail (#1713) --- test/tags/Mail.cfc | 172 +++++++++++++++++++++++++++++++++++++++ test/tags/_Mail.cfc | 190 -------------------------------------------- 2 files changed, 172 insertions(+), 190 deletions(-) create mode 100644 test/tags/Mail.cfc delete mode 100644 test/tags/_Mail.cfc diff --git a/test/tags/Mail.cfc b/test/tags/Mail.cfc new file mode 100644 index 0000000000..fd88405e47 --- /dev/null +++ b/test/tags/Mail.cfc @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2015, Lucee Association Switzerland. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +component extends="org.lucee.cfml.test.LuceeTestCase" labels="mail" { + + variables.from="susi@sorglos.de"; + variables.to="geisse@peter.ch"; + + public function beforeTests(){ + variables.creds = getCredentials(); + + if (structCount(creds.smtp) == 0 || structCount(creds.pop) == 0) return; + + admin + action="updateMailServer" + type="web" + password="#request.WEBADMINPASSWORD#" + hostname="#creds.smtp.SERVER#" + dbusername="#creds.smtp.USERNAME#" + dbpassword="" + life="1" + idle="1" + port="#creds.smtp.PORT_INSECURE#" + id="123" + tls="true" + ssl="false" + reuseConnection=false; + } + + public function afterTests(){ + admin + action="removeMailServer" + type="web" + password="#request.WEBADMINPASSWORD#" + hostname="#creds.smtp.SERVER#"; + } + + private function getCredentials() { + return { + smtp : server.getTestService("smtp"), + pop : server.getTestService("pop") + } + } + + public boolean function notHasServices() { + return structCount(server.getTestService("smtp")) == 0 || structCount(server.getTestService("pop")) == 0; + } + + private struct function getMails() localmode=true { + pop action="getAll" name="local.inboxemails" server="#creds.pop.server#" password="#creds.pop.password#" port="#creds.pop.PORT_INSECURE#" secure="no" username="#variables.to#"; + + sct = queryGetRow(inboxemails,queryRecordCount(inboxemails)); + + // delete inbox mails after getting mail for the test So the inbox always have one mail and getting mail will take less time + pop action="delete" server="#creds.pop.server#" password="#creds.pop.password#" port="#creds.pop.PORT_INSECURE#" secure="no" username="#variables.to#"; + + sct.header = headerToStruct(sct.header); + return sct; + } + + // converts the header string to struct + private struct function headerToStruct(Required string str) localmode=true { + a = reMatchNoCase("\s([a-zA-Z-]+\:)",str); + + keyWithComma = arrayToList(a,"|").listmap((e)=>{ + return "," & e; + },"|"); + + l = replaceListNocase(str, arrayToList(a), keyWithComma, ",", "|"); + + sct = {}; + loop list="#l#" item="e" index="i" { + sct[trim(listFirst(e,":"))] = trim(listLast(e,":")); + } + return sct; + } + + + // tests + public function testSimpleMail() localmode="true" skip="notHasServices" { + + mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + echo("This is a text email!"); + } + + mails=getMails(); + + assertEquals("test mail1",mails.subject); + assertEquals("This is a text email!",mails["body"].trim()); + assertEquals(variables.from,mails["from"]); + assertEquals(variables.to,mails["to"]); + assertEquals("text/plain; charset=UTF-8",mails.header["content-type"]); + } + + + public function testHTMLMail() localmode="true" skip="notHasServices" { + mail type="html" to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + echo("This is a HTML email!"); + } + mails=getMails(); + + assertEquals("text/html; charset=UTF-8",mails.header["content-type"]); + assertEquals(variables.from,mails["from"]); + assertEquals(variables.to,mails["to"]); + } + + public function testTextMail() localmode="true" skip="notHasServices" { + mail type="plain" to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + echo("This is a text email!"); + } + mails=getMails(); + + assertEquals("text/plain; charset=UTF-8",mails.header["content-type"]); + assertEquals(variables.from,mails["from"]); + assertEquals(variables.to,mails["to"]); + } + + public function testTextMailPart() localmode="true" skip="notHasServices" { + mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + mailpart type="text" { + echo("This is a text email!"); + } + } + mails=getMails(); + + assertEquals("test mail1",mails.subject); + assertEquals("This is a text email!",mails["body"].trim()); + assertEquals(variables.from,mails["from"]); + assertEquals(variables.to,mails["to"]); + assertEquals("text/plain; charset=UTF-8",mails.header["content-type"]); + } + + + public function testHTMLMailPart() localmode="true" skip="notHasServices" { + mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + mailpart type="html" { + echo("This is a html email!"); + } + } + mails=getMails(); + + assertEquals("text/html; charset=UTF-8",mails.header["content-type"]); + } + + public function testMultiMailPart() localmode="true" skip="notHasServices" { + mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { + mailpart type="html" { + echo("This is a html email!"); + } + mailpart type="text" { + echo("This is a text email!"); + } + } + mails=getMails(); + + expect(findNoCase("multipart/alternative;", mails.header["Content-Type"])).toBeGT(0); + } + +} \ No newline at end of file diff --git a/test/tags/_Mail.cfc b/test/tags/_Mail.cfc deleted file mode 100644 index 4d4b4ed9ff..0000000000 --- a/test/tags/_Mail.cfc +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Copyright (c) 2015, Lucee Association Switzerland. All rights reserved. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see . - */ -component extends="org.lucee.cfml.test.LuceeTestCase" { - - variables.PORT=2526; - variables.from="susi@sorglos.de"; - variables.to="geisse@peter.ch"; - - public function beforeTests(){ - admin - action="updateMailServer" - type="web" - password="#request.WEBADMINPASSWORD#" - hostname="localhost" - dbusername="" - dbpassword="" - life="1" - idle="1" - port="#PORT#" - id="123" - tls="false" - ssl="false" - reuseConnection=false; - - } - public function afterTests(){ - admin - action="removeMailServer" - type="web" - password="#request.WEBADMINPASSWORD#" - hostname="localhost"; - - } - - - private array function getMails(smtpServer) localmode=true{ - arr=[]; - it=smtpServer.getReceivedEmail(); - while(it.hasNext()) { - email=it.next(); - names=email.getHeaderNames(); - sct={}; - while(names.hasNext()) { - name=names.next(); - sct[name]=email.getHeaderValue(name); - } - if(StructCount(sct)){ - sct["body"]=email.getBody(); - arrayAppend(arr,sct); - } - } - return arr; - } - - private function start() { - return createObject("java","com.dumbster.smtp.SimpleSmtpServer","smtp.dumbster","1.6.0").start(PORT); - } - - - public function testSimpleMail() localmode="true"{ - try { - smtpServer=start(); - mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - echo("This is a text email!"); - } - } - finally { - smtpServer.stop(); - } - - mails=getMails(smtpServer); - - assertEquals(1,mails.len()); - assertEquals("test mail1",mails[1].subject); - assertEquals("This is a text email!",mails[1]["body"]); - assertEquals(variables.from,mails[1]["from"]); - assertEquals(variables.to,mails[1]["to"]); - assertEquals("text/plain; charset=UTF-8",mails[1]["content-type"]); - } - - - public function testHTMLMail() localmode="true"{ - try { - smtpServer=start(); - mail type="html" to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - echo("This is a HTML email!"); - } - } - finally { - smtpServer.stop(); - } - mails=getMails(smtpServer); - - assertEquals("text/html; charset=UTF-8",mails[1]["content-type"]); - } - - public function testTextMail() localmode="true"{ - try { - smtpServer=start(); - mail type="plain" to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - echo("This is a text email!"); - } - } - finally { - smtpServer.stop(); - } - mails=getMails(smtpServer); - - assertEquals("text/plain; charset=UTF-8",mails[1]["content-type"]); - } - - public function testTextMailPart() localmode="true"{ - try { - smtpServer=start(); - mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - mailpart type="text" { - echo("This is a text email!"); - } - } - } - finally { - smtpServer.stop(); - } - mails=getMails(smtpServer); - - assertEquals(1,mails.len()); - assertEquals("test mail1",mails[1].subject); - assertEquals("This is a text email!",mails[1]["body"]); - assertEquals(variables.from,mails[1]["from"]); - assertEquals(variables.to,mails[1]["to"]); - assertEquals("text/plain; charset=UTF-8",mails[1]["content-type"]); - } - - - public function testHTMLMailPart() localmode="true"{ - try { - smtpServer=start(); - mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - mailpart type="html" { - echo("This is a html email!"); - } - } - } - finally { - smtpServer.stop(); - } - mails=getMails(smtpServer); - - assertEquals(1,mails.len()); - // dump(mails); - } - - public function testMultiMailPart() localmode="true"{ - try { - smtpServer=start(); - mail to=variables.to from=variables.from subject="test mail1" spoolEnable=false { - mailpart type="html" { - echo("This is a html email!"); - } - mailpart type="text" { - echo("This is a text email!"); - } - } - } - finally { - smtpServer.stop(); - } - mails=getMails(smtpServer); - - assertEquals(1,mails.len()); - // dump(mails); - } - - - -} \ No newline at end of file