- Fixed the incorrect API URL in MailgunAccount class. - Improved error handling for email sending failures by providing detailed error messages based on response content. - Updated test file to export the tap.start() function for better module integration.
		
			
				
	
	
		
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			70 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { expect, tap } from '@push.rocks/tapbundle';
 | |
| import { Qenv } from '@push.rocks/qenv';
 | |
| import * as smartmail from '@push.rocks/smartmail';
 | |
| 
 | |
| const testQenv = new Qenv('./', './.nogit');
 | |
| 
 | |
| import * as mailgun from '../ts/index.js';
 | |
| import { type IMailgunMessage } from '../ts/index.js';
 | |
| 
 | |
| let testMailgunAccount: mailgun.MailgunAccount;
 | |
| let testSmartmail: smartmail.Smartmail<IMailgunMessage>;
 | |
| 
 | |
| tap.test('should create a mailgun account', async () => {
 | |
|   testMailgunAccount = new mailgun.MailgunAccount({
 | |
|     apiToken: await testQenv.getEnvVarOnDemand('MAILGUN_API_TOKEN'),
 | |
|     region: 'eu',
 | |
|   });
 | |
|   await testMailgunAccount.addSmtpCredentials(
 | |
|     await testQenv.getEnvVarOnDemand('MAILGUN_SMTP_CREDENTIALS'),
 | |
|   );
 | |
|   expect(testMailgunAccount).toBeInstanceOf(mailgun.MailgunAccount);
 | |
| });
 | |
| 
 | |
| tap.test('should create a smartmail', async () => {
 | |
|   testSmartmail = new smartmail.Smartmail({
 | |
|     body: 'hi there. This is the body.',
 | |
|     from: 'Lossless GmbH <noreply@mail.lossless.com>',
 | |
|     subject: 'TestMessage from @mojoio/mailgun test',
 | |
|   });
 | |
|   expect(testSmartmail).toBeInstanceOf(smartmail.Smartmail);
 | |
| });
 | |
| 
 | |
| tap.test('should send a smartmail', async () => {
 | |
|   await testMailgunAccount.sendSmartMail(
 | |
|     testSmartmail,
 | |
|     'Sandbox Team <sandbox@mail.git.zone>',
 | |
|   );
 | |
| });
 | |
| 
 | |
| tap.test('should send a smartmail with empty body', async () => {
 | |
|   const emptyBodySmartmail = new smartmail.Smartmail<IMailgunMessage>({
 | |
|     body: '',
 | |
|     from: 'Lossless GmbH <noreply@mail.lossless.one>',
 | |
|     subject: 'A message with no body from @mojoio/mailgun test',
 | |
|   });
 | |
|   await testMailgunAccount.sendSmartMail(
 | |
|     emptyBodySmartmail,
 | |
|     'Sandbox Team <sandbox@mail.git.zone>',
 | |
|   );
 | |
| });
 | |
| 
 | |
| tap.test('should retrieve a mail using a retrieval url', async () => {
 | |
|   const result = await testMailgunAccount.retrieveSmartMailFromMessageUrl(
 | |
|     'https://sw.api.mailgun.net/v3/domains/mail.lossless.one/messages/AgMFnnnAKC8xp_dDa79LyoxhloxtaVmnRA==',
 | |
|   );
 | |
|   console.log(result);
 | |
| 
 | |
|   // TODO handle empty body
 | |
|   if (false) {
 | |
|     result.options.subject = 'hi there. This is a testmail with attachment';
 | |
|     result.options.from = 'noreply@mail.lossless.com';
 | |
|     await testMailgunAccount.sendSmartMail(
 | |
|       result,
 | |
|       'Sandbox Team <sandbox@mail.git.zone>',
 | |
|     );
 | |
|   }
 | |
| });
 | |
| 
 | |
| export default tap.start();
 |