-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from Code-Hammers/167-che-204-be-controller-t…
…ests-getallprofiles [167] BE CONTROLLER TESTS - getallprofiles
- Loading branch information
Showing
3 changed files
with
116 additions
and
333 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
90 changes: 90 additions & 0 deletions
90
server/controllers/profileController/getAllProfiles/getAllProfiles.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import request from 'supertest'; | ||
import app from '../../../app'; | ||
import Profile from '../../../models/profileModel'; | ||
import User from '../../../models/userModel'; | ||
|
||
const testEmail1 = '[email protected]'; | ||
const testEmail2 = '[email protected]'; | ||
const testPassword = 'password123'; | ||
|
||
//TODO Success tests passed but are disabled until this can be refactored to not ping AWS service | ||
/*eslint jest/no-disabled-tests: "off"*/ | ||
|
||
const createUser = async (email: string) => { | ||
const user = await User.create({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: email, | ||
password: testPassword, | ||
}); | ||
return user; | ||
}; | ||
|
||
const loginAndGetCookie = async () => { | ||
const response = await request(app) | ||
.post('/api/users/login') | ||
.send({ email: testEmail1, password: testPassword }); | ||
return response.headers['set-cookie']; | ||
}; | ||
|
||
// Commented out for linting pass - "defined but never used" | ||
// const createProfile = async (userId: string, profilePhoto: string | null = null) => { | ||
// const profile = await Profile.create({ | ||
// user: userId, | ||
// firstName: 'John', | ||
// lastName: 'Doe', | ||
// email: profilePhoto ? testEmail1 : testEmail2, | ||
// profilePhoto, | ||
// cohort: 'ECRI 44', | ||
// graduationYear: 2022, | ||
// }); | ||
// return profile; | ||
// }; | ||
//TODO Build this test with S3 mocks to avoid pinging the service every time the test is run. | ||
describe('Tests for profileController.getAllProfiles', () => { | ||
const baseUrl = '/api/profiles'; | ||
let authCookie: string; | ||
|
||
// Temporarily removed to avoid pinging AWS on every test run | ||
// beforeEach(async () => { | ||
// await User.deleteMany(); | ||
// await Profile.deleteMany(); | ||
// const user1 = await createUser(testEmail1); | ||
// const user2 = await createUser(testEmail2); | ||
// authCookie = await loginAndGetCookie(); | ||
// await createProfile(user1._id.toString(), 'photo.jpg'); | ||
// await createProfile(user2._id.toString(), null); | ||
// }); | ||
|
||
describe('Get All Profiles Success Tests', () => { | ||
xit('🧪 Retrieves all profiles successfully with a 200 status and processes S3 URLs', async () => { | ||
const response = await request(app).get(baseUrl).set('Cookie', authCookie).send(); | ||
|
||
expect(response.status).toEqual(201); | ||
expect(response.body.length).toEqual(2); | ||
|
||
expect(response.body[0].firstName).toEqual('John'); | ||
expect(response.body[0].email).toEqual(testEmail1); | ||
expect(response.body[0].profilePhoto).toContain('https://s3.amazonaws.com/'); | ||
|
||
expect(response.body[1].firstName).toEqual('John'); | ||
expect(response.body[1].email).toEqual(testEmail2); | ||
expect(response.body[1].profilePhoto).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('Get All Profiles Failure Tests', () => { | ||
beforeEach(async () => { | ||
await Profile.deleteMany(); | ||
}); | ||
|
||
it('🧪 Fails when no profiles are found, returning a 404 status', async () => { | ||
await createUser(testEmail1); | ||
authCookie = await loginAndGetCookie(); | ||
const response = await request(app).get(baseUrl).set('Cookie', authCookie).send(); | ||
|
||
expect(response.status).toEqual(404); | ||
expect(response.body[0].message).toEqual('Not Found'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.