Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[167] BE CONTROLLER TESTS - getallprofiles #177

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
296 changes: 0 additions & 296 deletions __tests__/profileController.test.ts

This file was deleted.

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');
});
});
});
Loading
Loading