From f4330177820a6c613c4906198b9bce32d21c5091 Mon Sep 17 00:00:00 2001 From: adhamhaddad Date: Tue, 25 Apr 2023 05:22:09 +0200 Subject: [PATCH] added new tests --- src/models/auth.ts | 9 ++--- src/models/tests/userSpec.ts | 65 ++++++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/models/auth.ts b/src/models/auth.ts index a8ad498..62a30a0 100644 --- a/src/models/auth.ts +++ b/src/models/auth.ts @@ -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 { + async updatePassword(id: string, p: PasswordType): Promise { return this.withConnection(async (connection: PoolClient) => { const query = { text: 'SELECT password FROM users WHERE id=$1', diff --git a/src/models/tests/userSpec.ts b/src/models/tests/userSpec.ts index 9210c65..4ee8943 100644 --- a/src/models/tests/userSpec.ts +++ b/src/models/tests/userSpec.ts @@ -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({