Skip to content

Commit

Permalink
Merge pull request #124 from atlp-rwanda/fix-user-profile-management
Browse files Browse the repository at this point in the history
fix user profile managment
  • Loading branch information
faid-terence authored and maxCastro1 committed Jul 24, 2024
2 parents c67cbca + 0b6e258 commit cbd338f
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 113 deletions.
154 changes: 79 additions & 75 deletions src/__test__/userServices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { cleanDatabase } from './test-assets/DatabaseCleanup';
import { v4 as uuid } from 'uuid';
import { dbConnection } from '../startups/dbConnection';

import bcrypt from 'bcrypt'
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

const userId = uuid();
Expand Down Expand Up @@ -132,66 +132,78 @@ describe('User service Test', () => {
});

it('should Login a user, with valid credentials', async () => {
const res = await request(app)
.post('/user/login')
.send({
email: '[email protected]',
password: 'password',
});
const res = await request(app).post('/user/login').send({
email: '[email protected]',
password: 'password',
});
// Assert
expect(res.status).toBe(200);
expect(res.body.data.token).toBeDefined();
});

it('should send OTP, if 2FA is enabled', async () => {
const res = await request(app)
.post('/user/login')
.send({
email: '[email protected]',
password: 'password',
});
const res = await request(app).post('/user/login').send({
email: '[email protected]',
password: 'password',
});
// Assert
expect(res.status).toBe(200);
expect(res.body.data.message).toBe('Please provide the OTP sent to your email or phone');
});

it('should not Login a user, with invalid credentials', async () => {
const res = await request(app)
.post('/user/login')
.send({
email: '[email protected]',
password: 'passwo',
});
const res = await request(app).post('/user/login').send({
email: '[email protected]',
password: 'passwo',
});
// Assert
expect(res.status).toBe(401);
});

it('should not login User if user email is not verified', async () => {
const res = await request(app)
.post('/user/login')
.send({
email: '[email protected]',
password: 'password',
});
const res = await request(app).post('/user/login').send({
email: '[email protected]',
password: 'password',
});

expect(res.status).toBe(400);
expect(res.body.message).toBe("Please verify your account");
expect(res.body.message).toBe('Please verify your account');
});

it('should not login User if user is currently suspended', async () => {
const res = await request(app)
.post('/user/login')
.send({
email: sampleUser1.email,
password: sampleUser1.password,
});
const res = await request(app).post('/user/login').send({
email: sampleUser1.email,
password: sampleUser1.password,
});

expect(res.status).toBe(400);
expect(res.body.message).toBe("Your account has been suspended");
expect(res.body.message).toBe('Your account has been suspended');
});
});

describe('User Profile update', () => {
it('should get user information', async () => {
const res = await request(app)
.get('/user/profile')
.set('Authorization', `Bearer ${getAccessToken(userId, sampleUser.email)}`);

expect(res.status).toBe(200);
expect(res.body.status).toBe('success');
expect(res.body.data.code).toBe(200);
expect(res.body.data.message).toBe('profile fetched successfully');
});

it('should get user information', async () => {
const res = await request(app)
.put('/user/profile')
.send({ images: [] })
.set('Authorization', `Bearer ${getAccessToken(userId, sampleUser.email)}`);

expect(res.status).toBe(400);
expect(res.body.status).toBe('error');
expect(res.body.message).toBe("Cannot read properties of undefined (reading 'length')");
});

it('should update user profile', async () => {
const res = await request(app)
.put('/user/update')
Expand All @@ -200,7 +212,6 @@ describe('User service Test', () => {
lastName: 'Doe',
gender: 'Male',
phoneNumber: '0789412421',
photoUrl: 'photo.jpg',
})
.set('Authorization', `Bearer ${getAccessToken(userId, sampleUser.email)}`);

Expand All @@ -220,7 +231,7 @@ describe('User service Test', () => {
.put('/user/update')
.send({
firstName: 'firstName updated',
lastName: 'lastName updated'
lastName: 'lastName updated',
})
.set('Authorization', `Bearer ${getAccessToken(userId, sampleUser.email)}`);

Expand All @@ -230,58 +241,51 @@ describe('User service Test', () => {

describe('User Reset Password', () => {
it('should return response error, if no email and userID provided', async () => {
const respond = await request(app)
.post('/user/password/reset');
const respond = await request(app).post('/user/password/reset');

expect(respond.status).toBe(400);
});

it('should not reset password, for no existing Users', async () => {
const respond = await request(app)
.post('/user/password/reset')
.query({
email: '[email protected]',
userid: uuid()
});
const respond = await request(app).post('/user/password/reset').query({
email: '[email protected]',
userid: uuid(),
});

expect(respond.status).toBe(404);
});

it('should not reset password, if no new password sent', async () => {
const respond = await request(app)
.post('/user/password/reset')
.query({
email: sampleUser.email,
userid: sampleUser.id
});
const respond = await request(app).post('/user/password/reset').query({
email: sampleUser.email,
userid: sampleUser.id,
});

expect(respond.status).toBe(400);
expect(respond.body.message).toBe('Please provide all required fields');
});

it('should not reset password, if new password doesn\'t match password in confirmPassword field', async () => {
it("should not reset password, if new password doesn't match password in confirmPassword field", async () => {
const respond = await request(app)
.post('/user/password/reset')
.query({
email: sampleUser.email,
userid: sampleUser.id
userid: sampleUser.id,
})
.send({
newPassword: 'new-password',
confirmPassword: 'password'
confirmPassword: 'password',
});

expect(respond.status).toBe(400);
expect(respond.body.message).toBe('new password must match confirm password');
});

it('should not reset password, for incorrect user id syntax (invalid uuid)', async () => {
const respond = await request(app)
.post('/user/password/reset')
.query({
email: sampleUser.email,
userid: 'invalid-=uuid'
});
const respond = await request(app).post('/user/password/reset').query({
email: sampleUser.email,
userid: 'invalid-=uuid',
});

expect(respond.status).toBe(500);
});
Expand All @@ -290,11 +294,11 @@ describe('User service Test', () => {
.post('/user/password/reset')
.query({
email: sampleUser.email,
userid: sampleUser.id
userid: sampleUser.id,
})
.send({
newPassword: 'new-password',
confirmPassword: 'new-password'
confirmPassword: 'new-password',
});

expect(respond.status).toBe(200);
Expand All @@ -304,22 +308,19 @@ describe('User service Test', () => {

describe('User password reset link', () => {
it('should send link to reset password', async () => {
const response = await request(app)
.post('/user/password/reset/link')
.query({ email: sampleUser.email });
const response = await request(app).post('/user/password/reset/link').query({ email: sampleUser.email });

expect(response.status).toBe(200);
});

it('should not send link to reset password, if no email provided', async () => {
const response = await request(app)
.post('/user/password/reset/link');
const response = await request(app).post('/user/password/reset/link');

expect(response.status).toBe(400);
expect(response.body.message).toBe('Missing required field');
});

it('should not send link to reset password, if email doesn\'t exist in DB', async () => {
it("should not send link to reset password, if email doesn't exist in DB", async () => {
const response = await request(app)
.post('/user/password/reset/link')
.query({ email: '[email protected]' });
Expand All @@ -330,7 +331,6 @@ describe('User service Test', () => {
});

describe('Start@FAProcess', () => {

it('should return 400 if not sent email in body on enabling 2fa', async () => {
const data = {};

Expand Down Expand Up @@ -359,7 +359,10 @@ describe('User service Test', () => {
const res = await request(app).post('/user/enable-2fa').send(data);

expect(res.status).toBe(200);
expect(res.body).toEqual({ status: 'success', message: 'Two factor authentication enabled successfully' });
expect(res.body.status).toBe('success');
expect(res.body.data.code).toBe(200);
expect(res.body.data.message).toBe('Two factor authentication enabled successfully');
expect(res.body.data.profile.twoFactorEnabled).toBe(true);
});

it('should return 400 if not sent email in body on disabling 2fa', async () => {
Expand Down Expand Up @@ -390,7 +393,10 @@ describe('User service Test', () => {
const res = await request(app).post('/user/disable-2fa').send(data);

expect(res.status).toBe(200);
expect(res.body).toEqual({ status: 'success', message: 'Two factor authentication disabled successfully' });
expect(res.body.status).toBe('success');
expect(res.body.data.code).toBe(200);
expect(res.body.data.message).toBe('Two factor authentication disabled successfully');
expect(res.body.data.profile.twoFactorEnabled).toBe(false);
});

it('should return 400 if not sent email and otp in body on verifying OTP', async () => {
Expand Down Expand Up @@ -454,12 +460,10 @@ describe('User service Test', () => {
});

it('should login user, if OTP provided is valid', async () => {
const res = await request(app)
.post('/user/verify-otp')
.send({
email: sampleUser3.email,
otp: '123456',
});
const res = await request(app).post('/user/verify-otp').send({
email: sampleUser3.email,
otp: '123456',
});

expect(res.status).toBe(200);
expect(res.body.data.token).toBeDefined();
Expand Down
12 changes: 11 additions & 1 deletion src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { deactivateUserService } from '../services/updateUserStatus/deactivateUs
import { userProfileUpdateServices } from '../services/userServices/userProfileUpdateServices';
import getAllUsers from '../services/userServices/getAllUsers';
import getUserById from '../services/userServices/getUserById';
import getUserProfile from '../services/userServices/getUserProfile';
import userUpdateProfilePicture from '../services/userServices/userUpdateProfileImage';

export const userRegistration = async (req: Request, res: Response) => {
await userRegistrationService(req, res);
Expand Down Expand Up @@ -76,4 +78,12 @@ export const getAllUsersController = async (req: Request, res: Response) => {

export const getUserByIdController = async (req: Request, res: Response) => {
await getUserById(req, res);
};
};

export const getUserProfileController = async (req: Request, res: Response) => {
await getUserProfile(req, res);
};

export const userUpdateProfilePictureController = async (req: Request, res: Response) => {
await userUpdateProfilePicture(req, res);
};
13 changes: 8 additions & 5 deletions src/controllers/productController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
productStatusServices,
viewSingleProduct,
searchProductService,
listAllProductsService,
listAllProductsService,
confirmPayment,
getAllCategories
getAllCategories,
transaction,
} from '../services';

export const readProduct = async (req: Request, res: Response) => {
Expand Down Expand Up @@ -53,12 +54,14 @@ export const singleProduct = async (req: Request, res: Response) => {
await viewSingleProduct(req, res);
};
export const searchProduct = async (req: Request, res: Response) => {
await searchProductService (req, res);

await searchProductService(req, res);
};
export const Payment = async (req: Request, res: Response) => {
await confirmPayment(req, res);
};
export const getAllCategory = async (req: Request, res: Response) => {
await getAllCategories(req, res);
};
};
export const getAllTransaction = async (req: Request, res: Response) => {
await transaction(req, res);
};
Loading

0 comments on commit cbd338f

Please sign in to comment.