mirror of
https://github.com/Permissionless-Software-Foundation/bch-email-api.git
synced 2026-09-21 16:52:04 -07:00
Merge pull request #3 from Permissionless-Software-Foundation/unstable
iRedMail endpoint
This commit is contained in:
+5
-1
@@ -10,6 +10,8 @@ const config = require('../config')
|
||||
|
||||
// Models
|
||||
const UsedQuota = require('../postgres/models/used-quota')
|
||||
const Mailbox = require('../postgres/models/mailbox')
|
||||
const Forwardings = require('../postgres/models/forwardings')
|
||||
|
||||
class IRedMail {
|
||||
constructor () {
|
||||
@@ -18,7 +20,9 @@ class IRedMail {
|
||||
|
||||
// Load the models
|
||||
this.models = {
|
||||
usedQuota: new UsedQuota(this.sequelize)
|
||||
usedQuota: new UsedQuota(this.sequelize),
|
||||
mailbox: new Mailbox(this.sequelize),
|
||||
forwardings: new Forwardings(this.sequelize)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-8
@@ -54,15 +54,17 @@ async function startServer () {
|
||||
await app.listen(config.port)
|
||||
console.log(`Server started on ${config.port}`)
|
||||
|
||||
try {
|
||||
const iRedMail = new IRedMail()
|
||||
await iRedMail.sequelize.authenticate()
|
||||
console.log('Connection to the iRedMail postgres database has been established successfully.')
|
||||
if (process.env.TEST_ENV !== 'unit') {
|
||||
try {
|
||||
const iRedMail = new IRedMail()
|
||||
await iRedMail.sequelize.authenticate()
|
||||
console.log('Connection to the iRedMail postgres database has been established successfully.')
|
||||
|
||||
// Attach to iRedMail instantiation to the context object.
|
||||
app.context.iRedMail = iRedMail
|
||||
} catch (err) {
|
||||
console.log(`Could not connect to iRedMail instance.`)
|
||||
// Attach to iRedMail instantiation to the context object.
|
||||
app.context.iRedMail = iRedMail
|
||||
} catch (err) {
|
||||
console.log(`Could not connect to iRedMail instance.`)
|
||||
}
|
||||
}
|
||||
|
||||
return app
|
||||
|
||||
Vendored
+1
-1
@@ -9,5 +9,5 @@ export default {
|
||||
module.exports = {
|
||||
session: 'secret-boilerplate-token',
|
||||
token: 'secret-jwt-token',
|
||||
database: 'mongodb://localhost:27017/p2pvps-server-dev'
|
||||
database: 'mongodb://localhost:27017/iredmail-dev'
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -9,5 +9,5 @@ export default {
|
||||
module.exports = {
|
||||
session: 'secret-boilerplate-token',
|
||||
token: 'secret-jwt-token',
|
||||
database: 'mongodb://localhost:27017/p2pvps-server-prod'
|
||||
database: 'mongodb://localhost:27017/iredmail-prod'
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -9,5 +9,5 @@ export default {
|
||||
module.exports = {
|
||||
session: 'secret-boilerplate-token',
|
||||
token: 'secret-jwt-token',
|
||||
database: 'mongodb://localhost:27017/p2pvps-server-test'
|
||||
database: 'mongodb://localhost:27017/irestmail-test'
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ try {
|
||||
}
|
||||
|
||||
// Default to development environment.
|
||||
const env = process.env.NODE_ENV || 'development'
|
||||
const env = process.env.APP_ENV || 'development'
|
||||
|
||||
// Load the config object for the selected environment.
|
||||
const config = require(`./env/${env}`)
|
||||
|
||||
+2
-1
@@ -5,7 +5,8 @@
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js",
|
||||
"test": "NODE_ENV=test nyc --reporter=text ./node_modules/.bin/mocha --exit",
|
||||
"test": "APP_ENV=test TEST_ENV=unit nyc --reporter=text ./node_modules/.bin/mocha --exit",
|
||||
"test:integration": "APP_ENV=test TEST_ENV=integration nyc --reporter=text ./node_modules/.bin/mocha --timeout 15000 --exit",
|
||||
"lint": "eslint src/**/*.js",
|
||||
"docs": "./node_modules/.bin/apidoc -i src/ -o docs",
|
||||
"coverage": "nyc report --reporter=text-lcov | coveralls",
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
Forwardings table in the iRedMail postgres database.
|
||||
|
||||
The constructor cxpects an instantiation of the sequalize class. Returns a
|
||||
Sequelize model.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
class Forwardings {
|
||||
// Expects an instantiation of the sequalize class. Returns a Sequelize model.
|
||||
constructor (sequelize) {
|
||||
return sequelize.define(
|
||||
'forwardings',
|
||||
{
|
||||
id: { type: Sequelize.INTEGER, primaryKey: true },
|
||||
address: Sequelize.STRING,
|
||||
forwarding: Sequelize.STRING,
|
||||
domain: Sequelize.STRING,
|
||||
dest_domain: Sequelize.STRING,
|
||||
is_maillist: Sequelize.INTEGER,
|
||||
is_list: Sequelize.INTEGER,
|
||||
is_forwarding: Sequelize.INTEGER,
|
||||
is_alias: Sequelize.INTEGER,
|
||||
active: Sequelize.INTEGER
|
||||
},
|
||||
{
|
||||
timestamps: false,
|
||||
// disable the modification of tablenames; By default, sequelize will automatically
|
||||
// transform all passed model names (first parameter of define) into plural.
|
||||
// if you don't want that, set the following
|
||||
freezeTableName: true,
|
||||
// define the table's name
|
||||
tableName: 'forwardings'
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Forwardings
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
mailbox table in the iRedMail postgres database.
|
||||
|
||||
The constructor expects an instantiation of the sequalize class. Returns a
|
||||
Sequelize model.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const Sequelize = require('sequelize')
|
||||
|
||||
class Mailbox {
|
||||
// Expects an instantiation of the sequalize class. Returns a Sequelize model.
|
||||
constructor (sequelize) {
|
||||
return sequelize.define(
|
||||
'mailbox',
|
||||
{
|
||||
username: { type: Sequelize.STRING, primaryKey: true },
|
||||
password: Sequelize.STRING,
|
||||
name: Sequelize.STRING,
|
||||
language: Sequelize.STRING,
|
||||
storagebasedirectory: Sequelize.STRING,
|
||||
storagenode: Sequelize.STRING,
|
||||
maildir: Sequelize.STRING,
|
||||
quota: Sequelize.BIGINT,
|
||||
domain: Sequelize.STRING,
|
||||
transport: Sequelize.STRING,
|
||||
department: Sequelize.STRING,
|
||||
rank: Sequelize.STRING,
|
||||
employeeid: Sequelize.STRING,
|
||||
isadmin: Sequelize.INTEGER,
|
||||
isglobaladmin: Sequelize.INTEGER,
|
||||
enablesmtp: Sequelize.INTEGER,
|
||||
enablesmtpsecured: Sequelize.INTEGER,
|
||||
enableimaptls: Sequelize.INTEGER,
|
||||
enabledeliver: Sequelize.INTEGER,
|
||||
enablelda: Sequelize.INTEGER,
|
||||
enablemanagesieve: Sequelize.INTEGER,
|
||||
enablemanagesievesecured: Sequelize.INTEGER,
|
||||
enablesieve: Sequelize.INTEGER,
|
||||
enablesievesecured: Sequelize.INTEGER,
|
||||
enablesievetls: Sequelize.INTEGER,
|
||||
enableinternal: Sequelize.INTEGER,
|
||||
enabledoveadm: Sequelize.INTEGER,
|
||||
'enablelib-storage': Sequelize.INTEGER,
|
||||
'enableindexer-worker': Sequelize.INTEGER,
|
||||
enablelmtp: Sequelize.INTEGER,
|
||||
enabledsync: Sequelize.INTEGER,
|
||||
enablesogo: Sequelize.INTEGER,
|
||||
allow_nets: Sequelize.STRING,
|
||||
lastlogindate: Sequelize.DATE,
|
||||
lastloginipv4: Sequelize.INET,
|
||||
lastloginprotocol: Sequelize.STRING,
|
||||
disclaimer: Sequelize.STRING,
|
||||
allowedsenders: Sequelize.STRING,
|
||||
rejectedsenders: Sequelize.STRING,
|
||||
allowedrecipients: Sequelize.STRING,
|
||||
rejectedrecipients: Sequelize.STRING,
|
||||
settings: Sequelize.STRING,
|
||||
passwordlastchange: Sequelize.DATE,
|
||||
created: Sequelize.DATE,
|
||||
modified: Sequelize.DATE,
|
||||
expired: Sequelize.DATE,
|
||||
active: Sequelize.INTEGER
|
||||
},
|
||||
{
|
||||
timestamps: false,
|
||||
// disable the modification of tablenames; By default, sequelize will automatically
|
||||
// transform all passed model names (first parameter of define) into plural.
|
||||
// if you don't want that, set the following
|
||||
freezeTableName: true,
|
||||
// define the table's name
|
||||
tableName: 'mailbox'
|
||||
}
|
||||
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Mailbox
|
||||
@@ -0,0 +1,65 @@
|
||||
// const User = require('../../models/users')
|
||||
|
||||
const util = require('util')
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
async function createUser (ctx) {
|
||||
try {
|
||||
const modelMailbox = await ctx.iRedMail.models.mailbox.sync()
|
||||
const modelForwardings = await ctx.iRedMail.models.forwardings.sync()
|
||||
|
||||
// http://docs.sequelizejs.com/
|
||||
// await modelMailbox.create({})
|
||||
|
||||
ctx.body = {}
|
||||
} catch (err) {
|
||||
console.log(`Error in iredmail.createUser(): `, err)
|
||||
ctx.throw(500, err.message)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Create a new user by manipulating the postgres database.
|
||||
*/
|
||||
async function getUsers (ctx) {
|
||||
try {
|
||||
// Sync the model.
|
||||
// const model = await ctx.iRedMail.models.usedQuota.sync()
|
||||
const model = await ctx.iRedMail.models.mailbox.sync()
|
||||
// const model = await ctx.iRedMail.models.forwardings.sync()
|
||||
|
||||
// Get every model in the database.
|
||||
const data = await model.findAll()
|
||||
|
||||
// Get just the user name.
|
||||
const users = data.map(x => x.username)
|
||||
|
||||
ctx.body = users
|
||||
} catch (err) {
|
||||
console.log(`Error in iredmail.getUsers(): `, err)
|
||||
ctx.throw(500, err.message)
|
||||
}
|
||||
/*
|
||||
const user = new User(ctx.request.body.user)
|
||||
try {
|
||||
await user.save()
|
||||
} catch (err) {
|
||||
ctx.throw(422, err.message)
|
||||
}
|
||||
|
||||
const token = user.generateToken()
|
||||
const response = user.toJSON()
|
||||
|
||||
delete response.password
|
||||
|
||||
ctx.body = {
|
||||
user: response,
|
||||
token
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createUser,
|
||||
getUsers
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// const ensureUser = require('../../middleware/validators')
|
||||
const iredmail = require('./controller')
|
||||
|
||||
module.exports.baseUrl = '/iredmail'
|
||||
|
||||
module.exports.routes = [
|
||||
{
|
||||
method: 'POST',
|
||||
route: '/adduser',
|
||||
handlers: [
|
||||
iredmail.createUser
|
||||
]
|
||||
},
|
||||
{
|
||||
method: 'GET',
|
||||
route: '/',
|
||||
handlers: [
|
||||
iredmail.getUsers
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const rp = require('request-promise')
|
||||
const assert = require('chai').assert
|
||||
|
||||
const IRedMail = require('../bin/iredmail')
|
||||
|
||||
const util = require('util')
|
||||
util.inspect.defaultOptions = { depth: 1 }
|
||||
|
||||
const LOCALHOST = 'http://localhost:5000'
|
||||
|
||||
describe('#iRedMail', () => {
|
||||
let testType = 'unit'
|
||||
|
||||
before(async () => {
|
||||
// Detect integration test flag.
|
||||
if (process.env.TEST_ENV === 'integration') {
|
||||
testType = 'integration'
|
||||
|
||||
// iRedMail = new IRedMail()
|
||||
// await iRedMail.sequelize.authenticate()
|
||||
}
|
||||
})
|
||||
|
||||
describe('#createUser', () => {
|
||||
if (process.env.TEST_ENV === 'integration') {
|
||||
it('should get database models', async () => {
|
||||
try {
|
||||
// console.log(`Hello world!`)
|
||||
|
||||
const options = {
|
||||
method: 'POST',
|
||||
uri: `${LOCALHOST}/iredmail/adduser`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true,
|
||||
body: {
|
||||
}
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
console.log(`result: ${util.inspect(result.body)}`)
|
||||
} catch (err) {
|
||||
console.log(`Error in #createUser test: `, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
describe('#getUsers', () => {
|
||||
if (process.env.TEST_ENV === 'integration') {
|
||||
it('should get usernames of all users in mailbox table', async () => {
|
||||
try {
|
||||
// console.log(`Hello world!`)
|
||||
|
||||
const options = {
|
||||
method: 'GET',
|
||||
uri: `${LOCALHOST}/iredmail`,
|
||||
resolveWithFullResponse: true,
|
||||
json: true
|
||||
}
|
||||
|
||||
let result = await rp(options)
|
||||
// console.log(`result: ${util.inspect(result.body)}`)
|
||||
|
||||
// Returns an array of username/email strings.
|
||||
assert.isArray(result.body)
|
||||
assert.isString(result.body[0])
|
||||
} catch (err) {
|
||||
console.log(`Error in #createUser test: `, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user