Skip to content

Commit

Permalink
bugfix(api): guard code against invalid values in tube repository
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-bergoens authored Dec 12, 2024
1 parent 1a587cd commit 8c60df2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/lib/infrastructure/repositories/tube-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export async function list() {

export async function findByNames({ tubeNames, locale }) {
if (!config.featureToggles.useNewLearningContent) return oldTubeRepository.findByNames({ tubeNames, locale });
if (!tubeNames) {
return [];
}
const ids = await knex.pluck('id').from(TABLE_NAME).whereIn('name', tubeNames).orderBy('name');
const tubeDtos = await getInstance().loadMany(ids);
return toDomainList(tubeDtos, locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,20 @@ describe('Integration | Repository | tube-repository', function () {
expect(tubes).to.deep.equal([]);
});
});

context('when invalid value provided for tubeNames argument', function () {
it('should return an empty array', async function () {
// when
const tubes1 = await tubeRepository.findByNames({ tubeNames: null });
const tubes2 = await tubeRepository.findByNames({ tubeNames: undefined });
const tubes3 = await tubeRepository.findByNames({ tubeNames: [] });

// then
expect(tubes1).to.deep.equal([]);
expect(tubes2).to.deep.equal([]);
expect(tubes3).to.deep.equal([]);
});
});
});

describe('#findByRecordIds', function () {
Expand Down

0 comments on commit 8c60df2

Please sign in to comment.