mirror of
https://github.com/n8n-io/n8n.git
synced 2024-09-11 13:15:28 +03:00
🔥 Remove test descriptor wrappers
This commit is contained in:
parent
9ab9c10860
commit
7b6af448eb
@ -11,133 +11,129 @@ import { LOGGED_OUT_RESPONSE_BODY, REST_PATH_SEGMENT } from './shared/constants'
|
||||
import { Db } from '../../src';
|
||||
import { User } from '../../src/databases/entities/User';
|
||||
|
||||
describe('auth endpoints', () => {
|
||||
describe('Owner requests', () => {
|
||||
let app: express.Application;
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ auth: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ auth: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
|
||||
const newOwner = new User();
|
||||
const newOwner = new User();
|
||||
|
||||
Object.assign(newOwner, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(TEST_USER.password, genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
|
||||
await Db.collections.User!.save(newOwner);
|
||||
|
||||
config.set('userManagement.hasOwner', true);
|
||||
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
test('POST /login should log user in', async () => {
|
||||
const cookieLessAgent = request.agent(app);
|
||||
cookieLessAgent.use(utils.prefix(REST_PATH_SEGMENT));
|
||||
|
||||
const response = await cookieLessAgent.post('/login').send({
|
||||
email: TEST_USER.email,
|
||||
password: TEST_USER.password,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(password).toBeUndefined();
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole).toBeDefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
const authToken = utils.getAuthToken(response);
|
||||
expect(authToken).not.toBeUndefined();
|
||||
});
|
||||
|
||||
test('GET /login should receive logged in user', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
const response = await ownerAgent.get('/login');
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(password).toBeUndefined();
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole).toBeDefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
expect(response.headers['set-cookie']).toBeUndefined();
|
||||
});
|
||||
|
||||
test('GET /logout should log user out', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
const response = await ownerAgent.get('/logout');
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(LOGGED_OUT_RESPONSE_BODY);
|
||||
|
||||
const authToken = utils.getAuthToken(response);
|
||||
expect(authToken).toBeUndefined();
|
||||
});
|
||||
Object.assign(newOwner, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(TEST_USER.password, genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
|
||||
await Db.collections.User!.save(newOwner);
|
||||
|
||||
config.set('userManagement.hasOwner', true);
|
||||
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
test('POST /login should log user in', async () => {
|
||||
const cookieLessAgent = request.agent(app);
|
||||
cookieLessAgent.use(utils.prefix(REST_PATH_SEGMENT));
|
||||
|
||||
const response = await cookieLessAgent.post('/login').send({
|
||||
email: TEST_USER.email,
|
||||
password: TEST_USER.password,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(password).toBeUndefined();
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole).toBeDefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
const authToken = utils.getAuthToken(response);
|
||||
expect(authToken).not.toBeUndefined();
|
||||
});
|
||||
|
||||
test('GET /login should receive logged in user', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
const response = await ownerAgent.get('/login');
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(password).toBeUndefined();
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole).toBeDefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
expect(response.headers['set-cookie']).toBeUndefined();
|
||||
});
|
||||
|
||||
test('GET /logout should log user out', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
const response = await ownerAgent.get('/logout');
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(LOGGED_OUT_RESPONSE_BODY);
|
||||
|
||||
const authToken = utils.getAuthToken(response);
|
||||
expect(authToken).toBeUndefined();
|
||||
});
|
||||
|
||||
const TEST_USER = {
|
||||
|
@ -4,40 +4,30 @@ import { REST_PATH_SEGMENT } from './shared/constants';
|
||||
|
||||
import * as utils from './shared/utils';
|
||||
|
||||
describe('/me endpoints', () => {
|
||||
let app: express.Application;
|
||||
let app: express.Application;
|
||||
|
||||
const meRoutes = ['GET /me', 'PATCH /me', 'PATCH /me/password', 'POST /me/survey'];
|
||||
const meRoutes = ['GET /me', 'PATCH /me', 'PATCH /me/password', 'POST /me/survey'];
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({}, { applyAuth: true });
|
||||
});
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({}, { applyAuth: true });
|
||||
});
|
||||
|
||||
describe('Unauthorized requests', () => {
|
||||
meRoutes.forEach((route) => {
|
||||
const [method, endpoint] = route.split(' ').map((i) => i.toLowerCase());
|
||||
meRoutes.forEach((route) => {
|
||||
const [method, endpoint] = route.split(' ').map((i) => i.toLowerCase());
|
||||
|
||||
test(`${route} should return 401 Unauthorized`, async () => {
|
||||
const response = await request(app)[method](endpoint).use(utils.prefix(REST_PATH_SEGMENT));
|
||||
test(`${route} should return 401 Unauthorized`, async () => {
|
||||
const response = await request(app)[method](endpoint).use(utils.prefix(REST_PATH_SEGMENT));
|
||||
|
||||
expect(response.statusCode).toBe(401);
|
||||
});
|
||||
});
|
||||
expect(response.statusCode).toBe(401);
|
||||
});
|
||||
});
|
||||
|
||||
describe('/owner endpoint', () => {
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({}, { applyAuth: true });
|
||||
});
|
||||
|
||||
describe('Unauthorized requests', () => {
|
||||
test(`POST /owner should return 401 Unauthorized`, async () => {
|
||||
const response = await request(app).post('/owner').use(utils.prefix(REST_PATH_SEGMENT));
|
||||
|
||||
expect(response.statusCode).toBe(401);
|
||||
});
|
||||
});
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({}, { applyAuth: true });
|
||||
});
|
||||
|
||||
test(`POST /owner should return 401 Unauthorized`, async () => {
|
||||
const response = await request(app).post('/owner').use(utils.prefix(REST_PATH_SEGMENT));
|
||||
|
||||
expect(response.statusCode).toBe(401);
|
||||
});
|
||||
|
@ -10,40 +10,70 @@ import { SUCCESS_RESPONSE_BODY } from './shared/constants';
|
||||
import { Db } from '../../src';
|
||||
import { User } from '../../src/databases/entities/User';
|
||||
|
||||
describe('/me endpoints', () => {
|
||||
describe('Shell requests', () => {
|
||||
let app: express.Application;
|
||||
describe('Shell requests', () => {
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
|
||||
await Db.collections.User!.save({
|
||||
id: uuid(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
globalRole: role,
|
||||
});
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
await Db.collections.User!.save({
|
||||
id: uuid(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
globalRole: role,
|
||||
});
|
||||
});
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
test('GET /me should return sanitized shell', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
const response = await shellAgent.get('/me');
|
||||
|
||||
test('GET /me should return sanitized shell', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const response = await shellAgent.get('/me');
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBeNull();
|
||||
expect(firstName).toBeNull();
|
||||
expect(lastName).toBeNull();
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await shellAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
@ -59,150 +89,150 @@ describe('/me endpoints', () => {
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBeNull();
|
||||
expect(firstName).toBeNull();
|
||||
expect(lastName).toBeNull();
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await shellAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
for (const invalidPayload of INVALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await shellAgent.patch('/me').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const validPayloads = Array.from({ length: 3 }, () => ({
|
||||
password: utils.randomValidPassword(),
|
||||
}));
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await shellAgent.patch('/me/password').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const invalidPayloads = [
|
||||
...Array.from({ length: 3 }, () => ({ password: utils.randomInvalidPassword() })),
|
||||
{},
|
||||
undefined,
|
||||
'',
|
||||
];
|
||||
|
||||
for (const invalidPayload of invalidPayloads) {
|
||||
const response = await shellAgent.patch('/me/password').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('POST /me/survey should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const validPayloads = [SURVEY, {}];
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await shellAgent.post('/me/survey').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('Member requests', () => {
|
||||
let app: express.Application;
|
||||
test('PATCH /me should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
for (const invalidPayload of INVALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await shellAgent.patch('/me').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const validPayloads = Array.from({ length: 3 }, () => ({
|
||||
password: utils.randomValidPassword(),
|
||||
}));
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await shellAgent.patch('/me/password').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const invalidPayloads = [
|
||||
...Array.from({ length: 3 }, () => ({ password: utils.randomInvalidPassword() })),
|
||||
{},
|
||||
undefined,
|
||||
'',
|
||||
];
|
||||
|
||||
for (const invalidPayload of invalidPayloads) {
|
||||
const response = await shellAgent.patch('/me/password').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('POST /me/survey should succeed with valid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const validPayloads = [SURVEY, {}];
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await shellAgent.post('/me/survey').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Member requests', () => {
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'member', scope: 'global' });
|
||||
|
||||
const newMember = new User();
|
||||
|
||||
Object.assign(newMember, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(utils.randomValidPassword(), genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'member', scope: 'global' });
|
||||
await Db.collections.User!.save(newMember);
|
||||
|
||||
const newMember = new User();
|
||||
config.set('userManagement.hasOwner', true);
|
||||
|
||||
Object.assign(newMember, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(utils.randomValidPassword(), genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
|
||||
await Db.collections.User!.save(newMember);
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
config.set('userManagement.hasOwner', true);
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
test('GET /me should return sanitized member', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
const response = await memberAgent.get('/me');
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
test('GET /me should return sanitized member', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
const response = await memberAgent.get('/me');
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('member');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await memberAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
@ -218,150 +248,150 @@ describe('/me endpoints', () => {
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('member');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await memberAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('member');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me should fail with invalid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
for (const invalidPayload of INVALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await memberAgent.patch('/me').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const validPayloads = Array.from({ length: 3 }, () => ({
|
||||
password: utils.randomValidPassword(),
|
||||
}));
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await memberAgent.patch('/me/password').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should fail with invalid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const invalidPayloads = [
|
||||
...Array.from({ length: 3 }, () => ({ password: utils.randomInvalidPassword() })),
|
||||
{},
|
||||
undefined,
|
||||
'',
|
||||
];
|
||||
|
||||
for (const invalidPayload of invalidPayloads) {
|
||||
const response = await memberAgent.patch('/me/password').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('POST /me/survey should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const validPayloads = [SURVEY, {}];
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await memberAgent.post('/me/survey').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
describe('Owner requests', () => {
|
||||
let app: express.Application;
|
||||
test('PATCH /me should fail with invalid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
for (const invalidPayload of INVALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await memberAgent.patch('/me').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const validPayloads = Array.from({ length: 3 }, () => ({
|
||||
password: utils.randomValidPassword(),
|
||||
}));
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await memberAgent.patch('/me/password').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
|
||||
test('PATCH /me/password should fail with invalid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const invalidPayloads = [
|
||||
...Array.from({ length: 3 }, () => ({ password: utils.randomInvalidPassword() })),
|
||||
{},
|
||||
undefined,
|
||||
'',
|
||||
];
|
||||
|
||||
for (const invalidPayload of invalidPayloads) {
|
||||
const response = await memberAgent.patch('/me/password').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
test('POST /me/survey should succeed with valid inputs', async () => {
|
||||
const member = await Db.collections.User!.findOneOrFail();
|
||||
const memberAgent = await utils.createAuthAgent(app, member);
|
||||
|
||||
const validPayloads = [SURVEY, {}];
|
||||
|
||||
for (const validPayload of validPayloads) {
|
||||
const response = await memberAgent.post('/me/survey').send(validPayload);
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toEqual(SUCCESS_RESPONSE_BODY);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('Owner requests', () => {
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ me: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
|
||||
const newOwner = new User();
|
||||
|
||||
Object.assign(newOwner, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(utils.randomValidPassword(), genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
await Db.collections.User!.save(newOwner);
|
||||
|
||||
const newOwner = new User();
|
||||
config.set('userManagement.hasOwner', true);
|
||||
|
||||
Object.assign(newOwner, {
|
||||
id: uuid(),
|
||||
email: TEST_USER.email,
|
||||
firstName: TEST_USER.firstName,
|
||||
lastName: TEST_USER.lastName,
|
||||
password: hashSync(utils.randomValidPassword(), genSaltSync(10)),
|
||||
globalRole: role,
|
||||
});
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
|
||||
await Db.collections.User!.save(newOwner);
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
config.set('userManagement.hasOwner', true);
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
await Db.collections.Settings!.update(
|
||||
{ key: 'userManagement.hasOwner' },
|
||||
{ value: JSON.stringify(true) },
|
||||
);
|
||||
});
|
||||
test('GET /me should return sanitized owner', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
const response = await ownerAgent.get('/me');
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
test('GET /me should return sanitized owner', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
const response = await ownerAgent.get('/me');
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await ownerAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
@ -377,47 +407,15 @@ describe('/me endpoints', () => {
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
});
|
||||
|
||||
test('PATCH /me should succeed with valid inputs', async () => {
|
||||
const owner = await Db.collections.User!.findOneOrFail();
|
||||
const ownerAgent = await utils.createAuthAgent(app, owner);
|
||||
|
||||
for (const validPayload of VALID_PATCH_ME_PAYLOADS) {
|
||||
const response = await ownerAgent.patch('/me').send(validPayload);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(validPayload.email);
|
||||
expect(firstName).toBe(validPayload.firstName);
|
||||
expect(lastName).toBe(validPayload.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -7,86 +7,82 @@ import * as utils from './shared/utils';
|
||||
import { Db } from '../../src';
|
||||
import config = require('../../config');
|
||||
|
||||
describe('/owner endpoints', () => {
|
||||
describe('Shell requests', () => {
|
||||
let app: express.Application;
|
||||
let app: express.Application;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ owner: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
beforeAll(async () => {
|
||||
app = utils.initTestServer({ owner: true }, { applyAuth: true });
|
||||
await utils.initTestDb();
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
beforeEach(async () => {
|
||||
const role = await Db.collections.Role!.findOneOrFail({ name: 'owner', scope: 'global' });
|
||||
|
||||
await Db.collections.User!.save({
|
||||
id: uuid(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
globalRole: role,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
test('POST /owner should create owner and enable hasOwner setting', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const response = await shellAgent.post('/owner').send(TEST_USER);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
const owner = await Db.collections.User!.findOneOrFail(id);
|
||||
expect(owner.password).not.toBe(TEST_USER.password);
|
||||
|
||||
const hasOwnerConfig = config.get('userManagement.hasOwner');
|
||||
expect(hasOwnerConfig).toBe(true);
|
||||
|
||||
const hasOwnerSetting = await utils.getHasOwnerSetting();
|
||||
expect(hasOwnerSetting).toBe(true);
|
||||
});
|
||||
|
||||
test('POST /owner should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
for (const invalidPayload of INVALID_POST_OWNER_PAYLOADS) {
|
||||
const response = await shellAgent.post('/owner').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
await Db.collections.User!.save({
|
||||
id: uuid(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
globalRole: role,
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await utils.truncateUserTable();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
return getConnection().close();
|
||||
});
|
||||
|
||||
test('POST /owner should create owner and enable hasOwner setting', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
const response = await shellAgent.post('/owner').send(TEST_USER);
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const {
|
||||
id,
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
personalizationAnswers,
|
||||
globalRole,
|
||||
password,
|
||||
resetPasswordToken,
|
||||
} = response.body.data;
|
||||
|
||||
expect(validator.isUUID(id)).toBe(true);
|
||||
expect(email).toBe(TEST_USER.email);
|
||||
expect(firstName).toBe(TEST_USER.firstName);
|
||||
expect(lastName).toBe(TEST_USER.lastName);
|
||||
expect(personalizationAnswers).toBeNull();
|
||||
expect(password).toBeUndefined();
|
||||
expect(resetPasswordToken).toBeUndefined();
|
||||
expect(globalRole.name).toBe('owner');
|
||||
expect(globalRole.scope).toBe('global');
|
||||
|
||||
const owner = await Db.collections.User!.findOneOrFail(id);
|
||||
expect(owner.password).not.toBe(TEST_USER.password);
|
||||
|
||||
const hasOwnerConfig = config.get('userManagement.hasOwner');
|
||||
expect(hasOwnerConfig).toBe(true);
|
||||
|
||||
const hasOwnerSetting = await utils.getHasOwnerSetting();
|
||||
expect(hasOwnerSetting).toBe(true);
|
||||
});
|
||||
|
||||
test('POST /owner should fail with invalid inputs', async () => {
|
||||
const shell = await Db.collections.User!.findOneOrFail();
|
||||
const shellAgent = await utils.createAuthAgent(app, shell);
|
||||
|
||||
for (const invalidPayload of INVALID_POST_OWNER_PAYLOADS) {
|
||||
const response = await shellAgent.post('/owner').send(invalidPayload);
|
||||
expect(response.statusCode).toBe(400);
|
||||
}
|
||||
});
|
||||
|
||||
const TEST_USER = {
|
||||
email: utils.randomEmail(),
|
||||
firstName: utils.randomName(),
|
||||
|
Loading…
Reference in New Issue
Block a user