From 3916ceb4d11af6b594047440e2472588f3c55263 Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Wed, 13 Nov 2019 17:52:14 -0300 Subject: [PATCH 1/7] Fix bug in update users institution_id with null value --- package.json | 2 +- src/application/domain/model/institution.ts | 2 +- src/application/domain/validator/update.user.validator.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index ded88c9..56989b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "account", - "version": "2.0.6", + "version": "2.0.7", "description": "Microservice responsible for user management and authentication on the OCARIoT platform.", "main": "dist/server.js", "scripts": { diff --git a/src/application/domain/model/institution.ts b/src/application/domain/model/institution.ts index 445d427..a689b30 100644 --- a/src/application/domain/model/institution.ts +++ b/src/application/domain/model/institution.ts @@ -67,7 +67,7 @@ export class Institution extends Entity implements IJSONSerializable, IJSONDeser } if (json.institution_id !== undefined) { - super.id = json.institution_id ? json.institution_id : undefined + super.id = json.institution_id ? json.institution_id : '' return this } diff --git a/src/application/domain/validator/update.user.validator.ts b/src/application/domain/validator/update.user.validator.ts index 03dc51e..c9706ca 100644 --- a/src/application/domain/validator/update.user.validator.ts +++ b/src/application/domain/validator/update.user.validator.ts @@ -14,7 +14,7 @@ export class UpdateUserValidator { } } if (user.username !== undefined) StringValidator.validate(user.username, 'username') - if (user.institution && user.institution.id) { + if (user.institution && user.institution.id !== undefined) { try { ObjectIdValidator.validate(user.institution.id, Strings.INSTITUTION.PARAM_ID_NOT_VALID_FORMAT) } catch (err) { From 7bf3f04539b628eae7c7b52a5fba7c170096b771 Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Thu, 14 Nov 2019 13:56:38 -0300 Subject: [PATCH 2/7] Fix bugs in children attribute of Family and ChildrenGroup models Defining exceptions for non-array values in children attribute, and for invalid values passed within the array --- src/application/domain/model/child.ts | 2 +- src/application/domain/model/children.group.ts | 7 ++++--- src/application/domain/model/family.ts | 7 ++++--- .../validator/create.children.group.validator.ts | 8 +++++++- .../domain/validator/create.family.validator.ts | 8 +++++++- .../validator/update.children.group.validator.ts | 7 ++++++- .../domain/validator/update.family.validator.ts | 8 +++++++- src/utils/strings.ts | 5 ++++- test/integration/routes/family.controller.spec.ts | 5 ++--- test/unit/models/institution.model.spec.ts | 2 +- test/unit/services/children.group.service.spec.ts | 14 ++++++-------- test/unit/services/educator.service.spec.ts | 10 ++++------ test/unit/services/family.service.spec.ts | 6 ++---- .../services/health.professional.service.spec.ts | 10 ++++------ .../create.children.group.validator.spec.ts | 5 ++--- .../validators/create.family.validator.spec.ts | 5 ++--- 16 files changed, 63 insertions(+), 46 deletions(-) diff --git a/src/application/domain/model/child.ts b/src/application/domain/model/child.ts index be61fff..c9d0bba 100644 --- a/src/application/domain/model/child.ts +++ b/src/application/domain/model/child.ts @@ -69,7 +69,7 @@ export class Child extends User implements IJSONSerializable, IJSONDeserializabl } public fromJSON(json: any): Child { - if (!json) return this + if (!json || json === 'null') return this super.fromJSON(json) if (typeof json === 'string') { diff --git a/src/application/domain/model/children.group.ts b/src/application/domain/model/children.group.ts index 3493c17..5d2860f 100644 --- a/src/application/domain/model/children.group.ts +++ b/src/application/domain/model/children.group.ts @@ -34,7 +34,7 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes } set children(value: Array | undefined) { - this._children = value ? this.removesRepeatedChildren(value) : value + this._children = value && value instanceof Array ? this.removesRepeatedChildren(value) : value } get school_class(): string | undefined { @@ -79,8 +79,9 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes if (json.id !== undefined) super.id = json.id if (json.name !== undefined) this.name = json.name if (json.school_class !== undefined) this.school_class = json.school_class - if (json.children !== undefined && json.children instanceof Array) { - this.children = json.children.map(child => new Child().fromJSON(child)) + if (json.children !== undefined) { + if (json.children instanceof Array) this.children = json.children.map(child => new Child().fromJSON(child)) + else this.children = json.children } return this diff --git a/src/application/domain/model/family.ts b/src/application/domain/model/family.ts index 4df76f3..a6edb3c 100644 --- a/src/application/domain/model/family.ts +++ b/src/application/domain/model/family.ts @@ -52,7 +52,7 @@ export class Family extends User implements IJSONSerializable, IJSONDeserializab } set children(value: Array | undefined) { - this._children = value ? this.removesRepeatedChildren(value) : value + this._children = value && value instanceof Array ? this.removesRepeatedChildren(value) : value } public addChild(child: Child): void { @@ -75,8 +75,9 @@ export class Family extends User implements IJSONSerializable, IJSONDeserializab json = JSON.parse(json) } - if (json.children !== undefined && json.children instanceof Array) { - this.children = json.children.map(child => new Child().fromJSON(child)) + if (json.children !== undefined) { + if (json.children instanceof Array) this.children = json.children.map(child => new Child().fromJSON(child)) + else this.children = json.children } return this diff --git a/src/application/domain/validator/create.children.group.validator.ts b/src/application/domain/validator/create.children.group.validator.ts index 776770f..0fa6521 100644 --- a/src/application/domain/validator/create.children.group.validator.ts +++ b/src/application/domain/validator/create.children.group.validator.ts @@ -20,12 +20,18 @@ export class CreateChildrenGroupValidator { if (!childrenGroup.user || !childrenGroup.user.id) fields.push('user') else ObjectIdValidator.validate(childrenGroup.user.id) + if (childrenGroup.children !== undefined && !(childrenGroup.children instanceof Array)) { + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + 'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + } + if (!childrenGroup.children || !childrenGroup.children.length) { fields.push('Collection with children IDs') } else { childrenGroup.children.forEach(child => { if (!child.id) { - fields.push('Collection with children IDs (ID can not be empty)') + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } else { try { ObjectIdValidator.validate(child.id) diff --git a/src/application/domain/validator/create.family.validator.ts b/src/application/domain/validator/create.family.validator.ts index 950f732..376fc4b 100644 --- a/src/application/domain/validator/create.family.validator.ts +++ b/src/application/domain/validator/create.family.validator.ts @@ -16,12 +16,18 @@ export class CreateFamilyValidator { fields.push(err.description.split(',')) } + if (family.children !== undefined && !(family.children instanceof Array)) { + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + 'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + } + if (!family.children || !family.children.length) { fields.push('Collection with children IDs') } else { family.children.forEach(child => { if (!child.id) { - fields.push('Collection with children IDs (ID can not be empty)') + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } else { try { ObjectIdValidator.validate(child.id) diff --git a/src/application/domain/validator/update.children.group.validator.ts b/src/application/domain/validator/update.children.group.validator.ts index 8e2e827..63cd4ef 100644 --- a/src/application/domain/validator/update.children.group.validator.ts +++ b/src/application/domain/validator/update.children.group.validator.ts @@ -16,10 +16,15 @@ export class UpdateChildrenGroupValidator { if (childrenGroup.school_class !== undefined) { StringValidator.validate(childrenGroup.school_class, 'school_class') } + if (childrenGroup.children !== undefined && !(childrenGroup.children instanceof Array)) { + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + 'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + } if (childrenGroup.children && childrenGroup.children.length > 0) { childrenGroup.children.forEach(child => { if (!child.id) { - fields.push('Collection with children IDs (ID can not be empty)') + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } else { try { ObjectIdValidator.validate(child.id) diff --git a/src/application/domain/validator/update.family.validator.ts b/src/application/domain/validator/update.family.validator.ts index e43353a..f30cfdf 100644 --- a/src/application/domain/validator/update.family.validator.ts +++ b/src/application/domain/validator/update.family.validator.ts @@ -22,10 +22,16 @@ export class UpdateFamilyValidator { throw err } + if (family.children !== undefined && !(family.children instanceof Array)) { + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + 'children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + } + if (family.children && family.children.length > 0) { family.children.forEach(child => { if (!child.id) { - fields.push('Collection with children IDs (ID can not be empty)') + throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, + Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } else { try { ObjectIdValidator.validate(child.id) diff --git a/src/utils/strings.ts b/src/utils/strings.ts index 14c745e..7ffe4df 100644 --- a/src/utils/strings.ts +++ b/src/utils/strings.ts @@ -83,6 +83,8 @@ export abstract class Strings { UUID_NOT_VALID_FORMAT: 'Some ID provided does not have a valid format!', UUID_NOT_VALID_FORMAT_DESC: 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.', MULTIPLE_UUID_NOT_VALID_FORMAT: 'The following IDs from children attribute are not in valid format: ', + INVALID_MULTIPLE_UUID: 'Children field contains invalid IDs. It is expected that each item in the array is a ' + + '24-byte hex string like this: 507f191e810c19729de860ea', INTERNAL_SERVER_ERROR: 'An internal server error has occurred.', REQUIRED_FIELDS: 'Required fields were not provided...', REQUIRED_FIELDS_DESC: ' are required!', @@ -90,6 +92,7 @@ export abstract class Strings { INVALID_STRING: ' must be a string!', EMPTY_STRING: ' must have at least one character!', INVALID_DATE: ', is not in valid ISO 8601 format.', - INVALID_DATE_DESC: 'Date must be in the format: yyyy-MM-dd\'T\'HH:mm:ssZ' + INVALID_DATE_DESC: 'Date must be in the format: yyyy-MM-dd\'T\'HH:mm:ssZ', + INVALID_ARRAY: ' must be an array!', } } diff --git a/test/integration/routes/family.controller.spec.ts b/test/integration/routes/family.controller.spec.ts index 8b3f72a..69b1e8a 100644 --- a/test/integration/routes/family.controller.spec.ts +++ b/test/integration/routes/family.controller.spec.ts @@ -687,9 +687,8 @@ describe('Routes: Family', () => { .set('Content-Type', 'application/json') .expect(400) .then(err => { - expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - expect(err.body.description).to.eql('Collection with children IDs (ID can not be empty)' - .concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql(Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) diff --git a/test/unit/models/institution.model.spec.ts b/test/unit/models/institution.model.spec.ts index 9d4931c..eb96f0b 100644 --- a/test/unit/models/institution.model.spec.ts +++ b/test/unit/models/institution.model.spec.ts @@ -56,7 +56,7 @@ describe('Models: Educator', () => { } const result = new Institution().fromJSON(jsonWithBlankInstitutionID) - assert.propertyVal(result, 'id', undefined) + assert.propertyVal(result, 'id', '') assert.propertyVal(result, 'name', undefined) assert.propertyVal(result, 'type', undefined) assert.propertyVal(result, 'address', undefined) diff --git a/test/unit/services/children.group.service.spec.ts b/test/unit/services/children.group.service.spec.ts index 3268fea..b2de9a3 100644 --- a/test/unit/services/children.group.service.spec.ts +++ b/test/unit/services/children.group.service.spec.ts @@ -112,9 +112,8 @@ describe('Services: ChildrenGroup', () => { return childrenGroupService.add(incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'name, user, Collection with ' + - 'children IDs (ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) @@ -250,9 +249,8 @@ describe('Services: ChildrenGroup', () => { return childrenGroupService.update(incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'Collection with children IDs ' + - '(ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) @@ -266,8 +264,8 @@ describe('Services: ChildrenGroup', () => { return childrenGroupService.update(incorrectChildrenGroup) .catch(err => { assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) - assert.propertyVal(err, 'description', - Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT.concat('507f1f77bcf86cd7994390111')) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT + .concat('507f1f77bcf86cd7994390111')) }) }) }) diff --git a/test/unit/services/educator.service.spec.ts b/test/unit/services/educator.service.spec.ts index c392db0..fd1b289 100644 --- a/test/unit/services/educator.service.spec.ts +++ b/test/unit/services/educator.service.spec.ts @@ -447,9 +447,8 @@ describe('Services: Educator', () => { return educatorService.saveChildrenGroup(educator.id!, incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'name, user, Collection with ' + - 'children IDs (ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) @@ -679,9 +678,8 @@ describe('Services: Educator', () => { return educatorService.updateChildrenGroup(educator.id!, incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'Collection with children IDs ' + - '(ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) diff --git a/test/unit/services/family.service.spec.ts b/test/unit/services/family.service.spec.ts index 07b5c8e..a86525f 100644 --- a/test/unit/services/family.service.spec.ts +++ b/test/unit/services/family.service.spec.ts @@ -126,10 +126,8 @@ describe('Services: Family', () => { return familyService.add(incorrectFamily) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'username, password, type, institution, ' + - 'Collection with children IDs (ID can not be empty)' - .concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) diff --git a/test/unit/services/health.professional.service.spec.ts b/test/unit/services/health.professional.service.spec.ts index 812f342..3cf7ea8 100644 --- a/test/unit/services/health.professional.service.spec.ts +++ b/test/unit/services/health.professional.service.spec.ts @@ -447,9 +447,8 @@ describe('Services: HealthProfessional', () => { return healthProfessionalService.saveChildrenGroup(healthProfessional.id!, incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'name, user, Collection with ' + - 'children IDs (ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) @@ -687,9 +686,8 @@ describe('Services: HealthProfessional', () => { return healthProfessionalService.updateChildrenGroup(healthProfessional.id!, incorrectChildrenGroup) .catch(err => { - assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.propertyVal(err, 'description', 'Collection with children IDs ' + - '(ID can not be empty)'.concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.propertyVal(err, 'message', Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.propertyVal(err, 'description', Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) }) }) }) diff --git a/test/unit/validators/create.children.group.validator.spec.ts b/test/unit/validators/create.children.group.validator.spec.ts index d44c7ae..b02dad8 100644 --- a/test/unit/validators/create.children.group.validator.spec.ts +++ b/test/unit/validators/create.children.group.validator.spec.ts @@ -50,9 +50,8 @@ describe('Validators: ChildrenGroup', () => { try { CreateChildrenGroupValidator.validate(childrenGroup) } catch (err) { - assert.equal(err.message, Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.equal(err.description, 'Collection with children IDs (ID can not be empty)' - .concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.equal(err.message, Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.equal(err.description, Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } }) diff --git a/test/unit/validators/create.family.validator.spec.ts b/test/unit/validators/create.family.validator.spec.ts index 25cf7ed..4387403 100644 --- a/test/unit/validators/create.family.validator.spec.ts +++ b/test/unit/validators/create.family.validator.spec.ts @@ -97,9 +97,8 @@ describe('Validators: Family', () => { try { CreateFamilyValidator.validate(family) } catch (err) { - assert.equal(err.message, Strings.ERROR_MESSAGE.REQUIRED_FIELDS) - assert.equal(err.description, 'Collection with children IDs (ID can not be empty)' - .concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) + assert.equal(err.message, Strings.ERROR_MESSAGE.INVALID_FIELDS) + assert.equal(err.description, Strings.ERROR_MESSAGE.INVALID_MULTIPLE_UUID) } }) From bdeedcf505fffbc41093f9ae4f4ec77f3ea82ecf Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Thu, 14 Nov 2019 17:45:47 -0300 Subject: [PATCH 3/7] Validating the scenario of trying to create users by passing null as the 'institution_id' parameter value --- src/application/domain/validator/create.user.validator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/application/domain/validator/create.user.validator.ts b/src/application/domain/validator/create.user.validator.ts index a076ab9..4ba3109 100644 --- a/src/application/domain/validator/create.user.validator.ts +++ b/src/application/domain/validator/create.user.validator.ts @@ -15,7 +15,7 @@ export class CreateUserValidator { else StringValidator.validate(user.password, 'password') if (!user.type) fields.push('type') - if (!user.institution || !user.institution.id) { + if (!user.institution || user.institution.id === undefined) { if (user.type !== UserType.APPLICATION) fields.push('institution') } else ObjectIdValidator.validate(user.institution.id, Strings.INSTITUTION.PARAM_ID_NOT_VALID_FORMAT) From b78351697b61d1eae31d967352209228a4c6b242 Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Mon, 18 Nov 2019 10:45:42 -0300 Subject: [PATCH 4/7] Optimizing children attribute validators Creating new integration tests --- .../update.children.group.validator.ts | 4 -- .../validator/update.family.validator.ts | 4 -- .../routes/educator.controller.spec.ts | 70 ++++++++++++++++++- .../routes/family.controller.spec.ts | 64 +++++++++++++++++ 4 files changed, 132 insertions(+), 10 deletions(-) diff --git a/src/application/domain/validator/update.children.group.validator.ts b/src/application/domain/validator/update.children.group.validator.ts index 63cd4ef..1846eaf 100644 --- a/src/application/domain/validator/update.children.group.validator.ts +++ b/src/application/domain/validator/update.children.group.validator.ts @@ -6,7 +6,6 @@ import { StringValidator } from './string.validator' export class UpdateChildrenGroupValidator { public static validate(childrenGroup: ChildrenGroup): void | ValidationException { - const fields: Array = [] const invalid_ids: Array = [] if (childrenGroup.id) ObjectIdValidator.validate(childrenGroup.id, Strings.CHILDREN_GROUP.PARAM_ID_NOT_VALID_FORMAT) @@ -38,9 +37,6 @@ export class UpdateChildrenGroupValidator { if (invalid_ids.length > 0) { throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT.concat(invalid_ids.join(', '))) - } else if (fields.length > 0) { - throw new ValidationException(Strings.ERROR_MESSAGE.REQUIRED_FIELDS, - fields.join(', ').concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) } } } diff --git a/src/application/domain/validator/update.family.validator.ts b/src/application/domain/validator/update.family.validator.ts index f30cfdf..6ff6239 100644 --- a/src/application/domain/validator/update.family.validator.ts +++ b/src/application/domain/validator/update.family.validator.ts @@ -6,7 +6,6 @@ import { Strings } from '../../../utils/strings' export class UpdateFamilyValidator { public static validate(family: Family): void | ValidationException { - const fields: Array = [] const invalid_ids: Array = [] try { @@ -45,9 +44,6 @@ export class UpdateFamilyValidator { if (invalid_ids.length > 0) { throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, Strings.ERROR_MESSAGE.MULTIPLE_UUID_NOT_VALID_FORMAT.concat(invalid_ids.join(', '))) - } else if (fields.length > 0) { - throw new ValidationException(Strings.ERROR_MESSAGE.REQUIRED_FIELDS, - fields.join(', ').concat(Strings.ERROR_MESSAGE.REQUIRED_FIELDS_DESC)) } } } diff --git a/test/integration/routes/educator.controller.spec.ts b/test/integration/routes/educator.controller.spec.ts index 9b9b0a2..7a1cf54 100644 --- a/test/integration/routes/educator.controller.spec.ts +++ b/test/integration/routes/educator.controller.spec.ts @@ -758,6 +758,24 @@ describe('Routes: Educator', () => { expect(err.body.description).to.eql('school_class'.concat(Strings.ERROR_MESSAGE.EMPTY_STRING)) }) }) + + it('should return status code 400 and info message from invalid ChildrenGroup children', () => { + const body = { + name: defaultChildrenGroup.name, + children: null, + school_class: defaultChildrenGroup.school_class + } + + return request + .post(`/v1/educators/${resultEducator.id}/children/groups`) + .send(body) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + }) + }) }) context('when the children id (ids) is (are) invalid', () => { @@ -1119,7 +1137,7 @@ describe('Routes: Educator', () => { }) }) - context('when the children group was updated with an invalid name', () => { + context('when the children group was updated with an invalid attribute', () => { let resultEducator let resultChild let resultChildrenGroup @@ -1158,6 +1176,18 @@ describe('Routes: Educator', () => { } }) it('should return status code 400 and info message from invalid name', () => { + return request + .patch(`/v1/educators/${resultEducator.id}/children/groups/${resultChildrenGroup.id}`) + .send({ name: 123 }) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('name'.concat(Strings.ERROR_MESSAGE.INVALID_STRING)) + }) + }) + + it('should return status code 400 and info message from empty name', () => { return request .patch(`/v1/educators/${resultEducator.id}/children/groups/${resultChildrenGroup.id}`) .send({ name: '' }) @@ -1165,7 +1195,43 @@ describe('Routes: Educator', () => { .expect(400) .then(err => { expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) - expect(err.body.description).to.eql('name must have at least one character!') + expect(err.body.description).to.eql('name'.concat(Strings.ERROR_MESSAGE.EMPTY_STRING)) + }) + }) + + it('should return status code 400 and info message from invalid school_class', () => { + return request + .patch(`/v1/educators/${resultEducator.id}/children/groups/${resultChildrenGroup.id}`) + .send({ school_class: 123 }) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('school_class'.concat(Strings.ERROR_MESSAGE.INVALID_STRING)) + }) + }) + + it('should return status code 400 and info message from empty school_class', () => { + return request + .patch(`/v1/educators/${resultEducator.id}/children/groups/${resultChildrenGroup.id}`) + .send({ school_class: '' }) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('school_class'.concat(Strings.ERROR_MESSAGE.EMPTY_STRING)) + }) + }) + + it('should return status code 400 and info message from invalid children', () => { + return request + .patch(`/v1/educators/${resultEducator.id}/children/groups/${resultChildrenGroup.id}`) + .send({ children: null }) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) }) }) }) diff --git a/test/integration/routes/family.controller.spec.ts b/test/integration/routes/family.controller.spec.ts index 69b1e8a..1cd2068 100644 --- a/test/integration/routes/family.controller.spec.ts +++ b/test/integration/routes/family.controller.spec.ts @@ -300,6 +300,27 @@ describe('Routes: Family', () => { }) }) }) + + context('when the children provided was null', () => { + it('should return status code 400 and message for invalid children', () => { + const body = { + username: defaultFamily.username, + password: defaultFamily.password, + children: null, + institution_id: institution.id + } + + return request + .post('/v1/families') + .send(body) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + }) + }) + }) }) describe('GET /v1/families/:family_id', () => { @@ -693,6 +714,49 @@ describe('Routes: Family', () => { }) }) + context('when the family was updated with a null children array', () => { + let resultChild + let resultFamily + + before(async () => { + try { + await deleteAllUsers() + + resultChild = await createUser({ + username: defaultChild.username, + password: defaultChild.password, + type: UserType.CHILD, + gender: defaultChild.gender, + age: defaultChild.age, + institution: new ObjectID(institution.id), + scopes: new Array('users:read') + }) + + resultFamily = await createUser({ + username: defaultFamily.username, + password: defaultFamily.password, + type: UserType.FAMILY, + institution: new ObjectID(institution.id), + children: new Array(resultChild.id), + scopes: new Array('users:read') + }) + } catch (err) { + throw new Error('Failure on Family test: ' + err.message) + } + }) + it('should return status code 400 and info message from invalid ids)', () => { + return request + .patch(`/v1/families/${resultFamily.id}`) + .send({ children: null }) + .set('Content-Type', 'application/json') + .expect(400) + .then(err => { + expect(err.body.message).to.eql(Strings.ERROR_MESSAGE.INVALID_FIELDS) + expect(err.body.description).to.eql('children'.concat(Strings.ERROR_MESSAGE.INVALID_ARRAY)) + }) + }) + }) + context('when the family was updated with a not existent child id', () => { let resultChild let resultFamily From e1dd8669753ee165c7b26a00a946690aa5f239bf Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Wed, 20 Nov 2019 11:49:53 -0300 Subject: [PATCH 5/7] Fix bug with last_login and last_sync attributes These parameters are now mapped in the response of sublevel routes such as childrengroups, families.children... Refactoring associated tests and create new ones --- src/application/domain/model/child.ts | 1 + .../domain/model/children.group.ts | 6 +- src/application/domain/model/user.ts | 13 +- src/ui/controller/application.controller.ts | 1 + src/ui/controller/child.controller.ts | 2 + src/ui/controller/educator.controller.ts | 1 + src/ui/controller/family.controller.ts | 1 + .../health.professional.controller.ts | 1 + .../routes/child.controller.spec.ts | 42 ++---- .../routes/educator.controller.spec.ts | 133 +++++++----------- .../routes/family.controller.spec.ts | 124 ++++++---------- .../health.professional.controller.spec.ts | 121 ++++++---------- .../routes/institution.controller.spec.ts | 3 +- .../routes/user.controller.spec.ts | 7 +- test/unit/models/user.model.spec.ts | 53 +------ 15 files changed, 164 insertions(+), 345 deletions(-) diff --git a/src/application/domain/model/child.ts b/src/application/domain/model/child.ts index c9d0bba..a4c2585 100644 --- a/src/application/domain/model/child.ts +++ b/src/application/domain/model/child.ts @@ -82,6 +82,7 @@ export class Child extends User implements IJSONSerializable, IJSONDeserializabl } if (json.gender !== undefined) this.gender = json.gender if (json.age !== undefined) this.age = json.age + if (json.last_sync !== undefined) this.last_sync = json.last_sync return this } diff --git a/src/application/domain/model/children.group.ts b/src/application/domain/model/children.group.ts index 5d2860f..3ab2d59 100644 --- a/src/application/domain/model/children.group.ts +++ b/src/application/domain/model/children.group.ts @@ -93,11 +93,9 @@ export class ChildrenGroup extends Entity implements IJSONSerializable, IJSONDes name: this.name, children: this.children ? this.children.map(child => { - child.toJSON() child.type = undefined - return child - }) : - this.children, + return child.toJSON() + }) : this.children, school_class: this.school_class } } diff --git a/src/application/domain/model/user.ts b/src/application/domain/model/user.ts index b7df067..914de2d 100644 --- a/src/application/domain/model/user.ts +++ b/src/application/domain/model/user.ts @@ -70,17 +70,6 @@ export class User extends Entity implements IJSONSerializable, IJSONDeserializab this._scopes = value } - public addScope(scope: string): void { - if (!this.scopes) this._scopes = [] - if (scope) this._scopes.push(scope) - } - - public removeScope(scope: string): void { - if (scope) { - this.scopes = this.scopes.filter(item => item !== scope) - } - } - public fromJSON(json: any): User { if (!json) return this if (typeof json === 'string' && JsonUtils.isJsonString(json)) { @@ -96,7 +85,7 @@ export class User extends Entity implements IJSONSerializable, IJSONDeserializab } else if (json.institution_id !== undefined) { this.institution = new Institution().fromJSON(json) } - if (json.scope !== undefined) this.scopes = json.scope + if (json.last_login !== undefined) this.last_login = json.last_login return this } diff --git a/src/ui/controller/application.controller.ts b/src/ui/controller/application.controller.ts index 5c9fb57..4361792 100644 --- a/src/ui/controller/application.controller.ts +++ b/src/ui/controller/application.controller.ts @@ -45,6 +45,7 @@ export class ApplicationController { try { const application: Application = new Application().fromJSON(req.body) application.id = undefined + application.last_login = undefined const result: Application = await this._applicationService.add(application) return res.status(HttpStatus.CREATED).send(this.toJSONView(result)) } catch (err) { diff --git a/src/ui/controller/child.controller.ts b/src/ui/controller/child.controller.ts index 10ca1b7..1d4ac7e 100644 --- a/src/ui/controller/child.controller.ts +++ b/src/ui/controller/child.controller.ts @@ -45,6 +45,8 @@ export class ChildController { try { const child: Child = new Child().fromJSON(req.body) child.id = undefined + child.last_login = undefined + child.last_sync = undefined const result: Child = await this._childService.add(child) return res.status(HttpStatus.CREATED).send(this.toJSONView(result)) } catch (err) { diff --git a/src/ui/controller/educator.controller.ts b/src/ui/controller/educator.controller.ts index bbe872e..a06fdc4 100644 --- a/src/ui/controller/educator.controller.ts +++ b/src/ui/controller/educator.controller.ts @@ -46,6 +46,7 @@ export class EducatorController { try { const educator: Educator = new Educator().fromJSON(req.body) educator.id = undefined + educator.last_login = undefined const result: Educator = await this._educatorService.add(educator) return res.status(HttpStatus.CREATED).send(this.toJSONView(result)) } catch (err) { diff --git a/src/ui/controller/family.controller.ts b/src/ui/controller/family.controller.ts index 290f55d..dd86053 100644 --- a/src/ui/controller/family.controller.ts +++ b/src/ui/controller/family.controller.ts @@ -46,6 +46,7 @@ export class FamilyController { try { const family: Family = new Family().fromJSON(req.body) family.id = undefined + family.last_login = undefined const result: Family = await this._familyService.add(family) return res.status(HttpStatus.CREATED).send(this.toJSONView(result)) } catch (err) { diff --git a/src/ui/controller/health.professional.controller.ts b/src/ui/controller/health.professional.controller.ts index 3c5e8fe..703f91d 100644 --- a/src/ui/controller/health.professional.controller.ts +++ b/src/ui/controller/health.professional.controller.ts @@ -46,6 +46,7 @@ export class HealthProfessionalController { try { const healthProfessional: HealthProfessional = new HealthProfessional().fromJSON(req.body) healthProfessional.id = undefined + healthProfessional.last_login = undefined const result: HealthProfessional = await this._healthProfessionalService.add(healthProfessional) return res.status(HttpStatus.CREATED).send(this.toJSONView(result)) } catch (err) { diff --git a/test/integration/routes/child.controller.spec.ts b/test/integration/routes/child.controller.spec.ts index ea38fe9..24cfa60 100644 --- a/test/integration/routes/child.controller.spec.ts +++ b/test/integration/routes/child.controller.spec.ts @@ -165,8 +165,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) @@ -382,8 +381,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) @@ -452,8 +450,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await rabbitmq.initialize(process.env.RABBITMQ_URI || Default.RABBITMQ_URI, @@ -518,8 +515,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) @@ -558,8 +554,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) result = await createUser({ @@ -568,8 +563,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) @@ -752,8 +746,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -762,8 +755,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -772,8 +764,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -782,8 +773,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) @@ -818,8 +808,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -828,8 +817,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -838,8 +826,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -848,8 +835,7 @@ describe('Routes: Child', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Child test: ' + err.message) diff --git a/test/integration/routes/educator.controller.spec.ts b/test/integration/routes/educator.controller.spec.ts index 7a1cf54..033d945 100644 --- a/test/integration/routes/educator.controller.spec.ts +++ b/test/integration/routes/educator.controller.spec.ts @@ -110,8 +110,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -208,8 +207,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -218,8 +216,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -310,8 +307,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await rabbitmq.initialize(process.env.RABBITMQ_URI || Default.RABBITMQ_URI, @@ -373,8 +369,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -407,16 +402,14 @@ describe('Routes: Educator', () => { username: 'anothercoolusername', password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) result = await createUser({ username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -526,16 +519,14 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator = await createUser({ username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -582,8 +573,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -592,8 +582,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createChildrenGroup({ @@ -636,8 +625,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -672,16 +660,14 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator = await createUser({ username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -790,8 +776,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -829,8 +814,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -862,6 +846,8 @@ describe('Routes: Educator', () => { let resultEducator let resultChild let resultChildrenGroup + const lastLogin: Date = new Date() + const lastSync: Date = new Date() before(async () => { try { @@ -872,8 +858,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -883,7 +868,8 @@ describe('Routes: Educator', () => { gender: defaultChild.gender, age: defaultChild.age, institution: new ObjectID(institution.id), - scopes: new Array('users:read') + last_login: lastLogin, + last_sync: lastSync }) resultChildrenGroup = await createChildrenGroup({ @@ -916,6 +902,8 @@ describe('Routes: Educator', () => { expect(child.institution_id).to.eql(institution.id) expect(child.gender).to.eql(defaultChild.gender) expect(child.age).to.eql(defaultChild.age) + expect(child.last_login).to.eql(lastLogin.toISOString()) + expect(child.last_sync).to.eql(lastSync.toISOString()) } expect(res.body.school_class).to.eql(defaultChildrenGroup.school_class) }) @@ -934,8 +922,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -982,8 +969,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -992,8 +978,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1042,8 +1027,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1052,8 +1036,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1099,8 +1082,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1109,8 +1091,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1151,8 +1132,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1161,8 +1141,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1250,8 +1229,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1260,8 +1238,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1304,8 +1281,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1314,8 +1290,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1351,8 +1326,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -1399,8 +1373,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1409,8 +1382,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1472,8 +1444,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1482,8 +1453,7 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1541,8 +1511,7 @@ describe('Routes: Educator', () => { username: defaultEducator.username, password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Educator test: ' + err.message) @@ -1579,24 +1548,21 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator = await createUser({ username: 'EDUBR0001', password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator2 = await createUser({ username: 'EDUBR0002', password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1673,24 +1639,21 @@ describe('Routes: Educator', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator = await createUser({ username: 'EDUBR0001', password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultEducator2 = await createUser({ username: 'EDUBR0002', password: defaultEducator.password, type: UserType.EDUCATOR, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ diff --git a/test/integration/routes/family.controller.spec.ts b/test/integration/routes/family.controller.spec.ts index 1cd2068..30559ec 100644 --- a/test/integration/routes/family.controller.spec.ts +++ b/test/integration/routes/family.controller.spec.ts @@ -82,8 +82,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -159,16 +158,14 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ username: defaultFamily.username, password: defaultFamily.password, type: UserType.FAMILY, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -253,8 +250,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -338,8 +334,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -347,8 +342,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -424,8 +418,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -433,8 +426,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) await rabbitmq.initialize(process.env.RABBITMQ_URI || Default.RABBITMQ_URI, @@ -506,8 +498,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -515,8 +506,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -559,8 +549,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -568,8 +557,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) resultFamily = await createUser({ @@ -577,8 +565,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -685,8 +672,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -694,8 +680,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -728,8 +713,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -737,8 +721,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -771,8 +754,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -780,8 +762,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -815,8 +796,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -824,8 +804,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -862,8 +841,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild2 = await createUser({ @@ -872,8 +850,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -881,8 +858,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -923,8 +899,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -932,8 +907,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -979,8 +953,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -988,8 +961,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -1020,8 +992,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -1029,8 +1000,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -1077,8 +1047,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild2 = await createUser({ @@ -1087,8 +1056,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultFamily = await createUser({ @@ -1096,8 +1064,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id, resultChild2.id), - scopes: new Array('users:read') + children: new Array(resultChild.id, resultChild2.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -1169,8 +1136,7 @@ describe('Routes: Family', () => { username: defaultFamily.username, password: defaultFamily.password, type: UserType.FAMILY, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -1218,6 +1184,8 @@ describe('Routes: Family', () => { describe('GET /v1/families', () => { context('when want get all families in database', () => { let resultChild + const lastLogin: Date = new Date() + const lastSync: Date = new Date() before(async () => { try { @@ -1230,7 +1198,8 @@ describe('Routes: Family', () => { gender: defaultChild.gender, age: defaultChild.age, institution: new ObjectID(institution.id), - scopes: new Array('users:read') + last_login: lastLogin, + last_sync: lastSync }) await createUser({ @@ -1238,8 +1207,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) await createUser({ @@ -1247,8 +1215,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) @@ -1272,6 +1239,8 @@ describe('Routes: Family', () => { expect(child).to.have.property('institution_id') expect(child).to.have.property('gender') expect(child).to.have.property('age') + expect(child.last_login).to.eql(lastLogin.toISOString()) + expect(child.last_sync).to.eql(lastSync.toISOString()) } } }) @@ -1291,8 +1260,7 @@ describe('Routes: Family', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createUser({ @@ -1300,8 +1268,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) await createUser({ @@ -1309,8 +1276,7 @@ describe('Routes: Family', () => { password: defaultFamily.password, type: UserType.FAMILY, institution: new ObjectID(institution.id), - children: new Array(resultChild.id), - scopes: new Array('users:read') + children: new Array(resultChild.id) }) } catch (err) { throw new Error('Failure on Family test: ' + err.message) diff --git a/test/integration/routes/health.professional.controller.spec.ts b/test/integration/routes/health.professional.controller.spec.ts index 12b9808..e5caacd 100644 --- a/test/integration/routes/health.professional.controller.spec.ts +++ b/test/integration/routes/health.professional.controller.spec.ts @@ -110,8 +110,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -209,8 +208,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -219,8 +217,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -311,8 +308,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await rabbitmq.initialize(process.env.RABBITMQ_URI || Default.RABBITMQ_URI, @@ -374,8 +370,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -408,16 +403,14 @@ describe('Routes: HealthProfessional', () => { username: 'anothercoolusername', password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) result = await createUser({ username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -527,16 +520,14 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultHealthProfessional = await createUser({ username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -583,8 +574,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -593,8 +583,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) await createChildrenGroup({ @@ -637,8 +626,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -670,8 +658,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -709,8 +696,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -742,6 +728,8 @@ describe('Routes: HealthProfessional', () => { let resultHealthProfessional let resultChild let resultChildrenGroup + const lastLogin: Date = new Date() + const lastSync: Date = new Date() before(async () => { try { @@ -752,8 +740,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -763,7 +750,8 @@ describe('Routes: HealthProfessional', () => { gender: defaultChild.gender, age: defaultChild.age, institution: new ObjectID(institution.id), - scopes: new Array('users:read') + last_login: lastLogin, + last_sync: lastSync }) resultChildrenGroup = await createChildrenGroup({ @@ -799,6 +787,8 @@ describe('Routes: HealthProfessional', () => { expect(child.institution_id).to.eql(institution.id) expect(child.gender).to.eql(defaultChild.gender) expect(child.age).to.eql(defaultChild.age) + expect(child.last_login).to.eql(lastLogin.toISOString()) + expect(child.last_sync).to.eql(lastSync.toISOString()) } expect(res.body.school_class).to.eql(defaultChildrenGroup.school_class) }) @@ -817,8 +807,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -867,8 +856,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -877,8 +865,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -930,8 +917,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -940,8 +926,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -990,8 +975,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1000,8 +984,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1045,8 +1028,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1055,8 +1037,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1102,8 +1083,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1112,8 +1092,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1152,8 +1131,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -1203,8 +1181,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1213,8 +1190,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1276,8 +1252,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) resultChild = await createUser({ @@ -1286,8 +1261,7 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1346,8 +1320,7 @@ describe('Routes: HealthProfessional', () => { username: defaultHealthProfessional.username, password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read'), + institution: new ObjectID(institution.id) }) } catch (err) { throw new Error('Failure on HealthProfessional test: ' + err.message) @@ -1384,24 +1357,21 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultHealthProfessional = await createUser({ username: 'HPROFBR0001', password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultHealthProfessional2 = await createUser({ username: 'HPROFBR0002', password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ @@ -1478,24 +1448,21 @@ describe('Routes: HealthProfessional', () => { type: UserType.CHILD, gender: defaultChild.gender, age: defaultChild.age, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultHealthProfessional = await createUser({ username: 'HPROFBR0001', password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultHealthProfessional2 = await createUser({ username: 'HPROFBR0002', password: defaultHealthProfessional.password, type: UserType.HEALTH_PROFESSIONAL, - institution: new ObjectID(institution.id), - scopes: new Array('users:read') + institution: new ObjectID(institution.id) }) resultChildrenGroup = await createChildrenGroup({ diff --git a/test/integration/routes/institution.controller.spec.ts b/test/integration/routes/institution.controller.spec.ts index 254926d..82835b9 100644 --- a/test/integration/routes/institution.controller.spec.ts +++ b/test/integration/routes/institution.controller.spec.ts @@ -709,8 +709,7 @@ describe('Routes: Institution', () => { type: UserType.CHILD, gender: 'male', age: 11, - institution: new ObjectID(resultInstitution.id), - scopes: new Array('users:read') + institution: new ObjectID(resultInstitution.id) }) } catch (err) { throw new Error('Failure on Institution test: ' + err.message) diff --git a/test/integration/routes/user.controller.spec.ts b/test/integration/routes/user.controller.spec.ts index cb5b8c9..c0006c1 100644 --- a/test/integration/routes/user.controller.spec.ts +++ b/test/integration/routes/user.controller.spec.ts @@ -325,7 +325,6 @@ describe('Routes: User', () => { admin.username = 'anotheradminuser' admin.password = 'mysecretkey' admin.institution = institution.id - admin.scopes = ['users:read'] try { await createUser(admin) @@ -351,8 +350,7 @@ describe('Routes: User', () => { type: UserType.CHILD, gender: 'male', age: 11, - institution: institution.id, - scopes: new Array('users:read') + institution: institution.id }).then(user => { return request .delete(`/v1/users/${user._id}`) @@ -374,7 +372,6 @@ describe('Routes: User', () => { password: 'mysecretkey', type: UserType.EDUCATOR, institution: institution.id, - scopes: new Array('users:read'), children_groups: [] }).then(user => { return request @@ -397,7 +394,6 @@ describe('Routes: User', () => { password: 'mysecretkey', type: UserType.HEALTH_PROFESSIONAL, institution: institution.id, - scopes: new Array('users:read'), children_groups: [] }).then(user => { return request @@ -420,7 +416,6 @@ describe('Routes: User', () => { password: 'mysecretkey', type: UserType.FAMILY, institution: institution.id, - scopes: new Array('users:read'), children: [] }).then(user => { return request diff --git a/test/unit/models/user.model.spec.ts b/test/unit/models/user.model.spec.ts index f113473..74c2aaf 100644 --- a/test/unit/models/user.model.spec.ts +++ b/test/unit/models/user.model.spec.ts @@ -7,8 +7,7 @@ describe('Models: User', () => { id: new ObjectID(), username: 'ihaveaunknowusername', password: 'mysecretkey', - institution: new ObjectID(), - scope: ['users:read'] + institution: new ObjectID() } describe('fromJSON()', () => { @@ -45,54 +44,4 @@ describe('Models: User', () => { }) }) }) - - describe('addScope(scope: string)', () => { - context('when the user scope array already exists', () => { - it('should add scope in scope array', () => { - const result = new User().fromJSON(userJSON) - result.addScope('users:write') - assert.isArray(result.scopes) - assert.isNotEmpty(result.scopes) - }) - }) - - context('when user scope array is undefined', () => { - it('should set scope as empty array', () => { - const result = new User() - result.addScope('users:read') - assert.isArray(result.scopes) - assert.isNotEmpty(result.scopes) - }) - }) - - context('when scope is undefined', () => { - it('should not add scope', () => { - const result = new User().fromJSON(userJSON) - result.addScope(undefined!) - assert.isArray(result.scopes) - assert.isNotEmpty(result.scopes) - }) - }) - }) - - describe('removeScope(scope: string)', () => { - context('when the user scope array contains the scope passed by parameter', () => { - it('should remove the scope', () => { - const result = new User().fromJSON(userJSON) - result.removeScope(result.scopes[0]) - assert.isArray(result.scopes) - // Size check equal to 1 because the user scope array was with 2 elements - assert.equal(result.scopes.length, 1) - }) - }) - - context('when the parameter is undefined', () => { - it('should not do anything', () => { - const result = new User().fromJSON(userJSON) - result.removeScope(undefined!) - assert.isArray(result.scopes) - assert.isNotEmpty(result.scopes) - }) - }) - }) }) From 1982aa5f764a549e7dcfb7289f78fee82f675e78 Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Wed, 20 Nov 2019 17:43:12 -0300 Subject: [PATCH 6/7] Fix some problems with attributes receiving null values Refactoring error throwing of Base Repository --- src/application/domain/validator/create.child.validator.ts | 2 +- src/application/domain/validator/update.child.validator.ts | 2 +- src/application/domain/validator/update.user.validator.ts | 2 +- src/infrastructure/repository/base/base.repository.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/application/domain/validator/create.child.validator.ts b/src/application/domain/validator/create.child.validator.ts index 92e1631..7141f41 100644 --- a/src/application/domain/validator/create.child.validator.ts +++ b/src/application/domain/validator/create.child.validator.ts @@ -15,7 +15,7 @@ export class CreateChildValidator { fields.push(err.description.split(',')) } - if (!child.gender) fields.push('gender') + if (child.gender === undefined) fields.push('gender') else if (!genders.includes(child.gender)) { throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, `The names of the allowed genders are: ${genders.join(', ')}.`) diff --git a/src/application/domain/validator/update.child.validator.ts b/src/application/domain/validator/update.child.validator.ts index 9ebb7d7..e1c7f36 100644 --- a/src/application/domain/validator/update.child.validator.ts +++ b/src/application/domain/validator/update.child.validator.ts @@ -20,7 +20,7 @@ export class UpdateChildValidator { throw err } - if (child.gender && !genders.includes(child.gender)) { + if (child.gender !== undefined && !genders.includes(child.gender)) { throw new ValidationException(Strings.ERROR_MESSAGE.INVALID_FIELDS, `The names of the allowed genders are: ${genders.join(', ')}.`) } diff --git a/src/application/domain/validator/update.user.validator.ts b/src/application/domain/validator/update.user.validator.ts index c9706ca..c1b6169 100644 --- a/src/application/domain/validator/update.user.validator.ts +++ b/src/application/domain/validator/update.user.validator.ts @@ -22,7 +22,7 @@ export class UpdateUserValidator { } } // validate parameters that can not be updated. - if (user.password) { + if (user.password !== undefined) { throw new ValidationException('This parameter could not be updated.', 'A specific route to update user password already exists.' + `Access: PATCH /users/${user.id}/password to update your password.`) diff --git a/src/infrastructure/repository/base/base.repository.ts b/src/infrastructure/repository/base/base.repository.ts index 3972ee2..202c30f 100644 --- a/src/infrastructure/repository/base/base.repository.ts +++ b/src/infrastructure/repository/base/base.repository.ts @@ -169,8 +169,8 @@ export abstract class BaseRepository implements IRepos return new ValidationException('Invalid query parameters!') } } - return new RepositoryException(err && err.message ? err.message : '', - err && err.description ? err.description : '') + return new RepositoryException(err && err.message ? err.message : Strings.ERROR_MESSAGE.INTERNAL_SERVER_ERROR, + err && err.description ? err.description : undefined) } private findPopulate(q: any, populate?: any): Promise> { From d5077cd8938e5b9d86bbd3c1603305b88f5a9f73 Mon Sep 17 00:00:00 2001 From: JeffMedeiros Date: Wed, 20 Nov 2019 17:51:21 -0300 Subject: [PATCH 7/7] Fixing tests that broke with the commit of id: 1982aa5f764a549e7dcfb7289f78fee82f675e78 --- test/unit/services/application.service.spec.ts | 4 ++-- test/unit/services/child.service.spec.ts | 4 ++-- test/unit/services/educator.service.spec.ts | 4 ++-- test/unit/services/family.service.spec.ts | 6 +++--- test/unit/services/health.professional.service.spec.ts | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/unit/services/application.service.spec.ts b/test/unit/services/application.service.spec.ts index b8f3b66..21aa302 100644 --- a/test/unit/services/application.service.spec.ts +++ b/test/unit/services/application.service.spec.ts @@ -227,7 +227,7 @@ describe('Services: Application', () => { describe('update(application: Application)', () => { context('when the Application exists in the database', () => { it('should return the Application that was updated', () => { - application.password = '' + application.password = undefined application.id = '507f1f77bcf86cd799439011' // Make mock return an updated Application return applicationService.update(application) @@ -294,7 +294,7 @@ describe('Services: Application', () => { context('when the Application is incorrect (the institution is not registered)', () => { it('should throw a ValidationException', () => { - application.password = '' + application.password = undefined application.institution!.id = '507f1f77bcf86cd799439012' return applicationService.update(application) diff --git a/test/unit/services/child.service.spec.ts b/test/unit/services/child.service.spec.ts index 1106224..ea73e88 100644 --- a/test/unit/services/child.service.spec.ts +++ b/test/unit/services/child.service.spec.ts @@ -247,7 +247,7 @@ describe('Services: Child', () => { describe('update(child: Child)', () => { context('when the Child exists in the database', () => { it('should return the Child that was updated', () => { - child.password = '' + child.password = undefined return childService.update(child) .then(result => { @@ -317,7 +317,7 @@ describe('Services: Child', () => { context('when the Child is incorrect (the institution is not registered)', () => { it('should throw a ValidationException', () => { - child.password = '' + child.password = undefined child.institution!.id = '507f1f77bcf86cd799439012' return childService.update(child) diff --git a/test/unit/services/educator.service.spec.ts b/test/unit/services/educator.service.spec.ts index fd1b289..b8ff562 100644 --- a/test/unit/services/educator.service.spec.ts +++ b/test/unit/services/educator.service.spec.ts @@ -234,7 +234,7 @@ describe('Services: Educator', () => { describe('update(educator: Educator)', () => { context('when the Educator exists in the database', () => { it('should return the Educator that was updated', () => { - educator.password = '' + educator.password = undefined educator.id = '507f1f77bcf86cd799439011' // Make mock return an updated child return educatorService.update(educator) @@ -303,7 +303,7 @@ describe('Services: Educator', () => { context('when the Educator is incorrect (the institution is not registered)', () => { it('should throw a ValidationException', () => { - educator.password = '' + educator.password = undefined educator.institution!.id = '507f1f77bcf86cd799439012' return educatorService.update(educator) diff --git a/test/unit/services/family.service.spec.ts b/test/unit/services/family.service.spec.ts index a86525f..3d34ad1 100644 --- a/test/unit/services/family.service.spec.ts +++ b/test/unit/services/family.service.spec.ts @@ -246,7 +246,7 @@ describe('Services: Family', () => { it('should return the Family that was updated', () => { family.institution!.id = '507f1f77bcf86cd799439011' family.children![0].id = '507f1f77bcf86cd799439011' - family.password = '' + family.password = undefined family.id = '507f1f77bcf86cd799439011' // Make mock return an updated Family return familyService.update(family) @@ -313,7 +313,7 @@ describe('Services: Family', () => { context('when the Family exists in the database but the children are not registered', () => { it('should throw a ValidationException', () => { - family.password = '' + family.password = undefined family.children![0].id = '507f1f77bcf86cd799439012' // Make mock throw an exception return familyService.update(family) @@ -327,7 +327,7 @@ describe('Services: Family', () => { context('when the Family is incorrect (the institution is not registered)', () => { it('should throw a ValidationException', () => { - family.password = '' + family.password = undefined family.children![0].id = '507f1f77bcf86cd799439011' family.institution!.id = '507f1f77bcf86cd799439012' // Make mock throw an exception diff --git a/test/unit/services/health.professional.service.spec.ts b/test/unit/services/health.professional.service.spec.ts index 3cf7ea8..aef53aa 100644 --- a/test/unit/services/health.professional.service.spec.ts +++ b/test/unit/services/health.professional.service.spec.ts @@ -234,7 +234,7 @@ describe('Services: HealthProfessional', () => { describe('update(healthProfessional: HealthProfessional)', () => { context('when the HealthProfessional exists in the database', () => { it('should return the HealthProfessional that was updated', () => { - healthProfessional.password = '' + healthProfessional.password = undefined healthProfessional.id = '507f1f77bcf86cd799439011' // Make mock return an updated child return healthProfessionalService.update(healthProfessional) @@ -303,7 +303,7 @@ describe('Services: HealthProfessional', () => { context('when the Child is incorrect (the institution is not registered)', () => { it('should throw a ValidationException', () => { - healthProfessional.password = '' + healthProfessional.password = undefined healthProfessional.institution!.id = '507f1f77bcf86cd799439012' return healthProfessionalService.update(healthProfessional)