Adding testing to worker, still WIP.

This commit is contained in:
mike12345567 2021-04-23 14:58:06 +01:00
parent 0a733b7c7a
commit ea8e3b7d3f
11 changed files with 2874 additions and 39 deletions

View File

@ -36,6 +36,7 @@ module.exports = {
buildAuthMiddleware: authenticated, buildAuthMiddleware: authenticated,
passport, passport,
google, google,
jwt,
}, },
StaticDatabases, StaticDatabases,
constants: require("./constants"), constants: require("./constants"),

View File

@ -103,7 +103,6 @@
"jimp": "0.16.1", "jimp": "0.16.1",
"joi": "17.2.1", "joi": "17.2.1",
"jsonschema": "1.4.0", "jsonschema": "1.4.0",
"jsonwebtoken": "8.5.1",
"koa": "2.7.0", "koa": "2.7.0",
"koa-body": "4.2.0", "koa-body": "4.2.0",
"koa-compress": "4.0.1", "koa-compress": "4.0.1",

View File

@ -1,5 +1,4 @@
const { BUILTIN_ROLE_IDS } = require("../../utilities/security/roles") const { BUILTIN_ROLE_IDS } = require("../../utilities/security/roles")
const jwt = require("jsonwebtoken")
const env = require("../../environment") const env = require("../../environment")
const { const {
basicTable, basicTable,
@ -16,6 +15,7 @@ const controllers = require("./controllers")
const supertest = require("supertest") const supertest = require("supertest")
const { cleanup } = require("../../utilities/fileSystem") const { cleanup } = require("../../utilities/fileSystem")
const { Cookies } = require("@budibase/auth").constants const { Cookies } = require("@budibase/auth").constants
const { jwt } = require("@budibase/auth").auth
const EMAIL = "babs@babs.com" const EMAIL = "babs@babs.com"
const PASSWORD = "babs_password" const PASSWORD = "babs_password"

View File

@ -46,7 +46,9 @@
"server-destroy": "^1.0.1" "server-destroy": "^1.0.1"
}, },
"devDependencies": { "devDependencies": {
"jest": "^26.6.3",
"nodemon": "^2.0.7", "nodemon": "^2.0.7",
"pouchdb-adapter-memory": "^7.2.2" "pouchdb-adapter-memory": "^7.2.2",
"supertest": "^6.1.3"
} }
} }

View File

@ -12,12 +12,14 @@ const GLOBAL_DB = StaticDatabases.GLOBAL.name
exports.save = async function(ctx) { exports.save = async function(ctx) {
const db = new CouchDB(GLOBAL_DB) const db = new CouchDB(GLOBAL_DB)
const configDoc = ctx.request.body const { type, config } = ctx.request.body
const { type, group, user } = configDoc const { group, user } = config
// insert the type into the doc
config.type = type
// Config does not exist yet // Config does not exist yet
if (!configDoc._id) { if (!config._id) {
configDoc._id = generateConfigID({ config._id = generateConfigID({
type, type,
group, group,
user, user,
@ -27,12 +29,12 @@ exports.save = async function(ctx) {
// verify the configuration // verify the configuration
switch (type) { switch (type) {
case Configs.SMTP: case Configs.SMTP:
await email.verifyConfig(configDoc) await email.verifyConfig(config)
break; break;
} }
try { try {
const response = await db.post(configDoc) const response = await db.post(config)
ctx.body = { ctx.body = {
type, type,
_id: response.id, _id: response.id,

View File

@ -7,12 +7,11 @@ const { Configs } = require("../../../constants")
const router = Router() const router = Router()
function smtpValidation() { function smtpValidation() {
// prettier-ignore
return Joi.object({ return Joi.object({
port: Joi.number().required(), port: Joi.number().required(),
host: Joi.string().required(), host: Joi.string().required(),
from: Joi.string() from: Joi.string().email().required(),
.email()
.required(),
secure: Joi.boolean().optional(), secure: Joi.boolean().optional(),
selfSigned: Joi.boolean().optional(), selfSigned: Joi.boolean().optional(),
auth: Joi.object({ auth: Joi.object({
@ -24,6 +23,7 @@ function smtpValidation() {
} }
function settingValidation() { function settingValidation() {
// prettier-ignore
return Joi.object({ return Joi.object({
url: Joi.string().valid("", null), url: Joi.string().valid("", null),
logoUrl: Joi.string().valid("", null), logoUrl: Joi.string().valid("", null),
@ -32,6 +32,7 @@ function settingValidation() {
} }
function googleValidation() { function googleValidation() {
// prettier-ignore
return Joi.object({ return Joi.object({
clientID: Joi.string().required(), clientID: Joi.string().required(),
clientSecret: Joi.string().required(), clientSecret: Joi.string().required(),

View File

@ -0,0 +1,31 @@
const setup = require("./utilities")
const { EmailTemplatePurpose } = require("../../../constants")
// mock the email system
const sendMailMock = jest.fn()
jest.mock("nodemailer")
const nodemailer = require("nodemailer")
nodemailer.createTransport.mockReturnValue({"sendMail": sendMailMock});
describe("/api/admin/email", () => {
let request = setup.getRequest()
let config = setup.getConfig()
afterAll(setup.afterAll)
it("should be able to send an email (with mocking)", async () => {
// initially configure settings
await config.saveSmtpConfig()
await config.saveSettingsConfig()
const res = await request
.post(`/api/admin/email/send`)
.send({
email: "test@test.com",
purpose: EmailTemplatePurpose.INVITATION,
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body._rev).toBeDefined()
})
})

View File

@ -0,0 +1,78 @@
const env = require("../../environment")
const controllers = require("./controllers")
const supertest = require("supertest")
const { jwt } = require("@budibase/auth").auth
const { Cookies } = require("@budibase/auth").constants
const { Configs } = require("../../../../constants")
class TestConfiguration {
constructor(openServer = true) {
if (openServer) {
env.PORT = 4003
this.server = require("../../../../index")
// we need the request for logging in, involves cookies, hard to fake
this.request = supertest(this.server)
}
}
getRequest() {
return this.request
}
async _req(config, params, controlFunc) {
const request = {}
// fake cookies, we don't need them
request.cookies = { set: () => {}, get: () => {} }
request.config = { jwtSecret: env.JWT_SECRET }
request.appId = this.appId
request.user = { appId: this.appId }
request.request = {
body: config,
}
if (params) {
request.params = params
}
await controlFunc(request)
return request.body
}
defaultHeaders() {
const user = {
userId: "us_uuid1",
builder: {
global: true,
},
}
const authToken = jwt.sign(user, env.JWT_SECRET)
return {
Accept: "application/json",
Cookie: [
`${Cookies.Auth}=${authToken}`,
],
}
}
async saveSettingsConfig() {
await this._req({
type: Configs.SETTINGS,
config: {
url: "http://localhost:10000",
logoUrl: "http://localhost:10000/logo",
company: "TestCompany",
}
}, null, controllers.config.save)
}
async saveSmtpConfig() {
await this._req({
type: Configs.SMTP,
config: {
url: "http://localhost:10000",
logoUrl: "http://localhost:10000/logo",
company: "TestCompany",
}
}, null, controllers.config.save)
}
}
module.exports = TestConfiguration

View File

@ -0,0 +1,7 @@
module.exports = {
email: require("../../../controllers/admin/email"),
groups: require("../../../controllers/admin/groups"),
config: require("../../../controllers/admin/groups"),
templates: require("../../../controllers/admin/groups"),
users: require("../../../controllers/admin/groups"),
}

View File

@ -0,0 +1,30 @@
const TestConfig = require("./TestConfiguration")
let request, config
exports.beforeAll = () => {
config = new TestConfig()
request = config.getRequest()
}
exports.afterAll = () => {
if (config) {
config.end()
}
request = null
config = null
}
exports.getRequest = () => {
if (!request) {
exports.beforeAll()
}
return request
}
exports.getConfig = () => {
if (!config) {
exports.beforeAll()
}
return config
}

File diff suppressed because it is too large Load Diff