-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create and update with a scope when it is not found (#1107)
- Loading branch information
1 parent
4198d59
commit 9a138c7
Showing
4 changed files
with
192 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { scopeManager } from 'forest-express'; | ||
import Sequelize from 'sequelize'; | ||
import createError from 'http-errors'; | ||
import ResourceUpdater from '../../src/services/resource-updater'; | ||
import ResourceGetter from '../../src/services/resource-getter'; | ||
import QueryOptions from '../../src/services/query-options'; | ||
|
||
describe('services > resources-updater', () => { | ||
const user = { renderingId: 1 }; | ||
const params = { timezone: 'Europe/Paris' }; | ||
|
||
const buildModelMock = () => { | ||
// Sequelize is created here without connection to a database | ||
const sequelize = new Sequelize({ dialect: 'postgres' }); | ||
|
||
const Actor = sequelize.define('actor', {}); | ||
const Film = sequelize.define('film', {}); | ||
const ActorFilm = sequelize.define('ActorFilem', { | ||
actorId: { | ||
type: Sequelize.DataTypes.INTEGER, | ||
primaryKey: true, | ||
}, | ||
filmId: { | ||
type: Sequelize.DataTypes.INTEGER, | ||
primaryKey: true, | ||
}, | ||
}); | ||
|
||
ActorFilm.belongsTo(Actor); | ||
ActorFilm.belongsTo(Film); | ||
|
||
return { Actor, Film, ActorFilm }; | ||
}; | ||
|
||
describe('when it update with a scope and it is not in scope anymore', () => { | ||
it('should still return the record', async () => { | ||
expect.assertions(1); | ||
|
||
const { Film } = buildModelMock(); | ||
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] }); | ||
|
||
const record = { dataValues: { id: 1, title: 'The Godfather' }, validate: () => {}, save: () => {} }; | ||
|
||
jest.spyOn(record, 'validate'); | ||
jest.spyOn(record, 'save'); | ||
|
||
const error = new Error('Record not found'); | ||
error.statusCode = 404; | ||
jest | ||
.spyOn(ResourceGetter.prototype, 'perform') | ||
.mockRejectedValue(error); | ||
|
||
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue(); | ||
|
||
const resourceUpdater = new ResourceUpdater(Film, params, { name: 'new name' }, user); | ||
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record); | ||
|
||
const result = await resourceUpdater.perform(); | ||
|
||
expect(result).toStrictEqual(record); | ||
}); | ||
}); | ||
|
||
describe('when it update with a scope but the record does not exist', () => { | ||
it('should throw 404', async () => { | ||
expect.assertions(1); | ||
|
||
const { Film } = buildModelMock(); | ||
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] }); | ||
|
||
const record = null; | ||
|
||
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue(); | ||
|
||
const resourceUpdater = new ResourceUpdater(Film, { ...params, recordId: 2 }, { name: 'new name' }, user); | ||
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record); | ||
|
||
await expect(resourceUpdater.perform()).rejects.toThrow(createError(404, 'The film #2 does not exist.')); | ||
}); | ||
}); | ||
|
||
describe('when there is a scope and ResourcesGetter throw an error', () => { | ||
it('should throw the error', async () => { | ||
expect.assertions(1); | ||
|
||
const { Film } = buildModelMock(); | ||
jest.spyOn(scopeManager, 'getScopeForUser').mockReturnValue({ aggregator: 'and', conditions: [{ field: 'name', operator: 'contains', value: 'Scope value' }] }); | ||
|
||
const record = { dataValues: { id: 1, title: 'The Godfather' }, validate: () => {}, save: () => {} }; | ||
|
||
jest.spyOn(record, 'validate'); | ||
jest.spyOn(record, 'save'); | ||
|
||
const error = new Error('Unauthorized'); | ||
error.statusCode = 401; | ||
jest | ||
.spyOn(ResourceGetter.prototype, 'perform') | ||
.mockRejectedValue(error); | ||
|
||
jest.spyOn(QueryOptions.prototype, 'filterByConditionTree').mockResolvedValue(); | ||
|
||
const resourceUpdater = new ResourceUpdater(Film, { ...params, recordId: 2 }, { name: 'new name' }, user); | ||
jest.spyOn(resourceUpdater._model, 'findOne').mockReturnValue(record); | ||
|
||
await expect(resourceUpdater.perform()).rejects.toThrow(error); | ||
}); | ||
}); | ||
}); |