1
- import { FailedUsagerImportLogContext } from "./../../../modules/app-logs/app-log-context.types" ;
2
1
import { HttpStatus } from "@nestjs/common" ;
3
2
4
3
import { pathExists } from "fs-extra" ;
@@ -18,7 +17,7 @@ import { ImportController } from "./import.controller";
18
17
import { UsagerHistoryStateService } from "../../services/usagerHistoryState.service" ;
19
18
import { UsagersModule } from "../../usagers.module" ;
20
19
import { AppLogsService } from "../../../modules/app-logs/app-logs.service" ;
21
- import { AppLog } from "../../../_common/model " ;
20
+ import { appLogsRepository } from "../../../database " ;
22
21
23
22
const importFilesDir = resolve (
24
23
__dirname ,
@@ -27,26 +26,15 @@ const importFilesDir = resolve(
27
26
28
27
describe ( "Import Controller" , ( ) => {
29
28
let controller : ImportController ;
30
- let appLogService : AppLogsService ;
31
29
32
30
let context : AppTestContext ;
33
31
let authInfo : TestUserStructure ;
34
32
beforeAll ( async ( ) => {
35
- appLogService = {
36
- create : jest . fn ( ) ,
37
- } ;
38
33
context = await AppTestHelper . bootstrapTestApp (
39
34
{
40
35
controllers : [ ImportController ] ,
41
36
imports : [ UsersModule , StructuresModule , UsagersModule ] ,
42
- providers : [
43
- UsagersService ,
44
- UsagerHistoryStateService ,
45
- {
46
- provide : AppLogsService ,
47
- useValue : appLogService ,
48
- } ,
49
- ] ,
37
+ providers : [ UsagersService , UsagerHistoryStateService , AppLogsService ] ,
50
38
} ,
51
39
{ initApp : true }
52
40
) ;
@@ -58,8 +46,8 @@ describe("Import Controller", () => {
58
46
controller = context . module . get < ImportController > ( ImportController ) ;
59
47
} ) ;
60
48
61
- beforeEach ( ( ) => {
62
- jest . clearAllMocks ( ) ;
49
+ beforeEach ( async ( ) => {
50
+ await appLogsRepository . clear ( ) ;
63
51
} ) ;
64
52
65
53
afterAll ( async ( ) => {
@@ -71,20 +59,6 @@ describe("Import Controller", () => {
71
59
} ) ;
72
60
73
61
it ( `❌ Import d'un fichier Incorrect` , async ( ) => {
74
- const expectedLogContextEntree : FailedUsagerImportLogContext = {
75
- nombreActifs : 0 ,
76
- nombreErreurs : 5 ,
77
- nombreTotal : 2 ,
78
- } ;
79
-
80
- const expectedLog : AppLog = {
81
- userId : authInfo . id ,
82
- structureId : authInfo . structureId ,
83
- role : authInfo . role ,
84
- context : expectedLogContextEntree ,
85
- action : "IMPORT_USAGERS_FAILED" ,
86
- } ;
87
-
88
62
const importFilePath = resolve ( importFilesDir , "import_ko_1.xlsx" ) ;
89
63
90
64
expect ( await pathExists ( importFilePath ) ) . toBeTruthy ( ) ;
@@ -98,9 +72,21 @@ describe("Import Controller", () => {
98
72
context,
99
73
} ) ;
100
74
101
- expect ( appLogService . create ) . toHaveBeenCalledWith ( expectedLog ) ;
102
-
103
75
expect ( response . status ) . toBe ( HttpStatus . BAD_REQUEST ) ;
76
+
77
+ const logs = await appLogsRepository . find ( {
78
+ where : {
79
+ userId : authInfo . id ,
80
+ structureId : authInfo . structureId ,
81
+ } ,
82
+ } ) ;
83
+ expect ( logs . length ) . toBe ( 1 ) ;
84
+ expect ( logs [ 0 ] . action ) . toBe ( "IMPORT_USAGERS_FAILED" ) ;
85
+ expect ( logs [ 0 ] . context ) . toEqual ( {
86
+ nombreActifs : 0 ,
87
+ nombreErreurs : 5 ,
88
+ nombreTotal : 2 ,
89
+ } ) ;
104
90
} ) ;
105
91
106
92
it ( `✅ Import d'un fichier Valide 1️⃣` , async ( ) => {
@@ -116,16 +102,6 @@ describe("Import Controller", () => {
116
102
attachments : { file : importFilePath } ,
117
103
context,
118
104
} ) ;
119
- expect ( appLogService . create ) . toHaveBeenCalledWith ( {
120
- action : "IMPORT_USAGERS_SUCCESS" ,
121
- context : {
122
- nombreActifs : 19 ,
123
- nombreTotal : 0 ,
124
- } ,
125
- userId : authInfo . id ,
126
- structureId : authInfo . structureId ,
127
- role : authInfo . role ,
128
- } ) ;
129
105
expect ( response . status ) . toBe ( HttpStatus . OK ) ;
130
106
expect ( JSON . parse ( response . text ) ) . toEqual ( {
131
107
importMode : "confirm" ,
@@ -136,11 +112,23 @@ describe("Import Controller", () => {
136
112
totalCount : 0 ,
137
113
} ,
138
114
} ) ;
115
+
116
+ const logs = await appLogsRepository . find ( {
117
+ where : {
118
+ userId : authInfo . id ,
119
+ structureId : authInfo . structureId ,
120
+ } ,
121
+ } ) ;
122
+ expect ( logs . length ) . toBe ( 1 ) ;
123
+ expect ( logs [ 0 ] . action ) . toBe ( "IMPORT_USAGERS_SUCCESS" ) ;
124
+ expect ( logs [ 0 ] . context ) . toEqual ( {
125
+ nombreActifs : 19 ,
126
+ nombreTotal : 19 ,
127
+ } ) ;
139
128
} ) ;
140
129
141
130
it ( `✅ Import d'un fichier Valide 2️⃣` , async ( ) => {
142
131
const importFilePath = resolve ( importFilesDir , "import_ok_2.xlsx" ) ;
143
-
144
132
expect ( await pathExists ( importFilePath ) ) . toBeTruthy ( ) ;
145
133
146
134
const headers : { [ name : string ] : string } = { } ;
@@ -151,16 +139,6 @@ describe("Import Controller", () => {
151
139
attachments : { file : importFilePath } ,
152
140
context,
153
141
} ) ;
154
- expect ( appLogService . create ) . toHaveBeenCalledWith ( {
155
- action : "IMPORT_USAGERS_SUCCESS" ,
156
- context : {
157
- nombreActifs : 2 ,
158
- nombreTotal : 0 ,
159
- } ,
160
- userId : authInfo . id ,
161
- structureId : authInfo . structureId ,
162
- role : authInfo . role ,
163
- } ) ;
164
142
expect ( response . status ) . toBe ( HttpStatus . OK ) ;
165
143
expect ( JSON . parse ( response . text ) ) . toEqual ( {
166
144
importMode : "confirm" ,
@@ -171,5 +149,18 @@ describe("Import Controller", () => {
171
149
totalCount : 0 ,
172
150
} ,
173
151
} ) ;
152
+
153
+ const logs = await appLogsRepository . find ( {
154
+ where : {
155
+ userId : authInfo . id ,
156
+ structureId : authInfo . structureId ,
157
+ } ,
158
+ } ) ;
159
+ expect ( logs . length ) . toBe ( 1 ) ;
160
+ expect ( logs [ 0 ] . action ) . toBe ( "IMPORT_USAGERS_SUCCESS" ) ;
161
+ expect ( logs [ 0 ] . context ) . toEqual ( {
162
+ nombreActifs : 2 ,
163
+ nombreTotal : 4 ,
164
+ } ) ;
174
165
} ) ;
175
166
} ) ;
0 commit comments