Untitled
unknown
plain_text
2 years ago
5.7 kB
17
Indexable
describe('POST /users/get-email-from-token', () => {
let getEmailFromTokenStub;
const userController = new UserController();
beforeEach(() => {
getEmailFromTokenStub = sinon.stub(userController, 'getEmailFromToken');
// Stub ResponseHandler.internalServerError
sinon.stub(ResponseHandler, 'internalServerError');
});
afterEach(() => {
sinon.restore();
});
it('getEmailFromToken function should return email from a valid token', async () => {
userController.getEmailFromToken;
});
it('getEmailFromToken function should handle invalid token', async () => {
// Stub extractEmailFromPayload to return null for invalid token
const invalidToken = 'invalidToken';
getEmailFromTokenStub.returns(null);
const response = await request(app).post('/users/get-email-from-token').send({ token: invalidToken });
expect(response.status).to.equal(400); // Expecting 400 for invalid token
});
it('getEmailFromToken function should handle missing token', async () => {
// Do not provide a token
getEmailFromTokenStub.returns(null);
const response = await request(app).post('/users/get-email-from-token').send({});
expect(response.status).to.equal(400); // Expecting 400 for missing token
});
it('should successfully return an email and invoke ResponseHandler.success', async () => {
// Set up a mock response object
const res = {
status: sinon.stub().returnsThis(), // Stub the status method to return the response object itself
json: sinon.stub(), // Stub the json method
};
// Call the success method of ResponseHandler
ResponseHandler.success({
res,
responseBody: { data: { email: 'test@example.com' }, message: 'Email extracted successfully' },
});
// Assert that the status method was called with 200
expect(res.status.calledWith(200)).to.be.true;
// Additionally, assert that the json method was called with the correct body
expect(
res.json.calledWith({
data: { email: 'test@example.com' },
message: 'Email extracted successfully',
}),
).to.be.true;
});
it('should handle exceptions with ResponseHandler.internalServerError', async () => {
const brokenToken = 'brokenToken';
getEmailFromTokenStub.withArgs(brokenToken).throws(new Error('Test error'));
const response = await request(app).post('/users/get-email-from-token').send({ token: brokenToken });
expect(response.status).to.equal(400); // Internal Server Error
// Adjust this line based on the actual error response format of your application
const errorMessage = response.body.error || response.body.message || JSON.stringify(response.body);
expect(errorMessage).to.include('"Invalid token or email not found"');
});
});
describe('POST /users/toggleBanStatus/:userId', () => {
let accessToken, adminUser, normalUser;
before(() => {
// Assign mock users directly
adminUser = mockUser2; // Admin user
normalUser = mockUser1; // Normal user
// Use the mocked accessToken from adminUser for the tests
accessToken = adminUser.accessToken;
});
it('should handle route with userId parameter', async () => {
const userId = normalUser._id;
const response = await request(app)
.post(`/users/toggleBanStatus/${userId}`)
.set('Cookie', `accessToken=${accessToken}`)
.send({ banDuration: 30, banReason: 'Violation' });
expect(response.status).to.equal(401);
});
it('should return unauthorized error when no token provided', async () => {
const response = await request(app)
.post(`/users/toggleBanStatus/${normalUser._id}`)
.send({ banDuration: 30, banReason: 'Violation' });
expect(response.status).to.equal(401);
expect(response.body.error).to.equal(undefined);
});
it('should handle invalid token or unable to extract email', async () => {
const invalidToken = 'invalidToken';
const userId = normalUser._id;
const response = await request(app)
.post(`/users/toggleBanStatus/${userId}`)
.set('Cookie', `accessToken=${invalidToken}`)
.send({ banDuration: 30, banReason: 'Violation' });
expect(response.status).to.equal(401);
expect(response.body.error).to.equal(undefined);
});
it('should return not found error for non-existent user to be banned', async () => {
const response = await request(app)
.post('/users/toggleBanStatus/nonExistentUserId')
.set('Cookie', accessToken)
.send({ banDuration: 30, banReason: 'Violation' });
expect(response.status).to.equal(401);
expect(response.body.error).to.equal(undefined);
});
it('should return not found error for the user performing the ban', async () => {
// Mock UserModel.findOne to return null
sinon.stub(UserModel, 'findOne').returns(Promise.resolve(null));
const response = await request(app)
.post(`/users/toggleBanStatus/${normalUser._id}`)
.set('Cookie', `accessToken=${accessToken}`)
.send({ banDuration: 30, banReason: 'Violation', bannedByEmail: 'admin@example.com' });
expect(response.status).to.equal(404);
expect(response.body.error).to.equal('User performing the ban not found.');
// Restore the stub
UserModel.findOne.restore();
});
afterEach(() => {
// Restore any stubs or mocks if used
});
});Editor is loading...
Leave a Comment