Skip to content

Commit

Permalink
Adding some integration tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rocha committed Oct 10, 2019
1 parent 18111f4 commit 413f0a1
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/application/domain/model/measurement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/application/service/user.auth.data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class UserAuthDataService implements IUserAuthDataService {
}
}).catch(err => reject(err))
} catch (err) {
return Promise.reject(err)
return reject(err)
}
})
}
Expand Down
114 changes: 114 additions & 0 deletions test/integration/routes/user.fitbit.auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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))
}
25 changes: 25 additions & 0 deletions test/integration/routes/user.fitbit.sync.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -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.')
})
})
})
})
})
16 changes: 16 additions & 0 deletions test/unit/models/sleep.pattern.data.set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 413f0a1

Please sign in to comment.