1919import com .thoughtworks .go .plugin .api .request .GoPluginApiRequest ;
2020import com .thoughtworks .go .plugin .api .response .DefaultGoPluginApiResponse ;
2121import com .thoughtworks .go .plugin .infra .PluginManager ;
22+ import com .thoughtworks .go .server .web .ResponseCodeView ;
2223import com .thoughtworks .go .util .ReflectionUtil ;
2324import org .junit .Before ;
2425import org .junit .Test ;
2930import javax .servlet .http .HttpServletRequest ;
3031import javax .servlet .http .HttpServletResponse ;
3132import java .io .PrintWriter ;
32- import java .util .*;
33+ import java .util .ArrayList ;
34+ import java .util .Arrays ;
35+ import java .util .Enumeration ;
36+ import java .util .HashMap ;
37+ import java .util .List ;
38+ import java .util .Map ;
3339
3440import static org .hamcrest .Matchers .nullValue ;
3541import static org .hamcrest .core .Is .is ;
@@ -71,6 +77,7 @@ public void setUp() throws Exception {
7177 @ Test
7278 public void shouldForwardWebRequestToPlugin () throws Exception {
7379 when (pluginManager .submitTo (eq (PLUGIN_ID ), requestArgumentCaptor .capture ())).thenReturn (new DefaultGoPluginApiResponse (200 ));
80+ when (pluginManager .isPluginOfType (any (String .class ), any (String .class ))).thenReturn (true );
7481
7582 Map <String , String []> springParameterMap = new HashMap <String , String []>();
7683 springParameterMap .put ("k1" , new String []{"v1" });
@@ -101,6 +108,7 @@ public void shouldForwardWebRequestToPlugin() throws Exception {
101108
102109 @ Test
103110 public void shouldRenderPluginResponseWithDefaultContentTypeOn200 () throws Exception {
111+ when (pluginManager .isPluginOfType (any (String .class ), any (String .class ))).thenReturn (true );
104112 DefaultGoPluginApiResponse apiResponse = new DefaultGoPluginApiResponse (200 );
105113 String responseBody = "response-body" ;
106114 apiResponse .setResponseBody (responseBody );
@@ -119,6 +127,7 @@ public void shouldRenderPluginResponseWithDefaultContentTypeOn200() throws Excep
119127
120128 @ Test
121129 public void shouldRenderPluginResponseWithSpecifiedContentTypeOn200 () throws Exception {
130+ when (pluginManager .isPluginOfType (any (String .class ), any (String .class ))).thenReturn (true );
122131 DefaultGoPluginApiResponse apiResponse = new DefaultGoPluginApiResponse (200 );
123132 String contentType = "image/png" ;
124133 apiResponse .responseHeaders ().put ("Content-Type" , contentType );
@@ -138,6 +147,7 @@ public void shouldRenderPluginResponseWithSpecifiedContentTypeOn200() throws Exc
138147
139148 @ Test
140149 public void shouldRedirectToSpecifiedLocationOn302 () throws Exception {
150+ when (pluginManager .isPluginOfType (any (String .class ), any (String .class ))).thenReturn (true );
141151 DefaultGoPluginApiResponse apiResponse = new DefaultGoPluginApiResponse (302 );
142152 String redirectLocation = "/go/plugin/interact/plugin.id/request.name" ;
143153 apiResponse .responseHeaders ().put ("Location" , redirectLocation );
@@ -152,6 +162,35 @@ public void shouldRedirectToSpecifiedLocationOn302() throws Exception {
152162 assertThat (modelAndView .getViewName (), is ("redirect:" + redirectLocation ));
153163 }
154164
165+ @ Test
166+ public void shouldAllowInteractionOnlyForAuthPlugins () {
167+ when (pluginManager .isPluginOfType ("authentication" , "github.pr" )).thenReturn (false );
168+
169+ ModelAndView modelAndView = pluginController .handlePluginInteractRequest (PLUGIN_ID , REQUEST_NAME , servletRequest );
170+ ResponseCodeView view = (ResponseCodeView ) modelAndView .getView ();
171+
172+ assertThat (view .getStatusCode (), is (403 ));
173+ }
174+
175+ @ Test
176+ public void shouldDisallowRequestsWhichNeedAuthentication () {
177+ when (pluginManager .isPluginOfType (any (String .class ), any (String .class ))).thenReturn (true );
178+
179+ List <String > restrictedRequests = Arrays .asList ("go.plugin-settings.get-configuration" ,
180+ "go.plugin-settings.get-view" ,
181+ "go.plugin-settings.validate-configuration" ,
182+ "go.authentication.plugin-configuration" ,
183+ "go.authentication.authenticate-user" ,
184+ "go.authentication.search-user" );
185+
186+ for (String requestName : restrictedRequests ) {
187+ ModelAndView modelAndView = pluginController .handlePluginInteractRequest (PLUGIN_ID , requestName , servletRequest );
188+ ResponseCodeView view = (ResponseCodeView ) modelAndView .getView ();
189+
190+ assertThat (view .getStatusCode (), is (403 ));
191+ }
192+ }
193+
155194 private Enumeration <String > getMockEnumeration (List <String > elements ) {
156195 Enumeration <String > enumeration = new Enumeration <String >() {
157196 private List <String > elements ;
0 commit comments