29
29
import javax .inject .Provider ;
30
30
import javax .servlet .http .Part ;
31
31
32
+ import ch .qos .logback .classic .Level ;
32
33
import jakarta .servlet .http .HttpSession ;
33
34
34
35
import org .junit .jupiter .api .BeforeEach ;
35
36
import org .junit .jupiter .api .Test ;
37
+ import org .junit .jupiter .api .extension .RegisterExtension ;
36
38
import org .junit .jupiter .params .ParameterizedTest ;
37
39
import org .junit .jupiter .params .provider .NullSource ;
38
40
import org .junit .jupiter .params .provider .ValueSource ;
44
46
import org .xwiki .model .reference .DocumentReference ;
45
47
import org .xwiki .model .reference .SpaceReference ;
46
48
import org .xwiki .store .TemporaryAttachmentException ;
49
+ import org .xwiki .test .LogLevel ;
47
50
import org .xwiki .test .TestEnvironment ;
48
51
import org .xwiki .test .annotation .ComponentList ;
52
+ import org .xwiki .test .junit5 .LogCaptureExtension ;
49
53
import org .xwiki .test .junit5 .mockito .ComponentTest ;
50
54
import org .xwiki .test .junit5 .mockito .InjectMockComponents ;
51
55
import org .xwiki .test .junit5 .mockito .MockComponent ;
60
64
import static com .xpn .xwiki .plugin .fileupload .FileUploadPlugin .UPLOAD_MAXSIZE_PARAMETER ;
61
65
import static java .nio .charset .StandardCharsets .UTF_8 ;
62
66
import static org .junit .jupiter .api .Assertions .assertEquals ;
67
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
63
68
import static org .junit .jupiter .api .Assertions .assertNotNull ;
64
69
import static org .junit .jupiter .api .Assertions .assertThrows ;
65
70
import static org .junit .jupiter .api .Assertions .assertTrue ;
@@ -97,6 +102,9 @@ class DefaultTemporaryAttachmentSessionsManagerTest
97
102
@ MockComponent
98
103
private Container container ;
99
104
105
+ @ RegisterExtension
106
+ private LogCaptureExtension logCapture = new LogCaptureExtension (LogLevel .WARN );
107
+
100
108
@ Mock
101
109
private AttachmentValidator attachmentValidator ;
102
110
@@ -106,28 +114,41 @@ class DefaultTemporaryAttachmentSessionsManagerTest
106
114
@ Mock
107
115
private HttpSession httpSession ;
108
116
117
+ @ Mock
118
+ private ServletSession servletSession ;
119
+
109
120
@ BeforeEach
110
121
void setup (MockitoComponentManager mockitoComponentManager ) throws Exception
111
122
{
112
123
when (this .contextProvider .get ()).thenReturn (this .context );
113
-
114
- ServletSession session = mock (ServletSession .class );
115
- when (session .getSession ()).thenReturn (this .httpSession );
116
- when (this .container .getSession ()).thenReturn (session );
124
+ when (this .container .getSession ()).thenReturn (this .servletSession );
117
125
Utils .setComponentManager (mockitoComponentManager );
118
126
119
127
when (this .attachmentValidatorProvider .get ()).thenReturn (this .attachmentValidator );
120
128
}
121
129
130
+ private void setupSession ()
131
+ {
132
+ when (this .servletSession .getSession ()).thenReturn (this .httpSession );
133
+ }
134
+
122
135
@ Test
123
136
void uploadAttachment () throws Exception
124
137
{
125
- String sessionId = "mySession" ;
126
- when (this .httpSession .getId ()).thenReturn (sessionId );
127
-
128
138
DocumentReference documentReference = new DocumentReference ("xwiki" , "Space" , "Document" );
129
139
Part part = mock (Part .class );
130
140
141
+ // session is null
142
+ TemporaryAttachmentException temporaryAttachmentException =
143
+ assertThrows (TemporaryAttachmentException .class , () -> {
144
+ this .attachmentManager .uploadAttachment (documentReference , part );
145
+ });
146
+ assertEquals ("Cannot find a user http session." , temporaryAttachmentException .getMessage ());
147
+
148
+ setupSession ();
149
+ String sessionId = "mySession" ;
150
+ when (this .httpSession .getId ()).thenReturn (sessionId );
151
+
131
152
String filename = "fileFoo.xml" ;
132
153
when (part .getSubmittedFileName ()).thenReturn (filename );
133
154
InputStream inputStream = new ByteArrayInputStream ("foo" .getBytes (UTF_8 ));
@@ -166,6 +187,14 @@ void uploadAttachmentWithFilename() throws Exception
166
187
XWiki xwiki = mock (XWiki .class );
167
188
Part part = mock (Part .class );
168
189
190
+ // session is null
191
+ TemporaryAttachmentException temporaryAttachmentException =
192
+ assertThrows (TemporaryAttachmentException .class , () -> {
193
+ this .attachmentManager .uploadAttachment (documentReference , part , "newFilename" );
194
+ });
195
+ assertEquals ("Cannot find a user http session." , temporaryAttachmentException .getMessage ());
196
+
197
+ setupSession ();
169
198
when (this .context .getWiki ()).thenReturn (xwiki );
170
199
when (xwiki .getSpacePreference (UPLOAD_MAXSIZE_PARAMETER , spaceReference , this .context ))
171
200
.thenReturn ("42" );
@@ -181,6 +210,7 @@ void uploadAttachmentWithFilename() throws Exception
181
210
@ Test
182
211
void uploadAttachmentInvalid () throws Exception
183
212
{
213
+ setupSession ();
184
214
DocumentReference documentReference = new DocumentReference ("xwiki" , "XWiki" , "Document" );
185
215
SpaceReference spaceReference = documentReference .getLastSpaceReference ();
186
216
@@ -215,6 +245,7 @@ void uploadAttachmentInvalid() throws Exception
215
245
})
216
246
void uploadAttachmentWithEmptyFilename (String filename ) throws Exception
217
247
{
248
+ setupSession ();
218
249
DocumentReference documentReference = new DocumentReference ("xwiki" , "XWiki" , "Document" );
219
250
SpaceReference spaceReference = documentReference .getLastSpaceReference ();
220
251
@@ -248,6 +279,10 @@ void getUploadedAttachments()
248
279
List <XWikiAttachment > expectedList = Arrays .asList (attachment1 , attachment2 , attachment3 );
249
280
when (temporaryAttachmentSession .getAttachments (documentReference )).thenReturn (expectedList );
250
281
282
+ // session is null
283
+ assertEquals (Collections .emptyList (), this .attachmentManager .getUploadedAttachments (documentReference ));
284
+
285
+ setupSession ();
251
286
assertEquals (expectedList ,
252
287
this .attachmentManager .getUploadedAttachments (documentReference ));
253
288
}
@@ -267,6 +302,11 @@ void getUploadedAttachment()
267
302
268
303
when (temporaryAttachmentSession .getAttachment (documentReference , filename ))
269
304
.thenReturn (Optional .of (attachment1 ));
305
+
306
+ // session is null
307
+ assertEquals (Optional .empty (), this .attachmentManager .getUploadedAttachment (documentReference , filename ));
308
+
309
+ setupSession ();
270
310
assertEquals (Optional .of (attachment1 ),
271
311
this .attachmentManager .getUploadedAttachment (documentReference , filename ));
272
312
}
@@ -280,8 +320,18 @@ void removeUploadedAttachment()
280
320
when (httpSession .getAttribute (ATTRIBUTE_KEY )).thenReturn (temporaryAttachmentSession );
281
321
282
322
DocumentReference documentReference = mock (DocumentReference .class );
323
+ when (documentReference .toString ()).thenReturn ("reference" );
283
324
String filename = "foobar" ;
284
325
when (temporaryAttachmentSession .removeAttachment (documentReference , filename )).thenReturn (true );
326
+
327
+ // session is null
328
+ assertFalse (this .attachmentManager .removeUploadedAttachment (documentReference , filename ));
329
+ assertEquals (1 , this .logCapture .size ());
330
+ assertEquals ("Cannot find a user http session to remove attachment [foobar] from [reference]." ,
331
+ this .logCapture .getMessage (0 ));
332
+ assertEquals (Level .WARN , this .logCapture .getLogEvent (0 ).getLevel ());
333
+
334
+ setupSession ();
285
335
assertTrue (this .attachmentManager .removeUploadedAttachment (documentReference , filename ));
286
336
}
287
337
@@ -293,20 +343,40 @@ void removeUploadedAttachments()
293
343
TemporaryAttachmentSession temporaryAttachmentSession = mock (TemporaryAttachmentSession .class );
294
344
when (httpSession .getAttribute (ATTRIBUTE_KEY )).thenReturn (temporaryAttachmentSession );
295
345
DocumentReference documentReference = mock (DocumentReference .class );
346
+ when (documentReference .toString ()).thenReturn ("reference" );
296
347
when (temporaryAttachmentSession .removeAttachments (documentReference )).thenReturn (true );
348
+
349
+ // session is null
350
+ assertFalse (this .attachmentManager .removeUploadedAttachments (documentReference ));
351
+ assertEquals (1 , this .logCapture .size ());
352
+ assertEquals ("Cannot find a user http session to remove attachments from [reference]." ,
353
+ this .logCapture .getMessage (0 ));
354
+ assertEquals (Level .WARN , this .logCapture .getLogEvent (0 ).getLevel ());
355
+
356
+ setupSession ();
297
357
assertTrue (this .attachmentManager .removeUploadedAttachments (documentReference ));
298
358
}
299
359
300
360
@ Test
301
- void temporarilyAttach () throws TemporaryAttachmentException
361
+ void temporarilyAttach ()
302
362
{
303
363
DocumentReference documentReference = mock (DocumentReference .class );
364
+ when (documentReference .toString ()).thenReturn ("reference" );
304
365
XWikiAttachment attachment = mock (XWikiAttachment .class );
366
+ when (attachment .toString ()).thenReturn ("attachment" );
305
367
String sessionId = "removeUploadedAttachmentsPlural" ;
306
368
when (httpSession .getId ()).thenReturn (sessionId );
307
369
TemporaryAttachmentSession temporaryAttachmentSession = mock (TemporaryAttachmentSession .class );
308
370
when (httpSession .getAttribute (ATTRIBUTE_KEY )).thenReturn (temporaryAttachmentSession );
309
371
372
+ // session is null
373
+ this .attachmentManager .temporarilyAttach (attachment , documentReference );
374
+ assertEquals (1 , this .logCapture .size ());
375
+ assertEquals ("Cannot find a user http session to attach [attachment] to [reference]." ,
376
+ this .logCapture .getMessage (0 ));
377
+ assertEquals (Level .ERROR , this .logCapture .getLogEvent (0 ).getLevel ());
378
+
379
+ setupSession ();
310
380
this .attachmentManager .temporarilyAttach (attachment , documentReference );
311
381
verify (temporaryAttachmentSession ).addAttachment (documentReference , attachment );
312
382
@@ -343,6 +413,7 @@ void attachTemporaryAttachmentsInDocument()
343
413
when (temporaryAttachmentSession .getAttachment (documentReference , file1 )).thenReturn (Optional .of (attachment1 ));
344
414
when (temporaryAttachmentSession .getAttachment (documentReference , file2 )).thenReturn (Optional .of (attachment2 ));
345
415
416
+ setupSession ();
346
417
this .attachmentManager .attachTemporaryAttachmentsInDocument (document , List .of (
347
418
"foo" ,
348
419
file1 ,
0 commit comments