diff --git a/src/application/domain/model/measurement.ts b/src/application/domain/model/measurement.ts index 30e5484..c39070a 100644 --- a/src/application/domain/model/measurement.ts +++ b/src/application/domain/model/measurement.ts @@ -24,7 +24,7 @@ export class Measurement extends Entity implements IJSONSerializable, IJSONDeser } set type(value: string | undefined) { - if (value !== undefined) this._type = value.toLowerCase().trim() + this._type = value } get timestamp(): string | undefined { @@ -66,7 +66,7 @@ export class Measurement extends Entity implements IJSONSerializable, IJSONDeser } if (json.id !== undefined) super.id = json.id - if (json.type !== undefined) this.type = json.type + if (json.type !== undefined) this.type = json.type.toLowerCase().trim() if (json.timestamp !== undefined) this.timestamp = json.timestamp if (json.value !== undefined) this.value = json.value if (json.unit !== undefined) this.unit = json.unit diff --git a/src/application/service/user.auth.data.service.ts b/src/application/service/user.auth.data.service.ts index 3d25d95..e61f94e 100644 --- a/src/application/service/user.auth.data.service.ts +++ b/src/application/service/user.auth.data.service.ts @@ -147,7 +147,7 @@ export class UserAuthDataService implements IUserAuthDataService { } }).catch(err => reject(err)) } catch (err) { - return Promise.reject(err) + return reject(err) } }) } diff --git a/test/integration/routes/user.fitbit.auth.controller.spec.ts b/test/integration/routes/user.fitbit.auth.controller.spec.ts new file mode 100644 index 0000000..5362c03 --- /dev/null +++ b/test/integration/routes/user.fitbit.auth.controller.spec.ts @@ -0,0 +1,114 @@ +import { Identifier } from '../../../src/di/identifiers' +import { App } from '../../../src/app' +import { expect } from 'chai' +import { DIContainer } from '../../../src/di/di' +import { IDatabase } from '../../../src/infrastructure/port/database.interface' +import { Default } from '../../../src/utils/default' +import { DefaultEntityMock } from '../../mocks/models/default.entity.mock' +import { UserAuthRepoModel } from '../../../src/infrastructure/database/schema/oauth.data.schema' +import { Strings } from '../../../src/utils/strings' + +const app: App = DIContainer.get(Identifier.APP) +const request = require('supertest')(app.getExpress()) +const dbConnection: IDatabase = DIContainer.get(Identifier.MONGODB_CONNECTION) + +describe('Routes: UserFitbitAuthController', () => { + + before(async () => { + try { + await dbConnection.connect(process.env.MONGODB_URI_TEST || Default.MONGODB_URI_TEST, { interval: 100 }) + await deleteAll({}) + await saveData(DefaultEntityMock.USER_AUTH_DATA) + } catch (err) { + throw new Error('Failure on Educator test: ' + err.message) + } + } + ) + after(async () => { + try { + await deleteAll({}) + await dbConnection.dispose() + } catch (err) { + throw new Error('Failure on Educator test: ' + err.message) + } + }) + describe('POST /v1/users/:user_id/fitbit/auth', () => { + context('when a validation error occurs', () => { + it('should return status code 400 and message from invalid parameter', () => { + return request + .post('/v1/users/123/fitbit/auth') + .send(DefaultEntityMock.FITBIT_AUTH_DATA_BEFORE) + .set('Content-Type', 'application/json') + .expect(400) + .then(res => { + expect(res.body).to.have.property('message', 'Some ID provided does not have a valid format!') + expect(res.body).to.have.property('description', + 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.') + }) + }) + }) + }) + describe('GET /v1/users/:user_id/fitbit/auth', () => { + context('when get fitbit auth data', () => { + it('should return status code 200 and fitbit auth data', () => { + return request + .get(`/v1/users/${DefaultEntityMock.USER_AUTH_DATA.user_id}/fitbit/auth`) + .set('Content-Type', 'application/json') + .expect(200) + .then(res => { + expect(res.body).to.have.property('access_token', DefaultEntityMock.FITBIT_AUTH_DATA.access_token) + expect(res.body).to.have.property('refresh_token', DefaultEntityMock.FITBIT_AUTH_DATA.refresh_token) + expect(res.body).to.have.property('status', DefaultEntityMock.FITBIT_AUTH_DATA.status) + }) + }) + }) + context('when auth data is not found', () => { + it('should return status code 404 and message from auth darta not found', () => { + return request + .get(`/v1/users/${DefaultEntityMock.USER_IDS.does_not_exists}/fitbit/auth`) + .set('Content-Type', 'application/json') + .expect(404) + .then(res => { + expect(res.body).to.have.property('message', Strings.FITBIT.AUTH_NOT_FOUND) + expect(res.body).to.have.property('description', Strings.FITBIT.AUTH_NOT_FOUND_DESCRIPTION) + }) + }) + }) + context('when a validation error occurs', () => { + it('should return status code 400 and message from invalid parameter', () => { + return request + .get('/v1/users/123/fitbit/auth') + .set('Content-Type', 'application/json') + .expect(400) + .then(res => { + expect(res.body).to.have.property('message', 'Some ID provided does not have a valid format!') + expect(res.body).to.have.property('description', + 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.') + }) + }) + }) + }) + describe('POST /v1/users/:user_id/fitbit/auth/revoke', () => { + context('when a validation error occurs', () => { + it('should return status code 400 and message from invalid parameter', () => { + return request + .post('/v1/users/123/fitbit/auth/revoke') + .set('Content-Type', 'application/json') + .expect(400) + .then(res => { + expect(res.body).to.have.property('message', 'Some ID provided does not have a valid format!') + expect(res.body).to.have.property('description', + 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.') + }) + }) + }) + }) +}) + +function saveData(data) { + UserAuthRepoModel.create(data).then(res => Promise.resolve(res)).catch(err => Promise.reject(err)) +} + +function deleteAll(query) { + UserAuthRepoModel.deleteMany(query).then(res => Promise.resolve(res)).catch(err => Promise.reject(err)) +} diff --git a/test/integration/routes/user.fitbit.sync.controller.spec.ts b/test/integration/routes/user.fitbit.sync.controller.spec.ts new file mode 100644 index 0000000..47f4a69 --- /dev/null +++ b/test/integration/routes/user.fitbit.sync.controller.spec.ts @@ -0,0 +1,25 @@ +import { Identifier } from '../../../src/di/identifiers' +import { App } from '../../../src/app' +import { expect } from 'chai' +import { DIContainer } from '../../../src/di/di' + +const app: App = DIContainer.get(Identifier.APP) +const request = require('supertest')(app.getExpress()) + +describe('Routes: UserFitbitSyncController', () => { + describe('POST /v1/users/:user_id/fitbit/sync', () => { + context('when a validation error occurs', () => { + it('should return status code 400 and message from invalid parameter', () => { + return request + .post('/v1/users/123/fitbit/sync') + .set('Content-Type', 'application/json') + .expect(400) + .then(res => { + expect(res.body).to.have.property('message', 'Some ID provided does not have a valid format!') + expect(res.body).to.have.property('description', + 'A 24-byte hex ID similar to this: 507f191e810c19729de860ea is expected.') + }) + }) + }) + }) +}) diff --git a/test/unit/models/sleep.pattern.data.set.spec.ts b/test/unit/models/sleep.pattern.data.set.spec.ts index 7b1f844..dd792fc 100644 --- a/test/unit/models/sleep.pattern.data.set.spec.ts +++ b/test/unit/models/sleep.pattern.data.set.spec.ts @@ -58,6 +58,22 @@ describe('Models: SleepPatternDataSet', () => { const res: any = model.toJSON() assert.deepEqual(res, DefaultEntityMock.SLEEP_PATTERN_DATA_SET) }) + it('should return a json', () => { + const wake = { + start_time: '2019-09-12T13:36:49.741Z', + name: 'wake', + duration: 10000 + } + const awake = { + start_time: '2019-09-12T13:36:49.741Z', + name: 'awake', + duration: 10000 + } + const model: SleepPatternDataSet = + new SleepPatternDataSet().fromJSON(wake) + const res: any = model.toJSON() + assert.deepEqual(res, awake) + }) }) context('when the model parameters are undefined', () => { it('should return a json with undefined parameters', () => {