PageRenderTime 28ms CodeModel.GetById 7ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 1ms

/node_modules/nodemailer/node_modules/nodemailer-direct-transport/test/direct-transport-test.js

https://gitlab.com/viddeshshinde/email
JavaScript | 160 lines | 133 code | 27 blank | 0 comment | 5 complexity | 3afe03a352ddd5178a05410895c4c713 MD5 | raw file
  1'use strict';
  2
  3process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
  4
  5var chai = require('chai');
  6var expect = chai.expect;
  7var directTransport = require('../src/direct-transport');
  8var SMTPServer = require('smtp-server').SMTPServer;
  9chai.config.includeStack = true;
 10
 11var PORT_NUMBER = 8712;
 12
 13function MockBuilder(envelope, message) {
 14    this.envelope = envelope;
 15    this.message = message;
 16    this._headers = [];
 17}
 18
 19MockBuilder.prototype.getEnvelope = function() {
 20    return this.envelope;
 21};
 22
 23MockBuilder.prototype.createReadStream = function() {
 24    return this.message;
 25};
 26
 27MockBuilder.prototype.getHeader = function() {
 28    return 'teretere';
 29};
 30
 31describe('SMTP Transport Tests', function() {
 32    this.timeout(100 * 1000);
 33
 34    var server;
 35    var retryCount = 0;
 36
 37    beforeEach(function(done) {
 38        server = new SMTPServer({
 39            disabledCommands: ['STARTTLS', 'AUTH'],
 40
 41            onData: function(stream, session, callback) {
 42                stream.on('data', function() {});
 43                stream.on('end', function() {
 44                    var err;
 45                    if (/retry@/.test(session.envelope.mailFrom.address) && retryCount++ < 3) {
 46                        err = new Error('Please try again later');
 47                        err.responseCode = 451;
 48                        callback(err);
 49                    } else {
 50                        callback(null, 'OK');
 51                    }
 52                });
 53            },
 54
 55            onMailFrom: function(address, session, callback) {
 56                if (/invalid@/.test(address.address)) {
 57                    return callback(new Error('Invalid sender'));
 58                }
 59                return callback(); // Accept the address
 60            },
 61            onRcptTo: function(address, session, callback) {
 62                if (/invalid@/.test(address.address)) {
 63                    return callback(new Error('Invalid recipient'));
 64                }
 65                return callback(); // Accept the address
 66            },
 67            logger: false
 68        });
 69
 70        server.listen(PORT_NUMBER, done);
 71    });
 72
 73    afterEach(function(done) {
 74        server.close(done);
 75    });
 76
 77    it('Should expose version number', function() {
 78        var client = directTransport();
 79        expect(client.name).to.exist;
 80        expect(client.version).to.exist;
 81    });
 82
 83    it('Should send mail', function(done) {
 84        var client = directTransport({
 85            port: PORT_NUMBER,
 86            debug: false
 87        });
 88
 89        client.on('log', console.log);
 90
 91        var chunks = [],
 92            message = new Array(1024).join('teretere, vana kere\n');
 93
 94        server.on('data', function(connection, chunk) {
 95            chunks.push(chunk);
 96        });
 97
 98        server.on('dataReady', function(connection, callback) {
 99            var body = Buffer.concat(chunks);
100            expect(body.toString()).to.equal(message.trim().replace(/\n/g, '\r\n'));
101            callback(null, true);
102        });
103
104        client.send({
105            data: {},
106            message: new MockBuilder({
107                from: 'test@[127.0.0.1]',
108                to: 'test@[127.0.0.1]'
109            }, message)
110        }, function(err, info) {
111            expect(err).to.not.exist;
112            expect(info.accepted).to.deep.equal(['test@[127.0.0.1]']);
113            done();
114        });
115    });
116
117    it('Should retry mail', function(done) {
118        var client = directTransport({
119            port: PORT_NUMBER,
120            debug: false,
121            retryDelay: 1000
122        });
123
124        client.on('log', console.log);
125
126        client.send({
127            data: {},
128            message: new MockBuilder({
129                from: 'retry@[127.0.0.1]',
130                to: ['test@[127.0.0.1]', 'test2@[127.0.0.1]']
131            }, 'test')
132        }, function(err, info) {
133            expect(err).to.not.exist;
134            expect(info.pending.length).to.equal(1);
135            done();
136        });
137    });
138
139    it('Should reject mail', function(done) {
140        var client = directTransport({
141            port: PORT_NUMBER,
142            debug: false,
143            retryDelay: 1000
144        });
145
146        client.on('log', console.log);
147
148        client.send({
149            data: {},
150            message: new MockBuilder({
151                from: 'invalid@[127.0.0.1]',
152                to: ['test@[127.0.0.1]', 'test2@[127.0.0.1]']
153            }, 'test')
154        }, function(err) {
155            expect(err).to.exist;
156            expect(err.errors[0].recipients).to.deep.equal(['test@[127.0.0.1]', 'test2@[127.0.0.1]']);
157            done();
158        });
159    });
160});