import { User, UserStore } from '../../models/user' import bcrypt from 'bcrypt' const { BCRYPT_PASSWORD } = process.env const pepper = BCRYPT_PASSWORD const store = new UserStore() const testUser: User = { id: 1, firstname: 'John', lastname: 'Doe', username: 'Jd', password: 'password' } const updatedUser: User = { id: 1, firstname: 'Sara', lastname: 'Doe', username: 'Sd', password: 'password123' } describe("User model", () => { console.log("USER") it('Should have an index method', () => { expect(store.index).toBeDefined(); }); it('Should have a read method', () => { expect(store.read).toBeDefined; }); it('Should have a create method', () => { expect(store.create).toBeDefined; }); it('Should have a update method', () => { expect(store.update).toBeDefined; }); it('Should have a delete method', () => { expect(store.delete).toBeDefined; }); it('Create method should create a new user', async () => { const result = await store.create(testUser); expect(result.firstname).toEqual(testUser.firstname) expect(result.lastname).toEqual(testUser.lastname) expect(result.username).toEqual(testUser.username) expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue }) it('Index method should should return a list of users', async () => { const result = await store.index(); expect(result[0].firstname).toEqual(testUser.firstname) expect(result[0].lastname).toEqual(testUser.lastname) expect(result[0].username).toEqual(testUser.username) expect(bcrypt.compareSync(testUser.password + pepper, result[0].password)).toBeTrue }) it('Read method should return user', async () => { const result = await store.read(1); expect(result.firstname).toEqual(testUser.firstname) expect(result.lastname).toEqual(testUser.lastname) expect(result.username).toEqual(testUser.username) expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue }) it('Authenticate method', async () => { const authUser = { username: 'Jd', password: 'password' } const result = await store.authenticate(authUser.username, authUser.password); if (result) { expect(result.firstname).toEqual(testUser.firstname) expect(result.lastname).toEqual(testUser.lastname) expect(result.username).toEqual(testUser.username) expect(bcrypt.compareSync(testUser.password + pepper, result.password)).toBeTrue } }) it('Update method should create a new user', async () => { const result = await store.update(updatedUser); expect(result.firstname).toEqual(updatedUser.firstname) expect(result.lastname).toEqual(updatedUser.lastname) expect(result.username).toEqual(updatedUser.username) expect(bcrypt.compareSync(updatedUser.password + pepper, result.password)).toBeTrue }) it('Delete method should delete a user by Id', async () => { const result = await store.delete(1); const checkUsers = await store.index() expect(checkUsers).toEqual([]) }) })