Skip to content

Commit

Permalink
feat(general): join body through campaign
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 26, 2020
1 parent 2bc8680 commit a430583
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/permissions-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class PermissionsManager {
const combinedSplit = combined.split(':');
if (combinedSplit.length === 2) {
return [
'global:' + combinedSplit,
'local:' + combinedSplit,
'join_request:' + combinedSplit,
'global:' + combined,
'local:' + combined,
'join_request:' + combined,
];
}

Expand Down
10 changes: 10 additions & 0 deletions middlewares/campaigns.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
User,
MailConfirmation,
BodyMembership,
Campaign
} = require('../models');
const errors = require('../lib/errors');
Expand All @@ -19,6 +20,15 @@ exports.registerUser = async (req, res) => {
...req.body,
campaign_id: campaign.id
});

// Adding a person to a body if campaign has the autojoin body.
if (campaign.autojoin_body_id) {
await BodyMembership.create({
user_id: user.id,
body_id: campaign.autojoin_body_id
});
}

const confirmation = await MailConfirmation.createForUser(user.id);

return res.json({
Expand Down
28 changes: 28 additions & 0 deletions test/api/campaigns-submission.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { startServer, stopServer } = require('../../lib/server.js');
const { request } = require('../scripts/helpers');
const generator = require('../scripts/generator');
const { BodyMembership } = require('../../models');

describe('Campaign submission', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -172,4 +173,31 @@ describe('Campaign submission', () => {
expect(res.body).toHaveProperty('data');
expect(res.body).not.toHaveProperty('errors');
});

test('should create a body membership if autojoin_body_id is provided', async () => {
const body = await generator.createBody();
const campaign = await generator.createCampaign({ autojoin_body_id: body.id });
const user = generator.generateUser();

const res = await request({
uri: '/signup/' + campaign.url,
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: user
});

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

const membershipFromDb = await BodyMembership.findOne({
where: {
body_id: body.id,
user_id: res.body.data.user.id
}
});

expect(membershipFromDb).not.toEqual(null);
});
});

0 comments on commit a430583

Please sign in to comment.