added new tests
This commit is contained in:
parent
bc258d4227
commit
f433017782
@ -3,11 +3,11 @@ import { PoolClient } from 'pg';
|
|||||||
import { compare, hash as hashPass } from '../utils/password';
|
import { compare, hash as hashPass } from '../utils/password';
|
||||||
import { UserType } from './user';
|
import { UserType } from './user';
|
||||||
|
|
||||||
type AuthType = {
|
export type AuthType = {
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
};
|
};
|
||||||
type PasswordType = {
|
export type PasswordType = {
|
||||||
old_password: string;
|
old_password: string;
|
||||||
new_password: string;
|
new_password: string;
|
||||||
};
|
};
|
||||||
@ -58,10 +58,7 @@ class Auth {
|
|||||||
return result.rows[0];
|
return result.rows[0];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async updatePassword(
|
async updatePassword(id: string, p: PasswordType): Promise<UserType> {
|
||||||
id: string,
|
|
||||||
p: AuthType & PasswordType
|
|
||||||
): Promise<UserType> {
|
|
||||||
return this.withConnection(async (connection: PoolClient) => {
|
return this.withConnection(async (connection: PoolClient) => {
|
||||||
const query = {
|
const query = {
|
||||||
text: 'SELECT password FROM users WHERE id=$1',
|
text: 'SELECT password FROM users WHERE id=$1',
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import database from '../../database';
|
import database from '../../database';
|
||||||
import User, { UserType } from '../user';
|
import User, { UserType } from '../user';
|
||||||
|
import Auth, { AuthType, PasswordType } from '../auth';
|
||||||
|
|
||||||
|
const auth = new Auth();
|
||||||
const user = new User();
|
const user = new User();
|
||||||
|
|
||||||
describe('User Model', () => {
|
describe('User Model', () => {
|
||||||
@ -39,17 +41,6 @@ describe('User Model', () => {
|
|||||||
connection.release();
|
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 () => {
|
it('createUser method should return a new user', async () => {
|
||||||
const result = await user.createUser(newUser1);
|
const result = await user.createUser(newUser1);
|
||||||
@ -83,7 +74,59 @@ describe('User Model', () => {
|
|||||||
email: 'adhamhaddad.dev@gmail.com'
|
email: 'adhamhaddad.dev@gmail.com'
|
||||||
} as UserType);
|
} 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 () => {
|
it('deleteUser method should return object with deleted user id', async () => {
|
||||||
const result = await user.deleteUser('1');
|
const result = await user.deleteUser('1');
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user