Skip to content

Commit

Permalink
feat(general): memberships filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Mar 21, 2020
1 parent aac2a27 commit 2d64fa6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
6 changes: 5 additions & 1 deletion middlewares/memberships.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
User
} = require('../models');
const helpers = require('../lib/helpers');
const constants = require('../lib/constants');
const errors = require('../lib/errors');

exports.listAllMemberships = async (req, res) => {
Expand All @@ -12,7 +13,10 @@ exports.listAllMemberships = async (req, res) => {
}

const result = await BodyMembership.findAndCountAll({
where: { body_id: req.currentBody.id },
where: {
body_id: req.currentBody.id,
...helpers.filterBy(req.query.query, constants.FIELDS_TO_QUERY.BODY_MEMBERSHIP)
},
...helpers.getPagination(req.query),
order: helpers.getSorting(req.query),
include: [User]
Expand Down
108 changes: 108 additions & 0 deletions test/api/memberships-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,112 @@ describe('Memberships list', () => {
expect(res.body).toHaveProperty('message');
expect(res.body).not.toHaveProperty('data');
});

test('should find by user first name', async () => {
const user = await generator.createUser({
superadmin: true,
first_name: 'aaa',
last_name: 'zzz',
email: 'test@test.io'
});
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();
const membership = await generator.createBodyMembership(body, user);

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

await generator.createUser({
superadmin: true,
first_name: 'zzz',
last_name: 'zzz',
email: 'test2@test.io'
});

const res = await request({
uri: '/bodies/' + body.id + '/members?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(membership.id);
});

test('should find by user last name', async () => {
const user = await generator.createUser({
superadmin: true,
first_name: 'zzz',
last_name: 'aaa',
email: 'test@test.io'
});
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();
const membership = await generator.createBodyMembership(body, user);

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

await generator.createUser({
superadmin: true,
first_name: 'zzz',
last_name: 'zzz',
email: 'test2@test.io'
});

const res = await request({
uri: '/bodies/' + body.id + '/members?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(membership.id);
});

test('should find by user email', async () => {
const user = await generator.createUser({
superadmin: true,
first_name: 'zzz',
last_name: 'zzz',
email: 'aaa@test.io'
});
const token = await generator.createAccessToken({}, user);

const body = await generator.createBody();
const membership = await generator.createBodyMembership(body, user);

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

await generator.createUser({
superadmin: true,
first_name: 'zzz',
last_name: 'zzz',
email: 'test@test.io'
});

const res = await request({
uri: '/bodies/' + body.id + '/members?query=aaa',
method: 'GET',
headers: { 'X-Auth-Token': token.value }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');

expect(res.body.data.length).toEqual(1);
expect(res.body.data[0].id).toEqual(membership.id);
});
});

0 comments on commit 2d64fa6

Please sign in to comment.