Skip to content

Commit

Permalink
feat(general): body campaigns filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Mar 18, 2020
1 parent cf32e85 commit 3f90a01
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 1 deletion.
5 changes: 4 additions & 1 deletion middlewares/body-campaigns.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ exports.listAllCampaigns = async (req, res) => {
}

const result = await Campaign.findAndCountAll({
where: { autojoin_body_id: req.currentBody.id },
where: {
autojoin_body_id: req.currentBody.id,
...helpers.filterBy(req.query.query, ['name', 'url', 'description_short', 'description_long'])
},
...helpers.getPagination(req.query),
order: helpers.getSorting(req.query)
});
Expand Down
288 changes: 288 additions & 0 deletions test/api/body-campaigns-listing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,292 @@ describe('Body campaigns listing', () => {
expect(res.body.data[0].id).toEqual(secondCampaign.id);
expect(res.body.data[1].id).toEqual(firstCampaign.id);
});

test('should find by name case-sensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'test1',
description_short: 'ZZZ',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'test2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?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(campaign.id);
});

test('should find by name case-insensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'test1',
description_short: 'ZZZ',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'test2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?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(campaign.id);
});

test('should find by url case-sensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'TEST1',
description_short: 'ZZZ',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'TEST2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=TEST1',
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(campaign.id);
});

test('should find by url case-insensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'test1',
description_short: 'ZZZ',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'test2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=test1',
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(campaign.id);
});

test('should find by description_short case-sensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'url',
description_short: 'TEST1',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'url2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=TEST1',
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(campaign.id);
});

test('should find by description_short case-insensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'zzz1',
description_short: 'TEST1',
description_long: 'ZZZ'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'zzz2',
description_short: 'TEST2',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=test1',
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(campaign.id);
});

test('should find by description_long case-sensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'url',
description_short: 'ZZZ',
description_long: 'TEST1'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'url2',
description_short: 'ZZZ',
description_long: 'ZZZ'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=TEST1',
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(campaign.id);
});

test('should find by description_short case-insensitive', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken({}, user);
const body = await generator.createBody();

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

const campaign = await generator.createCampaign({
autojoin_body_id: body.id,
name: 'AAA',
url: 'zzz1',
description_short: 'zzz',
description_long: 'TEST1'
});
await generator.createCampaign({
autojoin_body_id: body.id,
name: 'BBB',
url: 'zzz2',
description_short: 'zzz',
description_long: 'zzz'
});

const res = await request({
uri: '/bodies/' + body.id + '/campaigns?query=test1',
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(campaign.id);
});
});

0 comments on commit 3f90a01

Please sign in to comment.