habitica/test/api/v3/integration/coupons/POST-coupons_generate_event.test.js
Phillip Thelen f8d315ff6e
Upgrade to mongoose 7 (#14971)
* remove some unused dependencies

* update mongoose version

* make common tests pass

* Make unit tests pass

* make api v3 integration tests pass

* fix lint issues

* fix issue with package-lock

* fix(lint): we don't need no .js

* fix(lint): update to latest config-habitrpg

* chore(npm): update package locks

* fix(test): replace deprecated fn

* chore(package): update eslint-habitrpg again

* fix(lint): server linting

* fix(lint): client linting

* fix(client): correct mangled common imports

* chore(npm): update package-locks

* fix(lint): punctuation, module

---------

Co-authored-by: SabreCat <sabrecat@gmail.com>
Co-authored-by: SabreCat <sabe@habitica.com>
2024-01-16 15:18:47 -06:00

59 lines
1.6 KiB
JavaScript

import couponCode from 'coupon-code';
import {
generateUser,
translate as t,
resetHabiticaDB,
} from '../../../../helpers/api-integration/v3';
import apiError from '../../../../../website/server/libs/apiError';
describe('POST /coupons/generate/:event', () => {
let user;
before(async () => {
await resetHabiticaDB();
});
beforeEach(async () => {
user = await generateUser({
'permissions.coupons': true,
});
});
it('returns an error if user has no coupons permission', async () => {
await user.updateOne({
'permissions.coupons': false,
});
await expect(user.post('/coupons/generate/aaa')).to.eventually.be.rejected.and.eql({
code: 401,
error: 'NotAuthorized',
message: apiError('noPrivAccess'),
});
});
it('returns an error if event is invalid', async () => {
await expect(user.post('/coupons/generate/notValid?count=1')).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: 'Coupon validation failed',
});
});
it('returns an error if count is missing', async () => {
await expect(user.post('/coupons/generate/notValid')).to.eventually.be.rejected.and.eql({
code: 400,
error: 'BadRequest',
message: t('invalidReqParams'),
});
});
it('should generate coupons', async () => {
await user.updateOne({
'permissions.coupons': true,
});
const coupons = await user.post('/coupons/generate/wondercon?count=2');
expect(coupons.length).to.equal(2);
expect(coupons[0].event).to.equal('wondercon');
expect(couponCode.validate(coupons[1]._id)).to.not.equal(''); // '' means invalid
});
});