Files
habitica/test/api/v3/integration/emails/GET-email-unsubscribe.test.js
Matteo Pagliazzi 9a07ba7417 Better XSS Fix (#12483)
* Revert "fix(test): adjust expectations"

This reverts commit 205436d5b1.

* Revert "fix(escaping): global inoffensive apostrophe"

This reverts commit 2b8f94b244.

* change <%- to <%=

* fix interpolation only where necessary

* remove unused variable
2020-08-20 13:41:46 -05:00

69 lines
2.0 KiB
JavaScript

import { v4 as generateUUID } from 'uuid';
import {
generateUser,
translate as t,
} from '../../../../helpers/api-integration/v3';
import { encrypt } from '../../../../../website/server/libs/encryption';
describe('GET /email/unsubscribe', () => {
let user;
const testEmail = 'test@habitica.com';
beforeEach(async () => {
user = await generateUser();
});
it('return error when code is not provided', async () => {
await expect(user.get('/email/unsubscribe')).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'Invalid request parameters.',
});
});
it('return error when user is not found', async () => {
const code = encrypt(JSON.stringify({
_id: generateUUID(),
}));
await expect(user.get(`/email/unsubscribe?code=${code}`)).to.eventually.be.rejected.and.eql({
code: 404,
error: 'NotFound',
message: t('userNotFound'),
});
});
it('unsubscribes a user from email notifications', async () => {
const code = encrypt(JSON.stringify({
_id: user._id,
email: user.email,
}));
await user.get(`/email/unsubscribe?code=${code}`);
const unsubscribedUser = await user.get('/user');
expect(unsubscribedUser.preferences.emailNotifications.unsubscribeFromAll).to.be.true;
});
it('unsubscribes an email from notifications', async () => {
const code = encrypt(JSON.stringify({
email: testEmail,
}));
const unsubscribedMessage = await user.get(`/email/unsubscribe?code=${code}`);
expect(unsubscribedMessage).to.equal('<h1>Unsubscribed successfully!</h1> You won\'t receive any other email from Habitica.');
});
it('returns okay when email is already unsubscribed', async () => {
const code = encrypt(JSON.stringify({
email: testEmail,
}));
const unsubscribedMessage = await user.get(`/email/unsubscribe?code=${code}`);
expect(unsubscribedMessage).to.equal('<h1>Unsubscribed successfully!</h1> You won\'t receive any other email from Habitica.');
});
});