Skip to content

Commit 4de8417

Browse files
author
Vikas Agarwal
committed
Allowed zero in budget, spentBudget, progress and duration
1 parent af3922b commit 4de8417

File tree

4 files changed

+80
-20
lines changed

4 files changed

+80
-20
lines changed

src/routes/phases/create.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const addProjectPhaseValidations = {
1616
status: Joi.string().required(),
1717
startDate: Joi.date().max(Joi.ref('endDate')).optional(),
1818
endDate: Joi.date().optional(),
19-
duration: Joi.number().positive().optional(),
20-
budget: Joi.number().positive().optional(),
21-
spentBudget: Joi.number().positive().optional(),
22-
progress: Joi.number().positive().optional(),
19+
duration: Joi.number().min(0).optional(),
20+
budget: Joi.number().min(0).optional(),
21+
spentBudget: Joi.number().min(0).optional(),
22+
progress: Joi.number().min(0).optional(),
2323
details: Joi.any().optional(),
2424
}).required(),
2525
},

src/routes/phases/create.spec.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@ const body = {
1515
endDate: '2018-05-15T12:00:00Z',
1616
budget: 20.0,
1717
progress: 1.23456,
18+
spentBudget: 10.0,
19+
duration: 10,
1820
details: {
1921
message: 'This can be any json',
2022
},
2123
};
2224

25+
const validatePhase = (resJson, expectedPhase) => {
26+
should.exist(resJson);
27+
resJson.name.should.be.eql(expectedPhase.name);
28+
resJson.status.should.be.eql(expectedPhase.status);
29+
resJson.budget.should.be.eql(expectedPhase.budget);
30+
resJson.progress.should.be.eql(expectedPhase.progress);
31+
resJson.details.should.be.eql(expectedPhase.details);
32+
};
33+
2334
describe('Project Phases', () => {
2435
let projectId;
2536
const memberUser = {
@@ -190,12 +201,32 @@ describe('Project Phases', () => {
190201
done(err);
191202
} else {
192203
const resJson = res.body.result.content;
193-
should.exist(resJson);
194-
resJson.name.should.be.eql(body.name);
195-
resJson.status.should.be.eql(body.status);
196-
resJson.budget.should.be.eql(body.budget);
197-
resJson.progress.should.be.eql(body.progress);
198-
resJson.details.should.be.eql(body.details);
204+
validatePhase(resJson, body);
205+
done();
206+
}
207+
});
208+
});
209+
210+
it('should return 201 if payload is valid (0 for non negative numbers)', (done) => {
211+
const bodyWithZeros = _.cloneDeep(body);
212+
bodyWithZeros.duration = 0;
213+
bodyWithZeros.spentBudget = 0.0;
214+
bodyWithZeros.budget = 0.0;
215+
bodyWithZeros.progress = 0.0;
216+
request(server)
217+
.post(`/v4/projects/${projectId}/phases/`)
218+
.set({
219+
Authorization: `Bearer ${testUtil.jwts.copilot}`,
220+
})
221+
.send({ param: bodyWithZeros })
222+
.expect('Content-Type', /json/)
223+
.expect(201)
224+
.end((err, res) => {
225+
if (err) {
226+
done(err);
227+
} else {
228+
const resJson = res.body.result.content;
229+
validatePhase(resJson, bodyWithZeros);
199230
done();
200231
}
201232
});

src/routes/phases/update.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const updateProjectPhaseValidation = {
1717
status: Joi.string().optional(),
1818
startDate: Joi.date().optional(),
1919
endDate: Joi.date().optional(),
20-
duration: Joi.number().positive().optional(),
21-
budget: Joi.number().positive().optional(),
22-
spentBudget: Joi.number().positive().optional(),
23-
progress: Joi.number().positive().optional(),
20+
duration: Joi.number().min(0).optional(),
21+
budget: Joi.number().min(0).optional(),
22+
spentBudget: Joi.number().min(0).optional(),
23+
progress: Joi.number().min(0).optional(),
2424
details: Joi.any().optional(),
2525
}).required(),
2626
},

src/routes/phases/update.spec.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ const updateBody = {
3434
},
3535
};
3636

37+
const validatePhase = (resJson, expectedPhase) => {
38+
should.exist(resJson);
39+
resJson.name.should.be.eql(expectedPhase.name);
40+
resJson.status.should.be.eql(expectedPhase.status);
41+
resJson.budget.should.be.eql(expectedPhase.budget);
42+
resJson.progress.should.be.eql(expectedPhase.progress);
43+
resJson.details.should.be.eql(expectedPhase.details);
44+
};
45+
3746
describe('Project Phases', () => {
3847
let projectId;
3948
let phaseId;
@@ -187,12 +196,32 @@ describe('Project Phases', () => {
187196
done(err);
188197
} else {
189198
const resJson = res.body.result.content;
190-
should.exist(resJson);
191-
resJson.name.should.be.eql(updateBody.name);
192-
resJson.status.should.be.eql(updateBody.status);
193-
resJson.budget.should.be.eql(updateBody.budget);
194-
resJson.progress.should.be.eql(updateBody.progress);
195-
resJson.details.should.be.eql(updateBody.details);
199+
validatePhase(resJson, updateBody);
200+
done();
201+
}
202+
});
203+
});
204+
205+
it('should return updated phase when parameters are valid (0 for non -ve numbers)', (done) => {
206+
const bodyWithZeros = _.cloneDeep(updateBody);
207+
bodyWithZeros.duration = 0;
208+
bodyWithZeros.spentBudget = 0.0;
209+
bodyWithZeros.budget = 0.0;
210+
bodyWithZeros.progress = 0.0;
211+
request(server)
212+
.patch(`/v4/projects/${projectId}/phases/${phaseId}`)
213+
.set({
214+
Authorization: `Bearer ${testUtil.jwts.copilot}`,
215+
})
216+
.send({ param: bodyWithZeros })
217+
.expect('Content-Type', /json/)
218+
.expect(200)
219+
.end((err, res) => {
220+
if (err) {
221+
done(err);
222+
} else {
223+
const resJson = res.body.result.content;
224+
validatePhase(resJson, bodyWithZeros);
196225
done();
197226
}
198227
});

0 commit comments

Comments
 (0)