nthmail / test / login.js
all-in-one smtp+imap with minimal setup
git clone http://git.nthia.dev/nthmail

const assert = require('assert')
const os = require('os')
const path = require('path')
const { randomBytes } = require('crypto')
const Login = require('../lib/login.js')

let tmpdir = path.join(os.tmpdir(), randomBytes(4).toString('hex'))
let login = new Login({
  domains: [ 'localhost' ],
  accounts: [
    {
      id: 0,
      inbox: path.join(tmpdir,'inbox'),
      outbox: path.join(tmpdir,'outbox'),
      unbox: path.join(tmpdir,'unbox'),
      names: ['test']
    }
  ],
  logins: [
    {
      type: 'scrypt',
      account: 0,
      username: 'hi',
      salt: 'aed5c7437c31a507f36bd4a402081092',
      key: 'efb61e7b8dcf99044509e4704e8577bc850fc462df0ff695a4df22945c530e141fc'
        + '3a59c1bd381859eaa1a3de512aad1bc1286cb1f5c8665a8ecaab9a1baf14b',
    },
    {
      type: 'scrypt',
      account: 0,
      username: 'what',
      salt: '087e39e526a95b0b7a97052397c1a129',
      key: 'bfbd0e918d347d59868ea00107ce5c899ded04ef9dfa9c481762b2cbc68a2080da1'
        + '4c8023efc6ac568b9b78b4f06a978a35b86572772df89c8aad71a9bb895aa',
      fingerprint512: 'dfac29208f51c6606a31cb792da01e41d9bd1620b0ef5818a5853499fdb'
        + '9a4206eccc59aa2d0f74abf668c76c91b95b3098719ea78f23bd290099fa003bebcc8',
    },
  ],
})

let n = 0
process.once('exit', code => {
  assert.equal(n, 3)
})

{
  let opts = {
    type: 'plain',
    username: 'hi',
    password: 'hunter2',
  }
  login.checkLogin(opts, (err, id) => {
    assert.ifError(err)
    assert.equal(id, 0)
    n++
  })
}

{
  let opts = {
    type: 'plain',
    username: 'hi',
    password: 'wrongpassword',
  }
  login.checkLogin(opts, (err, id) => {
    assert.ifError(err)
    assert.equal(id, null)
    n++
  })
}

{
  let opts = {
    type: 'plain',
    username: 'what',
    password: 'whatever',
    fingerprint512: 'dfac29208f51c6606a31cb792da01e41d9bd1620b0ef5818a5853499fdb'
      + '9a4206eccc59aa2d0f74abf668c76c91b95b3098719ea78f23bd290099fa003bebcc8',
  }
  login.checkLogin(opts, (err, id) => {
    assert.ifError(err)
    assert.equal(id, 0)
    n++
  })
}