Skip to content

Commit 6e34934

Browse files
committed
fix(backend): adding userId and structureId to user create log
1 parent cd92224 commit 6e34934

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

packages/backend/src/modules/users/controllers/users.controller.spec.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,24 @@ import {
1313
} from "../../../util/test";
1414
import { UsersController } from "./users.controller";
1515
import { POST_USER_STRUCTURE_BODY } from "../../../_common/mocks";
16-
import { TESTS_USERS_STRUCTURE } from "../../../_tests";
16+
import { TESTS_USERS_STRUCTURE, TestUserStructure } from "../../../_tests";
1717
import { usersDeletor } from "../services/users-deletor.service";
1818
import { MailsModule } from "../../mails/mails.module";
1919
import { AppLogsService } from "../../app-logs/app-logs.service";
20-
import { userStructureRepository } from "../../../database";
20+
import { appLogsRepository } from "../../../database";
2121

2222
describe("Users Controller", () => {
2323
let controller: UsersController;
2424
let context: AppTestContext;
25-
let appLogService: AppLogsService;
25+
let authInfo: TestUserStructure;
2626
beforeAll(async () => {
27-
appLogService = {
28-
create: jest.fn(),
29-
};
3027
context = await AppTestHelper.bootstrapTestApp({
3128
controllers: [UsersController],
3229
imports: [MailsModule, StructuresModule, UsagersModule, HttpModule],
33-
providers: [
34-
{
35-
provide: AppLogsService,
36-
useValue: appLogService,
37-
},
38-
],
30+
providers: [AppLogsService],
3931
});
4032

41-
const authInfo =
33+
authInfo =
4234
TESTS_USERS_STRUCTURE.BY_EMAIL["preprod.domifa@fabrique.social.gouv.fr"];
4335
await AppTestHelper.authenticateStructure(authInfo, { context });
4436
controller = context.module.get<UsersController>(UsersController);
@@ -49,33 +41,56 @@ describe("Users Controller", () => {
4941
await AppTestHelper.tearDownTestApp(context);
5042
});
5143

44+
beforeEach(async () => {
45+
await appLogsRepository.clear();
46+
});
47+
5248
describe("> Register user", () => {
5349
describe("Nominal case", () => {
5450
it("should be defined", async () => {
5551
expect(controller).toBeDefined();
5652
});
5753

5854
it("should be 200", async () => {
55+
const structureAffectationId = 1;
5956
const response = await AppTestHttpClient.post("/users/register", {
6057
context,
6158
body: {
6259
...POST_USER_STRUCTURE_BODY,
6360
email: "test@test.com",
6461
structureId: 1,
65-
structure: { ...POST_USER_STRUCTURE_BODY.structure, id: 1 },
62+
structure: {
63+
...POST_USER_STRUCTURE_BODY.structure,
64+
id: structureAffectationId,
65+
},
6666
},
6767
});
68-
69-
const user = await userStructureRepository.findOneByOrFail({
70-
email: "test@test.com",
68+
const logs = await appLogsRepository.find({
69+
where: {
70+
role: authInfo.role,
71+
structureId: authInfo.structureId,
72+
userId: authInfo.id,
73+
action: "USER_CREATE",
74+
},
7175
});
72-
expect(appLogService.create).toHaveBeenCalledWith({
76+
expect(logs.length).toEqual(1);
77+
expect({
78+
userId: logs[0].userId,
79+
structureId: logs[0].structureId,
80+
role: logs[0].role,
81+
action: logs[0].action,
82+
context: {
83+
role: logs[0].context.role,
84+
structureId: logs[0].context.structureId,
85+
},
86+
}).toEqual({
87+
userId: authInfo.id,
88+
structureId: authInfo.structureId,
89+
role: authInfo.role,
7390
action: "USER_CREATE",
74-
userId: context.user.userId,
7591
context: {
7692
role: POST_USER_STRUCTURE_BODY.role,
77-
structureId: 1,
78-
userId: user.id,
93+
structureId: structureAffectationId,
7994
},
8095
});
8196
expect(response.status).toBe(200);

packages/backend/src/modules/users/controllers/users.controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ export class UsersController {
299299
await this.appLogService.create<UserStructureCreateLogContext>({
300300
action: "USER_CREATE",
301301
userId: user.id,
302+
structureId: "structureId" in user ? user.structureId : null,
303+
role: user.role,
302304
context: {
303305
role: newUser.role,
304306
userId: newUser.id,

0 commit comments

Comments
 (0)