1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-10 01:16:39 +02:00
unleash.unleash/src/lib/routes/auth/simple-password-provider.test.ts
Nuno Góis a792594e98
refactor: add OpenAPI schema to simple-password-provider controller (#1734)
* refactor: add OpenAPI schema to simple-password-provider controller

* finish implementation after merge

* refactor: address PR comments
2022-06-23 08:40:25 +01:00

109 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from 'supertest';
import express from 'express';
import User from '../../types/user';
import { SimplePasswordProvider } from './simple-password-provider';
import PasswordMismatchError from '../../error/password-mismatch';
import { createTestConfig } from '../../../test/config/test-config';
import { OpenApiService } from '../../services/openapi-service';
test('Should require password', async () => {
const config = createTestConfig();
const openApiService = new OpenApiService(config);
const app = express();
app.use(express.json());
const userService = () => {};
const ctr = new SimplePasswordProvider(config, {
// @ts-expect-error
userService,
openApiService,
});
app.use('/auth/simple', ctr.router);
const res = await request(app)
.post('/auth/simple/login')
.send({ name: 'john' });
expect(400).toBe(res.status);
});
test('Should login user', async () => {
const config = createTestConfig();
const openApiService = new OpenApiService(config);
const username = 'ola';
const password = 'simplepass';
const user = new User({ id: 123, username });
const app = express();
app.use(express.json());
app.use((req, res, next) => {
// @ts-expect-error
req.session = {};
next();
});
const userService = {
loginUser: (u, p) => {
if (u === username && p === password) {
return user;
}
throw new Error('Wrong password');
},
};
const ctr = new SimplePasswordProvider(config, {
// @ts-expect-error
userService,
openApiService,
});
app.use('/auth/simple', ctr.router);
const res = await request(app)
.post('/auth/simple/login')
.send({ username, password });
expect(200).toBe(res.status);
expect(user.username).toBe(res.body.username);
});
test('Should not login user with wrong password', async () => {
const config = createTestConfig();
const openApiService = new OpenApiService(config);
const username = 'ola';
const password = 'simplepass';
const user = new User({ id: 133, username });
const app = express();
app.use(express.json());
app.use((req, res, next) => {
// @ts-expect-error
req.session = {};
next();
});
const userService = {
loginUser: (u, p) => {
if (u === username && p === password) {
return user;
}
throw new PasswordMismatchError();
},
};
const ctr = new SimplePasswordProvider(config, {
// @ts-expect-error
userService,
openApiService,
});
app.use('/auth/simple', ctr.router);
const res = await request(app)
.post('/auth/simple/login')
.send({ username, password: 'not-correct' });
expect(res.status).toBe(401);
});