@@ -13,32 +13,24 @@ import {
13
13
} from "../../../util/test" ;
14
14
import { UsersController } from "./users.controller" ;
15
15
import { POST_USER_STRUCTURE_BODY } from "../../../_common/mocks" ;
16
- import { TESTS_USERS_STRUCTURE } from "../../../_tests" ;
16
+ import { TESTS_USERS_STRUCTURE , TestUserStructure } from "../../../_tests" ;
17
17
import { usersDeletor } from "../services/users-deletor.service" ;
18
18
import { MailsModule } from "../../mails/mails.module" ;
19
19
import { AppLogsService } from "../../app-logs/app-logs.service" ;
20
- import { userStructureRepository } from "../../../database" ;
20
+ import { appLogsRepository } from "../../../database" ;
21
21
22
22
describe ( "Users Controller" , ( ) => {
23
23
let controller : UsersController ;
24
24
let context : AppTestContext ;
25
- let appLogService : AppLogsService ;
25
+ let authInfo : TestUserStructure ;
26
26
beforeAll ( async ( ) => {
27
- appLogService = {
28
- create : jest . fn ( ) ,
29
- } ;
30
27
context = await AppTestHelper . bootstrapTestApp ( {
31
28
controllers : [ UsersController ] ,
32
29
imports : [ MailsModule , StructuresModule , UsagersModule , HttpModule ] ,
33
- providers : [
34
- {
35
- provide : AppLogsService ,
36
- useValue : appLogService ,
37
- } ,
38
- ] ,
30
+ providers : [ AppLogsService ] ,
39
31
} ) ;
40
32
41
- const authInfo =
33
+ authInfo =
42
34
TESTS_USERS_STRUCTURE . BY_EMAIL [ "preprod.domifa@fabrique.social.gouv.fr" ] ;
43
35
await AppTestHelper . authenticateStructure ( authInfo , { context } ) ;
44
36
controller = context . module . get < UsersController > ( UsersController ) ;
@@ -49,33 +41,56 @@ describe("Users Controller", () => {
49
41
await AppTestHelper . tearDownTestApp ( context ) ;
50
42
} ) ;
51
43
44
+ beforeEach ( async ( ) => {
45
+ await appLogsRepository . clear ( ) ;
46
+ } ) ;
47
+
52
48
describe ( "> Register user" , ( ) => {
53
49
describe ( "Nominal case" , ( ) => {
54
50
it ( "should be defined" , async ( ) => {
55
51
expect ( controller ) . toBeDefined ( ) ;
56
52
} ) ;
57
53
58
54
it ( "should be 200" , async ( ) => {
55
+ const structureAffectationId = 1 ;
59
56
const response = await AppTestHttpClient . post ( "/users/register" , {
60
57
context,
61
58
body : {
62
59
...POST_USER_STRUCTURE_BODY ,
63
60
email : "test@test.com" ,
64
61
structureId : 1 ,
65
- structure : { ...POST_USER_STRUCTURE_BODY . structure , id : 1 } ,
62
+ structure : {
63
+ ...POST_USER_STRUCTURE_BODY . structure ,
64
+ id : structureAffectationId ,
65
+ } ,
66
66
} ,
67
67
} ) ;
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
+ } ,
71
75
} ) ;
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 ,
73
90
action : "USER_CREATE" ,
74
- userId : context . user . userId ,
75
91
context : {
76
92
role : POST_USER_STRUCTURE_BODY . role ,
77
- structureId : 1 ,
78
- userId : user . id ,
93
+ structureId : structureAffectationId ,
79
94
} ,
80
95
} ) ;
81
96
expect ( response . status ) . toBe ( 200 ) ;
0 commit comments