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

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

Merged
merged 4 commits into from
Oct 22, 2021
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
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should convert it to enum at some point, with a method isLocal that can be used here, but that's for the future development and doesn't block this

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree

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