Skip to content

Commit

Permalink
🔥 Remove test description wrappers (#2874)
Browse files Browse the repository at this point in the history
* 🔥 Remove /owner test wrappers

* 🔥 Remove auth middleware test wrappers

* 🔥 Remove auth endpoints test wrappers

* 🔥 Remove overlooked middleware wrappers

* 🔥 Remove me namespace test wrappers

Co-authored-by: Ben Hesseldieck <[email protected]>
  • Loading branch information
ivov and BHesseldieck authored Feb 25, 2022
1 parent f1a5523 commit 9705165
Show file tree
Hide file tree
Showing 3 changed files with 485 additions and 521 deletions.
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

0 comments on commit 9705165

Please sign in to comment.