2017-03-06 05:45:50 +08:00
|
|
|
/*eslint no-unused-expressions: 0, prefer-arrow-callback: 0 */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
const chai = require('chai');
|
|
|
|
const imapHandler = require('../lib/handler/imap-handler');
|
|
|
|
const expect = chai.expect;
|
2017-03-06 05:45:50 +08:00
|
|
|
chai.config.includeStack = true;
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('IMAP Command Compiler', function() {
|
|
|
|
describe('#compile', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
let command =
|
|
|
|
'* FETCH (ENVELOPE ("Mon, 2 Sep 2013 05:30:13 -0700 (PDT)" NIL ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "tr.ee")) NIL NIL NIL "<-4730417346358914070@unknownmsgid>") BODYSTRUCTURE (("MESSAGE" "RFC822" NIL NIL NIL "7BIT" 105 (NIL NIL ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "pangalink.net")) NIL NIL "<test1>" NIL) ("TEXT" "PLAIN" NIL NIL NIL "7BIT" 12 0 NIL NIL NIL) 5 NIL NIL NIL) ("MESSAGE" "RFC822" NIL NIL NIL "7BIT" 83 (NIL NIL ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "kreata.ee")) ((NIL NIL "andris" "pangalink.net")) NIL NIL "NIL" NIL) ("TEXT" "PLAIN" NIL NIL NIL "7BIT" 12 0 NIL NIL NIL) 4 NIL NIL NIL) ("TEXT" "HTML" ("CHARSET" "utf-8") NIL NIL "QUOTED-PRINTABLE" 19 0 NIL NIL NIL) "MIXED" ("BOUNDARY" "----mailcomposer-?=_1-1328088797399") NIL NIL))',
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed = imapHandler.parser(command, {
|
|
|
|
allowUntagged: true
|
|
|
|
}),
|
|
|
|
compiled = imapHandler.compiler(parsed);
|
|
|
|
|
|
|
|
expect(compiled).to.equal(command);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('Types', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed;
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
beforeEach(function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed = {
|
|
|
|
tag: '*',
|
|
|
|
command: 'CMD'
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('No attributes', function() {
|
|
|
|
it('should compile correctly', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('TEXT', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
parsed.attributes = [
|
|
|
|
{
|
|
|
|
type: 'TEXT',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
}
|
|
|
|
];
|
2017-03-06 05:45:50 +08:00
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD Tere tere!');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('SECTION', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
parsed.attributes = [
|
|
|
|
{
|
|
|
|
type: 'SECTION',
|
|
|
|
section: [
|
|
|
|
{
|
|
|
|
type: 'ATOM',
|
|
|
|
value: 'ALERT'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
];
|
2017-03-06 05:45:50 +08:00
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD [ALERT]');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('ATOM', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
parsed.attributes = [
|
2017-06-05 21:22:07 +08:00
|
|
|
//
|
2017-06-03 14:51:58 +08:00
|
|
|
{
|
|
|
|
type: 'ATOM',
|
|
|
|
value: 'ALERT'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'ATOM',
|
|
|
|
value: '\\ALERT'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'ATOM',
|
|
|
|
value: 'NO ALERT'
|
|
|
|
}
|
|
|
|
];
|
2017-03-06 05:45:50 +08:00
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD ALERT \\ALERT "NO ALERT"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('SEQUENCE', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
parsed.attributes = [
|
|
|
|
{
|
|
|
|
type: 'SEQUENCE',
|
|
|
|
value: '*:4,5,6'
|
|
|
|
}
|
|
|
|
];
|
2017-03-06 05:45:50 +08:00
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD *:4,5,6');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('NIL', function() {
|
|
|
|
it('should compile correctly', function() {
|
|
|
|
parsed.attributes = [null, null];
|
2017-03-06 05:45:50 +08:00
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD NIL NIL');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('TEXT', function() {
|
|
|
|
it('should compile correctly', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed.attributes = [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'String',
|
|
|
|
value: 'Tere tere!',
|
|
|
|
sensitive: true
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD "Tere tere!" "Vana kere"');
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should keep short strings', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed.attributes = [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'String',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed, false, true)).to.equal('* CMD "Tere tere!" "Vana kere"');
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should hide strings', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed.attributes = [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'String',
|
|
|
|
value: 'Tere tere!',
|
|
|
|
sensitive: true
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed, false, true)).to.equal('* CMD "(* value hidden *)" "Vana kere"');
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should hide long strings', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed.attributes = [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'String',
|
|
|
|
value: 'Tere tere! Tere tere! Tere tere! Tere tere! Tere tere!'
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
];
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed, false, true)).to.equal('* CMD "(* 54B string *)" "Vana kere"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('No Command', function() {
|
|
|
|
it('should compile correctly', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
parsed = {
|
|
|
|
tag: '*',
|
|
|
|
attributes: [
|
2017-06-03 14:51:58 +08:00
|
|
|
1,
|
|
|
|
{
|
2017-03-06 05:45:50 +08:00
|
|
|
type: 'ATOM',
|
|
|
|
value: 'EXPUNGE'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* 1 EXPUNGE');
|
|
|
|
});
|
|
|
|
});
|
2017-06-03 14:51:58 +08:00
|
|
|
describe('Literal', function() {
|
|
|
|
it('shoud return as text', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed = {
|
|
|
|
tag: '*',
|
|
|
|
command: 'CMD',
|
|
|
|
attributes: [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed)).to.equal('* CMD {10}\r\nTere tere! "Vana kere"');
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should return as an array text 1', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed = {
|
|
|
|
tag: '*',
|
|
|
|
command: 'CMD',
|
2017-06-03 14:51:58 +08:00
|
|
|
attributes: [
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Vana kere'
|
|
|
|
}
|
|
|
|
]
|
2017-03-06 05:45:50 +08:00
|
|
|
};
|
|
|
|
expect(imapHandler.compiler(parsed, true)).to.deep.equal(['* CMD {10}\r\n', 'Tere tere! {9}\r\n', 'Vana kere']);
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should return as an array text 2', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed = {
|
|
|
|
tag: '*',
|
|
|
|
command: 'CMD',
|
|
|
|
attributes: [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Tere tere!'
|
2017-06-03 14:51:58 +08:00
|
|
|
},
|
|
|
|
{
|
2017-03-06 05:45:50 +08:00
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Vana kere'
|
|
|
|
},
|
|
|
|
'zzz'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
expect(imapHandler.compiler(parsed, true)).to.deep.equal(['* CMD {10}\r\n', 'Tere tere! {9}\r\n', 'Vana kere "zzz"']);
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('should compile correctly without tag and command', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed = {
|
2017-06-03 14:51:58 +08:00
|
|
|
attributes: [
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Vana kere'
|
|
|
|
}
|
|
|
|
]
|
2017-03-06 05:45:50 +08:00
|
|
|
};
|
|
|
|
expect(imapHandler.compiler(parsed, true)).to.deep.equal(['{10}\r\n', 'Tere tere! {9}\r\n', 'Vana kere']);
|
|
|
|
});
|
|
|
|
|
2017-06-03 14:51:58 +08:00
|
|
|
it('shoud return byte length', function() {
|
2017-03-06 05:45:50 +08:00
|
|
|
let parsed = {
|
|
|
|
tag: '*',
|
|
|
|
command: 'CMD',
|
|
|
|
attributes: [
|
|
|
|
// keep indentation
|
|
|
|
{
|
|
|
|
type: 'LITERAL',
|
|
|
|
value: 'Tere tere!'
|
|
|
|
},
|
|
|
|
'Vana kere'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(imapHandler.compiler(parsed, false, true)).to.equal('* CMD "(* 10B literal *)" "Vana kere"');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|