Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove test description wrappers for user management backend #2874

Merged
merged 6 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 118 additions & 122 deletions packages/cli/test/integration/auth.endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,129 +14,125 @@ import { getGlobalOwnerRole } from './shared/utils';

let globalOwnerRole: Role;

describe('auth endpoints', () => {
describe('Owner requests', () => {
let app: express.Application;

beforeAll(async () => {
app = utils.initTestServer({ namespaces: ['auth'], applyAuth: true });
await utils.initTestDb();
await utils.truncate(['User']);

globalOwnerRole = await getGlobalOwnerRole();
utils.initLogger();
});

beforeEach(async () => {
await utils.createUser({
id: uuid(),
email: TEST_USER.email,
firstName: TEST_USER.firstName,
lastName: TEST_USER.lastName,
password: hashSync(TEST_USER.password, genSaltSync(10)),
role: globalOwnerRole,
});

config.set('userManagement.hasOwner', true);

await Db.collections.Settings!.update(
{ key: 'userManagement.hasOwner' },
{ value: JSON.stringify(true) },
);
});

afterEach(async () => {
await utils.truncate(['User']);
});

afterAll(() => {
return getConnection().close();
});

test('POST /login should log user in', async () => {
const authlessAgent = await utils.createAgent(app);

const response = await authlessAgent.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).toBeDefined();
});

test('GET /login should receive logged in user', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = await utils.createAgent(app, { auth: true, user: owner });

const response = await authOwnerAgent.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('POST /logout should log user out', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = await utils.createAgent(app, { auth: true, user: owner });

const response = await authOwnerAgent.post('/logout');

expect(response.statusCode).toBe(200);
expect(response.body).toEqual(LOGGED_OUT_RESPONSE_BODY);

const authToken = utils.getAuthToken(response);
expect(authToken).toBeUndefined();
});
let app: express.Application;

beforeAll(async () => {
app = utils.initTestServer({ namespaces: ['auth'], applyAuth: true });
await utils.initTestDb();
await utils.truncate(['User']);

globalOwnerRole = await getGlobalOwnerRole();
utils.initLogger();
});

beforeEach(async () => {
await utils.createUser({
id: uuid(),
email: TEST_USER.email,
firstName: TEST_USER.firstName,
lastName: TEST_USER.lastName,
password: hashSync(TEST_USER.password, genSaltSync(10)),
role: globalOwnerRole,
});

config.set('userManagement.hasOwner', true);

await Db.collections.Settings!.update(
{ key: 'userManagement.hasOwner' },
{ value: JSON.stringify(true) },
);
});

afterEach(async () => {
await utils.truncate(['User']);
});

afterAll(() => {
return getConnection().close();
});

test('POST /login should log user in', async () => {
const authlessAgent = await utils.createAgent(app);

const response = await authlessAgent.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).toBeDefined();
});

test('GET /login should receive logged in user', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = await utils.createAgent(app, { auth: true, user: owner });

const response = await authOwnerAgent.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('POST /logout should log user out', async () => {
const owner = await Db.collections.User!.findOneOrFail();
const authOwnerAgent = await utils.createAgent(app, { auth: true, user: owner });

const response = await authOwnerAgent.post('/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 = {
Expand Down
Loading