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

Upgraded packages and improved code #27

Merged
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
13 changes: 12 additions & 1 deletion @types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { User } from "../../src/entities/user";
import type { Request } from 'express';

import type { User } from '../../src/entities/user';

declare module 'express-serve-static-core' {
interface Request {
user?: User;
}
}

declare global {
/**
* Interface used to type request body.
*/
interface TypedRequestBody<T> extends Request {
body: Partial<T>; // Use Partial to set all properties optional because we cannot be sure the client will send all required properties
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ End-to-end tests are already implemented. The code coverage is 100%.

Packages are frequently upgraded. You can easily see the packages version status [here](https://docs.google.com/spreadsheets/d/1vIeh02Y_SNqVuoQYIxjEXZTnDHQHr5ctQAk2EgS84KQ/edit?usp=share_link).

⭐ If you like it, please leave a star. it helps me a lot! ⭐

---

## Features
Expand Down Expand Up @@ -381,7 +383,7 @@ const res = await request(server).get('/api/auth/authenticated');
```
To test a route as an authenticated user, use the `createAuthenticatedAgent` function:
```typescript
const agent = await createAuthenticatedAgent(server);
const { agent } = await createAuthenticatedAgent(server);
const res = await agent.get('/api/auth/authenticated');
```
Agents allow maintaining a session between multiple requests.
Expand Down
10 changes: 5 additions & 5 deletions __tests__/e2e/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Auth routes', () => {
test('Throw an error if authenticated user tries to login', async () => {
const username = 'fakeUser';
const password = 'fakeUserPwd';
const agent = await createAuthenticatedAgent(server, { username, password });
const { agent } = await createAuthenticatedAgent(server, { username, password });

const res = await agent.post('/api/auth/login').send({ login: username, password });
expect(res.statusCode).toEqual(403);
Expand Down Expand Up @@ -129,7 +129,7 @@ describe('Auth routes', () => {
});

test('Send "You are authenticated" for authenticated user', async () => {
const agent = await createAuthenticatedAgent(server);
const { agent } = await createAuthenticatedAgent(server);

const res = await agent.get('/api/auth/authenticated');

Expand All @@ -146,7 +146,7 @@ describe('Auth routes', () => {

test('Authenticated user is considered as non authenticated if user has been deleted', async () => {
const username = 'fakeUser';
const agent = await createAuthenticatedAgent(server, { username });
const { agent } = await createAuthenticatedAgent(server, { username });

const repo = AppDataSource.getRepository(User);
await repo.delete({ username });
Expand All @@ -158,7 +158,7 @@ describe('Auth routes', () => {
});

test('Logout user', async () => {
const agent = await createAuthenticatedAgent(server);
const { agent } = await createAuthenticatedAgent(server);

const res = await agent.post('/api/auth/logout');

Expand All @@ -176,7 +176,7 @@ describe('Auth routes', () => {
const serverFailingLogout = await createTestServer(7778, true, {
logout: (cb) => { cb('Error'); },
});
const agent = await createAuthenticatedAgent(serverFailingLogout);
const { agent } = await createAuthenticatedAgent(serverFailingLogout);
const res = await agent.post('/api/auth/logout');

try {
Expand Down
7 changes: 4 additions & 3 deletions __tests__/utils/testsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ export const clearDatabase = async () => {
* @returns The created agent.
*/
export const createAuthenticatedAgent = async (server: Server, testUser?: TestUserProps) => {
const agent = request.agent(server);
const userAgent = request.agent(server);
const user = await createTestUser(testUser);
await agent.post('/api/auth/login').send({ login: user.username, password: testUser?.password || 'password' });
return agent;
await userAgent.post('/api/auth/login').send({ login: user.username, password: testUser?.password || 'password' });

return { agent: userAgent, user };
};
2 changes: 1 addition & 1 deletion __tests__/utils/userHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface TestUserProps {
/**
* Create a user in database.
* @param testUser - User informations. Optional.
* @returns
* @returns The created user
*/
export const createTestUser = async (testUser?: TestUserProps) => {
const userRepo = AppDataSource.getRepository(User);
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
"@types/bcryptjs": "^2.4.4",
"@types/connect-pg-simple": "^7.0.1",
"@types/cors": "^2.8.14",
"@types/express": "^4.17.18",
"@types/express": "^4.17.19",
"@types/express-session": "^1.17.8",
"@types/http-errors": "^2.0.2",
"@types/jest": "^29.5.5",
"@types/morgan": "^1.9.6",
"@types/node": "^20.8.0",
"@types/node": "^20.8.6",
"@types/passport": "^1.0.13",
"@types/passport-local": "^1.0.36",
"@types/supertest": "^2.0.13",
"@types/validator": "^13.11.2",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"eslint": "^8.50.0",
"@types/supertest": "^2.0.14",
"@types/validator": "^13.11.3",
"@typescript-eslint/eslint-plugin": "^6.8.0",
"@typescript-eslint/parser": "^6.8.0",
"eslint": "^8.51.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.28.1",
Expand Down
1 change: 0 additions & 1 deletion src/controllers/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { HttpError } from 'http-errors';
import createHttpError from 'http-errors';
import passport from 'passport';

import type { TypedRequestBody } from '../../types/express';
import type { AuthLoginBody, AuthLoginResponse } from '../../types/routes/auth';
import type { User } from '../../entities/user';
import { validateLoginBody } from './validators';
Expand Down
1 change: 0 additions & 1 deletion src/controllers/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import createHttpError from 'http-errors';

import { AppDataSource } from '../../data-source';
import { User } from '../../entities/user';
import type { TypedRequestBody } from '../../types/express';
import type { UsersCreateBody } from '../../types/routes/users';
import { validateCreateBody } from './validators';

Expand Down
8 changes: 0 additions & 8 deletions src/types/express/index.ts

This file was deleted.

Loading