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

Feature/participant amount endpoint #163

Merged
merged 3 commits into from
Jul 21, 2016
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
18 changes: 18 additions & 0 deletions src/common/models/participant.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ export default function (Participant) {

};

Participant.participantAmount = (subCamp, cb) => {
const countParticipants = Promise.promisify(Participant.count, { context: Participant });
const filter = { presence: 3 };
if (subCamp) {
filter.subCamp = subCamp;
}

countParticipants(filter).asCallback(cb);
};

Participant.remoteMethod('massAssignField',
{
http: { path: '/massAssign', verb: 'post' },
Expand All @@ -376,4 +386,12 @@ export default function (Participant) {
returns: { type: 'object', root: true },
}
);

Participant.remoteMethod('participantAmount',
{
http: { path: '/participantAmount', verb: 'get' },
accepts: { arg: 'subCamp', type: 'string', required: false },
returns: { arg: 'amount', type: 'string' },
}
);
}
7 changes: 7 additions & 0 deletions src/common/models/participant.json
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@
"principalId": "registryUser",
"permission": "DENY",
"property": "getParticipantInformationForApp"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW",
"property": "participantAmount"
}
],
"methods": {}
Expand Down
5 changes: 5 additions & 0 deletions test/acl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ describe('http api access control', () => {

it('appInformation: UNAUTHORIZED', () => get('/api/participants/appInformation?memberNumber=1234').expect(UNAUTHORIZED));
it('massedit: UNAUTHORIZED', () => post('/api/participants/massAssign', { ids: [1], newValue: 1, fieldName: 'presence' }).expect(UNAUTHORIZED));
it('participantAmount: OK', () => get('/api/participants/participantAmount').expect(OK));
});

describe('Authenticated user without roles', () => {
Expand All @@ -325,6 +326,7 @@ describe('http api access control', () => {

it('appInformation: UNAUTHORIZED', () => get('/api/participants/appInformation?memberNumber=1234', noRolesAccessToken).expect(UNAUTHORIZED));
it('massedit: UNAUTHORIZED', () => post('/api/participants/massAssign', { ids: [1], newValue: 1, fieldName: 'presence' }, noRolesAccessToken).expect(UNAUTHORIZED));
it('participantAmount: OK', () => get('/api/participants/participantAmount', noRolesAccessToken).expect(OK));
});

describe('registryUser', () => {
Expand All @@ -342,6 +344,7 @@ describe('http api access control', () => {

it('appInformation: UNAUTHORIZED', () => get('/api/participants/appInformation?memberNumber=1234', registryUserAccessToken).expect(UNAUTHORIZED));
it('massedit: UNAUTHORIZED', () => post('/api/participants/massAssign', { ids: [1], newValue: 1, fieldName: 'presence' }, registryUserAccessToken).expect(OK));
it('participantAmount: OK', () => get('/api/participants/participantAmount', registryUserAccessToken).expect(OK));
});

describe('registryAdmin', () => {
Expand All @@ -359,6 +362,7 @@ describe('http api access control', () => {

it('appInformation: UNAUTHORIZED', () => get('/api/participants/appInformation?memberNumber=1234', registryAdminAccessToken).expect(UNAUTHORIZED));
it('massedit: UNAUTHORIZED', () => post('/api/participants/massAssign', { ids: [1], newValue: 1, fieldName: 'presence' }, registryAdminAccessToken).expect(UNAUTHORIZED));
it('participantAmount: OK', () => get('/api/participants/participantAmount', registryAdminAccessToken).expect(OK));
});

describe('roihuapp user', () => {
Expand All @@ -376,6 +380,7 @@ describe('http api access control', () => {

it('appInformation: OK', () => get('/api/participants/appInformation?memberNumber=1234', roihuappUserAccessToken).expect(OK));
it('massedit: UNAUTHORIZED', () => post('/api/participants/massAssign', { ids: [1], newValue: 1, fieldName: 'presence' }, roihuappUserAccessToken).expect(UNAUTHORIZED));
it('participantAmount: OK', () => get('/api/participants/participantAmount', roihuappUserAccessToken).expect(OK));
});
});

Expand Down
84 changes: 84 additions & 0 deletions test/participant-amount-endpoint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import app from '../src/server/server';
import request from 'supertest-as-promised';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import * as testUtils from './utils/test-utils';
import { resetDatabase } from '../scripts/seed-database';

const expect = chai.expect;
chai.use(chaiAsPromised);

describe('Participant amount endpoint test', () => {
const testParticipants = [
{
'participantId': 1,
'firstName': 'Teemu',
'lastName': 'Testihenkilö',
'nonScout': false,
'internationalGuest': false,
'localGroup': 'Testilippukunta',
'campGroup': 'Leirilippukunta',
'village': 'Testikylä',
'subCamp': 'Humina',
'ageGroup': 'sudenpentu',
'memberNumber': 123,
'presence': 3,
},
{
'participantId': 2,
'firstName': 'Tero',
'lastName': 'Esimerkki',
'nonScout': false,
'internationalGuest': false,
'localGroup': 'Testilippukunta',
'campGroup': 'Leirilippukunta',
'village': 'Testikylä',
'subCamp': 'Hurma',
'ageGroup': 'sudenpentu',
'memberNumber': 345,
'presence': 3,
},
{
'participantId': 3,
'firstName': 'Jussi',
'lastName': 'Jukola',
'nonScout': false,
'internationalGuest': false,
'localGroup': 'Testilippukunta',
'campGroup': 'Leirilippukunta',
'village': 'Testikylä',
'subCamp': 'Humina',
'ageGroup': 'seikkailija',
'memberNumber': 859,
'presence': 2,
},
];

beforeEach(() =>
resetDatabase()
.then(() => testUtils.createFixture('Participant', testParticipants))
);

function getParticipantAmount(subCamp) {
const req = request(app)
.get(`/api/Participants/participantAmount`);
if (subCamp) {
req.set('subCamp', subCamp);
}
return req;
}

it('should give amount of present participants in whole camp', () =>
getParticipantAmount().then(res => {
expect(200);
expect(res.body).to.have.property('amount', 2);
})
);

it('should give amount of present participants in one sub camp', () =>
getParticipantAmount('Humina').then(res => {
expect(200);
expect(res.body).to.have.property('amount', 1);
})
);
});