6
6
import it .gov .pagopa .pu .fileshare .dto .generated .FileOrigin ;
7
7
import it .gov .pagopa .pu .fileshare .dto .generated .IngestionFlowFileType ;
8
8
import it .gov .pagopa .pu .fileshare .exception .custom .FileAlreadyExistsException ;
9
+ import it .gov .pagopa .pu .fileshare .exception .custom .UnauthorizedFileDownloadException ;
9
10
import it .gov .pagopa .pu .fileshare .mapper .IngestionFlowFileDTOMapper ;
10
11
import it .gov .pagopa .pu .fileshare .service .FileService ;
11
12
import it .gov .pagopa .pu .fileshare .service .FileStorerService ;
25
26
import org .mockito .junit .jupiter .MockitoExtension ;
26
27
import org .springframework .http .MediaType ;
27
28
import org .springframework .mock .web .MockMultipartFile ;
29
+ import org .springframework .security .authorization .AuthorizationDeniedException ;
28
30
29
31
import java .io .ByteArrayInputStream ;
30
32
import java .io .InputStream ;
@@ -203,7 +205,7 @@ void givenAuthorizedUserWhenDownloadIngestionFlowFileThenReturnFileResource() {
203
205
String fileName = "testFile.zip" ;
204
206
Path fullFilePath = organizationBasePath .resolve (filePathName ).resolve (ARCHIVED_SUB_FOLDER );
205
207
206
- UserInfo user = TestUtils .getSampleUser ();
208
+ UserInfo user = TestUtils .getSampleAdminUser ();
207
209
208
210
IngestionFlowFile ingestionFlowFile = new IngestionFlowFile ();
209
211
ingestionFlowFile .setOrganizationId (organizationId );
@@ -230,6 +232,45 @@ void givenAuthorizedUserWhenDownloadIngestionFlowFileThenReturnFileResource() {
230
232
Mockito .verify (fileStorerServiceMock ).decryptFile (fullFilePath , fileName );
231
233
}
232
234
235
+ @ Test
236
+ void givenUnauthorizedOrganizationWhenDownloadIngestionFlowFileThenThrowAuthorizationDeniedException () {
237
+ String accessToken = "TOKEN" ;
238
+ Long organizationId = 1L ;
239
+ Long ingestionFlowFileId = 10L ;
240
+
241
+ UserInfo user = TestUtils .getSampleAdminUser ();
242
+
243
+ IngestionFlowFile ingestionFlowFile = new IngestionFlowFile ();
244
+ ingestionFlowFile .setOrganizationId (-1L );
245
+ ingestionFlowFile .setStatus (IngestionFlowFile .StatusEnum .COMPLETED );
246
+
247
+ Mockito .when (ingestionFlowFileServiceMock .getIngestionFlowFile (ingestionFlowFileId , accessToken )).thenReturn (ingestionFlowFile );
248
+
249
+ Assertions .assertThrows (AuthorizationDeniedException .class , () -> ingestionFlowFileService .downloadIngestionFlowFile (organizationId , ingestionFlowFileId , user , accessToken ));
250
+
251
+ Mockito .verify (userAuthorizationServiceMock ).checkUserAuthorization (organizationId , user , accessToken );
252
+ }
253
+
254
+ @ Test
255
+ void givenUnauthorizedUserWhenDownloadIngestionFlowFileThenThrowAuthorizationDeniedException () {
256
+ String accessToken = "TOKEN" ;
257
+ Long organizationId = 1L ;
258
+ Long ingestionFlowFileId = 10L ;
259
+
260
+ UserInfo user = TestUtils .getSampleUser ();
261
+
262
+ IngestionFlowFile ingestionFlowFile = new IngestionFlowFile ();
263
+ ingestionFlowFile .setOrganizationId (1L );
264
+ ingestionFlowFile .setOperatorExternalId ("OTHERUSER" );
265
+ ingestionFlowFile .setStatus (IngestionFlowFile .StatusEnum .COMPLETED );
266
+
267
+ Mockito .when (ingestionFlowFileServiceMock .getIngestionFlowFile (ingestionFlowFileId , accessToken )).thenReturn (ingestionFlowFile );
268
+
269
+ Assertions .assertThrows (UnauthorizedFileDownloadException .class , () -> ingestionFlowFileService .downloadIngestionFlowFile (organizationId , ingestionFlowFileId , user , accessToken ));
270
+
271
+ Mockito .verify (userAuthorizationServiceMock ).checkUserAuthorization (organizationId , user , accessToken );
272
+ }
273
+
233
274
@ Test
234
275
void givenIngestionFlowFileInProgressWhenDownloadIngestionFlowFileThenReturnFilePath () {
235
276
String accessToken = "TOKEN" ;
@@ -240,7 +281,7 @@ void givenIngestionFlowFileInProgressWhenDownloadIngestionFlowFileThenReturnFile
240
281
String fileName = "testFile.zip" ;
241
282
Path fullFilePath = organizationBasePath .resolve (filePathName );
242
283
243
- UserInfo user = TestUtils .getSampleUser ();
284
+ UserInfo user = TestUtils .getSampleAdminUser ();
244
285
245
286
IngestionFlowFile ingestionFlowFile = new IngestionFlowFile ();
246
287
ingestionFlowFile .setOrganizationId (organizationId );
0 commit comments