-
-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: WIP trying to fix demo auth tests
- Loading branch information
Showing
1 changed file
with
48 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |