Skip to content

Commit

Permalink
resolved test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
13XAVI committed May 8, 2024
1 parent 71a2195 commit ed849ee
Showing 1 changed file with 74 additions and 68 deletions.
142 changes: 74 additions & 68 deletions src/__test__/userController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

0 comments on commit ed849ee

Please sign in to comment.