diff --git a/src/__test__/userController.test.ts b/src/__test__/userController.test.ts index 973b64d1..5d4101ad 100644 --- a/src/__test__/userController.test.ts +++ b/src/__test__/userController.test.ts @@ -152,81 +152,87 @@ describe('User Registration Tests', () => { describe('User Login Tests', () => { it('should log in a user with valid credentials', async () => { - // Setup: Create a user for login - const userData = { - firstName: 'Test', - lastName: 'User', - email: 'test@gmail.com', - password: 'TestPassword123', - userType: 'buyer', - }; - await request(app).post('/api/v1/register').send(userData); - const updatedUser = await userRepository.findOne({ where: { email: userData.email } }); - if (updatedUser) { - - updatedUser.isVerified = true; - await userRepository.save(updatedUser); + const userData = { + firstName: 'Test', + lastName: 'User', + email: 'test@gmail.com', + password: 'TestPassword123', + userType: 'buyer', + }; + await request(app).post('/api/v1/register').send(userData); + const updatedUser = await userRepository.findOne({ where: { email: userData.email } }); + if (updatedUser) { + updatedUser.isVerified = true; + await userRepository.save(updatedUser); + + const loginResponse = await request(app).post('/api/v1/login').send({ + email: userData.email, + password: userData.password, + }); - const loginResponse = await request(app).post('/api/v1/login').send({ - email: userData.email, - password: userData.password, - }); - - expect(loginResponse.status).toBe(200); - expect(loginResponse.body.token).toBeDefined(); - expect(loginResponse.body.message).toBe('Successfully logged in'); - } else { - - throw new Error('User not found'); - } + expect(loginResponse.status).toBe(200); + expect(loginResponse.body.token).toBeDefined(); + expect(loginResponse.body.message).toBe('Successfully Logged in'); + } else { + throw new Error('User not found'); + } }); + it('should return a 401 status code if the email is not verified', async () => { - const userData = { - firstName: 'Test', - lastName: 'User', - email: 'test@gmail.com', - password: 'TestPassword123', - userType: 'buyer', - }; - await request(app).post('/api/v1/register').send(userData); - const updatedUser = await userRepository.findOne({ - where: { email: userData.email }, - }); + const userData = { + firstName: 'Test', + lastName: 'User', + email: 'test@gmail.com', + password: 'TestPassword123', + userType: 'buyer', + }; + await request(app).post('/api/v1/register').send(userData); + const updatedUser = await userRepository.findOne({ + where: { email: userData.email }, + }); + + if (updatedUser) { + updatedUser.isVerified = false; + await userRepository.save(updatedUser); + const loginResponse = await request(app).post('/api/v1/login').send({ + email: userData.email, + password: userData.password, + }); - if (updatedUser) { - - updatedUser.isVerified = false; - await userRepository.save(updatedUser); - const loginResponse = await request(app).post('/api/v1/login').send({ - email: userData.email, - password: userData.password, - }); - - expect(loginResponse.status).toBe(401); - expect(loginResponse.body.message).toBe( - 'Email not verified. Confirmation link sent to your email.' - ); - } else { - - throw new Error('User not found'); - } + expect(loginResponse.status).toBe(401); + expect(loginResponse.body.message).toBe('Please verify your email. Confirmation link has been sent.'); // Corrected message + } else { + throw new Error('User not found'); + } }); + it('should return a 401 status code if the password does not match', async () => { + const userData = { + firstName: 'Test', + lastName: 'User', + email: 'test@gmail.com', + password: 'TestPassword123', + userType: 'buyer', + }; + await request(app).post('/api/v1/register').send(userData); + + const loginResponse = await request(app).post('/api/v1/login').send({ + email: userData.email, + password: 'IncorrectPassword', + }); + expect(loginResponse.status).toBe(401); + expect(loginResponse.body.message).toBe('Password does not match'); + }); - const userData = { - firstName: 'Test', - lastName: 'User', - email: 'test@gmail.com', - password: 'TestPassword123', - userType: 'buyer', - }; - await request(app).post('/api/v1/register').send(userData); + it('should return a 404 status code if the user is not found', async () => { + const nonExistentEmail = 'nonexistent@example.com'; const loginResponse = await request(app).post('/api/v1/login').send({ - email: userData.email, - password: 'IncorrectPassword', + email: nonExistentEmail, + password: 'TestPassword123', }); - expect(loginResponse.status).toBe(401); - expect(loginResponse.body.message).toBe('Password does not match'); - }); -}); + + expect(loginResponse.status).toBe(404); + expect(loginResponse.body.message).toBe('User not found'); + }); + });