Skip to content

Commit 6d12051

Browse files
KalleVdhmlau
authored andcommitted
test(sequelize): update tests to replicate hidden property bug
Signed-off-by: KalleV <[email protected]>
1 parent ca7c8b5 commit 6d12051

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

extensions/sequelize/src/__tests__/fixtures/controllers/user-todo-list.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class UserTodoListController {
5858
schema: getModelSchemaRef(TodoList, {
5959
title: 'NewTodoListInUser',
6060
exclude: ['id'],
61-
optional: ['user'],
61+
optional: ['userId'],
6262
}),
6363
},
6464
},

extensions/sequelize/src/__tests__/fixtures/models/todo-list.model.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import {Entity, hasMany, model, property} from '@loopback/repository';
1+
import {
2+
Entity,
3+
hasMany,
4+
model,
5+
property,
6+
belongsTo,
7+
} from '@loopback/repository';
28
import {Todo} from './todo.model';
9+
import {User} from './user.model';
310

411
@model()
512
export class TodoList extends Entity {
@@ -21,10 +28,17 @@ export class TodoList extends Entity {
2128
})
2229
todos: Todo[];
2330

24-
@property({
25-
type: 'number',
26-
})
27-
user?: number;
31+
@belongsTo(
32+
() => User,
33+
{
34+
keyTo: 'id',
35+
keyFrom: 'userId',
36+
},
37+
{
38+
type: 'number',
39+
},
40+
)
41+
userId?: number;
2842

2943
constructor(data?: Partial<TodoList>) {
3044
super(data);

extensions/sequelize/src/__tests__/fixtures/models/user.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class User extends Entity {
6666
})
6767
dob?: Date;
6868

69-
@hasOne(() => TodoList, {keyTo: 'user'})
69+
@hasOne(() => TodoList, {keyTo: 'userId'})
7070
todoList: TodoList;
7171

7272
constructor(data?: Partial<User>) {

extensions/sequelize/src/__tests__/fixtures/repositories/todo-list.repository.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import {Getter, inject} from '@loopback/core';
2-
import {HasManyRepositoryFactory, repository} from '@loopback/repository';
2+
import {
3+
HasManyRepositoryFactory,
4+
repository,
5+
type BelongsToAccessor,
6+
} from '@loopback/repository';
37
import {SequelizeCrudRepository} from '../../../sequelize';
48
import {PrimaryDataSource} from '../datasources/primary.datasource';
5-
import {Todo, TodoList, TodoListRelations} from '../models/index';
9+
import {Todo, TodoList, TodoListRelations, User} from '../models/index';
610
import {TodoRepository} from './todo.repository';
11+
import {UserRepository} from './user.repository';
712

813
export class TodoListRepository extends SequelizeCrudRepository<
914
TodoList,
@@ -15,16 +20,23 @@ export class TodoListRepository extends SequelizeCrudRepository<
1520
typeof TodoList.prototype.id
1621
>;
1722

23+
public readonly user: BelongsToAccessor<User, typeof TodoList.prototype.id>;
24+
1825
constructor(
1926
@inject('datasources.primary') dataSource: PrimaryDataSource,
2027
@repository.getter('TodoRepository')
2128
protected todoRepositoryGetter: Getter<TodoRepository>,
29+
@repository.getter('UserRepository')
30+
protected userRepositoryGetter: Getter<UserRepository>,
2231
) {
2332
super(TodoList, dataSource);
2433
this.todos = this.createHasManyRepositoryFactoryFor(
2534
'todos',
2635
todoRepositoryGetter,
2736
);
2837
this.registerInclusionResolver('todos', this.todos.inclusionResolver);
38+
39+
this.user = this.createBelongsToAccessorFor('user', userRepositoryGetter);
40+
this.registerInclusionResolver('user', this.user.inclusionResolver);
2941
}
3042
}

extensions/sequelize/src/__tests__/integration/repository.integration.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ describe('Sequelize CRUD Repository (integration)', () => {
829829

830830
const user = getDummyUser();
831831
const userRes = await client.post('/users').send(user);
832-
const todoList = getDummyTodoList({user: userRes.body.id});
832+
const todoList = getDummyTodoList({userId: userRes.body.id});
833833
const todoListRes = await client.post('/todo-lists').send(todoList);
834834

835835
const filter = {include: ['todoList']};
@@ -863,11 +863,32 @@ describe('Sequelize CRUD Repository (integration)', () => {
863863
{
864864
...todoListRes.body,
865865
todos: [todoRes.body],
866-
user: null,
866+
userId: null,
867867
},
868868
]);
869869
});
870870

871+
it('hides hidden properties in related entities', async () => {
872+
await migrateSchema(['todos', 'todo-lists', 'users']);
873+
874+
const userRes = await client.post('/users').send(getDummyUser());
875+
876+
await client.post('/todo-lists').send(
877+
getDummyTodoList({
878+
title: 'Todo list one',
879+
userId: userRes.body.id,
880+
}),
881+
);
882+
883+
const filter = {include: ['user']};
884+
const relationRes = await client.get(`/todo-lists`).query({
885+
filter: JSON.stringify(filter),
886+
});
887+
888+
expect(relationRes.body.length).to.be.equal(1);
889+
expect(relationRes.body.at(0).user).not.to.have.property('password');
890+
});
891+
871892
it('supports `order` filter by associations', async () => {
872893
await migrateSchema(['todos', 'todo-lists']);
873894

@@ -898,12 +919,12 @@ describe('Sequelize CRUD Repository (integration)', () => {
898919
{
899920
...todoListRes1.body,
900921
todos: [todoRes1.body],
901-
user: null,
922+
userId: null,
902923
},
903924
{
904925
...todoListRes2.body,
905926
todos: [todoRes2.body],
906-
user: null,
927+
userId: null,
907928
},
908929
]);
909930

@@ -915,12 +936,12 @@ describe('Sequelize CRUD Repository (integration)', () => {
915936
{
916937
...todoListRes2.body,
917938
todos: [todoRes2.body],
918-
user: null,
939+
userId: null,
919940
},
920941
{
921942
...todoListRes1.body,
922943
todos: [todoRes1.body],
923-
user: null,
944+
userId: null,
924945
},
925946
]);
926947

@@ -932,12 +953,12 @@ describe('Sequelize CRUD Repository (integration)', () => {
932953
{
933954
...todoListRes1.body,
934955
todos: [todoRes1.body],
935-
user: null,
956+
userId: null,
936957
},
937958
{
938959
...todoListRes2.body,
939960
todos: [todoRes2.body],
940-
user: null,
961+
userId: null,
941962
},
942963
]);
943964
});
@@ -1013,13 +1034,13 @@ describe('Sequelize CRUD Repository (integration)', () => {
10131034
const todoOne = await client.post('/todo-lists').send(
10141035
getDummyTodoList({
10151036
title: 'Todo list one',
1016-
user: userRes.body.id,
1037+
userId: userRes.body.id,
10171038
}),
10181039
);
10191040
const todoListRes = await client.post('/todo-lists').send(
10201041
getDummyTodoList({
10211042
title: 'Another todo list',
1022-
user: userRes.body.id,
1043+
userId: userRes.body.id,
10231044
}),
10241045
);
10251046

0 commit comments

Comments
 (0)