forked from tsai/budibase
11 changed files with 2874 additions and 39 deletions
@ -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() |
||||
|
}) |
||||
|
}) |
||||
@ -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 |
||||
@ -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"), |
||||
|
} |
||||
@ -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
Loading…
Reference in new issue