Skip to content

Commit

Permalink
fix(bodies): allow empty foundation date for non-locals. Fixes HELP-1…
Browse files Browse the repository at this point in the history
…789 (#371)

* fix(bodies): allow empty foundation date for non-locals. Fixes HELP-1789

* style(eslint): fix linter

* chore(bodies): add extra check based on frontend tests

* fix(bodies): refactor validation to model
  • Loading branch information
WikiRik authored Oct 22, 2021
1 parent 2d0ca2a commit 05372de
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
14 changes: 14 additions & 0 deletions migrations/20211017230916-change-body-founded-at-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.changeColumn(
'bodies',
'founded_at',
Sequelize.DATEONLY,
{ allowNull: true, defaultValue: null }
),
down: (queryInterface, Sequelize) => queryInterface.changeColumn(
'bodies',
'founded_at',
Sequelize.DATEONLY,
{ allowNull: false, defaultValue: Sequelize.fn('NOW') }
),
};
11 changes: 9 additions & 2 deletions models/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,15 @@ const Body = sequelize.define('body', {
},
founded_at: {
type: Sequelize.DATEONLY,
allowNull: false,
defaultValue: Sequelize.fn('NOW')
allowNull: true,
defaultValue: null,
validate: {
isValid(value) {
if (['antenna', 'contact antenna', 'contact'].includes(this.type) && value === null) {
throw new Error('Foundation date should be set');
}
}
}
},
status: {
type: Sequelize.ENUM('active', 'deleted'),
Expand Down
47 changes: 47 additions & 0 deletions test/api/bodies-creating.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,51 @@ describe('Bodies creating', () => {
expect(res.body).toHaveProperty('data');
expect(res.body.data.name).toEqual(body.name);
});

for (const type of ['antenna', 'contact antenna', 'contact']) {
test(`should fail when foundation date is empty on ${type}`, async () => {
const user = await generator.createUser({ username: 'test', mail_confirmed_at: new Date(), superadmin: true });
const token = await generator.createAccessToken({}, user);

await generator.createPermission({ scope: 'global', action: 'create', object: 'body' });

const body = generator.generateBody({ type, founded_at: null });

const res = await request({
uri: '/bodies/',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body
});

expect(res.statusCode).toEqual(422);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('errors');
expect(res.body.errors).toHaveProperty('founded_at');
});
}

for (const type of ['interest group', 'working group', 'commission', 'committee', 'project', 'partner', 'other']) {
test(`should succeed when foundation date is empty on ${type}`, async () => {
const user = await generator.createUser({ username: 'test', mail_confirmed_at: new Date(), superadmin: true });
const token = await generator.createAccessToken({}, user);

await generator.createPermission({ scope: 'global', action: 'create', object: 'body' });

const body = generator.generateBody({ type, founded_at: null });

const res = await request({
uri: '/bodies/',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('data');
});
}
});
47 changes: 47 additions & 0 deletions test/api/bodies-editing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,51 @@ describe('Bodies editing', () => {
expect(res.body.data.email).not.toEqual('test@test.io');
expect(res.body.data.name).toEqual('bbb');
});

for (const type of ['antenna', 'contact antenna', 'contact']) {
test(`should fail when foundation date is empty on ${type}`, async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

await generator.createPermission({ scope: 'global', action: 'update', object: 'body' });

const body = await generator.createBody({ type });

const res = await request({
uri: '/bodies/' + body.id,
method: 'PUT',
headers: { 'X-Auth-Token': token.value },
body: { founded_at: null }
});

expect(res.statusCode).toEqual(422);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('errors');
expect(res.body.errors).toHaveProperty('founded_at');
});
}

for (const type of ['interest group', 'working group', 'commission', 'committee', 'project', 'partner', 'other']) {
test(`should succeed when foundation date is empty on ${type}`, async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);

await generator.createPermission({ scope: 'global', action: 'update', object: 'body' });

const body = await generator.createBody({ type });

const res = await request({
uri: '/bodies/' + body.id,
method: 'PUT',
headers: { 'X-Auth-Token': token.value },
body: { founded_at: null }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('data');
});
}
});
1 change: 1 addition & 0 deletions test/scripts/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ exports.generateBody = (options = {}) => {
if (notSet(options.email)) options.email = faker.internet.email();
if (notSet(options.phone)) options.phone = faker.phone.phoneNumber();
if (notSet(options.address)) options.address = faker.lorem.paragraph();
if (notSet(options.founded_at)) options.founded_at = faker.date.past();

return options;
};
Expand Down

0 comments on commit 05372de

Please sign in to comment.