From 1aaf5d1122d6a5dcb9b404b67fcf6f6298a44a4a Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 11 Aug 2024 09:07:00 -0400 Subject: [PATCH 1/4] CHE-203 Removed try/catch and Next import --- .../createProfile/createProfile.ts | 69 ++++++++----------- 1 file changed, 29 insertions(+), 40 deletions(-) diff --git a/server/controllers/profileController/createProfile/createProfile.ts b/server/controllers/profileController/createProfile/createProfile.ts index 5f324cf..da92a87 100644 --- a/server/controllers/profileController/createProfile/createProfile.ts +++ b/server/controllers/profileController/createProfile/createProfile.ts @@ -33,47 +33,36 @@ const createProfile = async (req: Request, res: Response, next: NextFunction) => blogOrWriting, } = req.body; - try { - const profile = await Profile.create({ - user, - fullName, - nickname, - profilePhoto, - cohort, - graduationYear, - email, - linkedInProfile, - gitHubProfile, - professionalSummary, - skills, - specializations, - careerInformation, - education, - projects, - personalBio, - testimonials, - socialMediaLinks, - availabilityForNetworking, - bootcampExperience, - achievementsAndCertifications, - volunteerWork, - eventParticipation, - gallery, - blogOrWriting, - }); + const profile = await Profile.create({ + user, + fullName, + nickname, + profilePhoto, + cohort, + graduationYear, + email, + linkedInProfile, + gitHubProfile, + professionalSummary, + skills, + specializations, + careerInformation, + education, + projects, + personalBio, + testimonials, + socialMediaLinks, + availabilityForNetworking, + bootcampExperience, + achievementsAndCertifications, + volunteerWork, + eventParticipation, + gallery, + blogOrWriting, + }); - if (profile) { - return res.status(201).json(profile); - } - } catch (error) { - console.error(error); - return next({ - log: 'Express error in createProfile Middleware', - status: 500, - message: { - err: 'An error occurred during profile creation. Please try again.', - }, - }); + if (profile) { + return res.status(201).json(profile); } }; From ad411ebe06685d5d4bdc8297703176932a97abe1 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 11 Aug 2024 12:47:51 -0400 Subject: [PATCH 2/4] CHE-203 Removed createProfile as it is never used. --- .../createProfile/createProfile.ts | 69 ------------------- server/routes/profileRoutes.ts | 8 +-- 2 files changed, 1 insertion(+), 76 deletions(-) delete mode 100644 server/controllers/profileController/createProfile/createProfile.ts diff --git a/server/controllers/profileController/createProfile/createProfile.ts b/server/controllers/profileController/createProfile/createProfile.ts deleted file mode 100644 index da92a87..0000000 --- a/server/controllers/profileController/createProfile/createProfile.ts +++ /dev/null @@ -1,69 +0,0 @@ -import Profile from '../../../models/profileModel'; -import { Request, Response, NextFunction } from 'express'; - -// ENDPOINT POST api/profiles/create -// PURPOSE Create a new profile -// ACCESS Private -const createProfile = async (req: Request, res: Response, next: NextFunction) => { - const { - user, - fullName, - nickname, - profilePhoto, - cohort, - graduationYear, - email, - linkedInProfile, - gitHubProfile, - professionalSummary, - skills, - specializations, - careerInformation, - education, - projects, - personalBio, - testimonials, - socialMediaLinks, - availabilityForNetworking, - bootcampExperience, - achievementsAndCertifications, - volunteerWork, - eventParticipation, - gallery, - blogOrWriting, - } = req.body; - - const profile = await Profile.create({ - user, - fullName, - nickname, - profilePhoto, - cohort, - graduationYear, - email, - linkedInProfile, - gitHubProfile, - professionalSummary, - skills, - specializations, - careerInformation, - education, - projects, - personalBio, - testimonials, - socialMediaLinks, - availabilityForNetworking, - bootcampExperience, - achievementsAndCertifications, - volunteerWork, - eventParticipation, - gallery, - blogOrWriting, - }); - - if (profile) { - return res.status(201).json(profile); - } -}; - -export default createProfile; diff --git a/server/routes/profileRoutes.ts b/server/routes/profileRoutes.ts index b2979a6..6455220 100644 --- a/server/routes/profileRoutes.ts +++ b/server/routes/profileRoutes.ts @@ -1,17 +1,11 @@ import express from 'express'; import { protect } from '../middleware/authMiddleware'; -import { - createProfile, - getAllProfiles, - getProfileById, - updateProfile, -} from '../controllers/profileController'; +import { getAllProfiles, getProfileById, updateProfile } from '../controllers/profileController'; const router = express.Router(); router.use(protect); /* Require Auth for ALL routes below */ -router.post('/', createProfile); router.put('/:userID', updateProfile); router.get('/:userID', getProfileById); router.get('/', getAllProfiles); From b01849c4df5f25bd25abf7f914bb87dc72b94c42 Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 11 Aug 2024 13:32:30 -0400 Subject: [PATCH 3/4] CHE-203 cleaned up some unused imports/exports --- server/controllers/profileController/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/server/controllers/profileController/index.ts b/server/controllers/profileController/index.ts index 1fd77ca..52f63ff 100644 --- a/server/controllers/profileController/index.ts +++ b/server/controllers/profileController/index.ts @@ -1,6 +1,5 @@ -import createProfile from './createProfile/createProfile'; import getAllProfiles from './getAllProfiles/getAllProfiles'; import getProfileById from './getProfileById/getProfileById'; import updateProfile from './updateProfile/updateProfile'; -export { createProfile, getAllProfiles, getProfileById, updateProfile }; +export { getAllProfiles, getProfileById, updateProfile }; From 893251824289c75d6ca472bfa696dcd0271235fe Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 11 Aug 2024 13:40:13 -0400 Subject: [PATCH 4/4] CHE-203 Removed legacy profile controller tests --- __tests__/profileController.test.ts | 296 ---------------------------- 1 file changed, 296 deletions(-) delete mode 100644 __tests__/profileController.test.ts diff --git a/__tests__/profileController.test.ts b/__tests__/profileController.test.ts deleted file mode 100644 index fbdc59b..0000000 --- a/__tests__/profileController.test.ts +++ /dev/null @@ -1,296 +0,0 @@ -import { Request, Response, NextFunction } from 'express'; -import { - createProfile, - updateProfile, - getAllProfiles, - getProfileById, -} from '../server/controllers/profileController'; -import Profile from '../server/models/profileModel'; - -// TODO -/*eslint jest/no-disabled-tests: "off"*/ - -jest.mock('../server/models/profileModel', () => ({ - findOneAndUpdate: jest.fn(), - findOne: jest.fn(), - create: jest.fn(), - find: jest.fn(), -})); - -xdescribe('Profile Controller Tests', () => { - let mockRequest: Partial; - let mockResponse: Partial; - let mockNext: NextFunction = jest.fn(); - - beforeEach(() => { - mockRequest = {}; - mockResponse = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - locals: {}, - }; - }); - - describe('createProfile function', () => { - xit('should handle profile creation', async () => { - (Profile.create as jest.Mock).mockResolvedValue({ - _id: 'someId', - bio: 'I am Code', - job: { - title: 'Senior Developer', - company: 'ACME Corp.', - description: 'Working on various projects...', - date: '2021-04-07T00:00:00.000Z', - }, - socials: { - linkedIn: 'https://www.linkedin.com/in/yourprofile', - github: 'https://github.com/yourprofile', - twitter: 'https://twitter.com/yourprofile', - facebook: 'https://www.facebook.com/yourprofile', - instagram: 'https://www.instagram.com/yourprofile', - }, - }); - - mockRequest.body = { - user: '65117c94f000c9930ef5c0ee', - bio: 'I am Code', - job: { - title: 'Senior Developer', - company: 'ACME Corp.', - description: 'Working on various projects', - date: '2021-04-07T00:00:00.000Z', - }, - socials: { - linkedIn: 'https://www.linkedin.com/in/yourprofile', - github: 'https://github.com/yourprofile', - twitter: 'https://twitter.com/yourprofile', - facebook: 'https://www.facebook.com/yourprofile', - instagram: 'https://www.instagram.com/yourprofile', - }, - }; - - await createProfile(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockResponse.json).toHaveBeenCalledWith( - expect.objectContaining({ - _id: 'someId', - bio: 'I am Code', - job: { - title: 'Senior Developer', - company: 'ACME Corp.', - description: 'Working on various projects...', - date: '2021-04-07T00:00:00.000Z', - }, - socials: { - linkedIn: 'https://www.linkedin.com/in/yourprofile', - github: 'https://github.com/yourprofile', - twitter: 'https://twitter.com/yourprofile', - facebook: 'https://www.facebook.com/yourprofile', - instagram: 'https://www.instagram.com/yourprofile', - }, - }), - ); - }); - }); - - describe('updateProfile function', () => { - beforeEach(() => { - jest.clearAllMocks(); - mockRequest = { - params: { userID: '65117c94f000c9930ef5c0ee' }, - body: { - availabilityForNetworking: true, - careerInformation: { - currentPosition: { - company: 'CodeHammers', - title: 'Developer', - }, - pastPositions: [ - { - company: 'CodeHammers', - title: 'Junior Developer', - startDate: '2020-01-01', - endDate: '2021-01-01', - }, - ], - }, - cohort: 'ECRI-TEST', - email: 'test@test.com', - gitHubProfile: 'Ghub', - linkedInProfile: 'Lin', - nickName: 'Johnny', - personalBio: 'I love dogs!', - skills: ['Javascript', 'Typescript', 'React', 'Nodejs'], - socialMediaLinks: 'SMlinks', - specializations: ['Javascript', 'React'], - }, - }; - mockResponse = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - }; - mockNext = jest.fn(); - }); - - it('should handle profile update', async () => { - (Profile.findOneAndUpdate as jest.Mock).mockResolvedValue({ - _id: '65117c94f000c9930ef5c0ee', - availabilityForNetworking: true, - careerInformation: { - currentPosition: { - company: 'CodeHammers', - title: 'Developer', - }, - pastPositions: [ - { - company: 'CodeHammers', - title: 'Junior Developer', - startDate: '2020-01-01', - endDate: '2021-01-01', - }, - ], - }, - cohort: 'ECRI-TEST', - email: 'test@test.com', - gitHubProfile: 'Ghub', - linkedInProfile: 'Lin', - nickName: 'Johnny', - personalBio: 'I love dogs!', - skills: ['Javascript', 'Typescript', 'React', 'Nodejs'], - socialMediaLinks: 'SMlinks', - specializations: ['Javascript', 'React'], - }); - - await updateProfile(mockRequest as Request, mockResponse as Response, mockNext); - - expect(Profile.findOneAndUpdate).toHaveBeenCalledWith( - { user: '65117c94f000c9930ef5c0ee' }, - mockRequest.body, - { new: true }, - ); - expect(mockResponse.status).toHaveBeenCalledWith(200); - expect(mockResponse.json).toHaveBeenCalledWith(expect.any(Object)); - }); - - it('should handle errors in profile updating', async () => { - (Profile.findOneAndUpdate as jest.Mock).mockRejectedValue(new Error('Update failed')); - - await updateProfile(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith(expect.anything()); - }); - - it('should handle the case where no profile is found', async () => { - (Profile.findOneAndUpdate as jest.Mock).mockResolvedValue(null); - - await updateProfile(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith( - expect.objectContaining({ - log: 'Express error in updateProfile Middleware - NO PROFILE FOUND', - status: 404, - message: { err: 'An error occurred during profile update' }, - }), - ); - }); - }); - - describe('getAllProfiles function', () => { - xit('should handle successful retrieval of all profiles', async () => { - const mockProfiles = [ - { _id: '1', user: 'user1', bio: 'Bio 1' }, - { _id: '2', user: 'user2', bio: 'Bio 2' }, - ]; - (Profile.find as jest.Mock).mockResolvedValue(mockProfiles); - - await getAllProfiles(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockResponse.status).toHaveBeenCalledWith(201); - expect(mockResponse.json).toHaveBeenCalledWith(mockProfiles); - }); - - it('should handle no profiles found', async () => { - (Profile.find as jest.Mock).mockResolvedValue([]); - - await getAllProfiles(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith( - expect.objectContaining({ - log: 'There are no profiles to retrieve', - status: 404, - message: { err: 'There were no profiles to retrieve' }, - }), - ); - }); - - it('should handle errors during profile retrieval', async () => { - const errorMessage = { message: 'Error finding profiles' }; - (Profile.find as jest.Mock).mockRejectedValue(errorMessage); - - await getAllProfiles(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith( - expect.objectContaining({ - log: 'Express error in getAllProfiles Middleware', - status: 500, - message: { err: 'An error occurred during profile creation' }, - }), - ); - }); - }); - - describe('getProfileById function', () => { - beforeEach(() => { - jest.clearAllMocks(); - mockRequest = { params: { userID: 'someUserId' } }; - mockResponse = { - status: jest.fn().mockReturnThis(), - json: jest.fn(), - }; - mockNext = jest.fn(); - }); - - it('should handle successful profile retrieval', async () => { - const mockProfile = { - _id: 'someUserId', - bio: 'User Bio', - //ABBRIEVIATED PROFILE OBJECT - }; - (Profile.findOne as jest.Mock).mockResolvedValue(mockProfile); - - await getProfileById(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockResponse.status).toHaveBeenCalledWith(200); - expect(mockResponse.json).toHaveBeenCalledWith(mockProfile); - }); - - it('should handle profile not found', async () => { - (Profile.findOne as jest.Mock).mockResolvedValue(null); - - await getProfileById(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith( - expect.objectContaining({ - log: 'Profile does not exist', - status: 404, - message: { err: 'An error occurred during profile retrieval' }, - }), - ); - }); - - it('should handle errors', async () => { - const errorMessage = { message: 'Error finding profile' }; - (Profile.findOne as jest.Mock).mockRejectedValue(errorMessage); - - await getProfileById(mockRequest as Request, mockResponse as Response, mockNext); - - expect(mockNext).toHaveBeenCalledWith( - expect.objectContaining({ - log: 'Express error in getProfileById Middleware', - status: 500, - message: { err: 'An error occurred during profile retrieval' }, - }), - ); - }); - }); -});