41
41
import org .xwiki .rendering .transformation .MacroTransformationContext ;
42
42
import org .xwiki .rendering .transformation .TransformationContext ;
43
43
import org .xwiki .security .authorization .AuthorExecutor ;
44
+ import org .xwiki .security .authorization .AuthorizationManager ;
45
+ import org .xwiki .security .authorization .Right ;
44
46
import org .xwiki .test .junit5 .mockito .ComponentTest ;
45
47
import org .xwiki .test .junit5 .mockito .InjectMockComponents ;
46
48
import org .xwiki .test .junit5 .mockito .MockComponent ;
57
59
import static org .mockito .ArgumentMatchers .any ;
58
60
import static org .mockito .ArgumentMatchers .eq ;
59
61
import static org .mockito .Mockito .mock ;
62
+ import static org .mockito .Mockito .verify ;
60
63
import static org .mockito .Mockito .when ;
61
64
62
65
@ ComponentTest
@@ -72,6 +75,9 @@ class DefaultGadgetSourceTest
72
75
@ MockComponent
73
76
private AuthorExecutor authorExecutor ;
74
77
78
+ @ MockComponent
79
+ private AuthorizationManager authorizationManager ;
80
+
75
81
@ Mock
76
82
private DocumentReference documentReference ;
77
83
@@ -99,6 +105,11 @@ class DefaultGadgetSourceTest
99
105
@ Mock
100
106
private MacroTransformationContext macroTransformationContext ;
101
107
108
+ @ Mock
109
+ private VelocityEngine velocityEngine ;
110
+
111
+ private ContentExecutor <MacroTransformationContext > contentExecutor ;
112
+
102
113
@ BeforeEach
103
114
void setup (MockitoComponentManager componentManager ) throws Exception
104
115
{
@@ -123,23 +134,14 @@ void setup(MockitoComponentManager componentManager) throws Exception
123
134
when (transformationContext .getId ()).thenReturn (transformationId );
124
135
125
136
VelocityManager velocityManager = componentManager .getInstance (VelocityManager .class );
126
- VelocityEngine velocityEngine = mock (VelocityEngine .class );
127
137
when (velocityManager .getVelocityEngine ()).thenReturn (velocityEngine );
128
- when (velocityEngine .evaluate (any (), any (), any (), any (String .class ))).then ((Answer <Void >) invocation -> {
129
- Object [] args = invocation .getArguments ();
130
- StringWriter stringWriter = (StringWriter ) args [1 ];
131
- String title = (String ) args [3 ];
132
- stringWriter .append (title );
133
- return null ;
134
- });
135
138
136
- AuthorExecutor authorExecutor = componentManager .getInstance (AuthorExecutor .class );
137
139
when (authorExecutor .call (any (), eq (ownerAuthorReference ), eq (ownerSourceReference ))).then (invocationOnMock -> {
138
140
Callable callable = (Callable ) invocationOnMock .getArguments ()[0 ];
139
141
return callable .call ();
140
142
});
141
143
142
- ContentExecutor < MacroTransformationContext > contentExecutor =
144
+ this . contentExecutor =
143
145
componentManager .getInstance (ContentExecutor .TYPE_MACRO_TRANSFORMATION );
144
146
when (contentExecutor .execute (any (), any (), any (), any ())).then ((Answer <XDOM >) invocationOnMock -> {
145
147
String content = invocationOnMock .getArgument (0 );
@@ -162,12 +164,50 @@ void getGadgets() throws Exception
162
164
when (gadgetObject1 .getLargeStringValue ("content" )).thenReturn ("Some content" );
163
165
when (gadgetObject1 .getStringValue ("position" )).thenReturn ("0" );
164
166
when (gadgetObject1 .getNumber ()).thenReturn (42 );
167
+ when (this .authorizationManager .hasAccess (Right .SCRIPT , ownerAuthorReference , ownerSourceReference )).thenReturn (true );
168
+ when (this .velocityEngine .evaluate (any (), any (), any (), eq ("Gadget 1" ))).then ((Answer <Void >) invocation -> {
169
+ Object [] args = invocation .getArguments ();
170
+ StringWriter stringWriter = (StringWriter ) args [1 ];
171
+ String title = "Evaluated velocity version of gadget 1" ;
172
+ stringWriter .append (title );
173
+ return null ;
174
+ });
165
175
166
176
List <Gadget > gadgets = this .defaultGadgetSource .getGadgets (testSource , macroTransformationContext );
167
177
assertEquals (1 , gadgets .size ());
168
178
Gadget gadget = gadgets .get (0 );
169
- assertEquals ("Gadget 1" , gadget .getTitle ().get (0 ).toString ());
179
+ assertEquals ("Evaluated velocity version of gadget 1" , gadget .getTitle ().get (0 ).toString ());
170
180
assertEquals ("Some content" , gadget .getContent ().get (0 ).toString ());
171
181
assertEquals ("42" , gadget .getId ());
182
+ verify (this .contentExecutor )
183
+ .execute (eq ("Evaluated velocity version of gadget 1" ), any (), any (), any ());
184
+ verify (this .contentExecutor )
185
+ .execute (eq ("Some content" ), any (), any (), any ());
186
+ }
187
+
188
+ @ Test
189
+ void getGadgetWithoutScriptRight () throws Exception
190
+ {
191
+ assertEquals (new ArrayList <>(), this .defaultGadgetSource .getGadgets (testSource , macroTransformationContext ));
192
+
193
+ BaseObject gadgetObject1 = mock (BaseObject .class );
194
+ when (xWikiDocument .getXObjects (gadgetClassReference )).thenReturn (Collections .singletonList (gadgetObject1 ));
195
+ when (gadgetObject1 .getOwnerDocument ()).thenReturn (ownerDocument );
196
+ when (gadgetObject1 .getStringValue ("title" )).thenReturn ("Gadget 2" );
197
+ when (gadgetObject1 .getLargeStringValue ("content" )).thenReturn ("Some other content" );
198
+ when (gadgetObject1 .getStringValue ("position" )).thenReturn ("2" );
199
+ when (gadgetObject1 .getNumber ()).thenReturn (12 );
200
+ when (this .authorizationManager .hasAccess (Right .SCRIPT , ownerAuthorReference , ownerSourceReference )).thenReturn (false );
201
+
202
+ List <Gadget > gadgets = this .defaultGadgetSource .getGadgets (testSource , macroTransformationContext );
203
+ assertEquals (1 , gadgets .size ());
204
+ Gadget gadget = gadgets .get (0 );
205
+ assertEquals ("Gadget 2" , gadget .getTitle ().get (0 ).toString ());
206
+ assertEquals ("Some other content" , gadget .getContent ().get (0 ).toString ());
207
+ assertEquals ("12" , gadget .getId ());
208
+ verify (this .contentExecutor )
209
+ .execute (eq ("Gadget 2" ), any (), any (), any ());
210
+ verify (this .contentExecutor )
211
+ .execute (eq ("Some other content" ), any (), any (), any ());
172
212
}
173
213
}
0 commit comments