added new tests

This commit is contained in:
adhamhaddad 2023-04-25 05:22:09 +02:00
parent bc258d4227
commit f433017782
2 changed files with 57 additions and 17 deletions

View File

@ -3,11 +3,11 @@ import { PoolClient } from 'pg';
import { compare, hash as hashPass } from '../utils/password';
import { UserType } from './user';
type AuthType = {
export type AuthType = {
email: string;
password: string;
};
type PasswordType = {
export type PasswordType = {
old_password: string;
new_password: string;
};
@ -58,10 +58,7 @@ class Auth {
return result.rows[0];
});
}
async updatePassword(
id: string,
p: AuthType & PasswordType
): Promise<UserType> {
async updatePassword(id: string, p: PasswordType): Promise<UserType> {
return this.withConnection(async (connection: PoolClient) => {
const query = {
text: 'SELECT password FROM users WHERE id=$1',

View File

@ -1,6 +1,8 @@
import database from '../../database';
import User, { UserType } from '../user';
import Auth, { AuthType, PasswordType } from '../auth';
const auth = new Auth();
const user = new User();
describe('User Model', () => {
@ -39,17 +41,6 @@ describe('User Model', () => {
connection.release();
}
});
afterAll(async () => {
const connection = await database.connect();
try {
const query = {
text: 'DELETE FROM users;\nALTER SEQUENCE users_id_seq RESTART WITH 1'
};
await connection.query(query);
} finally {
connection.release();
}
});
it('createUser method should return a new user', async () => {
const result = await user.createUser(newUser1);
@ -83,7 +74,59 @@ describe('User Model', () => {
email: 'adhamhaddad.dev@gmail.com'
} as UserType);
});
});
});
describe('Auth Model', () => {
describe('Test methods exists', () => {
it('expects updatePassword method to be exists', () => {
expect(auth.updatePassword).toBeDefined();
});
it('expects authMe method to be exists', () => {
expect(auth.authMe).toBeDefined();
});
it('expects authUser method to be exists', () => {
expect(auth.authUser).toBeDefined();
});
});
describe('Methods returns', () => {
const authenticate = {
email: 'adhamhaddad.dev@gmail.com',
password: 'adham123'
} as UserType;
const updatePassword = {
id: 1,
old_password: 'adham123',
new_password: 'adham12345'
} as PasswordType;
afterAll(async () => {
const connection = await database.connect();
try {
const query = {
text: 'DELETE FROM users;\nALTER SEQUENCE users_id_seq RESTART WITH 1'
};
await connection.query(query);
} finally {
connection.release();
}
});
it('authUser method should return a object user', async () => {
const result = await auth.authUser(authenticate);
expect(result).toEqual({
id: 1,
first_name: 'Adham',
last_name: 'Ashraf',
username: 'adhamhaddad1',
email: 'adhamhaddad.dev@gmail.com'
} as UserType);
});
it('updatePassword method should return object with user id', async () => {
const result = await auth.updatePassword('1', updatePassword);
expect(result).toEqual({ id: 1 } as UserType);
});
it('deleteUser method should return object with deleted user id', async () => {
const result = await user.deleteUser('1');
expect(result).toEqual({