Skip to content

Commit

Permalink
fix: WIP trying to fix demo auth tests
Browse files Browse the repository at this point in the history
  • Loading branch information
00Chaotic committed Apr 9, 2024
1 parent 1fb131a commit fcdc9b7
Showing 1 changed file with 48 additions and 71 deletions.
119 changes: 48 additions & 71 deletions src/lib/middleware/demo-authentication.test.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,60 @@
import { createServices } from '../services';
import { createTestConfig } from "../../test/config/test-config";
import { IAuthType } from "../server-impl";
import { Knex } from 'knex';
import createStores from '../../test/fixtures/store';
import getApp from '../app';
import sessionDb from './session-db';
import supertest from 'supertest';
import { IAuthType } from '../server-impl';
import dbInit, { ITestDb } from '../../test/e2e/helpers/database-init';
import { IUnleashStores } from '../types';
import { setupAppWithCustomAuth } from '../../test/e2e/helpers/test-helper';

// TODO: use before-each to call setup
let db: ITestDb;
let stores: IUnleashStores;

async function getSetup(adminLoginEnabled: boolean) {
const base = `/random${Math.round(Math.random() * 1000)}`;
const config = createTestConfig({
server: { baseUriPath: base },
authentication: {
authDemoAllowAdminLogin: adminLoginEnabled ? true : false,
type: IAuthType.DEMO,
}
});
beforeAll(async () => {
db = await dbInit('demo_auth_serial');
stores = db.stores;
}, 20000);

const stores = createStores();
const services = createServices(stores, config);

await services.accessService.createRole({
name: 'Admin',
description: 'Test role for admin user',
type: 'root-custom',
createdByUserId: -9999,
});
await services.accessService.createRole({
name: 'Editor',
description: 'Test role for normal user',
type: 'root-custom',
createdByUserId: -9999,
});

const unleashSession = sessionDb(config, {} as Knex);
const app = await getApp(config, stores, services, unleashSession);
afterAll(async () => {
await db?.destroy();
});

return {
base,
request: supertest(app),
services,
};
}
const getApp = (adminLoginEnabled: boolean) =>
setupAppWithCustomAuth(stores, () => {}, {
authentication: {
authDemoAllowAdminLogin: adminLoginEnabled,
type: IAuthType.DEMO,
createAdminUser: true,
},
});

test('should allow login with admin user if flag enabled', async () => {
const { base, request, services } = await getSetup(true);

services.userService.initAdminUser({});

return request
.post(`${base}/auth/demo/login`)
.send({email: 'admin'})
.expect(200)
.expect(res => {
expect(res.body.id).toBe(1);
expect(res.body.username).toBe('admin');
});
const app = await getApp(true);
return (
app.request
.post(`/auth/demo/login`)
.send({ email: 'admin' })
// .expect(200)
.expect((res) => {
expect(res.body).toBe(''); // TODO remove this - debugging
expect(res.body.id).toBe(1);
expect(res.body.username).toBe('admin');
})
);
});

test('should create regular user with flag enabled', async () => {
const { base, request, services } = await getSetup(true);

return request
.post(`${base}/auth/demo/login`)
.send({email: '[email protected]'})
// .expect(200)
.expect({}); // debugging - shows the body so we can see the error
// .expect(res => {
// expect(res.body.email).toBe('[email protected]');
// });
const app = await getApp(true);
return app.request
.post(`/auth/demo/login`)
.send({ email: '[email protected]' })
.expect(200)
.expect((res) => {
expect(res.body.email).toBe('[email protected]');
expect(res.body.id).not.toBe(1);
});
});

test('should return 403 for admin user if flag disabled', async() => {
const { base, request } = await getSetup(false);

return request
.post(`${base}/auth/demo/login`)
.send({email: 'admin'})
.expect(403);
test('should return 403 for admin user if flag disabled', async () => {
const app = await getApp(false);
return app.request
.post(`/auth/demo/login`)
.send({ email: 'admin' })
.expect(403);
});

0 comments on commit fcdc9b7

Please sign in to comment.